ap

Dependencies:   DnsQuery

Dependents:   WizFi310_TCP_Echo_Server_Example

Fork of NetworkSocketAPI by NetworkSocketAPI

Branch:
api-changes
Revision:
32:2c5fc105fc50
Parent:
31:7f15b95f2a1d
Child:
34:c17745683385
--- 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);
 }