version_2.0
Dependents: cc3000_ping_demo_try_2
Fork of cc3000_hostdriver_mbedsocket by
Socket/TCPSocketConnection.cpp@47:e9d12b82813e, 2014-06-25 (annotated)
- Committer:
- erezi
- Date:
- Wed Jun 25 06:08:39 2014 +0000
- Revision:
- 47:e9d12b82813e
- Parent:
- 28:8c7cc1c76ff8
version_2.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 5:245ac5b73132 | 1 | /* Copyright (C) 2013 mbed.org, MIT License |
Kojto | 0:615c697c33b0 | 2 | * |
Kojto | 0:615c697c33b0 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Kojto | 0:615c697c33b0 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Kojto | 0:615c697c33b0 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Kojto | 0:615c697c33b0 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Kojto | 0:615c697c33b0 | 7 | * furnished to do so, subject to the following conditions: |
Kojto | 0:615c697c33b0 | 8 | * |
Kojto | 0:615c697c33b0 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Kojto | 0:615c697c33b0 | 10 | * substantial portions of the Software. |
Kojto | 0:615c697c33b0 | 11 | * |
Kojto | 0:615c697c33b0 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Kojto | 0:615c697c33b0 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Kojto | 0:615c697c33b0 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Kojto | 0:615c697c33b0 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Kojto | 0:615c697c33b0 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Kojto | 0:615c697c33b0 | 17 | */ |
Kojto | 0:615c697c33b0 | 18 | |
Kojto | 0:615c697c33b0 | 19 | #include "TCPSocketConnection.h" |
Kojto | 0:615c697c33b0 | 20 | #include <algorithm> |
Kojto | 0:615c697c33b0 | 21 | |
Kojto | 5:245ac5b73132 | 22 | TCPSocketConnection::TCPSocketConnection() : _is_connected(false) { |
Kojto | 5:245ac5b73132 | 23 | _cc3000_module = cc3000::get_instance(); |
Kojto | 5:245ac5b73132 | 24 | if (_cc3000_module == NULL) { |
Kojto | 5:245ac5b73132 | 25 | error("Endpoint constructor error: no cc3000 instance available!\r\n"); |
Kojto | 5:245ac5b73132 | 26 | } |
Kojto | 0:615c697c33b0 | 27 | } |
Kojto | 0:615c697c33b0 | 28 | |
Kojto | 5:245ac5b73132 | 29 | int TCPSocketConnection::connect(const char *host, const int port) { |
Kojto | 5:245ac5b73132 | 30 | if (init_socket(SOCK_STREAM, IPPROTO_TCP) < 0) { |
SolderSplashLabs | 13:5e36c267e62f | 31 | DBG_SOCKET("Failed to create tcp socket"); |
Kojto | 5:245ac5b73132 | 32 | return -1; |
Kojto | 5:245ac5b73132 | 33 | } |
Kojto | 0:615c697c33b0 | 34 | |
Kojto | 5:245ac5b73132 | 35 | if (set_address(host, port) != 0) { |
SolderSplashLabs | 13:5e36c267e62f | 36 | DBG_SOCKET("Failed to set address (tcp)"); |
Kojto | 5:245ac5b73132 | 37 | return -1; |
Kojto | 5:245ac5b73132 | 38 | } |
Kojto | 5:245ac5b73132 | 39 | |
Kojto | 5:245ac5b73132 | 40 | if (_cc3000_module->_socket.connect(_sock_fd, (const sockaddr *)&_remote_host, sizeof(_remote_host)) < 0) { |
SolderSplashLabs | 13:5e36c267e62f | 41 | DBG_SOCKET("Failed to connect (tcp)"); |
Kojto | 5:245ac5b73132 | 42 | close(); |
Kojto | 5:245ac5b73132 | 43 | return -1; |
Kojto | 5:245ac5b73132 | 44 | } |
Kojto | 5:245ac5b73132 | 45 | |
Kojto | 5:245ac5b73132 | 46 | _is_connected = true; |
Kojto | 5:245ac5b73132 | 47 | |
Kojto | 5:245ac5b73132 | 48 | return 0; |
Kojto | 0:615c697c33b0 | 49 | } |
Kojto | 0:615c697c33b0 | 50 | |
Kojto | 5:245ac5b73132 | 51 | bool TCPSocketConnection::is_connected(void) { |
Kojto | 5:245ac5b73132 | 52 | return _is_connected; |
Kojto | 5:245ac5b73132 | 53 | } |
Kojto | 5:245ac5b73132 | 54 | |
Kojto | 5:245ac5b73132 | 55 | int TCPSocketConnection::send(char* data, int length) { |
Kojto | 5:245ac5b73132 | 56 | if ((_sock_fd < 0) || !_is_connected) { |
Kojto | 5:245ac5b73132 | 57 | return -1; |
Kojto | 5:245ac5b73132 | 58 | } |
Kojto | 0:615c697c33b0 | 59 | |
Kojto | 5:245ac5b73132 | 60 | if (!_blocking) { |
Kojto | 5:245ac5b73132 | 61 | TimeInterval timeout(_timeout); |
Kojto | 5:245ac5b73132 | 62 | if (wait_writable(timeout) != 0) { |
Kojto | 5:245ac5b73132 | 63 | return -1; |
Kojto | 5:245ac5b73132 | 64 | } |
Kojto | 5:245ac5b73132 | 65 | } |
Kojto | 5:245ac5b73132 | 66 | |
Kojto | 5:245ac5b73132 | 67 | int n = _cc3000_module->_socket.send(_sock_fd, data, length, 0); |
Kojto | 5:245ac5b73132 | 68 | _is_connected = (n != 0); |
Kojto | 5:245ac5b73132 | 69 | |
Kojto | 5:245ac5b73132 | 70 | return n; |
Kojto | 0:615c697c33b0 | 71 | } |
Kojto | 0:615c697c33b0 | 72 | |
Kojto | 5:245ac5b73132 | 73 | int TCPSocketConnection::send_all(char *data, int length) { |
Kojto | 5:245ac5b73132 | 74 | if ((_sock_fd < 0) || !_is_connected) { |
Kojto | 5:245ac5b73132 | 75 | return -1; |
Kojto | 5:245ac5b73132 | 76 | } |
Kojto | 0:615c697c33b0 | 77 | |
Kojto | 5:245ac5b73132 | 78 | int writtenLen = 0; |
Kojto | 5:245ac5b73132 | 79 | TimeInterval timeout(_timeout); |
Kojto | 5:245ac5b73132 | 80 | while (writtenLen < length) { |
Kojto | 5:245ac5b73132 | 81 | if (!_blocking) { |
Kojto | 5:245ac5b73132 | 82 | // Wait for socket to be writeable |
Kojto | 5:245ac5b73132 | 83 | if (wait_writable(timeout) != 0) { |
Kojto | 5:245ac5b73132 | 84 | return writtenLen; |
Kojto | 5:245ac5b73132 | 85 | } |
Kojto | 5:245ac5b73132 | 86 | } |
Kojto | 5:245ac5b73132 | 87 | |
Kojto | 5:245ac5b73132 | 88 | int ret = _cc3000_module->_socket.send(_sock_fd, data + writtenLen, length - writtenLen, 0); |
Kojto | 5:245ac5b73132 | 89 | if (ret > 0) { |
Kojto | 5:245ac5b73132 | 90 | writtenLen += ret; |
Kojto | 5:245ac5b73132 | 91 | continue; |
Kojto | 5:245ac5b73132 | 92 | } else if (ret == 0) { |
Kojto | 5:245ac5b73132 | 93 | _is_connected = false; |
Kojto | 5:245ac5b73132 | 94 | return writtenLen; |
Kojto | 5:245ac5b73132 | 95 | } else { |
Kojto | 5:245ac5b73132 | 96 | return -1; //Connnection error |
Kojto | 5:245ac5b73132 | 97 | } |
Kojto | 5:245ac5b73132 | 98 | } |
Kojto | 5:245ac5b73132 | 99 | |
Kojto | 5:245ac5b73132 | 100 | return writtenLen; |
Kojto | 0:615c697c33b0 | 101 | } |
Kojto | 0:615c697c33b0 | 102 | |
Kojto | 5:245ac5b73132 | 103 | int TCPSocketConnection::receive(char *data, int length) { |
Kojto | 5:245ac5b73132 | 104 | if ((_sock_fd < 0) || !_is_connected) { |
Kojto | 5:245ac5b73132 | 105 | return -1; |
Kojto | 5:245ac5b73132 | 106 | } |
Kojto | 0:615c697c33b0 | 107 | |
Kojto | 5:245ac5b73132 | 108 | if (!_blocking) { |
Kojto | 5:245ac5b73132 | 109 | TimeInterval timeout(_timeout); |
Kojto | 5:245ac5b73132 | 110 | if (wait_readable(timeout) != 0) |
Kojto | 5:245ac5b73132 | 111 | return -1; |
Kojto | 5:245ac5b73132 | 112 | } |
Kojto | 5:245ac5b73132 | 113 | |
Kojto | 5:245ac5b73132 | 114 | int n = _cc3000_module->_socket.recv(_sock_fd, data, length, 0); |
Kojto | 28:8c7cc1c76ff8 | 115 | if (n >= 0) { |
Kojto | 28:8c7cc1c76ff8 | 116 | _is_connected = 1; |
Kojto | 28:8c7cc1c76ff8 | 117 | } else { |
Kojto | 28:8c7cc1c76ff8 | 118 | _is_connected = 0; |
Kojto | 28:8c7cc1c76ff8 | 119 | } |
Kojto | 5:245ac5b73132 | 120 | |
Kojto | 5:245ac5b73132 | 121 | return n; |
Kojto | 0:615c697c33b0 | 122 | } |
Kojto | 0:615c697c33b0 | 123 | |
Kojto | 5:245ac5b73132 | 124 | int TCPSocketConnection::receive_all(char *data, int length) { |
Kojto | 5:245ac5b73132 | 125 | if ((_sock_fd < 0) || !_is_connected) { |
Kojto | 5:245ac5b73132 | 126 | return -1; |
Kojto | 5:245ac5b73132 | 127 | } |
Kojto | 0:615c697c33b0 | 128 | |
Kojto | 5:245ac5b73132 | 129 | int readLen = 0; |
Kojto | 5:245ac5b73132 | 130 | TimeInterval timeout(_timeout); |
Kojto | 5:245ac5b73132 | 131 | while (readLen < length) { |
Kojto | 5:245ac5b73132 | 132 | if (!_blocking) { |
Kojto | 5:245ac5b73132 | 133 | //Wait for socket to be readable |
Kojto | 5:245ac5b73132 | 134 | if (wait_readable(timeout) != 0) |
Kojto | 5:245ac5b73132 | 135 | return readLen; |
Kojto | 5:245ac5b73132 | 136 | } |
Kojto | 0:615c697c33b0 | 137 | |
Kojto | 5:245ac5b73132 | 138 | int ret = _cc3000_module->_socket.recv(_sock_fd, data + readLen, length - readLen, 0); |
Kojto | 5:245ac5b73132 | 139 | if (ret > 0) { |
Kojto | 5:245ac5b73132 | 140 | readLen += ret; |
Kojto | 5:245ac5b73132 | 141 | } else if (ret == 0) { |
Kojto | 5:245ac5b73132 | 142 | _is_connected = false; |
Kojto | 5:245ac5b73132 | 143 | return readLen; |
Kojto | 5:245ac5b73132 | 144 | } else { |
Kojto | 5:245ac5b73132 | 145 | return -1; //Connnection error |
Kojto | 5:245ac5b73132 | 146 | } |
Kojto | 5:245ac5b73132 | 147 | } |
Kojto | 5:245ac5b73132 | 148 | return readLen; |
Kojto | 0:615c697c33b0 | 149 | } |