BOOL UdpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendSize, LPBYTE* pRecvData, DWORD& dwRecvSize ) { UDP_HEADER sendHead; sendHead.flag = UDP_FLAG; sendHead.nSize = dwSendSize; BOOL ret = FALSE; do { if (! Send( targetIP, (PBYTE)&sendHead, sizeof(UDP_HEADER))) break; if (m_isSecure) XFC(pSendData,dwSendSize,pSendData,m_xorKey1,m_xorKey2); if (! Send( targetIP, pSendData, dwSendSize)) break; UDP_HEADER recvHead = {0}; if ( !ReceiveAll(m_sock,(char*)&recvHead, sizeof(UDP_HEADER))) break; ByteBuffer buffer; buffer.Alloc(recvHead.nSize); if (! ReceiveAll(m_sock,(LPBYTE)buffer,recvHead.nSize)) break; //¸´ÖÆÊý¾Ý *pRecvData = Alloc(recvHead.nSize); memcpy(*pRecvData, (LPBYTE)buffer, recvHead.nSize); dwRecvSize = recvHead.nSize; if(m_isSecure) XFC(*pRecvData,recvHead.nSize,*pRecvData,m_xorKey1,m_xorKey2); ret = TRUE; } while (FALSE); if ( !ret ) m_isConnected = FALSE; return ret; }
BOOL UdpComm::Send( ULONG targetIP, const LPBYTE pData, DWORD dwSize ) { IN_ADDR addr; addr.S_un.S_addr = targetIP; ByteBuffer sendByteBuffer; sendByteBuffer.Alloc(dwSize); memcpy((LPBYTE)sendByteBuffer, pData, dwSize); if ( !m_isConnected ) m_isConnected = Connect(targetIP, g_ConfigInfo.nPort); if ( m_isConnected ) m_isConnected = SendAll(m_sock,(LPBYTE)sendByteBuffer,sendByteBuffer.Size()); return m_isConnected; }
DWORD MyFile::Read( DWORD dwToRead, ByteBuffer& buffer ) { if (0 == dwToRead) return 0; buffer.Alloc(dwToRead); DWORD dwRead = 0; BOOL bRet = ::ReadFile(m_handle, (LPBYTE)buffer, dwToRead, &dwRead, NULL); if (bRet) { return dwRead; } else { buffer.Free(); return 0; } }
BOOL TcpComm::Send( ULONG targetIP, const LPBYTE pData, DWORD dwSize ) { IN_ADDR addr; addr.S_un.S_addr = targetIP; ByteBuffer sendByteBuffer; sendByteBuffer.Alloc(dwSize); memcpy((LPBYTE)sendByteBuffer, pData, dwSize); BOOL bSentOK = FALSE; if( !m_isConnected) m_isConnected = Connect(targetIP); if (m_isConnected) m_isConnected = m_sock.SendAll((LPBYTE)sendByteBuffer, sendByteBuffer.Size()); return m_isConnected; }
BOOL CommManager::HandleMessageAndReply( SOCKADDR_IN fromAddr, const LPBYTE pData, DWORD dwDataSize, COMM_NAME commName, BOOL& bValidData, DWORD replyMaxDataSize, ByteBuffer& replyBuffer ) { CPGUID cpguid; bValidData = HandleMessage(fromAddr, pData, dwDataSize, commName, cpguid); if (! bValidData) { return FALSE; } //找到需要发送的消息 ByteBuffer toSendData; if (! m_cp.GetMessageToSendById(cpguid, replyMaxDataSize, toSendData)) { m_cp.CreateEmptyPacket(toSendData); } replyBuffer.Alloc(toSendData.Size()); BOOL bEncryptOK = XorFibonacciCrypt((LPBYTE)toSendData, toSendData.Size(), (LPBYTE)replyBuffer, 2, 7); return bEncryptOK; }
void CutupProtocol::MergePackets( const PacketList& packetList, ByteBuffer& byteData ) const { DWORD dwDataSize = 0; PacketList::const_iterator iter = packetList.begin(); for (; iter != packetList.end(); iter++) { dwDataSize += (*iter)->header.size; } byteData.Alloc(dwDataSize); LPBYTE pData = byteData; iter = packetList.begin(); for (; iter != packetList.end(); iter++) { if ((*iter)->header.size > 0) { memcpy(pData, (*iter)->data, (*iter)->header.size); pData += (*iter)->header.size; } } }
BOOL TcpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendSize, LPBYTE* pRecvData, DWORD& dwRecvSize ) { TCP_HEADER sendHead; sendHead.flag = TCP_FLAG; sendHead.nSize = dwSendSize; if (! Send(m_sock, targetIP, (PBYTE)&sendHead, sizeof(TCP_HEADER))) return FALSE; if (! Send(m_sock, targetIP, pSendData, dwSendSize)) return FALSE; TCP_HEADER recvHead = {0}; int iRecv = m_sock.ReceiveAll((LPBYTE)&recvHead, sizeof(TCP_HEADER)); if (iRecv < 0) { errorLog(_T("recv http failed WE%d"), ::WSAGetLastError()); } ByteBuffer buffer; buffer.Alloc(recvHead.nSize); iRecv = m_sock.ReceiveAll(buffer,recvHead.nSize); if (iRecv < 0) { errorLog(_T("recv http failed WE%d"), ::WSAGetLastError()); } //¸´ÖÆÊý¾Ý *pRecvData = Alloc(recvHead.nSize); memcpy(*pRecvData, (LPBYTE)buffer, recvHead.nSize); dwRecvSize = recvHead.nSize; return TRUE; }