123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- #include "StdAfx.h"
- #include "IClientImpl.h"
- #include <comdef.h>
- #include <atlbase.h>
- //#include "crc32.h"
- //#include "ThreadPool.hpp"
- #include "lzari.h"
- const int AF_IPV4 = 0;
- const int AF_IPV6 = 1;
- const int SOCK_TCP = SOCK_STREAM-1;
- const int SOCK_UDP = SOCK_DGRAM-1;
- //DWORD g_dwLeng = 0;
- //BYTE *g_pData = NULL;
- DWORD g_dwLeng2 = 0;
- extern BYTE *g_pData2;
- DWORD g_dwcount;
- BOOL g_bConnLYFZ = 0;
- int g_conntype = 0;//正常
- void IClientImpl::DataToArray(IN BYTE *pData, IN CONST DWORD &dwLength, IN CArray<CStringArray, CStringArray>*List1array)
- {
- List1array->RemoveAll();
- if (dwLength == 0)return;
- if (m_tSendhead.code[0])
- {
- BYTE *lpszOut = NULL;
- int nOutSize = 0;
- LZARI Lzari;
- Lzari.UnCompress(pData, dwLength, (const BYTE*&)lpszOut, nOutSize);
- CMemFile memfile;
- memfile.Attach(lpszOut, nOutSize);
- Lzari.Release();
- try
- {
- CArchive ar(&memfile, CArchive::load);
- List1array->SetSize(m_tSendhead.count[0]);
- for (int i = 0; i < List1array->GetSize(); i++)
- {
- List1array->ElementAt(i).Serialize(ar);
- }
- ar.Close();
- memfile.Detach();
- }
- catch (CException* e)
- {
- e->ReportError();
- }
- }
- else
- {
- CMemFile memfile;
- memfile.Attach(pData, dwLength);
- CArchive ar(&memfile, CArchive::load);
- List1array->SetSize(m_tSendhead.count[0]);
- for (int ii = 0; ii < List1array->GetSize(); ii++)
- {
- List1array->ElementAt(ii).Serialize(ar);
- }
- ar.Close();
- memfile.Detach();
- }
- }
- //IClientImpl* IClientImpl::m_pTcpClient[TCPCLIENTNUM] = {NULL};
- void GetFileName(IN const TCHAR *pFullName,OUT TCHAR *pFileName)
- {
- TCHAR szExt[_MAX_EXT];
- _tsplitpath(pFullName, NULL, NULL, pFileName, szExt);
- _tcscat(pFileName, szExt);
- }
- IClientImpl::IClientImpl():m_nMode(AF_IPV4),m_nSockType(SOCK_TCP)
- {
- m_bSocket = FALSE;
- m_bStopbeat = FALSE;
- m_hRunObject = NULL;
- m_bRecevie = TRUE;
- m_hReConnectSrvThreadHandle = NULL;
- m_dwSumRecive = 0;
- m_dwCurRecive = 0;
- m_pRecivebuf = NULL;
- ZeroMemory(&m_tSendhead, sizeof(SENDHEAD));
- m_SocketClient.SetInterface(this);
- }
- IClientImpl::~IClientImpl()
- {
- //m_SocketClient.Terminate();
- net_CloseSocket();
- }
- BOOL IClientImpl::SolveDBError()
- {
- DWORD dwWSAError = WSAGetLastError();
- if ( dwWSAError != 0 )
- {
- //LOG4C_NO_FILENUM((LOG_NOTICE,"dwWSAError = %d~",dwWSAError));
- }
-
- switch(dwWSAError)
- {
- case WSAENOTSOCK:
- case WSAENETDOWN:
- case WSAENETUNREACH:
- case WSAENETRESET:
- case WSAECONNABORTED:
- case WSAECONNRESET:
- case WSAESHUTDOWN:
- case WSAEHOSTDOWN:
- case WSAEHOSTUNREACH:
- TRACE("-----------------WSAError = %d\n",dwWSAError);
- //LOG4C_NO_FILENUM((LOG_NOTICE,"dwWSAError = %d~",dwWSAError));
- m_bSocket = FALSE;
- break;
- default:
- break;
- }
- return TRUE;
- }
- void IClientImpl::GetAddress(const SockAddrIn& addrIn, CString& rString) const
- {
- TCHAR szIPAddr[MAX_PATH] = { 0 };
- CSocketHandle::FormatIP(szIPAddr, MAX_PATH, addrIn);
- rString.Format(_T("%s : %d"), szIPAddr, static_cast<int>(static_cast<UINT>(ntohs(addrIn.GetPort()))) );
- }
- void IClientImpl::AppendText(LPCTSTR lpszFormat, ...)
- {
- // if ( !::IsWindow(m_ctlMsgList.GetSafeHwnd()) ) return;
- // TCHAR szBuffer[512];
- // HWND hWnd = m_ctlMsgList.GetSafeHwnd();
- // DWORD dwResult = 0;
- // if (SendMessageTimeout(hWnd, WM_GETTEXTLENGTH, 0, 0, SMTO_NORMAL, 500L, &dwResult) != 0)
- // {
- // int nLen = (int) dwResult;
- // if (SendMessageTimeout(hWnd, EM_SETSEL, nLen, nLen, SMTO_NORMAL, 500L, &dwResult) != 0)
- // {
- // size_t cb = 0;
- // va_list args;
- // va_start(args, lpszFormat);
- // ::StringCchVPrintfEx(szBuffer, 512, NULL, &cb, 0, lpszFormat, args);
- // va_end(args);
- // SendMessageTimeout(hWnd, EM_REPLACESEL, FALSE, reinterpret_cast<LPARAM>(szBuffer), SMTO_NORMAL, 500L, &dwResult);
- // }
- // }
- }
- bool IClientImpl::GetDestination(SockAddrIn& addrIn) const
- {
- CString strPort;
- int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
- return addrIn.CreateFrom(NULL, strPort, nFamily);
- }
- bool IClientImpl::SetupMCAST()
- {
- const TCHAR szIPv4MCAST[] = TEXT("239.121.1.2");
- const TCHAR szIPv6MCAST[] = TEXT("FF02:0:0:0:0:0:0:1"); // All Nodes local address
- bool result = false;
- if ( m_nSockType == SOCK_UDP )
- {
- if ( m_nMode == AF_IPV4 ) {
- result = m_SocketClient->AddMembership(szIPv4MCAST, NULL);
- } else {
- result = m_SocketClient->AddMembership(szIPv6MCAST, NULL);
- HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
- hr = hr;
- }
- }
- return result;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void IClientImpl::OnThreadBegin(CSocketHandle* pSH)
- {
- ASSERT( pSH == m_SocketClient );
- (pSH);
- CString strAddr;
- SockAddrIn sockAddr;
- m_SocketClient->GetSockName(sockAddr);
- GetAddress( sockAddr, strAddr );
- }
- void IClientImpl::OnThreadExit(CSocketHandle* pSH)
- {
- ASSERT( pSH == m_SocketClient );
- (pSH);
- }
- void IClientImpl::OnConnectionDropped(CSocketHandle* pSH)
- {
- ASSERT( pSH == m_SocketClient );
- m_bSocket = FALSE;
- AppendText( _T("Connection lost with client.\r\n") );
- }
- void IClientImpl::OnConnectionError(CSocketHandle* pSH, DWORD dwError)
- {
- ASSERT( pSH == m_SocketClient );
- (pSH);
- _com_error err(dwError);
- AppendText( _T("Communication Error:\r\n%s\r\n"), err.ErrorMessage() );
- }
- /************************************************************************/
- /* 函数:OnDataReceived[3/21/2016 IT];
- /* 描述:;
- /* 参数:;
- /* [IN] pSH: 客户端实例对象;
- /* [IN] pbData: 客户端本次接收到的数据;
- /* [IN] dwCount: 客户端本次接收到的数据长度;
- /* [IN] addr: 服务端地址;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void IClientImpl::OnDataReceived(CSocketHandle* pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr)
- {
- ASSERT( pSH == m_SocketClient );
- if( !m_SocketClient->IsOpen() ) return;
- if (NULL == pbData) return;
- if ( m_pRecivebuf )
- {// 有上次接收的余包,认为服务分包发送数据,重新组包;
- if ( m_dwSumRecive < m_dwCurRecive + dwCount )
- {
- if ( m_pRecivebuf )
- delete []m_pRecivebuf;
- m_pRecivebuf = NULL;
- m_dwCurRecive = m_dwSumRecive = 0;
- m_bRecevie = TRUE;
- }
- memcpy(m_pRecivebuf + m_dwCurRecive, pbData, dwCount);
- m_dwCurRecive += dwCount;
- if ( m_dwCurRecive == m_dwSumRecive )
- {// 组包完整,回调处理;
- TRACE("完包Tickets = %d \n", GetTickCount());
- m_eventHandler(pSH->GetSocket(), Transport_ReadEv, m_pRecivebuf, m_dwSumRecive, 0, m_pContext);
- if ( m_pRecivebuf )
- delete []m_pRecivebuf;
- m_pRecivebuf = NULL;
- m_dwCurRecive = m_dwSumRecive = 0;
- }
- return;
- }
- TMessageHeader* pHeader = (TMessageHeader *)pbData;
- // 网络字节顺序的转换;
- ntohs(pHeader->wHeaderFlag);
- ntohs(pHeader->wMessageId);
- ntohs(pHeader->wMessageSubId);
- ntohl(pHeader->dwDataLen);
- ntohs(pHeader->wCheckSum);
- ntohl(pHeader->wReserve);
- if ( pHeader->dwDataLen + MESSAGE_HEADER_LEN > dwCount )
- {// 服务器分包发送;
- OutputDebugString(_T("服务器分包发送:"));
- DWORD dwTick = GetTickCount();
- TRACE("当前Tickets = %d \n", dwTick);
- if ( m_pRecivebuf )
- {
- delete []m_pRecivebuf;
- m_pRecivebuf = NULL;
- m_dwSumRecive = m_dwCurRecive = 0;
- }
- m_dwSumRecive = pHeader->dwDataLen + MESSAGE_HEADER_LEN;
- m_pRecivebuf = new BYTE[m_dwSumRecive];
- memset(m_pRecivebuf, 0 , m_dwSumRecive);
- memcpy(m_pRecivebuf + m_dwCurRecive, pbData, dwCount);
- m_dwCurRecive += dwCount;
- }
- else
- {// 服务器未分包发,包完整;
- m_eventHandler(pSH->GetSocket(), Transport_ReadEv, (void*)pbData, dwCount, 0, m_pContext);
- }
- }
- BOOL IClientImpl::Initialize()
- {
- TCHAR szIPAddr[MAX_PATH] = { 0 };
- CSocketHandle::GetLocalAddress(szIPAddr, MAX_PATH, AF_INET);
- AppendText(_T("Local Address (IPv4): %s\r\n"), szIPAddr);
- CSocketHandle::GetLocalAddress(szIPAddr, MAX_PATH, AF_INET6);
- AppendText(_T("Local Address (IPv6): %s\r\n"), szIPAddr);
- return TRUE;
- }
- void IClientImpl::StartReConnectSrvThread()
- {
- // Jeff.启用重连服务端线程.-------------------
- m_hRunObject = CreateEvent( NULL, TRUE, FALSE, _T("ClientThreadRun") );
- if ( m_hRunObject == NULL )
- {
- //LOG4C((LOG_NOTICE,"创建事件失败"));
- }
- m_hReConnectSrvThreadHandle = CreateThread(NULL,0,ReConnectSrvThread,this,0,NULL);
- if ( m_hReConnectSrvThreadHandle == NULL )
- {
- //LOG4C((LOG_NOTICE,"创建线程失败"));
- }
- }
- BOOL IClientImpl::ConnectServer(LPCTSTR strAddr, LPCTSTR strPort)
- {
- _stprintf_s(m_SvrAddr,_T("%s"),strAddr);
- _stprintf_s(m_SvrPort,_T("%s"),strPort);
- int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
- if ( !m_SocketClient.StartClient(NULL, strAddr, strPort, nFamily, (m_nSockType+1) ) )
- {
- m_bSocket = FALSE;
- return FALSE;
- }
- return TRUE;
- }
- void IClientImpl::net_CloseSocket()
- {
- if(m_hRunObject)
- SetEvent(m_hRunObject);
- if( m_hReConnectSrvThreadHandle )
- {
- if (WaitForSingleObject(m_hReConnectSrvThreadHandle,INFINITE) != WAIT_FAILED)
- {
- CloseHandle(m_hReConnectSrvThreadHandle);
- m_hReConnectSrvThreadHandle = NULL;
- }
- }
- if(m_hRunObject)
- CloseHandle( m_hRunObject );
- m_hRunObject = NULL;
- m_SocketClient.Terminate();
- }
- void IClientImpl::SendMsg(void *pMsg,const int nLen)
- {
- if ( m_SocketClient.IsOpen() )
- {
- USES_CONVERSION;
- if (m_nSockType == SOCK_TCP)
- {
- m_SocketClient.Write((const LPBYTE)(pMsg), nLen, NULL, 30000);
- }
- else
- {
- SockAddrIn sockAddr;
- GetDestination(sockAddr);
- m_SocketClient.Write((const LPBYTE)(pMsg), nLen, sockAddr, 30000);
- }
- }
- else
- {
- AfxMessageBox(_T("Socket is not connected"));
- }
- }
- DWORD WINAPI IClientImpl::ReConnectSrvThread(LPVOID pInstance)
- {
- IClientImpl *pClientImpl = (IClientImpl*)pInstance;
- do
- {
- // 检测连接状态;
- if ( pClientImpl->m_bSocket == FALSE )
- {
- if ( pClientImpl->m_SocketClient->IsOpen() == TRUE )
- {
- pClientImpl->m_SocketClient.Terminate();
- }
- pClientImpl->m_bSocket = pClientImpl->ConnectServer(pClientImpl->m_SvrAddr,pClientImpl->m_SvrPort);
- }
- } while( WaitForSingleObject(pClientImpl->m_hRunObject,200L) == WAIT_TIMEOUT );
- //LOG4C_NO_FILENUM((LOG_NOTICE,"重连服务器线程退出"));
- return 0;
- }
- //////////////////////////////////////////////////////////////////////////
- BOOL IClientImpl::net_Connect(LPCTSTR strAddr, LPCTSTR strPort)
- {
- _stprintf_s(m_SvrAddr,_T("%s"),strAddr);
- _stprintf_s(m_SvrPort,_T("%s"),strPort);
- int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
- if ( !m_SocketClient.StartClient(NULL, strAddr, strPort, nFamily, (m_nSockType+1) ) )
- {
- m_bSocket = FALSE;
- return FALSE;
- }
- else
- {
- m_bSocket = TRUE;
- SetupMCAST();
- return TRUE;
- }
- }
- DWORD IClientImpl::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);
- ulSendLen = m_SocketClient->Write((LPBYTE)pSendBuf, ulBufLen, NULL, 30000);
- delete [] pSendBuf;
- pSendBuf = NULL;
- return ulSendLen;
- }
- INT IClientImpl::net_OpenSocket( IN CONST INT& nType, IN CONST DWORD& dwPort, IN LPTRANSPORT_EVENT eventHandler, void *pContext )
- {
- m_eventHandler = eventHandler;
- m_pContext = pContext;
- return 0;
- }
|