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 mbed official

Committer:
leihen
Date:
Wed Jun 05 23:40:17 2013 +0000
Revision:
9:4f6f2f35a21a
Parent:
5:48d55083d2ff
Improved 'Accept' handling.
; Added 'peek' functionality for CBuffer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:fb4494783863 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 1:fb4494783863 2 *
samux 1:fb4494783863 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:fb4494783863 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 1:fb4494783863 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 1:fb4494783863 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 1:fb4494783863 7 * furnished to do so, subject to the following conditions:
samux 1:fb4494783863 8 *
samux 1:fb4494783863 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:fb4494783863 10 * substantial portions of the Software.
samux 1:fb4494783863 11 *
samux 1:fb4494783863 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:fb4494783863 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:fb4494783863 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:fb4494783863 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:fb4494783863 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:fb4494783863 17 */
samux 1:fb4494783863 18
samux 1:fb4494783863 19 #include "TCPSocketServer.h"
samux 1:fb4494783863 20 #include <string>
samux 1:fb4494783863 21
leihen 9:4f6f2f35a21a 22 #define _DEBUG 0
leihen 9:4f6f2f35a21a 23
leihen 9:4f6f2f35a21a 24 #if (_DEBUG && !defined(TARGET_LPC11U24))
leihen 9:4f6f2f35a21a 25 #define INFO(x, ...) std::printf("[TCPSocketServer : INFO]"x"\r\n", ##__VA_ARGS__);
leihen 9:4f6f2f35a21a 26 #define WARN(x, ...) std::printf("[TCPSocketServer : WARN]"x"\r\n", ##__VA_ARGS__);
leihen 9:4f6f2f35a21a 27 #define ERR(x, ...) std::printf("[TCPSocketServer : ERR]"x"\r\n", ##__VA_ARGS__);
leihen 9:4f6f2f35a21a 28 #else
leihen 9:4f6f2f35a21a 29 #define INFO(x, ...)
leihen 9:4f6f2f35a21a 30 #define WARN(x, ...)
leihen 9:4f6f2f35a21a 31 #define ERR(x, ...)
leihen 9:4f6f2f35a21a 32 #endif
leihen 9:4f6f2f35a21a 33
leihen 9:4f6f2f35a21a 34
leihen 9:4f6f2f35a21a 35 TCPSocketServer::TCPSocketServer() {connected = false;}
samux 1:fb4494783863 36
samux 1:fb4494783863 37 // Server initialization
samux 1:fb4494783863 38 int TCPSocketServer::bind(int port) {
samux 1:fb4494783863 39 char cmd[20];
samux 1:fb4494783863 40
samux 1:fb4494783863 41 // set TCP protocol
samux 1:fb4494783863 42 wifi->setProtocol(TCP);
samux 1:fb4494783863 43
samux 1:fb4494783863 44 // set local port
samux 1:fb4494783863 45 sprintf(cmd, "set i l %d\r", port);
samux 1:fb4494783863 46 if (!wifi->sendCommand(cmd, "AOK"))
samux 1:fb4494783863 47 return -1;
samux 3:9aa05e19c62e 48
samux 3:9aa05e19c62e 49 // save
samux 3:9aa05e19c62e 50 if (!wifi->sendCommand("save\r", "Stor"))
samux 3:9aa05e19c62e 51 return -1;
samux 3:9aa05e19c62e 52
samux 3:9aa05e19c62e 53 // reboot
samux 3:9aa05e19c62e 54 wifi->reboot();
samux 3:9aa05e19c62e 55
samux 3:9aa05e19c62e 56 // connect the network
samux 4:0bcec6272784 57 if (wifi->isDHCP()) {
samux 4:0bcec6272784 58 if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000))
samux 4:0bcec6272784 59 return -1;
samux 4:0bcec6272784 60 } else {
samux 4:0bcec6272784 61 if (!wifi->sendCommand("join\r", "Associated", NULL, 10000))
samux 4:0bcec6272784 62 return -1;
samux 4:0bcec6272784 63 }
samux 1:fb4494783863 64
samux 3:9aa05e19c62e 65 // exit
samux 1:fb4494783863 66 wifi->exit();
samux 3:9aa05e19c62e 67
samux 3:9aa05e19c62e 68 wait(0.2);
samux 3:9aa05e19c62e 69 wifi->flush();
samux 1:fb4494783863 70 return 0;
samux 1:fb4494783863 71 }
samux 1:fb4494783863 72
samux 1:fb4494783863 73 int TCPSocketServer::listen(int backlog) {
samux 1:fb4494783863 74 if (backlog != 1)
samux 1:fb4494783863 75 return -1;
samux 1:fb4494783863 76 return 0;
samux 1:fb4494783863 77 }
samux 1:fb4494783863 78
samux 1:fb4494783863 79
samux 1:fb4494783863 80 int TCPSocketServer::accept(TCPSocketConnection& connection) {
samux 1:fb4494783863 81 char c;
samux 1:fb4494783863 82 string str;
samux 1:fb4494783863 83 bool o_find = false;
leihen 5:48d55083d2ff 84 Timer tm;
samux 1:fb4494783863 85 while (1) {
leihen 9:4f6f2f35a21a 86 INFO("Trying accept (%s)\n", (connected ? "Connected" : "Unconnected"));
leihen 5:48d55083d2ff 87 while(!wifi->readable())
leihen 5:48d55083d2ff 88 {
leihen 5:48d55083d2ff 89 if (!_blocking && (tm.read_ms() > _timeout))
leihen 5:48d55083d2ff 90 return 1;
leihen 5:48d55083d2ff 91 }
leihen 9:4f6f2f35a21a 92 INFO("Data (\'%c\')\n", wifi->peek());
leihen 9:4f6f2f35a21a 93 if (!connected || (wifi->peek() == '*')) {
leihen 9:4f6f2f35a21a 94 while (!o_find) {
leihen 9:4f6f2f35a21a 95 c = wifi->getc();
leihen 9:4f6f2f35a21a 96 INFO("%c", c);
samux 1:fb4494783863 97 str += c;
leihen 9:4f6f2f35a21a 98 if (c == '*') {
leihen 9:4f6f2f35a21a 99 if (str.find("*OPEN*") != string::npos) {
leihen 9:4f6f2f35a21a 100 // connection found !
leihen 9:4f6f2f35a21a 101 INFO("Connection received !\n");
leihen 9:4f6f2f35a21a 102 connected = true;
leihen 9:4f6f2f35a21a 103 return 0;
leihen 9:4f6f2f35a21a 104 }
leihen 9:4f6f2f35a21a 105 if (str.find("*CLOS*") != string::npos) {
leihen 9:4f6f2f35a21a 106 // Connection closed !
leihen 9:4f6f2f35a21a 107 INFO("Connection closed !\n");
leihen 9:4f6f2f35a21a 108 str = "";
leihen 9:4f6f2f35a21a 109 connected = false;
leihen 9:4f6f2f35a21a 110 break;
leihen 9:4f6f2f35a21a 111 }
samux 1:fb4494783863 112 }
samux 1:fb4494783863 113 }
samux 1:fb4494783863 114 }
leihen 9:4f6f2f35a21a 115 else {
leihen 9:4f6f2f35a21a 116 // We are still connected and there is new data, so just behave as if the accept was done.
leihen 9:4f6f2f35a21a 117 INFO("Connection not yet closed, recycling because new data is available.\n");
leihen 9:4f6f2f35a21a 118 return 0;
leihen 9:4f6f2f35a21a 119 }
samux 1:fb4494783863 120 }
samux 1:fb4494783863 121 }