@ -226,6 +226,7 @@ class NetworkConnection { // Essentially a Socket wrapper
int listen();
int isConnected();
int available();
int read();
void NetworkConnection::close();
@ -249,9 +250,40 @@ int NetworkConnection::listen() { // TODO: Make private or protected?
return !!::listen(_socket); // TODO: Use C++ namespaces for the driver functions?
}
int NetworkConnection::available() {
/*
Functionality matches 'Serial.available()'.
Note: If the socket is not connected then this will return 0,
but it is intended that 'isConnected()' would be checked first.
Returns:
"The number of bytes available to read ... or 0 if none are available.
If any data has come in, [...].available() will be greater than 0."
*/
// TODO: Do we want to check for 'isConnected' as well, or not?
// We have to, I guess, for valid behaviour with 'getSn_*'.
if (!isConnected()) {
return 0;
}
return getSn_RX_RSR(_socket);
}
#define NO_READ_DATA_AVAILABLE -1
int NetworkConnection::read() {
/*
Functionality matches 'Serial.read()'.
Returns:
"an int, the first byte of incoming serial data available
(or -1 if no data is available)."
Note: I thought 'recv' blocked until data was available,
but it currently seems not to block after all.
However, because I'm seeking to match the 'Serial.read'
@ -261,6 +293,11 @@ int NetworkConnection::read() {
*/
uint8_t theByte;
if (!available()) {
return NO_READ_DATA_AVAILABLE;
}
recv(_socket, &theByte, 1);
return theByte;
}
@ -403,6 +440,14 @@ void setup () {
Serial.println("Connected...");
while (conn.isConnected()) {
if (conn.available()) {
Serial.print(conn.read(), BYTE);
}
}
Serial.println("");
conn.close();
Serial.println("End test and dummy loop");