Modify changes to test TCP socket.

Dependents:   EthernetInterface

Fork of Socket by mbed official

Committer:
rebonatto
Date:
Fri Dec 13 11:36:01 2013 +0000
Revision:
20:b06c58defd65
Parent:
16:2d471deff212
Modify

Who changed what in which revision?

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