#include "stdafx.h" #include "ClientTunnel.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //class CNetWorkBase; //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; //CSocketHandle::ReleaseLibrary(); } TransportStatus CClientTunnel::Transport_Init() { return CSocketHandle::InitLibrary(MAKEWORD(2, 2)); //return CNetWorkBase::Transport_Init(); } TransportStatus CClientTunnel::Transport_UnInit() { return CSocketHandle::ReleaseLibrary(); //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; m_Client.net_OpenSocket(eType, usPort, eventHandler, pContext); return TRANSPORT_OK; } 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; } int CClientTunnel::net_Connect(IN LPCTSTR strAddr, LPCTSTR strPort) { return m_Client.ConnectServer(strAddr, strPort); } unsigned long CClientTunnel::net_Send(IN void *pHeader, IN void *pMessage, IN CONST unsigned long ulDataLen) { return m_Client.net_Send(pHeader, pMessage, ulDataLen); } /************************************************************************/ /* 函数: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); } }