Echo Server based on the legacy EthernetNetIf libraries used for a performance comparison with the new networking libraries
Dependencies: EthernetNetIf mbed
Fork of EchoServer by
TCPEchoHandler.h@1:7b4661a721c1, 2012-08-01 (annotated)
- Committer:
- emilmont
- Date:
- Wed Aug 01 16:25:14 2012 +0000
- Revision:
- 1:7b4661a721c1
- Parent:
- 0:fcd581e3ad7d
Echo Server based on the legacy EthernetNetIf libraries used for a performance comparison with the new networking libraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
naegawa | 0:fcd581e3ad7d | 1 | #ifndef TCP_ECHO_HANDLER_H |
naegawa | 0:fcd581e3ad7d | 2 | #define TCP_ECHO_HANDLER_H |
naegawa | 0:fcd581e3ad7d | 3 | |
naegawa | 0:fcd581e3ad7d | 4 | #include "mbed.h" |
naegawa | 0:fcd581e3ad7d | 5 | #include "TCPSocket.h" |
naegawa | 0:fcd581e3ad7d | 6 | #include <netservice.h> |
naegawa | 0:fcd581e3ad7d | 7 | // Constant: ECHO_TIMOUT |
naegawa | 0:fcd581e3ad7d | 8 | // The timeout period for inactivity in milliseconds |
naegawa | 0:fcd581e3ad7d | 9 | #define ECHO_TIMEOUT 5000 |
naegawa | 0:fcd581e3ad7d | 10 | |
naegawa | 0:fcd581e3ad7d | 11 | /* |
naegawa | 0:fcd581e3ad7d | 12 | Class: TCPEchoHandler |
naegawa | 0:fcd581e3ad7d | 13 | A class instantiated to handle the incoming TCP client connection |
naegawa | 0:fcd581e3ad7d | 14 | Extends NetService to hook into the TCP/IP stack's polling |
naegawa | 0:fcd581e3ad7d | 15 | and destruction service |
naegawa | 0:fcd581e3ad7d | 16 | */ |
naegawa | 0:fcd581e3ad7d | 17 | class TCPEchoHandler : public NetService { |
naegawa | 0:fcd581e3ad7d | 18 | public: |
naegawa | 0:fcd581e3ad7d | 19 | // Constructor: TCPEchoHandler |
naegawa | 0:fcd581e3ad7d | 20 | // Setup and handle the incoming connection |
naegawa | 0:fcd581e3ad7d | 21 | TCPEchoHandler(TCPSocket*); |
naegawa | 0:fcd581e3ad7d | 22 | virtual ~TCPEchoHandler(); |
naegawa | 0:fcd581e3ad7d | 23 | private: |
naegawa | 0:fcd581e3ad7d | 24 | // Variable: clientSocket |
naegawa | 0:fcd581e3ad7d | 25 | // The incoming TCP socket from the client |
naegawa | 0:fcd581e3ad7d | 26 | TCPSocket* clientSocket; |
naegawa | 0:fcd581e3ad7d | 27 | // Variable: closed |
naegawa | 0:fcd581e3ad7d | 28 | // A marker to say whether this socket is already closed |
naegawa | 0:fcd581e3ad7d | 29 | int closed; |
naegawa | 0:fcd581e3ad7d | 30 | // Variable: timeoutWatchdog |
naegawa | 0:fcd581e3ad7d | 31 | // A timer to countdown from during inactivity and close |
naegawa | 0:fcd581e3ad7d | 32 | // dormant connections |
naegawa | 0:fcd581e3ad7d | 33 | Timeout timeoutWatchdog; |
naegawa | 0:fcd581e3ad7d | 34 | |
naegawa | 0:fcd581e3ad7d | 35 | // Function: onNetTcpSocketEvent |
naegawa | 0:fcd581e3ad7d | 36 | // The callback function called by the network stack whenever an |
naegawa | 0:fcd581e3ad7d | 37 | // event occurs on the TCP socket |
naegawa | 0:fcd581e3ad7d | 38 | // Parameter: e - The event that has occurred |
naegawa | 0:fcd581e3ad7d | 39 | void onTCPSocketEvent(TCPSocketEvent e); |
naegawa | 0:fcd581e3ad7d | 40 | // Function: close |
naegawa | 0:fcd581e3ad7d | 41 | // Closes the client socket and marks this class as done with |
naegawa | 0:fcd581e3ad7d | 42 | // for the TCP/IP stack to destroy |
naegawa | 0:fcd581e3ad7d | 43 | virtual void close(); |
naegawa | 0:fcd581e3ad7d | 44 | // Function: setTimeout |
naegawa | 0:fcd581e3ad7d | 45 | // Parameter: timeout - The length of time to wait for more activity in milliseconds |
naegawa | 0:fcd581e3ad7d | 46 | void setTimeout(unsigned int timeout); |
naegawa | 0:fcd581e3ad7d | 47 | // Function: onTimeout |
naegawa | 0:fcd581e3ad7d | 48 | // The handler called by the timeout watchdog to close the connection when timed out |
naegawa | 0:fcd581e3ad7d | 49 | void onTimeout(); |
naegawa | 0:fcd581e3ad7d | 50 | }; |
naegawa | 0:fcd581e3ad7d | 51 | |
naegawa | 0:fcd581e3ad7d | 52 | #endif |