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.

Files at this revision

API Documentation at this revision

Comitter:
emilmont
Date:
Wed Aug 01 13:02:32 2012 +0000
Parent:
10:d24738f4ef99
Child:
12:8867c9da3b08
Commit message:
[Socket library] Remove redundant UDPPacket class. Use centralized socket destructor. Add is_connected check.

Changed in this revision

Endpoint.h Show annotated file Show diff for this revision Revisions of this file
Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket.h Show annotated file Show diff for this revision Revisions of this file
TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
TCPSocketConnection.h Show annotated file Show diff for this revision Revisions of this file
TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
TCPSocketServer.h Show annotated file Show diff for this revision Revisions of this file
UDPPacket.cpp Show diff for this revision Revisions of this file
UDPPacket.h Show diff for this revision Revisions of this file
UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
UDPSocket.h Show annotated file Show diff for this revision Revisions of this file
--- a/Endpoint.h	Tue Jul 31 11:50:55 2012 +0000
+++ b/Endpoint.h	Wed Aug 01 13:02:32 2012 +0000
@@ -18,10 +18,13 @@
 #ifndef ENDPOINT_H
 #define ENDPOINT_H
 
+class UDPSocket;
+
 /**
 IP Endpoint (address, port)
 */
 class Endpoint {
+    friend class UDPSocket;
 
 public:
     /** IP Endpoint (address, port)
--- a/Socket.cpp	Tue Jul 31 11:50:55 2012 +0000
+++ b/Socket.cpp	Wed Aug 01 13:02:32 2012 +0000
@@ -20,7 +20,7 @@
 
 using std::memset;
 
-Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(3000) {
+Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
     
 }
 
@@ -71,6 +71,10 @@
     return 0;
 }
 
+Socket::~Socket() {
+    close(); //Don't want to leak
+}
+
 TimeInterval::TimeInterval(unsigned int ms) {
     _time.tv_sec = ms / 1000;
     _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
--- a/Socket.h	Tue Jul 31 11:50:55 2012 +0000
+++ b/Socket.h	Wed Aug 01 13:02:32 2012 +0000
@@ -43,7 +43,7 @@
     /** Set blocking or non-blocking mode of the socket and a timeout on
         blocking socket operations
     \param blocking  true for blocking mode, false for non-blocking mode.
-    \param timeout   timeout in ms.
+    \param timeout   timeout in ms [Default: (1500)ms].
     */
     void set_blocking(bool blocking, unsigned int timeout=1500);
     
@@ -51,6 +51,8 @@
      */
     int close();
     
+    ~Socket();
+    
 protected:
     int _sock_fd;
     int init_socket(int type);
--- a/TCPSocketConnection.cpp	Tue Jul 31 11:50:55 2012 +0000
+++ b/TCPSocketConnection.cpp	Wed Aug 01 13:02:32 2012 +0000
@@ -22,7 +22,7 @@
 using std::memcpy;
 
 TCPSocketConnection::TCPSocketConnection() :
-        _closedByRemoteHost(false) {
+        _is_connected(false) {
 }
 
 int TCPSocketConnection::connect(const char* host, const int port) {
@@ -36,12 +36,17 @@
         close();
         return -1;
     }
+    _is_connected = true;
     
     return 0;
 }
 
+bool TCPSocketConnection::is_connected(void) {
+    return _is_connected;
+}
+
 int TCPSocketConnection::send(char* data, int length) {
-    if ((_sock_fd < 0) || _closedByRemoteHost)
+    if ((_sock_fd < 0) || !_is_connected)
         return -1;
     
     if (!_blocking) {
@@ -51,24 +56,23 @@
     }
     
     int n = lwip_send(_sock_fd, data, length, 0);
-    _closedByRemoteHost = (n == 0);
+    _is_connected = (n != 0);
     
     return n;
 }
 
 // -1 if unsuccessful, else number of bytes written
 int TCPSocketConnection::send_all(char* data, int length) {
-    if ((_sock_fd < 0) || _closedByRemoteHost)
+    if ((_sock_fd < 0) || !_is_connected)
         return -1;
     
     size_t writtenLen = 0;
-    
     TimeInterval timeout(_timeout);
     while (writtenLen < length) {
         if (!_blocking) {
             // Wait for socket to be writeable
             if (wait_writable(timeout) != 0)
-                return writtenLen; //Timeout -- FIXME should we return -1 or writtenLength ?
+                return writtenLen;
         }
         
         int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
@@ -76,18 +80,17 @@
             writtenLen += ret;
             continue;
         } else if (ret == 0) {
-            _closedByRemoteHost = true;
-            return writtenLen; //Connection was closed by remote host -- FIXME how do we signal that the connection was closed ?
+            _is_connected = false;
+            return writtenLen;
         } else {
             return -1; //Connnection error
         }
     }
-    
     return writtenLen;
 }
 
 int TCPSocketConnection::receive(char* data, int length) {
-    if ((_sock_fd < 0) || _closedByRemoteHost)
+    if ((_sock_fd < 0) || !_is_connected)
         return -1;
     
     if (!_blocking) {
@@ -97,14 +100,14 @@
     }
     
     int n = lwip_recv(_sock_fd, data, length, 0);
-    _closedByRemoteHost = (n == 0);
+    _is_connected = (n != 0);
     
     return n;
 }
 
 // -1 if unsuccessful, else number of bytes received
 int TCPSocketConnection::receive_all(char* data, int length) {
-    if ((_sock_fd < 0) || _closedByRemoteHost)
+    if ((_sock_fd < 0) || !_is_connected)
         return -1;
     
     size_t readLen = 0;
@@ -113,22 +116,18 @@
         if (!_blocking) {
             //Wait for socket to be readable
             if (wait_readable(timeout) != 0)
-                return readLen; //Timeout -- FIXME should we return -1 or writtenLength ?
+                return readLen;
         }
         
         int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
         if (ret > 0) {
             readLen += ret;
         } else if (ret == 0) {
-            _closedByRemoteHost = true;
-            return readLen; //Connection was closed by remote host -- FIXME how do we signal that the connection was closed ?
+            _is_connected = false;
+            return readLen;
         } else {
             return -1; //Connnection error
         }
     }
     return readLen;
 }
