Robert Cook
/
NetworkAnimator
TCPEchoHandler.cpp@0:32a0996dff0f, 2012-06-13 (annotated)
- Committer:
- robertcook
- Date:
- Wed Jun 13 18:39:46 2012 +0000
- Revision:
- 0:32a0996dff0f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robertcook | 0:32a0996dff0f | 1 | #include "TCPEchoHandler.h" |
robertcook | 0:32a0996dff0f | 2 | extern char *screenBuf; |
robertcook | 0:32a0996dff0f | 3 | extern bool drawReadyFlag; |
robertcook | 0:32a0996dff0f | 4 | |
robertcook | 0:32a0996dff0f | 5 | #define SCRBUFSIZE 1440 |
robertcook | 0:32a0996dff0f | 6 | |
robertcook | 0:32a0996dff0f | 7 | // When the constructor's called, initialise the member variables |
robertcook | 0:32a0996dff0f | 8 | TCPEchoHandler::TCPEchoHandler(TCPSocket* tcpClientSocket) |
robertcook | 0:32a0996dff0f | 9 | : NetService() |
robertcook | 0:32a0996dff0f | 10 | , clientSocket(tcpClientSocket) |
robertcook | 0:32a0996dff0f | 11 | , closed(0) |
robertcook | 0:32a0996dff0f | 12 | , timeoutWatchdog() { |
robertcook | 0:32a0996dff0f | 13 | // Wire up the event handler on the client TCP socket |
robertcook | 0:32a0996dff0f | 14 | clientSocket->setOnEvent(this, &TCPEchoHandler::onTCPSocketEvent); |
robertcook | 0:32a0996dff0f | 15 | } |
robertcook | 0:32a0996dff0f | 16 | |
robertcook | 0:32a0996dff0f | 17 | TCPEchoHandler::~TCPEchoHandler() { |
robertcook | 0:32a0996dff0f | 18 | // Close the socket on destruction |
robertcook | 0:32a0996dff0f | 19 | close(); |
robertcook | 0:32a0996dff0f | 20 | } |
robertcook | 0:32a0996dff0f | 21 | |
robertcook | 0:32a0996dff0f | 22 | void TCPEchoHandler::onTCPSocketEvent(TCPSocketEvent e) { |
robertcook | 0:32a0996dff0f | 23 | printf("socketevent %i\n", (int) e); |
robertcook | 0:32a0996dff0f | 24 | switch (e) { |
robertcook | 0:32a0996dff0f | 25 | // If the socket is readable, do stuff |
robertcook | 0:32a0996dff0f | 26 | case TCPSOCKET_READABLE: |
robertcook | 0:32a0996dff0f | 27 | printf("made it!\n"); |
robertcook | 0:32a0996dff0f | 28 | // Disable the timeout watchdog timer |
robertcook | 0:32a0996dff0f | 29 | timeoutWatchdog.detach(); |
robertcook | 0:32a0996dff0f | 30 | // Read in any available data into the buffer |
robertcook | 0:32a0996dff0f | 31 | printf("made it again!\n"); |
robertcook | 0:32a0996dff0f | 32 | while ( int len = clientSocket->recv(screenBuf, 128) ) { |
robertcook | 0:32a0996dff0f | 33 | printf("."); |
robertcook | 0:32a0996dff0f | 34 | |
robertcook | 0:32a0996dff0f | 35 | } |
robertcook | 0:32a0996dff0f | 36 | drawReadyFlag = true; |
robertcook | 0:32a0996dff0f | 37 | printf("finished\n"); |
robertcook | 0:32a0996dff0f | 38 | // Reset timeout countdown |
robertcook | 0:32a0996dff0f | 39 | setTimeout(ECHO_TIMEOUT); |
robertcook | 0:32a0996dff0f | 40 | break; |
robertcook | 0:32a0996dff0f | 41 | case TCPSOCKET_CONTIMEOUT: |
robertcook | 0:32a0996dff0f | 42 | case TCPSOCKET_CONRST: |
robertcook | 0:32a0996dff0f | 43 | case TCPSOCKET_CONABRT: |
robertcook | 0:32a0996dff0f | 44 | case TCPSOCKET_ERROR: |
robertcook | 0:32a0996dff0f | 45 | case TCPSOCKET_DISCONNECTED: |
robertcook | 0:32a0996dff0f | 46 | // Close the socket on any terminal TCP event |
robertcook | 0:32a0996dff0f | 47 | close(); |
robertcook | 0:32a0996dff0f | 48 | break; |
robertcook | 0:32a0996dff0f | 49 | } |
robertcook | 0:32a0996dff0f | 50 | |
robertcook | 0:32a0996dff0f | 51 | } |
robertcook | 0:32a0996dff0f | 52 | |
robertcook | 0:32a0996dff0f | 53 | void TCPEchoHandler::close() { |
robertcook | 0:32a0996dff0f | 54 | // Prevent recursive calling or calling on an object being destructed by someone else |
robertcook | 0:32a0996dff0f | 55 | if ( closed ) |
robertcook | 0:32a0996dff0f | 56 | return; |
robertcook | 0:32a0996dff0f | 57 | closed = 1; |
robertcook | 0:32a0996dff0f | 58 | timeoutWatchdog.detach(); |
robertcook | 0:32a0996dff0f | 59 | if ( clientSocket ) { |
robertcook | 0:32a0996dff0f | 60 | clientSocket->resetOnEvent(); |
robertcook | 0:32a0996dff0f | 61 | clientSocket->close(); |
robertcook | 0:32a0996dff0f | 62 | delete clientSocket; //This fn might have been called by this socket (through an event), so DO NOT DESTROY IT HERE |
robertcook | 0:32a0996dff0f | 63 | } |
robertcook | 0:32a0996dff0f | 64 | // Flags this service as closed - will be destructed and deleted on |
robertcook | 0:32a0996dff0f | 65 | // the next call of NetService::poll() by Net::poll() |
robertcook | 0:32a0996dff0f | 66 | NetService::close(); |
robertcook | 0:32a0996dff0f | 67 | } |
robertcook | 0:32a0996dff0f | 68 | |
robertcook | 0:32a0996dff0f | 69 | void TCPEchoHandler::setTimeout(unsigned int timeout) { |
robertcook | 0:32a0996dff0f | 70 | // Attach our timeout handler to the timeout watchdog timer to close the socket if no activity |
robertcook | 0:32a0996dff0f | 71 | timeoutWatchdog.attach_us<TCPEchoHandler>(this, &TCPEchoHandler::onTimeout, ECHO_TIMEOUT * 1000); |
robertcook | 0:32a0996dff0f | 72 | } |
robertcook | 0:32a0996dff0f | 73 | |
robertcook | 0:32a0996dff0f | 74 | void TCPEchoHandler::onTimeout() { |
robertcook | 0:32a0996dff0f | 75 | // Nothing fancy, just close the socket and mark this class for destruction |
robertcook | 0:32a0996dff0f | 76 | close(); |
robertcook | 0:32a0996dff0f | 77 | } |