NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Revision 34:c17745683385, committed 2016-02-22
- Comitter:
- Christopher Haster
- Date:
- Mon Feb 22 19:56:44 2016 -0600
- Branch:
- api-changes
- Parent:
- 33:20bf72a57adb
- Child:
- 35:838393fbc2ca
- Commit message:
- Added url/ip/port as optional arguments to open
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 |
--- a/Socket.cpp Tue Feb 23 17:14:20 2016 +0000 +++ b/Socket.cpp Mon Feb 22 19:56:44 2016 -0600 @@ -31,7 +31,7 @@ if (_socket) close(); } -int32_t Socket::setURL(const char *url, uint16_t port) +int32_t Socket::setURL(const char *url) { int32_t err = _iface->getHostByName(url, _ip_address); if (err) return err; @@ -40,24 +40,16 @@ _socket->setIPAddress(_ip_address); } - if (port) { - setPort(port); - } - return 0; } -void Socket::setIPAddress(const char *ip, uint16_t port) +void Socket::setIPAddress(const char *ip) { strcpy(_ip_address, ip); if (_socket) { _socket->setIPAddress(_ip_address); } - - if (port) { - setPort(port); - } } void Socket::setPort(uint16_t port) @@ -79,9 +71,22 @@ return _port; } -int32_t Socket::open() +int32_t Socket::open(const char *url, uint16_t port) { - if (_socket) close(); + if (_socket) { + int32_t err = close(); + if (err) return err; + } + + if (url) { + int32_t err = setURL(url); + if (err) return err; + } + + if (port) { + setPort(port); + } + _socket = _iface->createSocket(_proto); if (!_socket) return -2; @@ -94,8 +99,8 @@ _socket->setPort(_port); } + int32_t err = _socket->open(); - if (err) { _iface->destroySocket(_socket); } @@ -119,14 +124,12 @@ int32_t Socket::send(const void *data, uint32_t len, uint32_t 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) { if (!_socket) return -2; - return _socket->recv(data, len, timeout_ms); }
--- a/Socket.h Tue Feb 23 17:14:20 2016 +0000 +++ b/Socket.h Mon Feb 22 19:56:44 2016 -0600 @@ -29,16 +29,14 @@ /** 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, uint16_t port = 0); + int32_t setURL(const char *url); /** 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, uint16_t port = 0); + void setIPAddress(const char *ip); /** Set the port of the socket * @param port Port to connect to @@ -57,9 +55,11 @@ /** Open a connection to the underlying address + * @param url Optional URL or IP address to connect to + * @param port Optional port to connect to * @return 0 on success */ - int32_t open(); + int32_t open(const char *url = 0, uint16_t port = 0); /** Close an open connection * @return 0 on success