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