NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Files at this revision

API Documentation at this revision

Comitter:
Christopher Haster
Date:
Mon Feb 22 19:07:36 2016 -0600
Branch:
api-changes
Parent:
31:7f15b95f2a1d
Child:
33:20bf72a57adb
Commit message:
Added open/close calls to all sockets

Changed in this revision

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
SocketInterface.h Show annotated file Show diff for this revision Revisions of this file
TCPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
TCPSocket.h Show annotated file Show diff for this revision Revisions of this file
--- a/Socket.cpp	Mon Feb 22 22:51:03 2016 +0000
+++ b/Socket.cpp	Mon Feb 22 19:07:36 2016 -0600
@@ -22,42 +22,19 @@
     , _proto(proto)
     , _socket(0)
 {
-
     memset(_ip_address, 0, SOCK_IP_SIZE);
     _port = 0;
 }
 
 Socket::~Socket()
 {
-    if (_socket) {
-        _iface->destroySocket(_socket);
-    }
-}
-
-SocketInterface *Socket::_get_socket()
-{
-    if (!_socket) {
-        _socket = _iface->createSocket(_proto);
-
-        if (_ip_address[0]) {
-            _socket->setIPAddress(_ip_address);
-        }
-
-        if (_port) {
-            _socket->setPort(_port);
-        }
-    }
-
-    return _socket;
+    if (_socket) close();
 }
 
 int32_t Socket::setURL(const char *url, uint16_t port)
 {
-    SocketInterface *s = _get_socket();
-    if (!s) return -2;
-
     int32_t err = _iface->getHostByName(url, _ip_address);
-    if (err < 0) return err;
+    if (err) return err;
 
     if (_socket) {
         _socket->setIPAddress(_ip_address);
@@ -102,17 +79,54 @@
     return _port;
 }
 
+int32_t Socket::open()
+{
+    if (_socket) close();
+
+    _socket = _iface->createSocket(_proto);
+    if (!_socket) return -2;
+
+    if (_ip_address[0]) {
+        _socket->setIPAddress(_ip_address);
+    }
+
+    if (_port) {
+        _socket->setPort(_port);
+    }
+
+    int32_t err = _socket->open();
+
+    if (err) {
+        _iface->destroySocket(_socket);
+    }
+
+    return err;
+}
+
+int32_t Socket::close()
+{
+    if (!_socket) return 0;
+
+    int32_t err = _socket->close();
+
+    if (!err) {
+        _iface->destroySocket(_socket);
+    }
+
+    return err;
+}
+
 int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms)
 {
-    SocketInterface *s = _get_socket();
-    if (!s) return -2;
-    return s->send(data, len, timeout_ms);
+    if (!_socket) return -2;
+
+    return _socket->send(data, len, timeout_ms);
 }
 
 int32_t Socket::recv(void *data, uint32_t len, uint32_t timeout_ms)
 {
-    SocketInterface *s = _get_socket();
-    if (!s) return -2;
-    return s->recv(data, len, timeout_ms);
+    if (!_socket) return -2;
+
+    return _socket->recv(data, len, timeout_ms);
 }
 
--- a/Socket.h	Mon Feb 22 22:51:03 2016 +0000
+++ b/Socket.h	Mon Feb 22 19:07:36 2016 -0600
@@ -56,6 +56,16 @@
     uint16_t getPort() const;
 
 
+    /** Open a connection to the underlying address
+     *  @return 0 on success
+     */
+    int32_t open();
+
+    /** Close an open connection
+     *  @return 0 on success
+     */
+    int32_t close();
+
     /** Send data over the socket
      *  @param data Buffer of data to send
      *  @param len Size of data to send
@@ -77,8 +87,6 @@
     Socket(NetworkInterface *iface, socket_protocol_t proto);
     ~Socket();
 
-    SocketInterface *_get_socket();
-
 private:
     NetworkInterface *_iface;
     socket_protocol_t _proto;
--- a/SocketInterface.h	Mon Feb 22 22:51:03 2016 +0000
+++ b/SocketInterface.h	Mon Feb 22 19:07:36 2016 -0600
@@ -47,13 +47,11 @@
     virtual void setPort(uint16_t port);
 
     /** Open a connection to the underlying address
-     *  Only used for TCP sockets
      *  @return 0 on success
      */
     virtual int32_t open() = 0;
 
     /** Close an open connection
-     *  Only used for TCP sockets
      *  @return 0 on success
      */
     virtual int32_t close() = 0;
--- a/TCPSocket.cpp	Mon Feb 22 22:51:03 2016 +0000
+++ b/TCPSocket.cpp	Mon Feb 22 19:07:36 2016 -0600
@@ -21,17 +21,3 @@
 {
 }
 
-int32_t TCPSocket::open()
-{
-    SocketInterface *s = _get_socket();
-    if (!s) return -2;
-    return s->close();
-}
-
-int32_t TCPSocket::close()
-{
-    SocketInterface *s = _get_socket();
-    if (!s) return -2;
-    return s->close();
-}
-
--- a/TCPSocket.h	Mon Feb 22 22:51:03 2016 +0000
+++ b/TCPSocket.h	Mon Feb 22 19:07:36 2016 -0600
@@ -33,17 +33,6 @@
      *  @param port Optional port to connect to
      */
     TCPSocket(NetworkInterface *iface);
-
-
-    /** Open a connection to the underlying address
-     *  @return 0 on success
-     */
-    int32_t open();
-
-    /** Close an open connection
-     *  @return 0 on success
-     */
-    int32_t close();
 };
 
 #endif