fix udp bug.
This commit is contained in:
parent
f38b58b6ea
commit
1f597f443b
|
@ -151,6 +151,8 @@ void CGeneratorDlg::InitDlgEdit()
|
|||
m_DefaultComm.SetItemData(1,COMMNAME_HTTPS);
|
||||
m_DefaultComm.InsertString(2,_T("TCP"));
|
||||
m_DefaultComm.SetItemData(2,COMMNAME_TCP);
|
||||
m_DefaultComm.InsertString(2,_T("UDP"));
|
||||
m_DefaultComm.SetItemData(2,COMMNAME_UDP);
|
||||
|
||||
|
||||
m_DefaultComm.SetCurSel(config.commType);
|
||||
|
|
|
@ -311,6 +311,7 @@ void CommManager::MessageSenderProc()
|
|||
//发送并接收
|
||||
/*if (! IsCommAvailable(commName)) continue;*/
|
||||
ByteBuffer recvByteData;
|
||||
|
||||
if (! SendAndRecv(commName, targetIP, toSendByteData, toSendByteData.Size(), recvByteData))
|
||||
{
|
||||
CmdRedirector &cmd = Manager::GetInstanceRef().m_cmdRedirector;
|
||||
|
|
|
@ -19,33 +19,73 @@ UdpComm::~UdpComm(void)
|
|||
CloseHandle(m_hRecvEvent);
|
||||
}
|
||||
|
||||
BOOL UdpComm::SendAll(UDTSOCKET s,LPCVOID lpBuf, int nBufLen)
|
||||
{
|
||||
if (UDT::INVALID_SOCK == s)
|
||||
{
|
||||
errorLog(_T("socket is invalid. send failed"));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
const char* p = (const char*) lpBuf;
|
||||
int iLeft = nBufLen;
|
||||
int iSent = UDT::send(s, p, iLeft, 0);
|
||||
while (iSent > 0 && iSent < iLeft)
|
||||
{
|
||||
iLeft -= iSent;
|
||||
p += iSent;
|
||||
|
||||
iSent = UDT::send(s, p, iLeft, 0);
|
||||
}
|
||||
|
||||
return (iSent > 0);
|
||||
}
|
||||
|
||||
BOOL UdpComm::ReceiveAll(UDTSOCKET s, LPCVOID lpBuf,int nBufLen)
|
||||
{
|
||||
if (UDT::INVALID_SOCK == s)
|
||||
{
|
||||
errorLog(_T("socket is invalid. recv failed"));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
char* p = (char*) lpBuf;
|
||||
int iLeft = nBufLen;
|
||||
int iRecv = UDT::recv(s, p, iLeft, 0);
|
||||
while (iRecv > 0 && iRecv < iLeft)
|
||||
{
|
||||
iLeft -= iRecv;
|
||||
p += iRecv;
|
||||
|
||||
iRecv = UDT::recv(s, p, iLeft, 0);
|
||||
}
|
||||
|
||||
return (iRecv > 0);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
BOOL bSentOK = FALSE;
|
||||
BOOL bSentOK = SendAll(m_sock,(LPBYTE)sendByteBuffer,sendByteBuffer.Size());
|
||||
|
||||
int sent = UDT::send(m_sock,(char*)((LPBYTE)sendByteBuffer), sendByteBuffer.Size(),0);
|
||||
if ( sent != sendByteBuffer.Size() )
|
||||
if ( !bSentOK )
|
||||
{
|
||||
if ( Connect(targetIP, g_ConfigInfo.nPort))
|
||||
{
|
||||
UDT::send(m_sock,(char*)((LPBYTE)sendByteBuffer), sendByteBuffer.Size(),0);
|
||||
sent = sendByteBuffer.Size();
|
||||
bSentOK = SendAll(m_sock,(LPBYTE)sendByteBuffer,sendByteBuffer.Size());
|
||||
}
|
||||
else
|
||||
{
|
||||
debugLog(_T("connect %x %s failed"), targetIP, a2t(inet_ntoa(addr)));
|
||||
}
|
||||
}
|
||||
|
||||
return sent == sendByteBuffer.Size();
|
||||
return bSentOK;
|
||||
}
|
||||
|
||||
|
||||
|
@ -55,34 +95,42 @@ BOOL UdpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
|
|||
sendHead.flag = UDP_FLAG;
|
||||
sendHead.nSize = dwSendSize;
|
||||
|
||||
if (! Send( targetIP, (PBYTE)&sendHead, sizeof(UDP_HEADER))) return FALSE;
|
||||
BOOL ret = FALSE;
|
||||
|
||||
if (! Send( targetIP, pSendData, dwSendSize)) return FALSE;
|
||||
|
||||
UDP_HEADER recvHead = {0};
|
||||
|
||||
int iRecv = UDT::recv(m_sock,(char*)&recvHead, sizeof(UDP_HEADER),0);
|
||||
|
||||
if (iRecv < 0)
|
||||
do
|
||||
{
|
||||
errorLog(_T("recv udp failed WE%d"), ::WSAGetLastError());
|
||||
}
|
||||
if (! Send( targetIP, (PBYTE)&sendHead, sizeof(UDP_HEADER))) break;
|
||||
|
||||
if (! Send( targetIP, pSendData, dwSendSize)) break;
|
||||
|
||||
UDP_HEADER recvHead = {0};
|
||||
|
||||
if ( !ReceiveAll(m_sock,(char*)&recvHead, sizeof(UDP_HEADER)))
|
||||
{
|
||||
errorLog(_T("recv udp failed WE%d"), ::WSAGetLastError());
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
ByteBuffer buffer;
|
||||
buffer.Alloc(recvHead.nSize);
|
||||
ByteBuffer buffer;
|
||||
buffer.Alloc(recvHead.nSize);
|
||||
|
||||
iRecv = UDT::recv(m_sock,(char*)((LPBYTE)buffer),recvHead.nSize,0);
|
||||
if (! ReceiveAll(m_sock,(LPBYTE)buffer,recvHead.nSize))
|
||||
{
|
||||
errorLog(_T("recv udp failed WE%d"), ::WSAGetLastError());
|
||||
break;
|
||||
}
|
||||
|
||||
//¸´ÖÆÊý¾Ý
|
||||
*pRecvData = Alloc(recvHead.nSize);
|
||||
memcpy(*pRecvData, (LPBYTE)buffer, recvHead.nSize);
|
||||
dwRecvSize = recvHead.nSize;
|
||||
|
||||
ret = TRUE;
|
||||
|
||||
} while (FALSE);
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -20,6 +20,9 @@ public:
|
|||
private:
|
||||
BOOL Connect(ULONG targetIP,int port);
|
||||
|
||||
BOOL ReceiveAll(UDTSOCKET s,LPCVOID lpBuf,int nBufLen);
|
||||
BOOL SendAll(UDTSOCKET s,LPCVOID lpBuf, int nBufLen);
|
||||
|
||||
UDTSOCKET m_sock;
|
||||
|
||||
HANDLE m_hRecvEvent;
|
||||
|
|
|
@ -22,6 +22,50 @@ CUdp::~CUdp(void)
|
|||
}
|
||||
|
||||
|
||||
BOOL SendAll(UDTSOCKET s,LPCVOID lpBuf, int nBufLen)
|
||||
{
|
||||
if (UDT::INVALID_SOCK == s)
|
||||
{
|
||||
errorLog(_T("socket is invalid. send failed"));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
const char* p = (const char*) lpBuf;
|
||||
int iLeft = nBufLen;
|
||||
int iSent = UDT::send(s, p, iLeft, 0);
|
||||
while (iSent > 0 && iSent < iLeft)
|
||||
{
|
||||
iLeft -= iSent;
|
||||
p += iSent;
|
||||
|
||||
iSent = UDT::send(s, p, iLeft, 0);
|
||||
}
|
||||
|
||||
return (iSent > 0);
|
||||
}
|
||||
|
||||
BOOL ReceiveAll(UDTSOCKET s, LPCVOID lpBuf,int nBufLen)
|
||||
{
|
||||
if (UDT::INVALID_SOCK == s)
|
||||
{
|
||||
errorLog(_T("socket is invalid. recv failed"));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
char* p = (char*) lpBuf;
|
||||
int iLeft = nBufLen;
|
||||
int iRecv = UDT::recv(s, p, iLeft, 0);
|
||||
while (iRecv > 0 && iRecv < iLeft)
|
||||
{
|
||||
iLeft -= iRecv;
|
||||
p += iRecv;
|
||||
|
||||
iRecv = UDT::recv(s, p, iLeft, 0);
|
||||
}
|
||||
|
||||
return (iRecv > 0);
|
||||
}
|
||||
|
||||
void CUdp::Init()
|
||||
{
|
||||
}
|
||||
|
@ -105,20 +149,20 @@ void CUdp::Worker(LPVOID lpParameter)
|
|||
|
||||
while(ret)
|
||||
{
|
||||
int rsize = UDT::recv(socket,(char*)&header,sizeof(UDP_HEADER),0);
|
||||
int ret = ReceiveAll(socket,(char*)&header,sizeof(UDP_HEADER));
|
||||
if (ret && header.flag == UDP_FLAG)
|
||||
{
|
||||
LPBYTE lpData = (LPBYTE)malloc(header.nSize);
|
||||
|
||||
rsize = UDT::recv(socket,(char*)lpData,header.nSize,0);
|
||||
ret = ReceiveAll(socket,(char*)lpData,header.nSize);
|
||||
|
||||
if ( rsize == header.nSize )
|
||||
if ( ret )
|
||||
{
|
||||
if (argv->handler(lpData,header.nSize,argv->sin,toSender))
|
||||
{
|
||||
header.nSize = toSender.Size();
|
||||
UDT::send(socket,(char*)&header,sizeof(UDP_HEADER),0);
|
||||
UDT::send(socket,(char*)((LPBYTE)toSender),toSender.Size(),0);
|
||||
SendAll(socket,(char*)&header,sizeof(UDP_HEADER));
|
||||
SendAll(socket,(char*)((LPBYTE)toSender),toSender.Size());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue