123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- #include "StdAfx.h"
- #include "IServerImpl.h"
- #include <comdef.h>
- #include <atlbase.h>
- #include <strsafe.h>
- #include "ThreadPool.hpp"
- #include "ClientProcess.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;
- namespace ServerSocketImpl
- {
- IServerImpl* g_pServerSocket = NULL;
- IServerImpl::IServerImpl():m_nMode(AF_IPV4)
- ,m_nSockType(SOCK_TCP)
- ,m_strPort(_T("64320"))
- ,m_nSocketIndex(0)
- {
- m_bStopbeat = FALSE;
- InitializeCriticalSection( &m_csProcessData );
- m_SocketServer.SetInterface(this);
- }
- IServerImpl::~IServerImpl()
- {
- m_SocketServer.Terminate();
- DeleteCriticalSection( &m_csProcessData );
- }
- BOOL IServerImpl::Initialize()
- {
- TCHAR szIPAddr[MAX_PATH] = { 0 };
- CSocketHandle::GetLocalAddress(szIPAddr, MAX_PATH, AF_INET);
-
- CSocketHandle::GetLocalAddress(szIPAddr, MAX_PATH, AF_INET6);
-
- return TRUE;
- }
- void IServerImpl::Start(IN LPCTSTR strPort,IN const int &nMode)
- {
- m_nMode = nMode;
- int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
- if (!m_SocketServer.StartServer(NULL, strPort, nFamily, (m_nSockType+1)))
- {
-
- AfxMessageBox(_T("Failed to start server."), NULL, MB_ICONSTOP);
- return;
- }
-
-
-
- }
- void IServerImpl::Stop()
- {
- if(m_hRunObject)
- SetEvent(m_hRunObject);
- if( m_hClearInvalidateSocketThread )
- {
- if (WaitForSingleObject(m_hClearInvalidateSocketThread,INFINITE) != WAIT_FAILED)
- {
- CloseHandle(m_hClearInvalidateSocketThread);
- m_hClearInvalidateSocketThread = NULL;
- }
- }
- if ( m_hRunObject )
- CloseHandle( m_hRunObject );
- m_hRunObject = NULL;
- m_SocketServer.Terminate();
-
- }
- void IServerImpl::Send()
- {
- if ( m_SocketServer.IsOpen() )
- {
- CString strMsg;
-
- if ( strMsg.IsEmpty() )
- {
-
- return;
- }
- USES_CONVERSION;
- if (m_nSockType == SOCK_TCP)
- {
- const LPBYTE lpbData = (const LPBYTE)(T2CA(strMsg));
-
- #ifdef SOCKHANDLE_USE_OVERLAPPED
- const SocketContextList& sl = m_SocketServer.GetSocketList();
- for(SocketContextList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
- #else
- const SocketList& sl = m_SocketServer.GetSocketList();
- for(SocketList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
- #endif
- {
- CSocketHandle sockHandle;
- sockHandle.Attach( (*citer) );
- sockHandle.Write(lpbData, strMsg.GetLength(), NULL);
- sockHandle.Detach();
- }
- }
- else
- {
- SockAddrIn servAddr, sockAddr;
- m_SocketServer->GetSockName(servAddr);
- GetDestination(sockAddr);
- if ( servAddr != sockAddr )
- {
- m_SocketServer.Write((const LPBYTE)(T2CA(strMsg)), strMsg.GetLength(), sockAddr);
- }
- else
- {
-
- }
- }
- }
- else
- {
- AfxMessageBox(_T("Socket is not connected"));
- }
- }
- void IServerImpl::SendAll(CSocketHandle &sockHandle, unsigned char *pMsg, int nLength)
- {
- if ( m_SocketServer.IsOpen() )
- {
- USES_CONVERSION;
- if (m_nSockType == SOCK_TCP)
- {
-
- const LPBYTE lpbData = (const LPBYTE)(pMsg);
- sockHandle.Write(lpbData, nLength, NULL);
- }
- else
- {
- SockAddrIn servAddr, sockAddr;
- m_SocketServer->GetSockName(servAddr);
- GetDestination(sockAddr);
- if ( servAddr != sockAddr )
- {
- m_SocketServer.Write((const LPBYTE)*pMsg, nLength, sockAddr);
- }
- else
- {
- }
- }
- }
- else
- {
- }
- }
- int IServerImpl::OnIntegrityPacket(IN PerSocketContext &sockHandle, IN void *pIntegrityPacket)
- {
-
- return 0;
- }
- void IServerImpl::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 IServerImpl::AppendText(LPCTSTR lpszFormat, ...)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- bool IServerImpl::GetDestination(SockAddrIn& addrIn) const
- {
- CString strPort;
-
- int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
- return addrIn.CreateFrom(NULL, strPort, nFamily);
- }
- bool IServerImpl::SetupMCAST()
- {
- const TCHAR szIPv4MCAST[] = TEXT("239.121.1.2");
- const TCHAR szIPv6MCAST[] = TEXT("FF02:0:0:0:0:0:0:1");
- bool result = false;
- if ( m_nSockType == SOCK_UDP )
- {
- if ( m_nMode == AF_IPV4 ) {
- result = m_SocketServer->AddMembership(szIPv4MCAST, NULL);
- } else {
- result = m_SocketServer->AddMembership(szIPv6MCAST, NULL);
- HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
- hr = hr;
- }
- }
- return result;
- }
-
- void IServerImpl::OnThreadBegin(CSocketHandle* pSH)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
- CString strAddr;
- SockAddrIn sockAddr;
- m_SocketServer->GetSockName(sockAddr);
- GetAddress( sockAddr, strAddr );
-
- }
- void IServerImpl::OnThreadExit(CSocketHandle* pSH)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
-
- }
- void IServerImpl::OnConnectionFailure(CSocketHandle* pSH, SOCKET newSocket)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
- CString strAddr;
- CSocketHandle sockHandle;
- SockAddrIn sockAddr;
- if (newSocket != INVALID_SOCKET)
- {
- sockHandle.Attach( newSocket );
- sockHandle.GetPeerName( sockAddr );
- GetAddress( sockAddr, strAddr );
- sockHandle.Close();
-
-
-
-
-
- }
- else
- {
-
-
-
-
-
- }
- }
- void IServerImpl::OnAddConnection(CSocketHandle* pSH, SOCKET newSocket)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
- CString strAddr;
- CSocketHandle sockHandle;
- SockAddrIn sockAddr;
- sockHandle.Attach( newSocket );
- sockHandle.GetPeerName( sockAddr );
- GetAddress( sockAddr, strAddr );
- sockHandle.Detach();
- OutputDebugString(_T("\n新的连接接入:"));
- OutputDebugString(strAddr);
- OutputDebugString(_T("\n"));
-
-
- }
-
- void IServerImpl::OnDataReceived(LPWSAOVERLAPPED pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr)
- {
-
-
-
-
-
- if ( pSH == NULL )
- return ;
-
-
-
-
-
-
-
-
- static ULONGLONG ulCount = 0;
- ulCount += dwCount;
- static TCHAR szcount[40]= {0};
- _ui64toa(ulCount, szcount, 10);
-
-
- LOG4C_NO_FILENUM((LOG_NOTICE,"累积:%s",szcount));
- if (m_nSockType == SOCK_TCP)
- {
- PerSocketContext *psio = (PerSocketContext*)pSH;
- EnterCriticalSection( &m_csProcessData );
- ToprocessRecivebuf(*psio,pbData,dwCount);
- LeaveCriticalSection( &m_csProcessData );
- }
- else
- {
- SockAddrIn servAddr, sockAddr;
- m_SocketServer->GetSockName(servAddr);
- GetDestination(sockAddr);
- if ( servAddr != sockAddr )
- {
-
- }
- else
- {
-
- }
- }
- }
- void IServerImpl::OnConnectionDropped(CSocketHandle* pSH)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
- CString strAddr;
- CSocketHandle sockHandle;
- SockAddrIn sockAddr;
-
- sockHandle.GetPeerName( sockAddr );
- GetAddress( sockAddr, strAddr );
-
-
-
- }
- void IServerImpl::OnConnectionError(CSocketHandle* pSH, DWORD dwError)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
- _com_error err(dwError);
-
- CString strAddr;
- CSocketHandle sockHandle;
- SockAddrIn sockAddr;
- sockHandle.GetPeerName( sockAddr );
- GetAddress( sockAddr, strAddr );
-
- }
- #if defined(SOCKHANDLE_USE_OVERLAPPED)
- void IServerImpl::OnRemoveConnection(CSocketHandle* pSH, SOCKET dropSocket)
- {
- return;
- ASSERT( pSH == m_SocketServer );
- (pSH);
- CString strAddr;
- CSocketHandle sockHandle;
- SockAddrIn sockAddr;
- sockHandle.Attach( dropSocket );
- sockHandle.GetPeerName( sockAddr );
- GetAddress( sockAddr, strAddr );
- sockHandle.Detach();
-
- }
- #endif
- DWORD IServerImpl::GetClientConnectCount()
- {
-
-
- return m_SocketServer.GetConnectionCount();
- }
- void IServerImpl::ToprocessRecivebuf(IN PerSocketContext &sockHandle, IN const BYTE* pReceivebuf, IN DWORD dwReceiveSize)
- {
-
- DWORD dwIndexOfProcessed = 0;
-
- DWORD dwOnceProcessedLen = 0;
- TheProhead *ptphead;
- while( dwIndexOfProcessed < dwReceiveSize )
- {
- ptphead = NULL;
- dwOnceProcessedLen = 0;
-
- if ( sockHandle.ncurSize == 0 )
- {
-
- if ( dwReceiveSize - dwIndexOfProcessed < sizeof( TheProhead ) )
- {
- sockHandle.ncurSize = dwReceiveSize - dwIndexOfProcessed;
- sockHandle.SetPendingCurPack(sockHandle.ncurSize);
- memcpy(sockHandle.pendingbuf, pReceivebuf+dwIndexOfProcessed, sockHandle.ncurSize);
- dwOnceProcessedLen = sockHandle.ncurSize;
-
- }
- else
- {
-
- ptphead = (TheProhead*)(pReceivebuf+dwIndexOfProcessed);
- if ( dwReceiveSize - dwIndexOfProcessed > ptphead->nDataLen + sizeof(TheProhead) )
- {
- sockHandle.ncurSize = sockHandle.npendingSize = ptphead->nDataLen + sizeof(TheProhead);
- sockHandle.SetPendingPack(sockHandle.ncurSize);
- memcpy(sockHandle.pendingbuf, pReceivebuf+dwIndexOfProcessed, sockHandle.ncurSize);
- dwOnceProcessedLen = sockHandle.ncurSize;
-
- }
- else
- {
- sockHandle.npendingSize = ptphead->nDataLen + sizeof(TheProhead);
- sockHandle.ncurSize = dwReceiveSize - dwIndexOfProcessed;
- sockHandle.SetPendingPack(sockHandle.npendingSize);
- memcpy(sockHandle.pendingbuf, pReceivebuf+dwIndexOfProcessed, sockHandle.ncurSize);
- dwOnceProcessedLen = sockHandle.ncurSize;
-
- }
- }
- }
- else
- {
- if ( sockHandle.ncurSize >= sizeof(TheProhead) )
- {
- ptphead = (TheProhead*)sockHandle.pendingbuf;
- if ( sockHandle.npendingSize == ptphead->nDataLen + sizeof(TheProhead) )
- {
- if ( sockHandle.npendingSize - sockHandle.ncurSize > dwReceiveSize - dwIndexOfProcessed )
- {
- memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed, dwReceiveSize - dwIndexOfProcessed);
- sockHandle.ncurSize += dwReceiveSize - dwIndexOfProcessed;
- dwOnceProcessedLen = dwReceiveSize - dwIndexOfProcessed;
-
- }
- else
- {
- memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed, sockHandle.npendingSize - sockHandle.ncurSize);
- dwOnceProcessedLen = sockHandle.npendingSize - sockHandle.ncurSize;
- sockHandle.ncurSize = sockHandle.npendingSize;
-
- }
- }
- else
- {
-
- LOG4C_NO_FILENUM((LOG_NOTICE, "数据包错误,丢包"));
-
- }
- }
- else
- {
- if ( sizeof(TheProhead) - sockHandle.ncurSize > dwReceiveSize - dwIndexOfProcessed )
- {
- sockHandle.ReSetPengingPack(sockHandle.ncurSize + dwReceiveSize - dwIndexOfProcessed);
- memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed, dwReceiveSize - dwIndexOfProcessed);
- sockHandle.ncurSize += dwReceiveSize - dwIndexOfProcessed;
- dwOnceProcessedLen = dwReceiveSize - dwIndexOfProcessed;
-
- }
- else
- {
-
- sockHandle.ReSetPengingPack(sizeof(TheProhead));
- memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed, sizeof(TheProhead) - sockHandle.ncurSize);
- dwOnceProcessedLen = sizeof(TheProhead) - sockHandle.ncurSize;
- sockHandle.ncurSize = sizeof(TheProhead);
- ptphead = (TheProhead*)sockHandle.pendingbuf;
- sockHandle.npendingSize = ptphead->nDataLen + sizeof(TheProhead);
- sockHandle.ReSetPengingPack(sockHandle.npendingSize);
- if ( sockHandle.npendingSize >= dwReceiveSize - dwIndexOfProcessed - dwOnceProcessedLen )
- {
- memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed + dwOnceProcessedLen, sockHandle.npendingSize - sockHandle.ncurSize);
- dwOnceProcessedLen += sockHandle.npendingSize - sockHandle.ncurSize;
- sockHandle.ncurSize = sockHandle.npendingSize;
-
- }
- else
- {
- memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed + dwOnceProcessedLen, dwReceiveSize - dwIndexOfProcessed - dwOnceProcessedLen);
- sockHandle.ncurSize += dwReceiveSize - dwIndexOfProcessed - dwOnceProcessedLen;
- dwOnceProcessedLen = dwReceiveSize - dwIndexOfProcessed;
-
- }
- }
- }
- }
- if ( sockHandle.ncurSize == sockHandle.npendingSize )
- {
-
- TheProbody *ptpb = (TheProbody*)sockHandle.pendingbuf;
- CFile cf;
- static int a = 0;
- CString strFile = _T("");
- strFile.Format(_T("D:\\接收文件\\%d-%d.dat"), (SOCKET)sockHandle,a);
- if ( cf.Open(strFile, CFile::modeCreate|CFile::modeReadWrite) )
- {
- cf.Write(ptpb->szBody, ptpb->tphead.nDataLen);
- cf.Close();
- a++;
- }
- else
- {
- LOG4C_NO_FILENUM((LOG_NOTICE, "创建文件失败"));
- }
- delete []sockHandle.pendingbuf;
- sockHandle.pendingbuf = NULL;
- sockHandle.ncurSize = sockHandle.npendingSize = 0;
- }
- dwIndexOfProcessed += dwOnceProcessedLen;
- }
- }
- };
|