ClientTunnel.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "stdafx.h"
  2. #include "ClientTunnel.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. //class CNetWorkBase;
  9. //CNetWorkBase* g_pNetWork = NULL;
  10. ///////////////////////////////////////////////////////////////////////////////
  11. ///////////////////////////////////////////////////////////////////////////////
  12. /////////////////////////Callback functions////////////////////////////////////
  13. /*Get the header length according to header struct*/
  14. static void netGetDataBufLenCb(
  15. IN SOCKET hSocket,
  16. IN void *pHeaderBuf,
  17. OUT unsigned long *nDataLen
  18. );
  19. /*net event*/
  20. static void netHandleNetEvent(
  21. IN SOCKET hSocket,
  22. IN ETransportEvent eEvent,
  23. IN void *pDataBuf,
  24. IN unsigned long nDataLen,
  25. IN int nError,
  26. IN void *pContext
  27. );
  28. CClientTunnel::CClientTunnel()
  29. {
  30. //g_pNetWork = new CNetWorkBase;
  31. }
  32. CClientTunnel::~CClientTunnel()
  33. {
  34. //if(g_pNetWork)
  35. // delete g_pNetWork;
  36. //g_pNetWork = NULL;
  37. //CSocketHandle::ReleaseLibrary();
  38. }
  39. TransportStatus CClientTunnel::Transport_Init()
  40. {
  41. return CSocketHandle::InitLibrary(MAKEWORD(2, 2));
  42. //return CNetWorkBase::Transport_Init();
  43. }
  44. TransportStatus CClientTunnel::Transport_UnInit()
  45. {
  46. return CSocketHandle::ReleaseLibrary();
  47. //return CNetWorkBase::Transport_UnInit();
  48. }
  49. int CClientTunnel::net_OpenSocket(
  50. IN CONST ETransportType eType,
  51. IN CONST unsigned short usPort,
  52. IN LPTRANSPORT_EVENT eventHandler,
  53. void *pContext
  54. )
  55. {
  56. TransportStatus status = TRANSPORT_OK;
  57. unsigned long ulHeaderLen = MESSAGE_HEADER_LEN;
  58. /*Use this layer handle event and context,
  59. save the application layer handle event and context*/
  60. m_eventHandler = eventHandler;
  61. m_pContext = pContext;
  62. LPTRANSPORT_GETDATABUFLEN cbHandler = netGetDataBufLenCb;
  63. m_Client.net_OpenSocket(eType, usPort, eventHandler, pContext);
  64. return TRANSPORT_OK;
  65. }
  66. void CClientTunnel::net_CloseSocket()
  67. {
  68. //g_pNetWork->net_CloseSocket();
  69. }
  70. /************************************************************************/
  71. /*
  72. 函数: net_Connect
  73. 描述: 连接服务
  74. 参数:
  75. IN unsigned long ulIPValue, ip地址
  76. IN unsigned short usPort 端口
  77. 返回:-1失败
  78. */
  79. /************************************************************************/
  80. int CClientTunnel::net_Connect(IN CONST unsigned long ulIPValue, IN CONST unsigned short usPort)
  81. {
  82. //m_nIndex = g_pNetWork->net_Connect(ulIPValue, usPort);
  83. return m_nIndex;
  84. }
  85. int CClientTunnel::net_Connect(IN LPCTSTR strAddr, LPCTSTR strPort)
  86. {
  87. return m_Client.ConnectServer(strAddr, strPort);
  88. }
  89. unsigned long CClientTunnel::net_Send(IN void *pHeader, IN void *pMessage, IN CONST unsigned long ulDataLen)
  90. {
  91. return m_Client.net_Send(pHeader, pMessage, ulDataLen);
  92. }
  93. /************************************************************************/
  94. /*
  95. 函数:ResetBuffer
  96. 描述:获取数据缓冲;
  97. 参数:
  98. IN SOCKET hSocket,
  99. IN void *pHeaderBuf, 消息头
  100. OUT unsigned long *nDataLen 返回长度
  101. 返回:
  102. */
  103. /************************************************************************/
  104. void netGetDataBufLenCb(IN SOCKET hSocket, IN void *pHeaderBuf, OUT unsigned long *nDataLen)
  105. {
  106. TMessageHeader *pHeader = NULL;
  107. *nDataLen = 0;
  108. pHeader = (TMessageHeader *)pHeaderBuf;
  109. if( NULL == pHeader )
  110. return;
  111. if( MESSAGE_HEADER_FLAG != pHeader->wHeaderFlag )
  112. return;
  113. unsigned long dwDataBufLen = pHeader->dwDataLen;
  114. ntohl(dwDataBufLen);
  115. *nDataLen = dwDataBufLen;
  116. }
  117. void netHandleNetEvent(
  118. IN SOCKET hSocket,
  119. IN ETransportEvent eEvent,
  120. IN void *pDataBuf,
  121. IN unsigned long nDataLen,
  122. IN int nError,
  123. IN void *pContext
  124. )
  125. {
  126. /*Notify application layer*/
  127. CClientTunnel *pClientTunnel = (CClientTunnel *)pContext;
  128. if( NULL == pClientTunnel )
  129. return;
  130. void *pUserData = pClientTunnel->m_pContext;
  131. TransportBool bHandle = TRUE;
  132. if( Transport_ReadEv == eEvent )
  133. {
  134. if( nDataLen <= 0 )
  135. return;
  136. /*提取消息头,获得消息ID*/
  137. char *pMessageBuf = (char *)pDataBuf;
  138. TMessageHeader *pHeader = (TMessageHeader *)pMessageBuf;
  139. /*网络字节顺序的转换*/
  140. ntohs(pHeader->wHeaderFlag);
  141. ntohs(pHeader->wMessageId);
  142. ntohs(pHeader->wMessageSubId);
  143. ntohl(pHeader->dwDataLen);
  144. ntohs(pHeader->wCheckSum);
  145. ntohl(pHeader->wReserve);
  146. bHandle = (MESSAGE_HEADER_FLAG == pHeader->wHeaderFlag);
  147. }
  148. if( bHandle )
  149. {
  150. if( NULL != pClientTunnel->m_eventHandler )
  151. pClientTunnel->m_eventHandler(hSocket, eEvent, pDataBuf, nDataLen, nError, pUserData);
  152. }
  153. }