ClientTunnel.cpp 4.8 KB

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