From 1b56de694b60a7a5e9ff72d2edabc8c14a9628f8 Mon Sep 17 00:00:00 2001 From: amcewen Date: Fri, 4 Feb 2011 21:44:51 +0000 Subject: [PATCH] Fixed bug in parsePacket where it could block indefinitely if called when no packets were available to be read. --- libraries/Ethernet/Udp.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/libraries/Ethernet/Udp.cpp b/libraries/Ethernet/Udp.cpp index 2c8b10332..aed5d983b 100644 --- a/libraries/Ethernet/Udp.cpp +++ b/libraries/Ethernet/Udp.cpp @@ -121,20 +121,25 @@ void UDP::write(const uint8_t *buffer, size_t size) int UDP::parsePacket() { - //HACK - hand-parse the UDP packet using TCP recv method - 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) + if (available() > 0) { - _remoteIP = tmpBuf; - _remotePort = tmpBuf[4]; - _remotePort = (_remotePort << 8) + tmpBuf[5]; - // When we get here, any remaining bytes are the data - ret = available(); + //HACK - hand-parse the UDP packet using TCP recv method + 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; + _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()