Forked from the DieterGraef Library

Fork of F7_Ethernet by Dieter Graef

Committer:
EmbeddedSam
Date:
Fri Nov 18 12:48:10 2016 +0000
Revision:
2:fdef48f830c0
Parent:
0:d26c1b55cfca
Linking MQTT together, starting with mbed-cli

Who changed what in which revision?

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