NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Revision 28:163fbe3263f4, committed 2016-02-18
- Comitter:
- Christopher Haster
- Date:
- Thu Feb 18 13:19:47 2016 -0600
- Branch:
- api-changes
- Parent:
- 27:d7ed39727306
- Child:
- 29:7bcec3189a93
- Commit message:
- Removed memory allocations for internal buffers
Changed in this revision
--- a/NetworkInterface.cpp Thu Feb 18 09:20:39 2016 -0600 +++ b/NetworkInterface.cpp Thu Feb 18 13:19:47 2016 -0600 @@ -18,32 +18,21 @@ #include <stdlib.h> #include <string.h> -#define IP_SIZE 16 - -NetworkInterface::NetworkInterface() - : _ip_address(0) - , _network_mask(0) - , _gateway(0) { -} - -NetworkInterface::~NetworkInterface() { - if (_ip_address) free(_ip_address); - if (_network_mask) free(_network_mask); - if (_gateway) free(_gateway); +NetworkInterface::NetworkInterface() { + memset(_ip_address, 0, SOCK_IP_SIZE); + memset(_network_mask, 0, SOCK_IP_SIZE); + memset(_gateway, 0, SOCK_IP_SIZE); } void NetworkInterface::setIPAddress(const char *ip) { - _ip_address = (char*)malloc(IP_SIZE); strcpy(_ip_address, ip); } void NetworkInterface::setNetworkMask(const char *mask) { - _network_mask = (char*)malloc(IP_SIZE); strcpy(_network_mask, mask); } void NetworkInterface::setGateway(const char *gateway) { - _gateway = (char*)malloc(IP_SIZE); strcpy(_gateway, gateway); }
--- a/NetworkInterface.h Thu Feb 18 09:20:39 2016 -0600 +++ b/NetworkInterface.h Thu Feb 18 13:19:47 2016 -0600 @@ -20,6 +20,8 @@ #include "stdint.h" #include "SocketInterface.h" +#define SOCK_IP_SIZE 16 + /** NetworkInterface class * Common interface that is shared between all hardware that @@ -76,7 +78,6 @@ protected: NetworkInterface(); - virtual ~NetworkInterface(); friend class Socket; @@ -93,9 +94,9 @@ virtual void destroySocket(SocketInterface *socket) = 0; private: - char *_ip_address; - char *_network_mask; - char *_gateway; + char _ip_address[SOCK_IP_SIZE]; + char _network_mask[SOCK_IP_SIZE]; + char _gateway[SOCK_IP_SIZE]; }; #endif
--- 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) {
--- a/Socket.h Thu Feb 18 09:20:39 2016 -0600 +++ b/Socket.h Thu Feb 18 13:19:47 2016 -0600 @@ -29,14 +29,16 @@ /** Set the URL of the socket * Performs DNS lookup if necessary * @param url URL to connect to + * @param port Optional port to connect to * @return 0 on success */ - int32_t setURL(const char *url); + int32_t setURL(const char *url, uint16_t port = 0); /** Set the IP address of the socket * @param ip IP address to connect to, copied internally + * @param port Optional port to connect to */ - void setIPAddress(const char *ip); + void setIPAddress(const char *ip, uint16_t port = 0); /** Set the port of the socket * @param port Port to connect to @@ -46,12 +48,12 @@ /** Gets the IP address * @return IP address to connect to */ - const char *getIPAddress(); + const char *getIPAddress() const; /** Gets the port * @return Port to connect to */ - uint16_t getPort(); + uint16_t getPort() const; /** Send data over the socket @@ -72,10 +74,7 @@ protected: - Socket( NetworkInterface *iface, - socket_protocol_t proto, - const char *url = 0, - uint16_t port = 0); + Socket(NetworkInterface *iface, socket_protocol_t proto); ~Socket(); SocketInterface *_get_socket(); @@ -85,9 +84,8 @@ socket_protocol_t _proto; SocketInterface *_socket; - char *_pending_url; - char *_pending_ip; - uint16_t _pending_port; + char _ip_address[SOCK_IP_SIZE]; + uint16_t _port; }; #endif
--- a/SocketInterface.cpp Thu Feb 18 09:20:39 2016 -0600 +++ b/SocketInterface.cpp Thu Feb 18 13:19:47 2016 -0600 @@ -15,34 +15,7 @@ */ #include "SocketInterface.h" -#include <stdlib.h> -#include <string.h> - -#define IP_SIZE 16 - -SocketInterface::SocketInterface() - : _ip_address(0) - , _port(0) { -} - -SocketInterface::~SocketInterface() { - if (_ip_address) free(_ip_address); -} -void SocketInterface::setIPAddress(const char *ip) { - _ip_address = (char*)malloc(IP_SIZE); - strcpy(_ip_address, ip); -} - -void SocketInterface::setPort(uint16_t port) { - _port = port; -} +void SocketInterface::setIPAddress(const char *ip) {} +void SocketInterface::setPort(uint16_t port) {} -const char *SocketInterface::getIPAddress() { - return _ip_address; -} - -uint16_t SocketInterface::getPort() { - return _port; -} -
--- a/SocketInterface.h Thu Feb 18 09:20:39 2016 -0600 +++ b/SocketInterface.h Thu Feb 18 13:19:47 2016 -0600 @@ -53,17 +53,6 @@ */ virtual void setPort(uint16_t port); - /** Get the IP address - * @return IP address to connect to - */ - virtual const char *getIPAddress(); - - /** Get the port - * @return Port to connect to - */ - virtual uint16_t getPort(); - - /** Open a connection to the underlying address * Only used for TCP sockets * @return 0 on success @@ -91,14 +80,6 @@ * @return Number of bytes sent or a negative value on failure */ virtual int32_t recv(void *data, uint32_t len, uint32_t timeout_ms) = 0; - -protected: - SocketInterface(); - virtual ~SocketInterface(); - -private: - char *_ip_address; - uint16_t _port; }; #endif
--- a/TCPSocket.cpp Thu Feb 18 09:20:39 2016 -0600 +++ b/TCPSocket.cpp Thu Feb 18 13:19:47 2016 -0600 @@ -16,8 +16,8 @@ #include "TCPSocket.h" -TCPSocket::TCPSocket(NetworkInterface *iface, const char *url, uint16_t port) - : Socket(iface, SOCK_TCP, url, port) { +TCPSocket::TCPSocket(NetworkInterface *iface) + : Socket(iface, SOCK_TCP) { } int32_t TCPSocket::open() {
--- a/TCPSocket.h Thu Feb 18 09:20:39 2016 -0600 +++ b/TCPSocket.h Thu Feb 18 13:19:47 2016 -0600 @@ -32,7 +32,7 @@ * @param url Optional URL to connect to, copied internally * @param port Optional port to connect to */ - TCPSocket(NetworkInterface *iface, const char *url = 0, uint16_t port = 0); + TCPSocket(NetworkInterface *iface); /** Open a connection to the underlying address
--- a/UDPSocket.cpp Thu Feb 18 09:20:39 2016 -0600 +++ b/UDPSocket.cpp Thu Feb 18 13:19:47 2016 -0600 @@ -16,7 +16,7 @@ #include "UDPSocket.h" -UDPSocket::UDPSocket(NetworkInterface *iface, const char *url, uint16_t port) - : Socket(iface, SOCK_UDP, url, port) { +UDPSocket::UDPSocket(NetworkInterface *iface) + : Socket(iface, SOCK_UDP) { }
--- a/UDPSocket.h Thu Feb 18 09:20:39 2016 -0600 +++ b/UDPSocket.h Thu Feb 18 13:19:47 2016 -0600 @@ -32,7 +32,7 @@ * @param ip Optional URL to connect to, copied internally * @param port Optional port to connect to */ - UDPSocket(NetworkInterface *iface, const char *url = 0, uint16_t port = 0); + UDPSocket(NetworkInterface *iface); }; #endif