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