-
-TCPSocketConnection::~TCPSocketConnection() {
-    close(); //Don't want to leak 
-}
--- a/TCPSocketConnection.h	Tue Jul 31 11:50:55 2012 +0000
+++ b/TCPSocketConnection.h	Wed Aug 01 13:02:32 2012 +0000
@@ -33,8 +33,6 @@
     */
     TCPSocketConnection();
     
-    ~TCPSocketConnection();
-    
     /** Connects this TCP socket to the server
     \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS.
     \param port The host's port to connect to.
@@ -42,6 +40,11 @@
     */
     int connect(const char* host, const int port);
     
+    /** Check if the socket is connected
+    \return true if connected, false otherwise.
+    */
+    bool is_connected(void);
+    
     /** Send data to the remote host.
     \param data The buffer to send to the host.
     \param length The length of the buffer to send.
@@ -71,7 +74,7 @@
     int receive_all(char* data, int length);
 
 private:
-    bool _closedByRemoteHost;
+    bool _is_connected;
 
 };
 
--- a/TCPSocketServer.cpp	Tue Jul 31 11:50:55 2012 +0000
+++ b/TCPSocketServer.cpp	Wed Aug 01 13:02:32 2012 +0000
@@ -71,10 +71,7 @@
     if (fd < 0)
         return -1; //Accept failed
     connection._sock_fd = fd;
+    connection._is_connected = true;
     
     return 0;
 }
-
-TCPSocketServer::~TCPSocketServer() {
-    close(); //Don't want to leak 
-}
--- a/TCPSocketServer.h	Tue Jul 31 11:50:55 2012 +0000
+++ b/TCPSocketServer.h	Wed Aug 01 13:02:32 2012 +0000
@@ -29,8 +29,6 @@
     */
     TCPSocketServer();
     
-    ~TCPSocketServer();
-    
     /** Bind a socket to a specific port.
     \param port The port to listen for incoming connections on.
     \return 0 on success, -1 on failure.
@@ -38,10 +36,11 @@
     int bind(int port);
     
     /** Start listening for incoming connections.
-    \param backlog number of pending connections that can be queued up at any one time.
+    \param backlog number of pending connections that can be queued up at any
+                   one time [Default: 1].
     \return 0 on success, -1 on failure.
     */
