mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-18 07:52:14 +01:00
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.
This commit is contained in:
parent
bc0f3c4fe1
commit
81b6c69f29
@ -77,14 +77,25 @@ int Client::available() {
|
||||
|
||||
int Client::read() {
|
||||
uint8_t b;
|
||||
if (!available())
|
||||
if ( recv(_sock, &b, 1) )
|
||||
{
|
||||
// recv worked
|
||||
return b;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No data available
|
||||
return -1;
|
||||
recv(_sock, &b, 1);
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
int Client::read(uint8_t *buf, size_t size) {
|
||||
return recv(_sock, buf, size);
|
||||
}
|
||||
|
||||
int Client::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;
|
||||
::peek(_sock, &b);
|
||||
|
@ -17,6 +17,7 @@ public:
|
||||
virtual void write(const uint8_t *buf, size_t size);
|
||||
virtual int available();
|
||||
virtual int read();
|
||||
virtual int read(uint8_t *buf, size_t size);
|
||||
virtual int peek();
|
||||
virtual void flush();
|
||||
void stop();
|
||||
|
@ -146,14 +146,18 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
|
||||
*/
|
||||
uint16_t recv(SOCKET s, uint8_t *buf, uint16_t len)
|
||||
{
|
||||
uint16_t ret=0;
|
||||
|
||||
if ( len > 0 )
|
||||
// Check how much data is available
|
||||
uint16_t ret = W5100.getRXReceivedSize(s);
|
||||
if (ret > len)
|
||||
{
|
||||
W5100.recv_data_processing(s, buf, len);
|
||||
W5100.execCmdSn(s, Sock_RECV);
|
||||
ret = len;
|
||||
}
|
||||
|
||||
if ( ret > 0 )
|
||||
{
|
||||
W5100.recv_data_processing(s, buf, ret);
|
||||
W5100.execCmdSn(s, Sock_RECV);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user