123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- #include "stdafx.h"
- #include "ClientTunnel.h"
- #include "..\NetWork\data_def.h"
- #include "..\NetWork\NetWorkInterface.h"
- #include "..\NetWork\NetWorkBase.h"
- #ifdef _DEBUG
- #pragma comment(lib,"..\\lib\\lyfzNetWork_d.lib")
- #else
- #pragma comment(lib,"..\\lib\\lyfzNetWork.lib")
- #endif //#ifdef _DEBUG
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CNetWorkBase* g_pNetWork = NULL;
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- /////////////////////////Callback functions////////////////////////////////////
- /*Get the header length according to header struct*/
- static void netGetDataBufLenCb(IN SOCKET hSocket, IN void *pHeaderBuf,
- OUT unsigned long *nDataLen);
- /*net event*/
- static void netHandleNetEvent(IN SOCKET hSocket, IN ETransportEvent eEvent,
- IN void *pDataBuf, IN unsigned long nDataLen,
- IN int nError, IN void *pContext);
- CClientTunnel::CClientTunnel()
- {
- g_pNetWork = new CNetWorkBase;
- }
- CClientTunnel::~CClientTunnel()
- {
- if(g_pNetWork)
- delete g_pNetWork;
- g_pNetWork = NULL;
- }
- TransportStatus CClientTunnel::Transport_Init()
- {
- return CNetWorkBase::Transport_Init();
- }
- TransportStatus CClientTunnel::Transport_UnInit()
- {
- return CNetWorkBase::Transport_UnInit();
- }
- int CClientTunnel::net_OpenSocket(IN CONST ETransportType eType, IN CONST unsigned short usPort, IN LPTRANSPORT_EVENT eventHandler, void *pContext)
- {
- TransportStatus status = TRANSPORT_OK;
-
- unsigned long ulHeaderLen = MESSAGE_HEADER_LEN;
- /*Use this layer handle event and context,
- save the application layer handle event and context*/
- m_eventHandler = eventHandler;
- m_pContext = pContext;
-
- LPTRANSPORT_GETDATABUFLEN cbHandler = netGetDataBufLenCb;
- SOCKET hSocket = g_pNetWork->net_OpenSocket(
- eType,
- usPort,
- netHandleNetEvent,
- cbHandler,
- ulHeaderLen,
- this);
- if( INVALID_SOCKET == hSocket )
- {
- return TRANSPORT_ERROR;
- }
- return status;
- }
- void CClientTunnel::net_CloseSocket()
- {
- g_pNetWork->net_CloseSocket();
- }
- /************************************************************************/
- /*
- 函数: net_Connect
- 描述: 连接服务
- 参数:
- IN unsigned long ulIPValue, ip地址
- IN unsigned short usPort 端口
- 返回:-1失败
- */
- /************************************************************************/
- int CClientTunnel::net_Connect(IN CONST unsigned long ulIPValue, IN CONST unsigned short usPort)
- {
- m_nIndex = g_pNetWork->net_Connect(ulIPValue, usPort);
- return m_nIndex;
- }
- unsigned long CClientTunnel::net_Send(IN void *pHeader, IN void *pMessage, IN CONST unsigned long ulDataLen)
- {
- unsigned long ulSendLen = 0;
- unsigned long ulBufLen = 0;
- TMessageHeader *pSendHeader = (TMessageHeader *)pHeader;
- ulBufLen = MESSAGE_HEADER_LEN + ulDataLen;
- char *pSendBuf = new char[ulBufLen];
- memset(pSendBuf, 0, ulBufLen);
- TMessageHeader *pMessageHeader = (TMessageHeader *)pSendBuf;
- pMessageHeader->byVersion = 101;
- pMessageHeader->wHeaderFlag = MESSAGE_HEADER_FLAG;
- pMessageHeader->wMessageId = pSendHeader->wMessageId;
- pMessageHeader->wMessageSubId = pSendHeader->wMessageSubId;
- pMessageHeader->dwDataLen = ulDataLen;
- pMessageHeader->wReserve = 0;
- memcpy((void *)(pSendBuf+MESSAGE_HEADER_LEN), pMessage, ulDataLen);
- /* convert network word */
- htons(pMessageHeader->wHeaderFlag);
- htons(pMessageHeader->wMessageId);
- htons(pMessageHeader->wMessageSubId);
- htonl(pMessageHeader->dwDataLen);
- htons(pMessageHeader->wCheckSum);
- htonl(pMessageHeader->wReserve);
- /*Send the message*/
- ulSendLen = g_pNetWork->net_Send(m_nIndex, (void *)pSendBuf, ulBufLen);
- /**/
- delete [] pSendBuf;
- pSendBuf = NULL;
- return ulSendLen;
- }
- /************************************************************************/
- /*
- 函数:ResetBuffer
- 描述:获取数据缓冲;
- 参数:
- IN SOCKET hSocket,
- IN void *pHeaderBuf, 消息头
- OUT unsigned long *nDataLen 返回长度
- 返回:
- */
- /************************************************************************/
- void netGetDataBufLenCb(IN SOCKET hSocket, IN void *pHeaderBuf, OUT unsigned long *nDataLen)
- {
- TMessageHeader *pHeader = NULL;
- *nDataLen = 0;
- pHeader = (TMessageHeader *)pHeaderBuf;
- if( NULL == pHeader )
- return;
- if( MESSAGE_HEADER_FLAG != pHeader->wHeaderFlag )
- return;
- unsigned long dwDataBufLen = pHeader->dwDataLen;
- ntohl(dwDataBufLen);
- *nDataLen = dwDataBufLen;
- }
- void netHandleNetEvent(IN SOCKET hSocket, IN ETransportEvent eEvent,
- IN void *pDataBuf, IN unsigned long nDataLen,
- IN int nError, IN void *pContext)
- {
- /*Notify application layer*/
- CClientTunnel *pClientTunnel = (CClientTunnel *)pContext;
- if( NULL == pClientTunnel )
- return;
- void *pUserData = pClientTunnel->m_pContext;
- TransportBool bHandle = TRUE;
- if( Transport_ReadEv == eEvent )
- {
- if( nDataLen <= 0 )
- return;
- /*提取消息头,获得消息ID*/
- char *pMessageBuf = (char *)pDataBuf;
- TMessageHeader *pHeader = (TMessageHeader *)pMessageBuf;
- /*网络字节顺序的转换*/
- ntohs(pHeader->wHeaderFlag);
- ntohs(pHeader->wMessageId);
- ntohs(pHeader->wMessageSubId);
- ntohl(pHeader->dwDataLen);
- ntohs(pHeader->wCheckSum);
- ntohl(pHeader->wReserve);
- bHandle = (MESSAGE_HEADER_FLAG == pHeader->wHeaderFlag);
- }
- if( bHandle )
- {
- if( NULL != pClientTunnel->m_eventHandler )
- pClientTunnel->m_eventHandler(hSocket, eEvent, pDataBuf, nDataLen, nError, pUserData);
- }
- }
|