Forked mbed official WiflyInterface (interface for Roving Networks Wifly modules) which includes the possibility to use TCPSocketServer::accept as a non-blocking cal.
Dependents: WiFlyHTTPServerSample MultiThreadingHTTPServer
Fork of WiflyInterface by
Diff: Socket/TCPSocketServer.cpp
- Revision:
- 9:4f6f2f35a21a
- Parent:
- 5:48d55083d2ff
--- a/Socket/TCPSocketServer.cpp Sun Jun 02 00:26:25 2013 +0000 +++ b/Socket/TCPSocketServer.cpp Wed Jun 05 23:40:17 2013 +0000 @@ -19,7 +19,20 @@ #include "TCPSocketServer.h" #include <string> -TCPSocketServer::TCPSocketServer() {} +#define _DEBUG 0 + +#if (_DEBUG && !defined(TARGET_LPC11U24)) +#define INFO(x, ...) std::printf("[TCPSocketServer : INFO]"x"\r\n", ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[TCPSocketServer : WARN]"x"\r\n", ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[TCPSocketServer : ERR]"x"\r\n", ##__VA_ARGS__); +#else +#define INFO(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) +#endif + + +TCPSocketServer::TCPSocketServer() {connected = false;} // Server initialization int TCPSocketServer::bind(int port) { @@ -65,31 +78,44 @@ int TCPSocketServer::accept(TCPSocketConnection& connection) { - int nb_available = 0, pos = 0; char c; string str; bool o_find = false; Timer tm; while (1) { + INFO("Trying accept (%s)\n", (connected ? "Connected" : "Unconnected")); while(!wifi->readable()) { if (!_blocking && (tm.read_ms() > _timeout)) return 1; } - nb_available = wifi->readable(); - for (int i = 0; i < nb_available; i++) { - c = wifi->getc(); - if (c == '*') { - o_find = true; - } - if (o_find && c != '\r' && c != '\n') { + INFO("Data (\'%c\')\n", wifi->peek()); + if (!connected || (wifi->peek() == '*')) { + while (!o_find) { + c = wifi->getc(); + INFO("%c", c); str += c; - pos = str.find("*OPEN*"); - if (pos != string::npos) { - wifi->flush(); - return 0; + if (c == '*') { + if (str.find("*OPEN*") != string::npos) { + // connection found ! + INFO("Connection received !\n"); + connected = true; + return 0; + } + if (str.find("*CLOS*") != string::npos) { + // Connection closed ! + INFO("Connection closed !\n"); + str = ""; + connected = false; + break; + } } } } + else { + // We are still connected and there is new data, so just behave as if the accept was done. + INFO("Connection not yet closed, recycling because new data is available.\n"); + return 0; + } } } \ No newline at end of file