The single-byte Client::read() function in the Ethernet module is horrendously expensive. It seems to be able to read less than 3k bytes per second. Since there's already a multibyte write() function, it makes sense to also have a multibyte read(). Patch follows:
The return code from this version of read, when there is not data to read, is 0. For other read methods, when there is nothing to read, the return code is -1.
Well. It returns the number for bytes read. Returning 0 for reading 0 bytes is consistent with that.
Also, see "man 2 read" on any Linux/Unix machine: "On success, the number of bytes read is returned (zero indicates end of file)..." OK. I know Client::read() doesn't have to be consistent with Unix read(), but it does indicate that's the sensible thing to do.
Client::read(void) returns -1 because 0 is a valid return value, which is the behavior of Unix getchar(), also following the same design necessities.
Please see socket.cpp file and recv function (arduino ide ver 21):
/**
* @brief This function is an application I/F function which is used to receive the data in TCP mode.
* It continues to wait for data as much as the application wants to receive.
*
* @return received data size for success else -1.
*/
uint16_t recv(SOCKET s, uint8_t *buf, uint16_t len)
{
uint16_t ret=0;
if ( len > 0 )
{
W5100.recv_data_processing(s, buf, len);
W5100.execCmdSn(s, Sock_RECV);
ret = len;
}
return ret;
}
It will not return -1 in any case Above description for return value is wrong
Anyway, I think that is optimized version for this function: