Custom and bugfix

Dependents:   HTTPClient_HelloWorld_WIZ820io NTPClient_HelloWorld_WIZ820io TinyHTTPServer_WIZ820io

Fork of WIZ820ioInterface by Norimasa Okamoto

Committer:
ban4jp
Date:
Tue Feb 04 03:20:06 2014 +0000
Revision:
8:6ff41cd782f5
Parent:
5:fb15c35d1e28
Fixed TCPSocketServer compatibility. (2nd connection is not accepted.)

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
samux 1:fb4494783863 21 TCPSocketServer::TCPSocketServer() {}
samux 1:fb4494783863 22
samux 1:fb4494783863 23 // Server initialization
samux 1:fb4494783863 24 int TCPSocketServer::bind(int port) {
ban4jp 8:6ff41cd782f5 25 _port = port;
ban4jp 8:6ff41cd782f5 26
va009039 5:fb15c35d1e28 27 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 28 _sock_fd = eth->new_socket();
va009039 5:fb15c35d1e28 29 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 30 return -1;
va009039 5:fb15c35d1e28 31 }
va009039 5:fb15c35d1e28 32 }
ban4jp 8:6ff41cd782f5 33
ban4jp 8:6ff41cd782f5 34 return bind_socket();
ban4jp 8:6ff41cd782f5 35 }
ban4jp 8:6ff41cd782f5 36
ban4jp 8:6ff41cd782f5 37 int TCPSocketServer::bind_socket() {
samux 1:fb4494783863 38 // set TCP protocol
va009039 5:fb15c35d1e28 39 eth->setProtocol(_sock_fd, TCP);
samux 1:fb4494783863 40 // set local port
ban4jp 8:6ff41cd782f5 41 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, _port);
samux 3:9aa05e19c62e 42 // connect the network
va009039 5:fb15c35d1e28 43 eth->scmd(_sock_fd, OPEN);
samux 1:fb4494783863 44 return 0;
samux 1:fb4494783863 45 }
samux 1:fb4494783863 46
samux 1:fb4494783863 47 int TCPSocketServer::listen(int backlog) {
va009039 5:fb15c35d1e28 48 if (_sock_fd < 0) {
samux 1:fb4494783863 49 return -1;
va009039 5:fb15c35d1e28 50 }
va009039 5:fb15c35d1e28 51 if (backlog != 1) {
va009039 5:fb15c35d1e28 52 return -1;
va009039 5:fb15c35d1e28 53 }
va009039 5:fb15c35d1e28 54 eth->scmd(_sock_fd, LISTEN);
samux 1:fb4494783863 55 return 0;
samux 1:fb4494783863 56 }
samux 1:fb4494783863 57
samux 1:fb4494783863 58
samux 1:fb4494783863 59 int TCPSocketServer::accept(TCPSocketConnection& connection) {
va009039 5:fb15c35d1e28 60 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 61 return -1;
va009039 5:fb15c35d1e28 62 }
ban4jp 8:6ff41cd782f5 63 int next_sock_fd;
va009039 5:fb15c35d1e28 64 Timer t;
va009039 5:fb15c35d1e28 65 t.reset();
va009039 5:fb15c35d1e28 66 t.start();
va009039 5:fb15c35d1e28 67 while(1) {
va009039 5:fb15c35d1e28 68 if (t.read_ms() > _timeout && _blocking == false) {
va009039 5:fb15c35d1e28 69 return -1;
va009039 5:fb15c35d1e28 70 }
va009039 5:fb15c35d1e28 71 if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
ban4jp 8:6ff41cd782f5 72 next_sock_fd = eth->new_socket();
ban4jp 8:6ff41cd782f5 73 if (next_sock_fd < 0) {
ban4jp 8:6ff41cd782f5 74 continue;
ban4jp 8:6ff41cd782f5 75 }
va009039 5:fb15c35d1e28 76 break;
samux 1:fb4494783863 77 }
samux 1:fb4494783863 78 }
va009039 5:fb15c35d1e28 79 uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
va009039 5:fb15c35d1e28 80 char host[16];
va009039 5:fb15c35d1e28 81 snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
va009039 5:fb15c35d1e28 82 uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
va009039 5:fb15c35d1e28 83 connection._sock_fd = _sock_fd;
va009039 5:fb15c35d1e28 84 connection.set_address(host, port);
ban4jp 8:6ff41cd782f5 85
ban4jp 8:6ff41cd782f5 86 _sock_fd = next_sock_fd;
ban4jp 8:6ff41cd782f5 87 bind_socket();
ban4jp 8:6ff41cd782f5 88 eth->scmd(_sock_fd, LISTEN);
ban4jp 8:6ff41cd782f5 89
va009039 5:fb15c35d1e28 90 return 0;
va009039 5:fb15c35d1e28 91 }