wip
Dependents: EthernetInterface_vz
Fork of Socket by
TCPSocketConnection.cpp@6:cd2e5559786d, 2012-07-27 (annotated)
- Committer:
- emilmont
- Date:
- Fri Jul 27 13:58:53 2012 +0000
- Revision:
- 6:cd2e5559786d
- Parent:
- 5:300e7ad2dc1d
- Child:
- 10:d24738f4ef99
thread safe timeouts
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 3:e6474399e057 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
emilmont | 3:e6474399e057 | 2 | * |
emilmont | 3:e6474399e057 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
emilmont | 3:e6474399e057 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
emilmont | 3:e6474399e057 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
emilmont | 3:e6474399e057 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
emilmont | 3:e6474399e057 | 7 | * furnished to do so, subject to the following conditions: |
emilmont | 3:e6474399e057 | 8 | * |
emilmont | 3:e6474399e057 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
emilmont | 3:e6474399e057 | 10 | * substantial portions of the Software. |
emilmont | 3:e6474399e057 | 11 | * |
emilmont | 3:e6474399e057 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
emilmont | 3:e6474399e057 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
emilmont | 3:e6474399e057 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
emilmont | 3:e6474399e057 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
emilmont | 3:e6474399e057 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
emilmont | 3:e6474399e057 | 17 | */ |
emilmont | 3:e6474399e057 | 18 | #include "TCPSocketConnection.h" |
emilmont | 3:e6474399e057 | 19 | #include <cstring> |
emilmont | 3:e6474399e057 | 20 | |
emilmont | 3:e6474399e057 | 21 | using std::memset; |
emilmont | 3:e6474399e057 | 22 | using std::memcpy; |
emilmont | 3:e6474399e057 | 23 | |
emilmont | 3:e6474399e057 | 24 | TCPSocketConnection::TCPSocketConnection() : |
emilmont | 3:e6474399e057 | 25 | _closedByRemoteHost(false) { |
emilmont | 3:e6474399e057 | 26 | } |
emilmont | 3:e6474399e057 | 27 | |
emilmont | 4:75988d748e4d | 28 | int TCPSocketConnection::connect(const char* host, const int port) { |
emilmont | 5:300e7ad2dc1d | 29 | if (init_socket(SOCK_STREAM) < 0) |
emilmont | 3:e6474399e057 | 30 | return -1; |
emilmont | 3:e6474399e057 | 31 | |
emilmont | 5:300e7ad2dc1d | 32 | if (set_address(host, port) != 0) |
emilmont | 5:300e7ad2dc1d | 33 | return -1; |
emilmont | 3:e6474399e057 | 34 | |
emilmont | 3:e6474399e057 | 35 | if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) { |
emilmont | 3:e6474399e057 | 36 | close(); |
emilmont | 3:e6474399e057 | 37 | return -1; |
emilmont | 3:e6474399e057 | 38 | } |
emilmont | 3:e6474399e057 | 39 | |
emilmont | 3:e6474399e057 | 40 | return 0; |
emilmont | 3:e6474399e057 | 41 | } |
emilmont | 3:e6474399e057 | 42 | |
emilmont | 6:cd2e5559786d | 43 | int TCPSocketConnection::send(char* data, int length, int timeout_ms) { |
emilmont | 3:e6474399e057 | 44 | if ((_sock_fd < 0) || _closedByRemoteHost) |
emilmont | 3:e6474399e057 | 45 | return -1; |
emilmont | 3:e6474399e057 | 46 | |
emilmont | 6:cd2e5559786d | 47 | TimeInterval timeout(timeout_ms); |
emilmont | 6:cd2e5559786d | 48 | if (wait_writable(timeout) != 0) |
emilmont | 3:e6474399e057 | 49 | return -1; |
emilmont | 3:e6474399e057 | 50 | |
emilmont | 3:e6474399e057 | 51 | int n = lwip_send(_sock_fd, data, length, 0); |
emilmont | 3:e6474399e057 | 52 | _closedByRemoteHost = (n == 0); |
emilmont | 3:e6474399e057 | 53 | |
emilmont | 3:e6474399e057 | 54 | return n; |
emilmont | 3:e6474399e057 | 55 | } |
emilmont | 3:e6474399e057 | 56 | |
emilmont | 3:e6474399e057 | 57 | // -1 if unsuccessful, else number of bytes written |
emilmont | 6:cd2e5559786d | 58 | int TCPSocketConnection::send_all(char* data, int length, int timeout_ms) { |
emilmont | 3:e6474399e057 | 59 | if ((_sock_fd < 0) || _closedByRemoteHost) |
emilmont | 3:e6474399e057 | 60 | return -1; |
emilmont | 3:e6474399e057 | 61 | |
emilmont | 3:e6474399e057 | 62 | size_t writtenLen = 0; |
emilmont | 6:cd2e5559786d | 63 | TimeInterval timeout(timeout_ms); |
emilmont | 3:e6474399e057 | 64 | while (writtenLen < length) { |
emilmont | 3:e6474399e057 | 65 | // Wait for socket to be writeable |
emilmont | 6:cd2e5559786d | 66 | if (wait_writable(timeout) != 0) |
emilmont | 3:e6474399e057 | 67 | return writtenLen; //Timeout -- FIXME should we return -1 or writtenLength ? |
emilmont | 3:e6474399e057 | 68 | |
emilmont | 3:e6474399e057 | 69 | int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0); |
emilmont | 3:e6474399e057 | 70 | if (ret > 0) { |
emilmont | 3:e6474399e057 | 71 | writtenLen += ret; |
emilmont | 3:e6474399e057 | 72 | continue; |
emilmont | 3:e6474399e057 | 73 | } else if (ret == 0) { |
emilmont | 3:e6474399e057 | 74 | _closedByRemoteHost = true; |
emilmont | 3:e6474399e057 | 75 | return writtenLen; //Connection was closed by remote host -- FIXME how do we signal that the connection was closed ? |
emilmont | 3:e6474399e057 | 76 | } else { |
emilmont | 3:e6474399e057 | 77 | return -1; //Connnection error |
emilmont | 3:e6474399e057 | 78 | } |
emilmont | 3:e6474399e057 | 79 | } |
emilmont | 3:e6474399e057 | 80 | |
emilmont | 3:e6474399e057 | 81 | return writtenLen; |
emilmont | 3:e6474399e057 | 82 | } |
emilmont | 3:e6474399e057 | 83 | |
emilmont | 6:cd2e5559786d | 84 | int TCPSocketConnection::receive(char* data, int length, int timeout_ms) { |
emilmont | 3:e6474399e057 | 85 | if ((_sock_fd < 0) || _closedByRemoteHost) |
emilmont | 3:e6474399e057 | 86 | return -1; |
emilmont | 3:e6474399e057 | 87 | |
emilmont | 6:cd2e5559786d | 88 | TimeInterval timeout(timeout_ms); |
emilmont | 6:cd2e5559786d | 89 | if (wait_readable(timeout) != 0) |
emilmont | 3:e6474399e057 | 90 | return -1; |
emilmont | 3:e6474399e057 | 91 | |
emilmont | 3:e6474399e057 | 92 | int n = lwip_recv(_sock_fd, data, length, 0); |
emilmont | 3:e6474399e057 | 93 | _closedByRemoteHost = (n == 0); |
emilmont | 3:e6474399e057 | 94 | |
emilmont | 3:e6474399e057 | 95 | return n; |
emilmont | 3:e6474399e057 | 96 | } |
emilmont | 3:e6474399e057 | 97 | |
emilmont | 3:e6474399e057 | 98 | // -1 if unsuccessful, else number of bytes received |
emilmont | 6:cd2e5559786d | 99 | int TCPSocketConnection::receive_all(char* data, int length, int timeout_ms) { |
emilmont | 3:e6474399e057 | 100 | if ((_sock_fd < 0) || _closedByRemoteHost) |
emilmont | 3:e6474399e057 | 101 | return -1; |
emilmont | 3:e6474399e057 | 102 | |
emilmont | 3:e6474399e057 | 103 | size_t readLen = 0; |
emilmont | 6:cd2e5559786d | 104 | TimeInterval timeout(timeout_ms); |
emilmont | 3:e6474399e057 | 105 | while (readLen < length) { |
emilmont | 3:e6474399e057 | 106 | //Wait for socket to be readable |
emilmont | 6:cd2e5559786d | 107 | if (wait_readable(timeout) != 0) |
emilmont | 3:e6474399e057 | 108 | return readLen; //Timeout -- FIXME should we return -1 or writtenLength ? |
emilmont | 3:e6474399e057 | 109 | |
emilmont | 3:e6474399e057 | 110 | int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0); |
emilmont | 3:e6474399e057 | 111 | if (ret > 0) { |
emilmont | 3:e6474399e057 | 112 | readLen += ret; |
emilmont | 3:e6474399e057 | 113 | } else if (ret == 0) { |
emilmont | 3:e6474399e057 | 114 | _closedByRemoteHost = true; |
emilmont | 3:e6474399e057 | 115 | return readLen; //Connection was closed by remote host -- FIXME how do we signal that the connection was closed ? |
emilmont | 3:e6474399e057 | 116 | } else { |
emilmont | 3:e6474399e057 | 117 | return -1; //Connnection error |
emilmont | 3:e6474399e057 | 118 | } |
emilmont | 3:e6474399e057 | 119 | } |
emilmont | 3:e6474399e057 | 120 | return readLen; |
emilmont | 3:e6474399e057 | 121 | } |
emilmont | 3:e6474399e057 | 122 | |
emilmont | 3:e6474399e057 | 123 | TCPSocketConnection::~TCPSocketConnection() { |
emilmont | 3:e6474399e057 | 124 | close(); //Don't want to leak |
emilmont | 3:e6474399e057 | 125 | } |