peek() function always returns the first byte of the packet.
Expected behavior
The peek()should return the same data byte as the next read() call.
Explanation
This is the implementation of the UDP peek function:
int EthernetUDP::peek()
{
uint8_t b;
// Unlike recv, peek doesn't check to see if there's any data available, so we must.
// If the user hasn't called parsePacket yet then return nothing otherwise they
// may get the UDP header
if (!_remaining) {
return -1;
}
b = pbuf_get_at(_udp.data.p, 0);
return b;
}
It is just a wrapper arround the lwip function pbuf_get_at(). This function returns the byte at index 0 of the current buffer.
It should returns the byte at the current read position of the current buffer.
Fix
The function is modified to take into account the read position.
int EthernetUDP::peek()
{
uint8_t b;
// Unlike recv, peek doesn't check to see if there's any data available, so we must.
// If the user hasn't called parsePacket yet then return nothing otherwise they
// may get the UDP header
if (!_remaining) {
return -1;
}
b = pbuf_get_at(_udp.data.p, _udp.data.p->tot_len - _udp.data.available);
return b;
}
Now peek() function returns the byte of the packet at the current read position as expected.
Same correction can be applied for the EthernetClient class (untested):
int EthernetClient::peek()
{
uint8_t b;
// Unlike recv, peek doesn't check to see if there's any data available, so we must
if (!available()) {
return -1;
}
b = pbuf_get_at(_tcp_client->data.p, _tcp_client->data.p->tot_len - _tcp_client->data.available);
return b;
}
Description The
peek()
functions of theEthernetUDP
andEthernetClient
classes always returns the first byte of a packet. A fix is proposed.To Reproduce
Code used:
Command to generate a packet:
Serial output:
peek()
function always returns the first byte of the packet.Expected behavior The
peek()
should return the same data byte as the nextread()
call.Explanation
This is the implementation of the UDP
peek
function:It is just a wrapper arround the lwip function
pbuf_get_at()
. This function returns the byte at index 0 of the current buffer. It should returns the byte at the current read position of the current buffer.Fix
The function is modified to take into account the read position.
Serial output:
Now
peek()
function returns the byte of the packet at the current read position as expected.Same correction can be applied for the
EthernetClient
class (untested):Desktop
Hardware