Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tuxitheone 0:ecaf3e593122 1 /* Copyright (C) 2012 mbed.org, MIT License
Tuxitheone 0:ecaf3e593122 2 *
Tuxitheone 0:ecaf3e593122 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Tuxitheone 0:ecaf3e593122 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Tuxitheone 0:ecaf3e593122 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Tuxitheone 0:ecaf3e593122 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Tuxitheone 0:ecaf3e593122 7 * furnished to do so, subject to the following conditions:
Tuxitheone 0:ecaf3e593122 8 *
Tuxitheone 0:ecaf3e593122 9 * The above copyright notice and this permission notice shall be included in all copies or
Tuxitheone 0:ecaf3e593122 10 * substantial portions of the Software.
Tuxitheone 0:ecaf3e593122 11 *
Tuxitheone 0:ecaf3e593122 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Tuxitheone 0:ecaf3e593122 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Tuxitheone 0:ecaf3e593122 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Tuxitheone 0:ecaf3e593122 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Tuxitheone 0:ecaf3e593122 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Tuxitheone 0:ecaf3e593122 17 */
Tuxitheone 0:ecaf3e593122 18 #include "TCPSocketServer.h"
Tuxitheone 0:ecaf3e593122 19
Tuxitheone 0:ecaf3e593122 20 #include <cstring>
Tuxitheone 0:ecaf3e593122 21
Tuxitheone 0:ecaf3e593122 22 using std::memset;
Tuxitheone 0:ecaf3e593122 23 using std::memcpy;
Tuxitheone 0:ecaf3e593122 24
Tuxitheone 0:ecaf3e593122 25 TCPSocketServer::TCPSocketServer() {
Tuxitheone 0:ecaf3e593122 26
Tuxitheone 0:ecaf3e593122 27 }
Tuxitheone 0:ecaf3e593122 28
Tuxitheone 0:ecaf3e593122 29 int TCPSocketServer::bind(int port) {
Tuxitheone 0:ecaf3e593122 30 if (init_socket(SOCK_STREAM) < 0)
Tuxitheone 0:ecaf3e593122 31 return -1;
Tuxitheone 0:ecaf3e593122 32
Tuxitheone 0:ecaf3e593122 33 struct sockaddr_in localHost;
Tuxitheone 0:ecaf3e593122 34 memset(&localHost, 0, sizeof(localHost));
Tuxitheone 0:ecaf3e593122 35
Tuxitheone 0:ecaf3e593122 36 localHost.sin_family = AF_INET;
Tuxitheone 0:ecaf3e593122 37 localHost.sin_port = htons(port);
Tuxitheone 0:ecaf3e593122 38 localHost.sin_addr.s_addr = INADDR_ANY;
Tuxitheone 0:ecaf3e593122 39
Tuxitheone 0:ecaf3e593122 40 if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
Tuxitheone 0:ecaf3e593122 41 close();
Tuxitheone 0:ecaf3e593122 42 return -1;
Tuxitheone 0:ecaf3e593122 43 }
Tuxitheone 0:ecaf3e593122 44
Tuxitheone 0:ecaf3e593122 45 return 0;
Tuxitheone 0:ecaf3e593122 46 }
Tuxitheone 0:ecaf3e593122 47
Tuxitheone 0:ecaf3e593122 48 int TCPSocketServer::listen(int max) {
Tuxitheone 0:ecaf3e593122 49 if (_sock_fd < 0)
Tuxitheone 0:ecaf3e593122 50 return -1;
Tuxitheone 0:ecaf3e593122 51
Tuxitheone 0:ecaf3e593122 52 if (lwip_listen(_sock_fd, max) < 0) {
Tuxitheone 0:ecaf3e593122 53 close();
Tuxitheone 0:ecaf3e593122 54 return -1;
Tuxitheone 0:ecaf3e593122 55 }
Tuxitheone 0:ecaf3e593122 56
Tuxitheone 0:ecaf3e593122 57 return 0;
Tuxitheone 0:ecaf3e593122 58 }
Tuxitheone 0:ecaf3e593122 59
Tuxitheone 0:ecaf3e593122 60 int TCPSocketServer::accept(TCPSocketConnection& connection) {
Tuxitheone 0:ecaf3e593122 61 if (_sock_fd < 0)
Tuxitheone 0:ecaf3e593122 62 return -1;
Tuxitheone 0:ecaf3e593122 63
Tuxitheone 0:ecaf3e593122 64 if (!_blocking) {
Tuxitheone 0:ecaf3e593122 65 TimeInterval timeout(_timeout);
Tuxitheone 0:ecaf3e593122 66 if (wait_readable(timeout) != 0)
Tuxitheone 0:ecaf3e593122 67 return -1;
Tuxitheone 0:ecaf3e593122 68 }
Tuxitheone 0:ecaf3e593122 69 connection.reset_address();
Tuxitheone 0:ecaf3e593122 70 socklen_t newSockRemoteHostLen = sizeof(connection._remoteHost);
Tuxitheone 0:ecaf3e593122 71 int fd = lwip_accept(_sock_fd, (struct sockaddr*) &connection._remoteHost, &newSockRemoteHostLen);
Tuxitheone 0:ecaf3e593122 72 if (fd < 0)
Tuxitheone 0:ecaf3e593122 73 return -1; //Accept failed
Tuxitheone 0:ecaf3e593122 74 connection._sock_fd = fd;
Tuxitheone 0:ecaf3e593122 75 connection._is_connected = true;
Tuxitheone 0:ecaf3e593122 76
Tuxitheone 0:ecaf3e593122 77 return 0;
Tuxitheone 0:ecaf3e593122 78 }