include some debugs

Fork of Socket by mbed official

Committer:
klauss
Date:
Thu Oct 02 12:37:35 2014 +0000
Revision:
20:5cccc791add1
Parent:
16:2d471deff212
debug msgs

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"
klauss 20:5cccc791add1 19 #include "debug.h"
emilmont 16:2d471deff212 20
emilmont 16:2d471deff212 21 #include <cstring>
emilmont 16:2d471deff212 22
emilmont 16:2d471deff212 23 using std::memset;
emilmont 16:2d471deff212 24 using std::memcpy;
emilmont 16:2d471deff212 25
emilmont 16:2d471deff212 26 TCPSocketServer::TCPSocketServer() {
emilmont 16:2d471deff212 27
emilmont 16:2d471deff212 28 }
emilmont 16:2d471deff212 29
emilmont 16:2d471deff212 30 int TCPSocketServer::bind(int port) {
emilmont 16:2d471deff212 31 if (init_socket(SOCK_STREAM) < 0)
emilmont 16:2d471deff212 32 return -1;
emilmont 16:2d471deff212 33
emilmont 16:2d471deff212 34 struct sockaddr_in localHost;
emilmont 16:2d471deff212 35 memset(&localHost, 0, sizeof(localHost));
emilmont 16:2d471deff212 36
emilmont 16:2d471deff212 37 localHost.sin_family = AF_INET;
emilmont 16:2d471deff212 38 localHost.sin_port = htons(port);
emilmont 16:2d471deff212 39 localHost.sin_addr.s_addr = INADDR_ANY;
emilmont 16:2d471deff212 40
emilmont 16:2d471deff212 41 if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
emilmont 16:2d471deff212 42 close();
emilmont 16:2d471deff212 43 return -1;
emilmont 16:2d471deff212 44 }
emilmont 16:2d471deff212 45
emilmont 16:2d471deff212 46 return 0;
emilmont 16:2d471deff212 47 }
emilmont 16:2d471deff212 48
emilmont 16:2d471deff212 49 int TCPSocketServer::listen(int max) {
emilmont 16:2d471deff212 50 if (_sock_fd < 0)
emilmont 16:2d471deff212 51 return -1;
emilmont 16:2d471deff212 52
emilmont 16:2d471deff212 53 if (lwip_listen(_sock_fd, max) < 0) {
emilmont 16:2d471deff212 54 close();
emilmont 16:2d471deff212 55 return -1;
emilmont 16:2d471deff212 56 }
emilmont 16:2d471deff212 57
emilmont 16:2d471deff212 58 return 0;
emilmont 16:2d471deff212 59 }
emilmont 16:2d471deff212 60
emilmont 16:2d471deff212 61 int TCPSocketServer::accept(TCPSocketConnection& connection) {
klauss 20:5cccc791add1 62 debug_msg("");
emilmont 16:2d471deff212 63 if (_sock_fd < 0)
emilmont 16:2d471deff212 64 return -1;
emilmont 16:2d471deff212 65
emilmont 16:2d471deff212 66 if (!_blocking) {
klauss 20:5cccc791add1 67 debug_msg("");
emilmont 16:2d471deff212 68 TimeInterval timeout(_timeout);
emilmont 16:2d471deff212 69 if (wait_readable(timeout) != 0)
emilmont 16:2d471deff212 70 return -1;
emilmont 16:2d471deff212 71 }
klauss 20:5cccc791add1 72 debug_msg("");
emilmont 16:2d471deff212 73 connection.reset_address();
klauss 20:5cccc791add1 74 debug_msg("");
emilmont 16:2d471deff212 75 socklen_t newSockRemoteHostLen = sizeof(connection._remoteHost);
klauss 20:5cccc791add1 76 debug_msg("");
emilmont 16:2d471deff212 77 int fd = lwip_accept(_sock_fd, (struct sockaddr*) &connection._remoteHost, &newSockRemoteHostLen);
emilmont 16:2d471deff212 78 if (fd < 0)
emilmont 16:2d471deff212 79 return -1; //Accept failed
klauss 20:5cccc791add1 80 debug_msg("");
emilmont 16:2d471deff212 81 connection._sock_fd = fd;
klauss 20:5cccc791add1 82 debug_msg("");
emilmont 16:2d471deff212 83 connection._is_connected = true;
emilmont 16:2d471deff212 84
emilmont 16:2d471deff212 85 return 0;
emilmont 16:2d471deff212 86 }