mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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