victor qiu / Mbed OS SimpleNTP

Fork of SimpleNTP by Akinori Hashimoto

Committer:
victorqiu2
Date:
Tue Aug 01 01:22:01 2017 +0000
Revision:
2:68ae27667d82
A

Who changed what in which revision?

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