mbed socket API

Dependents:   EthernetInterface EthernetInterface_RSF EthernetInterface EthernetInterface ... more

Deprecated

This is an mbed 2 sockets library. For mbed 5, network sockets have been revised to better support additional network stacks and thread safety here.

Committer:
emilmont
Date:
Thu Jul 26 10:07:43 2012 +0000
Revision:
4:75988d748e4d
Parent:
3:e6474399e057
Child:
5:300e7ad2dc1d
Add "const" specifier to "TCPSocketConnection::connect" parameters

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 3:e6474399e057 1 /* Copyright (C) 2012 mbed.org, MIT License
emilmont 3:e6474399e057 2 *
emilmont 3:e6474399e057 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
emilmont 3:e6474399e057 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
emilmont 3:e6474399e057 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
emilmont 3:e6474399e057 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
emilmont 3:e6474399e057 7 * furnished to do so, subject to the following conditions:
emilmont 3:e6474399e057 8 *
emilmont 3:e6474399e057 9 * The above copyright notice and this permission notice shall be included in all copies or
emilmont 3:e6474399e057 10 * substantial portions of the Software.
emilmont 3:e6474399e057 11 *
emilmont 3:e6474399e057 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
emilmont 3:e6474399e057 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
emilmont 3:e6474399e057 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
emilmont 3:e6474399e057 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
emilmont 3:e6474399e057 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
emilmont 3:e6474399e057 17 */
emilmont 3:e6474399e057 18 #include "TCPSocketConnection.h"
emilmont 3:e6474399e057 19 #include <cstring>
emilmont 3:e6474399e057 20
emilmont 3:e6474399e057 21 using std::memset;
emilmont 3:e6474399e057 22 using std::memcpy;
emilmont 3:e6474399e057 23
emilmont 3:e6474399e057 24 TCPSocketConnection::TCPSocketConnection() :
emilmont 3:e6474399e057 25 _closedByRemoteHost(false) {
emilmont 3:e6474399e057 26 memset(&_remoteHost, 0, sizeof(struct sockaddr_in));
emilmont 3:e6474399e057 27 _ipAddress[0] = '\0';
emilmont 3:e6474399e057 28 }
emilmont 3:e6474399e057 29
emilmont 4:75988d748e4d 30 int TCPSocketConnection::connect(const char* host, const int port) {
emilmont 3:e6474399e057 31 if (init(SOCK_STREAM) < 0)
emilmont 3:e6474399e057 32 return -1;
emilmont 3:e6474399e057 33
emilmont 3:e6474399e057 34 //Resolve DNS address or populate hard-coded IP address
emilmont 3:e6474399e057 35 struct hostent *server = gethostbyname(host);
emilmont 3:e6474399e057 36 if (server == NULL)
emilmont 3:e6474399e057 37 return -1; //Could not resolve address
emilmont 3:e6474399e057 38
emilmont 3:e6474399e057 39 memcpy((char*) &_remoteHost.sin_addr.s_addr,
emilmont 3:e6474399e057 40 (char*) server->h_addr_list[0], server->h_length);
emilmont 3:e6474399e057 41
emilmont 3:e6474399e057 42 _remoteHost.sin_family = AF_INET;
emilmont 3:e6474399e057 43 _remoteHost.sin_port = htons(port);
emilmont 3:e6474399e057 44 if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
emilmont 3:e6474399e057 45 close();
emilmont 3:e6474399e057 46 return -1;
emilmont 3:e6474399e057 47 }
emilmont 3:e6474399e057 48
emilmont 3:e6474399e057 49 return 0;
emilmont 3:e6474399e057 50 }
emilmont 3:e6474399e057 51
emilmont 3:e6474399e057 52 int TCPSocketConnection::send(char* data, int length, int timeout) {
emilmont 3:e6474399e057 53 if ((_sock_fd < 0) || _closedByRemoteHost)
emilmont 3:e6474399e057 54 return -1;
emilmont 3:e6474399e057 55
emilmont 3:e6474399e057 56 set_timeout(timeout);
emilmont 3:e6474399e057 57 if (wait_writable() != 0)
emilmont 3:e6474399e057 58 return -1;
emilmont 3:e6474399e057 59
emilmont 3:e6474399e057 60 int n = lwip_send(_sock_fd, data, length, 0);
emilmont 3:e6474399e057 61 _closedByRemoteHost = (n == 0);
emilmont 3:e6474399e057 62
emilmont 3:e6474399e057 63 return n;
emilmont 3:e6474399e057 64 }
emilmont 3:e6474399e057 65
emilmont 3:e6474399e057 66 // -1 if unsuccessful, else number of bytes written
emilmont 3:e6474399e057 67 int TCPSocketConnection::send_all(char* data, int length, int timeout) {
emilmont 3:e6474399e057 68 if ((_sock_fd < 0) || _closedByRemoteHost)
emilmont 3:e6474399e057 69 return -1;
emilmont 3:e6474399e057 70
emilmont 3:e6474399e057 71 size_t writtenLen = 0;
emilmont 3:e6474399e057 72 set_timeout(timeout);
emilmont 3:e6474399e057 73 while (writtenLen < length) {
emilmont 3:e6474399e057 74 // Wait for socket to be writeable
emilmont 3:e6474399e057 75 if (wait_writable() != 0)
emilmont 3:e6474399e057 76 return writtenLen; //Timeout -- FIXME should we return -1 or writtenLength ?
emilmont 3:e6474399e057 77
emilmont 3:e6474399e057 78 int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
emilmont 3:e6474399e057 79 if (ret > 0) {
emilmont 3:e6474399e057 80 writtenLen += ret;
emilmont 3:e6474399e057 81 continue;
emilmont 3:e6474399e057 82 } else if (ret == 0) {
emilmont 3:e6474399e057 83 _closedByRemoteHost = true;
emilmont 3:e6474399e057 84 return writtenLen; //Connection was closed by remote host -- FIXME how do we signal that the connection was closed ?
emilmont 3:e6474399e057 85 } else {
emilmont 3:e6474399e057 86 return -1; //Connnection error
emilmont 3:e6474399e057 87 }
emilmont 3:e6474399e057 88 }
emilmont 3:e6474399e057 89
emilmont 3:e6474399e057 90 return writtenLen;
emilmont 3:e6474399e057 91 }
emilmont 3:e6474399e057 92
emilmont 3:e6474399e057 93 int TCPSocketConnection::receive(char* data, int length, int timeout) {
emilmont 3:e6474399e057 94 if ((_sock_fd < 0) || _closedByRemoteHost)
emilmont 3:e6474399e057 95 return -1;
emilmont 3:e6474399e057 96
emilmont 3:e6474399e057 97 set_timeout(timeout);
emilmont 3:e6474399e057 98 if (wait_readable() != 0)
emilmont 3:e6474399e057 99 return -1;
emilmont 3:e6474399e057 100
emilmont 3:e6474399e057 101 int n = lwip_recv(_sock_fd, data, length, 0);
emilmont 3:e6474399e057 102 _closedByRemoteHost = (n == 0);
emilmont 3:e6474399e057 103
emilmont 3:e6474399e057 104 return n;
emilmont 3:e6474399e057 105 }
emilmont 3:e6474399e057 106
emilmont 3:e6474399e057 107 // -1 if unsuccessful, else number of bytes received
emilmont 3:e6474399e057 108 int TCPSocketConnection::receive_all(char* data, int length, int timeout) {
emilmont 3:e6474399e057 109 if ((_sock_fd < 0) || _closedByRemoteHost)
emilmont 3:e6474399e057 110 return -1;
emilmont 3:e6474399e057 111
emilmont 3:e6474399e057 112 size_t readLen = 0;
emilmont 3:e6474399e057 113 set_timeout(timeout);
emilmont 3:e6474399e057 114 while (readLen < length) {
emilmont 3:e6474399e057 115 //Wait for socket to be readable
emilmont 3:e6474399e057 116 if (wait_readable() != 0)
emilmont 3:e6474399e057 117 return readLen; //Timeout -- FIXME should we return -1 or writtenLength ?
emilmont 3:e6474399e057 118
emilmont 3:e6474399e057 119 int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
emilmont 3:e6474399e057 120 if (ret > 0) {
emilmont 3:e6474399e057 121 readLen += ret;
emilmont 3:e6474399e057 122 } else if (ret == 0) {
emilmont 3:e6474399e057 123 _closedByRemoteHost = true;
emilmont 3:e6474399e057 124 return readLen; //Connection was closed by remote host -- FIXME how do we signal that the connection was closed ?
emilmont 3:e6474399e057 125 } else {
emilmont 3:e6474399e057 126 return -1; //Connnection error
emilmont 3:e6474399e057 127 }
emilmont 3:e6474399e057 128 }
emilmont 3:e6474399e057 129 return readLen;
emilmont 3:e6474399e057 130 }
emilmont 3:e6474399e057 131
emilmont 3:e6474399e057 132 char* TCPSocketConnection::get_address() {
emilmont 3:e6474399e057 133 if (_ipAddress[0] == '\0') {
emilmont 3:e6474399e057 134 if (_remoteHost.sin_addr.s_addr == 0)
emilmont 3:e6474399e057 135 return NULL;
emilmont 3:e6474399e057 136 inet_ntoa_r(_remoteHost.sin_addr, _ipAddress, sizeof(_ipAddress));
emilmont 3:e6474399e057 137 }
emilmont 3:e6474399e057 138 return _ipAddress;
emilmont 3:e6474399e057 139 }
emilmont 3:e6474399e057 140 int TCPSocketConnection::get_port() {
emilmont 3:e6474399e057 141 return ntohs(_remoteHost.sin_port);
emilmont 3:e6474399e057 142 }
emilmont 3:e6474399e057 143
emilmont 3:e6474399e057 144 TCPSocketConnection::~TCPSocketConnection() {
emilmont 3:e6474399e057 145 close(); //Don't want to leak
emilmont 3:e6474399e057 146 }