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