Fixed bug in parsePacket where it could block indefinitely if called when no packets were available to be read.

This commit is contained in:
amcewen 2011-02-04 21:44:51 +00:00 committed by David A. Mellis
parent a9c2ebf455
commit 1b56de694b
1 changed files with 17 additions and 12 deletions

View File

@ -121,20 +121,25 @@ void UDP::write(const uint8_t *buffer, size_t size)
int UDP::parsePacket() int UDP::parsePacket()
{ {
//HACK - hand-parse the UDP packet using TCP recv method if (available() > 0)
uint8_t tmpBuf[8];
int ret =0;
//read 8 header bytes and get IP and port from it
ret = recv(_sock,tmpBuf,8);
if (ret > 0)
{ {
_remoteIP = tmpBuf; //HACK - hand-parse the UDP packet using TCP recv method
_remotePort = tmpBuf[4]; uint8_t tmpBuf[8];
_remotePort = (_remotePort << 8) + tmpBuf[5]; int ret =0;
// When we get here, any remaining bytes are the data //read 8 header bytes and get IP and port from it
ret = available(); ret = recv(_sock,tmpBuf,8);
if (ret > 0)
{
_remoteIP = tmpBuf;
_remotePort = tmpBuf[4];
_remotePort = (_remotePort << 8) + tmpBuf[5];
// When we get here, any remaining bytes are the data
ret = available();
}
return ret;
} }
return ret; // There aren't any packets available
return 0;
} }
int UDP::read() int UDP::read()