-    int listen(int backlog);
+    int listen(int backlog=1);
     
     /** Accept a new connection.
     \param connection A TCPSocketConnection instance that will handle the incoming connection.
--- a/UDPPacket.cpp	Tue Jul 31 11:50:55 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-#include "Socket/UDPPacket.h"
-
-#include <cstring>
-
-using std::memset;
-
-UDPPacket::UDPPacket(char* buffer, unsigned int length) {
-    set_data(buffer, length);
-}
-
-void UDPPacket::set_data(char* buffer, unsigned int length) {
-    _buffer = buffer;
-    _length = length;
-}
--- a/UDPPacket.h	Tue Jul 31 11:50:55 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-#ifndef UDPPACKET_H
-#define UDPPACKET_H
-
-#include "Socket/Socket.h"
-#include "Socket/Endpoint.h"
-
-// Forward declaration
-class UDPSocket;
-
-/** UDP Packet
-  */
-class UDPPacket : public Endpoint {
-    friend class UDPSocket;
-
-public:
-    /** UDP Packet
-      \param buffer   Pointer to the data buffer for this packet
-      \param length   length of the data buffer
-      */
-    UDPPacket(char* buffer, unsigned int length);
-    ~UDPPacket() {}
-    
-    /** Set the data buffer for this packet
-      \param buffer   Pointer to the data buffer for this packet
-      \param length   length of the data buffer
-      */
-    void set_data(char* buffer, unsigned int length);
-
-private:
-    unsigned int   _length;
-    char* _buffer;
-};
-
-#endif
--- a/UDPSocket.cpp	Tue Jul 31 11:50:55 2012 +0000
+++ b/UDPSocket.cpp	Wed Aug 01 13:02:32 2012 +0000
@@ -50,35 +50,30 @@
 }
 
 // -1 if unsuccessful, else number of bytes written
-int UDPSocket::sendTo(UDPPacket& packet) {
+int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
     if (_sock_fd < 0)
         return -1;
     
     if (!_blocking) {
         TimeInterval timeout(_timeout);
         if (wait_writable(timeout) != 0)
-            return -1;
+            return 0;
     }
     
-    return lwip_sendto(_sock_fd, packet._buffer, packet._length, 0, (const struct sockaddr *) &packet._remoteHost, sizeof(packet._remoteHost));
+    return lwip_sendto(_sock_fd, packet, length, 0, (const struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost));
 }
 
 // -1 if unsuccessful, else number of bytes received
-int UDPSocket::receiveFrom(UDPPacket& packet) {
+int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
     if (_sock_fd < 0)
         return -1;
     
     if (!_blocking) {
         TimeInterval timeout(_timeout);
         if (wait_readable(timeout) != 0)
-            return -1;
+            return 0;
     }
-    
-    packet.reset_address();
-    socklen_t remoteHostLen = sizeof(packet._remoteHost);
-    return lwip_recvfrom(_sock_fd, packet._buffer, packet._length, 0, (struct sockaddr*) &packet._remoteHost, &remoteHostLen);
+    remote.reset_address();
+    socklen_t remoteHostLen = sizeof(remote._remoteHost);
+    return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
 }
-
-UDPSocket::~UDPSocket() {
-    close(); //Don't want to leak
-}
--- a/UDPSocket.h	Tue Jul 31 11:50:55 2012 +0000
+++ b/UDPSocket.h	Wed Aug 01 13:02:32 2012 +0000
@@ -20,7 +20,8 @@
 #define UDPSOCKET_H
 
 #include "Socket/Socket.h"
-#include "Socket/UDPPacket.h"
+#include "Socket/Endpoint.h"
+
 #include <cstdint>
 
 /**
@@ -33,8 +34,6 @@
     */
     UDPSocket();
     
-    ~UDPSocket();
-    
     /** Init the UDP Client Socket without binding it to any specific port
     \return 0 on success, -1 on failure.
     */
@@ -46,19 +45,22 @@
     */
     int bind(int port);
     
-    /** Send a packet to the remote host.
-    \param packet UDP Packet to be sent to the remote host
+    /** Send a packet to a remote endpoint
+    \param remote   The remote endpoint
+    \param packet   The packet to be sent
+    \param length   The length of the packet to be sent
     \return the number of written bytes on success (>=0) or -1 on failure
     */
-    int sendTo(UDPPacket& packet);
+    int sendTo(Endpoint &remote, char *packet, int length);
     
-    /** Receive data from a remote host.
-    If a message is too long to fit in the supplied packet, excess bytes are
-    discarded.
-    \param packet UDP Packet received from the remote host
+    /** Receive a packet from a remote endpoint
+    \param remote   The remote endpoint
+    \param buffer   The buffer for storing the incoming packet data. If a packet
+           is too long to fit in the supplied buffer, excess bytes are discarded
+    \param length   The length of the buffer
     \return the number of received bytes on success (>=0) or -1 on failure
     */
-    int receiveFrom(UDPPacket& packet);
+    int receiveFrom(Endpoint &remote, char *buffer, int length);
 };
 
-#endif 
+#endif