Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TCPEchoHandler.cpp
00001 #include "TCPEchoHandler.h" 00002 00003 00004 char* screenBufB; 00005 int n = 0; 00006 extern int bytes_received; 00007 extern bool drawReadyFlag; 00008 extern bool networkFlag; 00009 00010 #define SCRBUFSIZE 1440 00011 00012 // When the constructor's called, initialise the member variables 00013 TCPEchoHandler::TCPEchoHandler(TCPSocket* tcpClientSocket, char* scrbuf) 00014 : NetService() 00015 , clientSocket(tcpClientSocket) 00016 , closed(0) 00017 , timeoutWatchdog() { 00018 // Wire up the event handler on the client TCP socket 00019 clientSocket->setOnEvent(this, &TCPEchoHandler::onTCPSocketEvent); 00020 screenBufB = scrbuf; 00021 } 00022 00023 TCPEchoHandler::~TCPEchoHandler() { 00024 // Close the socket on destruction 00025 close(); 00026 } 00027 00028 void TCPEchoHandler::onTCPSocketEvent(TCPSocketEvent e) { 00029 //printf("socketevent %i\r\n", (int) e); 00030 switch (e) { 00031 // If the socket is readable, do stuff 00032 case TCPSOCKET_READABLE: { 00033 timeoutWatchdog.detach(); 00034 //printf("starting read\r\n"); 00035 int len = 0; 00036 while (len = clientSocket->recv(&screenBufB[bytes_received], 1440 - bytes_received)) { 00037 //printf("len: %d\r\n", len); 00038 bytes_received += len; 00039 } 00040 //printf("received: %d\r\n", bytes_received); 00041 if (bytes_received >= 1440) { 00042 //printf("setting draw flag\r\n"); 00043 bytes_received = 0; 00044 drawReadyFlag = true; 00045 } 00046 00047 // Reset timeout countdown 00048 //printf("setting timeout"); 00049 setTimeout(ECHO_TIMEOUT); 00050 //close(); 00051 //printf("breaking"); 00052 break; 00053 } 00054 case TCPSOCKET_CONTIMEOUT: 00055 case TCPSOCKET_CONRST: 00056 case TCPSOCKET_CONABRT: 00057 case TCPSOCKET_ERROR: 00058 case TCPSOCKET_DISCONNECTED: { 00059 // Close the socket on any terminal TCP event 00060 printf("finished\r\n"); 00061 close(); 00062 break; 00063 } 00064 } 00065 } 00066 00067 void TCPEchoHandler::close() { 00068 // Prevent recursive calling or calling on an object being destructed by someone else 00069 if ( closed ) 00070 return; 00071 closed = 1; 00072 timeoutWatchdog.detach(); 00073 if ( clientSocket ) { 00074 clientSocket->resetOnEvent(); 00075 clientSocket->close(); 00076 delete clientSocket; //This fn might have been called by this socket (through an event), so DO NOT DESTROY IT HERE 00077 } 00078 // Flags this service as closed - will be destructed and deleted on 00079 // the next call of NetService::poll() by Net::poll() 00080 NetService::close(); 00081 } 00082 00083 void TCPEchoHandler::setTimeout(unsigned int timeout) { 00084 // Attach our timeout handler to the timeout watchdog timer to close the socket if no activity 00085 //timeoutWatchdog.attach_us<TCPEchoHandler>(this, &TCPEchoHandler::onTimeout, ECHO_TIMEOUT * 1000); 00086 } 00087 00088 void TCPEchoHandler::onTimeout() { 00089 // Nothing fancy, just close the socket and mark this class for destruction 00090 close(); 00091 }
Generated on Wed Jul 13 2022 09:03:52 by
1.7.2