mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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