ap

Dependencies:   DnsQuery

Dependents:   WizFi310_TCP_Echo_Server_Example

Fork of NetworkSocketAPI by NetworkSocketAPI

Branch:
api-changes
Revision:
28:163fbe3263f4
Parent:
27:d7ed39727306
Child:
29:7bcec3189a93
--- a/Socket.cpp	Thu Feb 18 09:20:39 2016 -0600
+++ b/Socket.cpp	Thu Feb 18 13:19:47 2016 -0600
@@ -15,96 +15,79 @@
  */
 
 #include "Socket.h"
-#include <cstdlib>
 #include <cstring>
 
-Socket::Socket(
-        NetworkInterface *iface,
-        socket_protocol_t proto, 
-        const char *url,
-        uint16_t port)
+Socket::Socket(NetworkInterface *iface, socket_protocol_t proto)
   : _iface(iface)
   , _proto(proto)
-  , _socket(0)
-  , _pending_url(0)
-  , _pending_ip(0)
-  , _pending_port(0) {
+  , _socket(0) {
 
-    if (url) {
-        _pending_url = (char*)malloc(strlen(url)+1);
-        strcpy(_pending_url, url);
-    }
-
-    if (port) {
-        _pending_port = port;
-    }
+    memset(_ip_address, 0, SOCK_IP_SIZE);
+    _port = 0;
 }
 
 Socket::~Socket() {
-    if (_socket) _iface->destroySocket(_socket);
-    free(_pending_url);
-    free(_pending_ip);
+    if (_socket) {
+        _iface->destroySocket(_socket);
+    }
 }
 
 SocketInterface *Socket::_get_socket() {
     if (!_socket) {
         _socket = _iface->createSocket(_proto);
 
-        if (_pending_url) {
-            _socket->setURL(_pending_url);
-            free(_pending_url);
-            _pending_url = 0;
+        if (_ip_address[0]) {
+            _socket->setIPAddress(_ip_address);
         }
 
-        if (_pending_ip) {
-            _socket->setIPAddress(_pending_ip);
-            free(_pending_ip);
-            _pending_ip = 0;
-        }
-
-        if (_pending_port) {
-            _socket->setPort(_pending_port);
-            _pending_port = 0;
+        if (_port) {
+            _socket->setPort(_port);
         }
     }
 
     return _socket;
 }
 
-int32_t Socket::setURL(const char *url) {
+int32_t Socket::setURL(const char *url, uint16_t port) {
     SocketInterface *s = _get_socket();
     if (!s) return -2;
-    return s->setURL(url);
+
+    int32_t error = s->setURL(url);
+    if (error < 0) return error;
+
+    if (port) {
+        setPort(port);
+    }
+
+    return 0;
 }
 
-void Socket::setIPAddress(const char *ip) {
-    if (!_socket) {
-        free(_pending_ip);
-        _pending_ip = (char*)malloc(strlen(ip)+1);
-        strcpy(_pending_ip, ip);
-    } else {
+void Socket::setIPAddress(const char *ip, uint16_t port) {
+    strcpy(_ip_address, ip);
+
+    if (_socket) {
         _socket->setIPAddress(ip);
     }
+
+    if (port) {
+        setPort(port);
+    }
 }
 
 void Socket::setPort(uint16_t port) {
-    if (!_socket) {
-        _pending_port = port;
-    } else {
+    _port = port;
+
+    if (_socket) {
         _socket->setPort(port);
     }
 }
 
-const char *Socket::getIPAddress() {
-    SocketInterface *s = _get_socket();
-	if (!s) return 0;
-	return s->getIPAddress();
+const char *Socket::getIPAddress() const {
+    return _ip_address;
 }
 
-uint16_t Socket::getPort() {
-    SocketInterface *s = _get_socket();
-	if (!s) return 0;
-	return s->getPort();
+uint16_t Socket::getPort() const {
+    return _port;
 }
 
 int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms) {