|
|
@ -423,6 +423,7 @@ class EchoServer {
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
EchoServer(int port);
|
|
|
|
EchoServer(int port);
|
|
|
|
|
|
|
|
void next(); // TODO: Return something useful?
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
NetworkConnection _connection; // TODO: Make public?
|
|
|
|
NetworkConnection _connection; // TODO: Make public?
|
|
|
@ -439,6 +440,41 @@ EchoServer::EchoServer(int port) : _connection (NetworkConnection(port)) {
|
|
|
|
_connection.listen(); // TODO: We should be using Network.listen(...) here and in initialisation list.
|
|
|
|
_connection.listen(); // TODO: We should be using Network.listen(...) here and in initialisation list.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void EchoServer::next() {
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Use better state machine implementation?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (_state == ECHO_CONNECT_WAIT) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (_connection.isConnected()) {
|
|
|
|
|
|
|
|
_state = ECHO_CONNECTED;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Keep waiting
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (_state == ECHO_CONNECTED) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (_connection.available()) {
|
|
|
|
|
|
|
|
_connection.print(_connection.read());
|
|
|
|
|
|
|
|
} else if (!_connection.isConnected()) {
|
|
|
|
|
|
|
|
// Data finished and client disconnected
|
|
|
|
|
|
|
|
_state == ECHO_CLOSE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (_state == ECHO_CLOSE) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_connection.close();
|
|
|
|
|
|
|
|
_state = ECHO_HALT;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (_state == ECHO_HALT) {
|
|
|
|
|
|
|
|
// Do nothing
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Unknown state, do nothing.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------- */
|
|
|
|
/* ----------------------- */
|
|
|
|
|
|
|
|
|
|
|
|