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