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