ClientTunnel.cpp 5.3 KB

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