Derek Fukumori / Mbed 2 deprecated NetworkAnimator

Dependencies:   mbed

Revision:
0:b6a536e40b5d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TCPEchoHandler.cpp	Wed Jun 20 22:16:31 2012 +0000
@@ -0,0 +1,91 @@
+#include "TCPEchoHandler.h"
+
+
+char* screenBufB;
+int n = 0;
+extern int bytes_received;
+extern bool drawReadyFlag;
+extern bool networkFlag;
+
+#define SCRBUFSIZE 1440
+
+// When the constructor's called, initialise the member variables
+TCPEchoHandler::TCPEchoHandler(TCPSocket* tcpClientSocket, char* scrbuf)
+        : NetService()
+        , clientSocket(tcpClientSocket)
+        , closed(0)
+        , timeoutWatchdog() {
+    // Wire up the event handler on the client TCP socket
+    clientSocket->setOnEvent(this, &TCPEchoHandler::onTCPSocketEvent);
+    screenBufB = scrbuf;
+}
+
+TCPEchoHandler::~TCPEchoHandler() {
+    // Close the socket on destruction
+    close();
+}
+
+void TCPEchoHandler::onTCPSocketEvent(TCPSocketEvent e) {
+    //printf("socketevent %i\r\n", (int) e);
+    switch (e) {
+        // If the socket is readable, do stuff
+        case TCPSOCKET_READABLE: {
+            timeoutWatchdog.detach();
+            //printf("starting read\r\n");
+            int len = 0;
+            while (len = clientSocket->recv(&screenBufB[bytes_received], 1440 - bytes_received)) {
+                //printf("len: %d\r\n", len);
+                bytes_received += len;
+            }
+            //printf("received: %d\r\n", bytes_received);
+            if (bytes_received >= 1440) {
+                //printf("setting draw flag\r\n");
+                bytes_received = 0;
+                drawReadyFlag = true;
+            }
+            
+            // Reset timeout countdown
+            //printf("setting timeout");
+            setTimeout(ECHO_TIMEOUT);
+            //close();
+            //printf("breaking");
+            break;
+        }
+        case TCPSOCKET_CONTIMEOUT:
+        case TCPSOCKET_CONRST:
+        case TCPSOCKET_CONABRT:
+        case TCPSOCKET_ERROR:
+        case TCPSOCKET_DISCONNECTED: {
+            // Close the socket on any terminal TCP event
+            printf("finished\r\n");
+            close();
+            break;
+        }
+    }
+}
+
+void TCPEchoHandler::close() {
+    // Prevent recursive calling or calling on an object being destructed by someone else
+    if ( closed )
+        return;
+    closed = 1;
+    timeoutWatchdog.detach();
+    if ( clientSocket ) {
+        clientSocket->resetOnEvent();
+        clientSocket->close();
+        delete clientSocket; //This fn might have been called by this socket (through an event), so DO NOT DESTROY IT HERE
+    }
+    // Flags this service as closed - will be destructed and deleted on
+    // the next call of NetService::poll() by Net::poll()
+    NetService::close();
+}
+
+void TCPEchoHandler::setTimeout(unsigned int timeout) {
+    // Attach our timeout handler to the timeout watchdog timer to close the socket if no activity
+    //timeoutWatchdog.attach_us<TCPEchoHandler>(this, &TCPEchoHandler::onTimeout, ECHO_TIMEOUT * 1000);
+}
+
+void TCPEchoHandler::onTimeout() {
+    // Nothing fancy, just close the socket and mark this class for destruction
+    close();
+}