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