Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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