// BillingAsyncSocket.cpp : ±¸Çö ÆÄÀÏÀÔ´Ï´Ù. // #include "stdafx.h" #include "BillingTestModule.h" #include "BillingAsyncSocket.h" #include ".\billingasyncsocket.h" // CBillingAsyncSocket CBillingAsyncSocket::CBillingAsyncSocket() : m_eConnectionMode(ACTIVE_CONNECTION), m_nAddressLen(0), m_nRefCount(1), m_nSendBytesFirstPacket(0), m_nBufferUsed(0) { memset(&m_PeerAddress, 0, sizeof(SOCKADDR)); } CBillingAsyncSocket::~CBillingAsyncSocket() { } // CBillingAsyncSocket ¸â¹ö ÇÔ¼öÀÔ´Ï´Ù. CBillingAsyncSocket* CBillingAsyncSocket::CreateBillingSocket(int nSocketType, UINT nSocketPort) { CBillingAsyncSocket* lpSocket = new CBillingAsyncSocket; if(0 != lpSocket) { if(!lpSocket->Create(nSocketPort, nSocketType)) { delete lpSocket; lpSocket = 0; } } return lpSocket; } void CBillingAsyncSocket::OnAccept(int nErrorCode) { // TODO: ¿©±â¿¡ Ư¼öÈ­µÈ Äڵ带 Ãß°¡ ¹×/¶Ç´Â ±âº» Ŭ·¡½º¸¦ È£ÃâÇÕ´Ï´Ù. if(0 == nErrorCode) { CBillingAsyncSocket* lpSocket = new CBillingAsyncSocket; if(0 != lpSocket) { lpSocket->m_nAddressLen = sizeof(SOCKADDR_IN); if(Accept(*lpSocket, &lpSocket->m_PeerAddress, &lpSocket->m_nAddressLen)) { lpSocket->m_eConnectionMode = PASSIVE_CONNECTION; theApp.RegisterSocket(*lpSocket); lpSocket->OnReceive(0); } else { lpSocket->Release(); lpSocket = 0; } } } else { // TODO : Make log here Close(); } CAsyncSocket::OnAccept(nErrorCode); } void CBillingAsyncSocket::OnReceive(int nErrorCode) { // TODO: ¿©±â¿¡ Ư¼öÈ­µÈ Äڵ带 Ãß°¡ ¹×/¶Ç´Â ±âº» Ŭ·¡½º¸¦ È£ÃâÇÕ´Ï´Ù. if(0 == nErrorCode) { while(true) { int nReceived = Receive(m_szRecvBuffer + m_nBufferUsed, MAX_RECV_BUFFER - m_nBufferUsed); if(SOCKET_ERROR != nReceived) { m_nBufferUsed += nReceived; size_t nBufferPos = 0; while(sizeof(unsigned short) + nBufferPos <= m_nBufferUsed) { unsigned short usPacketSize = ntohs(*reinterpret_cast(m_szRecvBuffer + nBufferPos)) + sizeof(unsigned short) + sizeof(unsigned short) + sizeof(unsigned long); if(m_nBufferUsed < usPacketSize + nBufferPos) { break; } theApp.AddRecvPacket(*this, m_szRecvBuffer, usPacketSize); nBufferPos += usPacketSize; } if(0 != nBufferPos) { memmove(m_szRecvBuffer, m_szRecvBuffer + nBufferPos, m_nBufferUsed - nBufferPos); m_nBufferUsed -= nBufferPos; } } else { if(GetLastError() != WSAEWOULDBLOCK) { // TODO : Make Log here Close(); } break; } } } else { // TODO : Make Log here Close(); } CAsyncSocket::OnReceive(nErrorCode); } void CBillingAsyncSocket::OnSend(int nErrorCode) { // TODO: ¿©±â¿¡ Ư¼öÈ­µÈ Äڵ带 Ãß°¡ ¹×/¶Ç´Â ±âº» Ŭ·¡½º¸¦ È£ÃâÇÕ´Ï´Ù. if(0 == nErrorCode || WSAEWOULDBLOCK == nErrorCode) { SendPacket(); } else { // TODO : Make Log here Close(); } CAsyncSocket::OnSend(nErrorCode); } void CBillingAsyncSocket::OnClose(int nErrorCode) { // TODO: ¿©±â¿¡ Ư¼öÈ­µÈ Äڵ带 Ãß°¡ ¹×/¶Ç´Â ±âº» Ŭ·¡½º¸¦ È£ÃâÇÕ´Ï´Ù. theApp.RemoveSocket(*this); CAsyncSocket::OnClose(nErrorCode); if(m_eConnectionMode == PASSIVE_CONNECTION) { // ¼¼¼Ç »èÁ¦. Release(); } } void CBillingAsyncSocket::OnConnect(int nErrorCode) { // TODO: ¿©±â¿¡ Ư¼öÈ­µÈ Äڵ带 Ãß°¡ ¹×/¶Ç´Â ±âº» Ŭ·¡½º¸¦ È£ÃâÇÕ´Ï´Ù. m_eConnectionMode = ACTIVE_CONNECTION; CAsyncSocket::OnConnect(nErrorCode); } LONG CBillingAsyncSocket::AddRef() { return ++m_nRefCount; } LONG CBillingAsyncSocket::Release() { LONG nRefCount = --m_nRefCount; if(0 == nRefCount) { delete this; } return nRefCount; } void CBillingAsyncSocket::SendPending(const HanUnitedBilling::GLTransStruct& glTransStruct) { m_SendPacketList.push_back(glTransStruct); SendPacket(); } void CBillingAsyncSocket::SendPacket() { while(!m_SendPacketList.empty()) { HanUnitedBilling::GLTransStruct& sendTransStruct = m_SendPacketList.front(); int nTotalBytes = ntohs(sendTransStruct.DataSize) + sizeof(sendTransStruct.DataSize) + sizeof(sendTransStruct.HeaderMsg) + sizeof(sendTransStruct.SeqID); int nSendLen = Send( reinterpret_cast(&sendTransStruct) + m_nSendBytesFirstPacket, nTotalBytes - m_nSendBytesFirstPacket); if(SOCKET_ERROR != nSendLen) { m_nSendBytesFirstPacket += nSendLen; if(m_nSendBytesFirstPacket == nTotalBytes) { m_SendPacketList.pop_front(); m_nSendBytesFirstPacket = 0; } } else { if(GetLastError() != WSAEWOULDBLOCK) { // TODO : Make Log here Close(); } else { break; } } } }