Skip to content

Commit 7f18110

Browse files
committed
Fixed bug in parsePacket where it could block indefinitely if called when no packets were available to be read.
1 parent 4000c91 commit 7f18110

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

libraries/Ethernet/Udp.cpp

+17-12
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,25 @@ void UDP::write(const uint8_t *buffer, size_t size)
121121

122122
int UDP::parsePacket()
123123
{
124-
//HACK - hand-parse the UDP packet using TCP recv method
125-
uint8_t tmpBuf[8];
126-
int ret =0;
127-
//read 8 header bytes and get IP and port from it
128-
ret = recv(_sock,tmpBuf,8);
129-
if (ret > 0)
124+
if (available() > 0)
130125
{
131-
_remoteIP = tmpBuf;
132-
_remotePort = tmpBuf[4];
133-
_remotePort = (_remotePort << 8) + tmpBuf[5];
134-
// When we get here, any remaining bytes are the data
135-
ret = available();
126+
//HACK - hand-parse the UDP packet using TCP recv method
127+
uint8_t tmpBuf[8];
128+
int ret =0;
129+
//read 8 header bytes and get IP and port from it
130+
ret = recv(_sock,tmpBuf,8);
131+
if (ret > 0)
132+
{
133+
_remoteIP = tmpBuf;
134+
_remotePort = tmpBuf[4];
135+
_remotePort = (_remotePort << 8) + tmpBuf[5];
136+
// When we get here, any remaining bytes are the data
137+
ret = available();
138+
}
139+
return ret;
136140
}
137-
return ret;
141+
// There aren't any packets available
142+
return 0;
138143
}
139144

140145
int UDP::read()

0 commit comments

Comments
 (0)