NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Revision 58:1caa187fa5af, committed 2016-02-25
- Comitter:
- Christopher Haster
- Date:
- Thu Feb 25 21:20:25 2016 -0600
- Parent:
- 57:3c873fab4207
- Child:
- 59:badee747a030
- Commit message:
- Removed setURL/IPAddress/Port set of functions from Socket and SocketInterface
Changed in this revision
--- a/Socket.cpp Thu Feb 25 19:00:39 2016 -0600 +++ b/Socket.cpp Thu Feb 25 21:20:25 2016 -0600 @@ -33,60 +33,6 @@ } } -int32_t Socket::setURL(const char *url) -{ - int32_t err = _iface->getHostByName(url, _ip_address); - if (err) { - return err; - } - - if (_socket) { - _socket->setIPAddress(_ip_address); - } - - return 0; -} - -void Socket::setIPAddress(const char *ip) -{ - strcpy(_ip_address, ip); - - if (_socket) { - _socket->setIPAddress(_ip_address); - } -} - -void Socket::setPort(uint16_t port) -{ - _port = port; - - if (_socket) { - _socket->setPort(_port); - } -} - -const char *Socket::getIPAddress() const -{ - if (_ip_address[0]) { - return _ip_address; - } else { - return 0; - } -} - -uint16_t Socket::getPort() const -{ - return _port; -} - -bool Socket::isConnected() -{ - if (!_socket) { - return false; - } - - return _socket->isConnected(); -} int32_t Socket::open(const char *address, uint16_t port) { @@ -97,19 +43,10 @@ return err; } - if (address) { - err = setURL(address); - if (err) { - return err; - } - } - - if (port) { - setPort(port); - } - - if (!getIPAddress() || !getPort()) { - return NS_ERROR_NO_ADDRESS; + err = _iface->getHostByName(address, _ip_address); + _port = port; + if (err) { + return err; } _socket = _iface->createSocket(_proto); @@ -166,3 +103,27 @@ } } + +const char *Socket::getIPAddress() const +{ + if (_ip_address[0]) { + return _ip_address; + } else { + return 0; + } +} + +uint16_t Socket::getPort() const +{ + return _port; +} + +bool Socket::isConnected() +{ + if (!_socket) { + return false; + } + + return _socket->isConnected(); +} +
--- a/Socket.h Thu Feb 25 19:00:39 2016 -0600 +++ b/Socket.h Thu Feb 25 21:20:25 2016 -0600 @@ -28,45 +28,13 @@ public: ~Socket(); - /** Set the URL of the socket - * Performs DNS lookup if necessary - * @param url URL to connect to + + /** Open a connection to the underlying address + * @param address URL or IP address to connect to + * @param port Port to connect to * @return 0 on success */ - int32_t setURL(const char *url); - - /** Set the IP address of the socket - * @param ip IP address to connect to, copied internally - */ - void setIPAddress(const char *ip); - - /** Set the port of the socket - * @param port Port to connect to - */ - void setPort(uint16_t port); - - /** Gets the IP address - * @return IP address to connect to - */ - const char *getIPAddress() const; - - /** Gets the port - * @return Port to connect to - */ - uint16_t getPort() const; - - /** Returns status of socket - * @return true if connected - */ - bool isConnected(); - - - /** Open a connection to the underlying address - * @param address Optional URL or IP address to connect to - * @param port Optional port to connect to - * @return 0 on success - */ - int32_t open(const char *address = 0, uint16_t port = 0); + int32_t open(const char *address, uint16_t port); /** Close an open connection * @return 0 on success @@ -89,6 +57,22 @@ int32_t recv(void *data, uint32_t size, bool blocking = true); + /** Gets the IP address + * @return IP address to connect to + */ + const char *getIPAddress() const; + + /** Gets the port + * @return Port to connect to + */ + uint16_t getPort() const; + + /** Returns status of socket + * @return true if connected + */ + bool isConnected(); + + protected: Socket(NetworkInterface *iface, ns_protocol_t proto);
--- a/SocketInterface.h Thu Feb 25 19:00:39 2016 -0600 +++ b/SocketInterface.h Thu Feb 25 21:20:25 2016 -0600 @@ -38,24 +38,6 @@ public: virtual ~SocketInterface() {} - /** Set the IP address of the socket - * @param ip IP address to connect to - */ - virtual void setIPAddress(const char *ip) { (void)ip; } - - /** Set the port of the socket - * @param port Port to connect to - */ - virtual void setPort(uint16_t port) { (void)port; } - - /** Status of the socket - * @return True if connected - */ - virtual bool isConnected() - { - // By default return true if socket was created successfully - return true; - } /** Open a connection to the underlying address * @param ip IP address to connect to @@ -86,6 +68,16 @@ * @return Number of bytes received or a negative value on failure */ virtual int32_t recv(void *data, uint32_t size) = 0; + + + /** Status of the socket + * @return True if connected + */ + virtual bool isConnected() + { + // By default return true if socket was created successfully + return true; + } }; #endif