.

Fork of WIZnet_Library by WIZnet

Committer:
nanjsk
Date:
Sun May 31 17:22:53 2015 +0000
Revision:
7:c1f1122adf7b
Parent:
3:48348a6eaa72
autu LED + Ethernet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bongjun 3:48348a6eaa72 1 /* Copyright (C) 2012 mbed.org, MIT License
Bongjun 3:48348a6eaa72 2 *
Bongjun 3:48348a6eaa72 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Bongjun 3:48348a6eaa72 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Bongjun 3:48348a6eaa72 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Bongjun 3:48348a6eaa72 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Bongjun 3:48348a6eaa72 7 * furnished to do so, subject to the following conditions:
Bongjun 3:48348a6eaa72 8 *
Bongjun 3:48348a6eaa72 9 * The above copyright notice and this permission notice shall be included in all copies or
Bongjun 3:48348a6eaa72 10 * substantial portions of the Software.
Bongjun 3:48348a6eaa72 11 *
Bongjun 3:48348a6eaa72 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Bongjun 3:48348a6eaa72 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Bongjun 3:48348a6eaa72 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Bongjun 3:48348a6eaa72 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Bongjun 3:48348a6eaa72 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Bongjun 3:48348a6eaa72 17 */
Bongjun 3:48348a6eaa72 18
Bongjun 3:48348a6eaa72 19 #include "TCPSocketServer.h"
Bongjun 3:48348a6eaa72 20
Bongjun 3:48348a6eaa72 21 TCPSocketServer::TCPSocketServer() {}
Bongjun 3:48348a6eaa72 22
Bongjun 3:48348a6eaa72 23 // Server initialization
Bongjun 3:48348a6eaa72 24 int TCPSocketServer::bind(int port)
Bongjun 3:48348a6eaa72 25 {
Bongjun 3:48348a6eaa72 26
Bongjun 3:48348a6eaa72 27 if (_sock_fd < 0) {
Bongjun 3:48348a6eaa72 28 _sock_fd = eth->new_socket();
Bongjun 3:48348a6eaa72 29 if (_sock_fd < 0) {
Bongjun 3:48348a6eaa72 30 return -1;
Bongjun 3:48348a6eaa72 31 }
Bongjun 3:48348a6eaa72 32 }
Bongjun 3:48348a6eaa72 33 // set the listen_port for next connection.
Bongjun 3:48348a6eaa72 34 listen_port = port;
Bongjun 3:48348a6eaa72 35 // set TCP protocol
Bongjun 3:48348a6eaa72 36 eth->setProtocol(_sock_fd, TCP);
Bongjun 3:48348a6eaa72 37 // set local port
Bongjun 3:48348a6eaa72 38 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
Bongjun 3:48348a6eaa72 39 // connect the network
Bongjun 3:48348a6eaa72 40 eth->scmd(_sock_fd, OPEN);
Bongjun 3:48348a6eaa72 41 return 0;
Bongjun 3:48348a6eaa72 42 }
jbkim 0:b72d22e10709 43
Bongjun 3:48348a6eaa72 44 int TCPSocketServer::listen(int backlog)
Bongjun 3:48348a6eaa72 45 {
Bongjun 3:48348a6eaa72 46 if (_sock_fd < 0) {
Bongjun 3:48348a6eaa72 47 return -1;
Bongjun 3:48348a6eaa72 48 }
Bongjun 3:48348a6eaa72 49 if (backlog != 1) {
Bongjun 3:48348a6eaa72 50 return -1;
Bongjun 3:48348a6eaa72 51 }
Bongjun 3:48348a6eaa72 52 eth->scmd(_sock_fd, LISTEN);
Bongjun 3:48348a6eaa72 53 return 0;
Bongjun 3:48348a6eaa72 54 }
Bongjun 3:48348a6eaa72 55
Bongjun 3:48348a6eaa72 56
Bongjun 3:48348a6eaa72 57 int TCPSocketServer::accept(TCPSocketConnection& connection)
Bongjun 3:48348a6eaa72 58 {
Bongjun 3:48348a6eaa72 59 if (_sock_fd < 0) {
Bongjun 3:48348a6eaa72 60 return -1;
Bongjun 3:48348a6eaa72 61 }
nanjsk 7:c1f1122adf7b 62 //Timer t;
nanjsk 7:c1f1122adf7b 63 // t.reset();
nanjsk 7:c1f1122adf7b 64 // t.start();
nanjsk 7:c1f1122adf7b 65 //while(1) {
nanjsk 7:c1f1122adf7b 66 // if (t.read_ms() > _timeout && _blocking == false) {
nanjsk 7:c1f1122adf7b 67 // return -1;
nanjsk 7:c1f1122adf7b 68 // }
nanjsk 7:c1f1122adf7b 69 // if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
nanjsk 7:c1f1122adf7b 70 // break;
nanjsk 7:c1f1122adf7b 71 // }
nanjsk 7:c1f1122adf7b 72 // }
nanjsk 7:c1f1122adf7b 73 if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
nanjsk 7:c1f1122adf7b 74 uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
nanjsk 7:c1f1122adf7b 75 char host[16];
nanjsk 7:c1f1122adf7b 76 snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
nanjsk 7:c1f1122adf7b 77 uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
nanjsk 7:c1f1122adf7b 78 // change this server socket to connection socket.
nanjsk 7:c1f1122adf7b 79 connection._sock_fd = _sock_fd;
nanjsk 7:c1f1122adf7b 80 connection.set_address(host, port);
nanjsk 7:c1f1122adf7b 81
nanjsk 7:c1f1122adf7b 82 // and then, for the next connection, server socket should be assigned new one.
nanjsk 7:c1f1122adf7b 83 _sock_fd = -1; // want to assign new available _sock_fd.
nanjsk 7:c1f1122adf7b 84 if(bind(listen_port) < 0) {
nanjsk 7:c1f1122adf7b 85 error("No more socket for listening");
nanjsk 7:c1f1122adf7b 86 } else {
nanjsk 7:c1f1122adf7b 87 //return -1;
nanjsk 7:c1f1122adf7b 88 if(listen(1) < 0) {
nanjsk 7:c1f1122adf7b 89 error("No more socket for listening");
nanjsk 7:c1f1122adf7b 90 }
Bongjun 3:48348a6eaa72 91 }
Bongjun 3:48348a6eaa72 92 }
nanjsk 7:c1f1122adf7b 93 else return -1;
Bongjun 3:48348a6eaa72 94 return 0;
Bongjun 3:48348a6eaa72 95 }
Bongjun 3:48348a6eaa72 96