Skip to content

Commit 81b6c69

Browse files
committed
An improved patch for the Client part of issue 416 (adding a multi-byte read). This one moves all of the checking into recv, so that single-byte reads also benefit. It also returns -1 if there's no data available unless we've reached EOF, in which case it returns 0.
1 parent bc0f3c4 commit 81b6c69

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

libraries/Ethernet/Client.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,25 @@ int Client::available() {
7777

7878
int Client::read() {
7979
uint8_t b;
80-
if (!available())
80+
if ( recv(_sock, &b, 1) )
81+
{
82+
// recv worked
83+
return b;
84+
}
85+
else
86+
{
87+
// No data available
8188
return -1;
82-
recv(_sock, &b, 1);
83-
return b;
89+
}
90+
}
91+
92+
int Client::read(uint8_t *buf, size_t size) {
93+
return recv(_sock, buf, size);
8494
}
8595

8696
int Client::peek() {
8797
uint8_t b;
98+
// Unlike recv, peek doesn't check to see if there's any data available, so we must
8899
if (!available())
89100
return -1;
90101
::peek(_sock, &b);

libraries/Ethernet/Client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Client : public Stream {
1717
virtual void write(const uint8_t *buf, size_t size);
1818
virtual int available();
1919
virtual int read();
20+
virtual int read(uint8_t *buf, size_t size);
2021
virtual int peek();
2122
virtual void flush();
2223
void stop();

libraries/Ethernet/utility/socket.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,17 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
146146
*/
147147
uint16_t recv(SOCKET s, uint8_t *buf, uint16_t len)
148148
{
149-
uint16_t ret=0;
149+
// Check how much data is available
150+
uint16_t ret = W5100.getRXReceivedSize(s);
151+
if (ret > len)
152+
{
153+
ret = len;
154+
}
150155

151-
if ( len > 0 )
156+
if ( ret > 0 )
152157
{
153-
W5100.recv_data_processing(s, buf, len);
158+
W5100.recv_data_processing(s, buf, ret);
154159
W5100.execCmdSn(s, Sock_RECV);
155-
ret = len;
156160
}
157161
return ret;
158162
}

0 commit comments

Comments
 (0)