NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Revision 96:656011e49d9f, committed 2016-04-19
- Comitter:
- Christopher Haster
- Date:
- Tue Apr 19 18:24:34 2016 -0500
- Parent:
- 95:b3c679f20d13
- Child:
- 97:68232387bc75
- Commit message:
- Added better support for SocketAddress/string addresses/ports
Changed in this revision
--- a/NetworkInterface.h Tue Apr 19 18:24:24 2016 -0500 +++ b/NetworkInterface.h Tue Apr 19 18:24:34 2016 -0500 @@ -22,7 +22,7 @@ /** Enum of standardized error codes - * @enum ns_error_t + * @enum nsapi_error_t */ enum nsapi_error_t { NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ @@ -38,9 +38,9 @@ }; /** Enum of available options - * @enum ns_opt_t + * @enum nsapi_opt_t */ -enum ns_opt_t { +enum nsapi_opt_t { }; /** Enum of socket protocols @@ -134,10 +134,10 @@ /** Bind a server socket to a specific port * @param handle Socket handle - * @param port The port to listen for incoming connections on + * @param address Local address to listen for incoming connections on * @return 0 on success, negative on failure. */ - virtual int socket_bind(void *handle, int port) = 0; + virtual int socket_bind(void *handle, const SocketAddress &address) = 0; /** Start listening for incoming connections * @param handle Socket handle
--- a/TCPServer.cpp Tue Apr 19 18:24:24 2016 -0500 +++ b/TCPServer.cpp Tue Apr 19 18:24:34 2016 -0500 @@ -33,11 +33,23 @@ int TCPServer::bind(uint16_t port) { + SocketAddress addr(0, port); + return bind(addr); +} + +int TCPServer::bind(const char *address, uint16_t port) +{ + SocketAddress addr(address, port); + return bind(addr); +} + +int TCPServer::bind(const SocketAddress &address) +{ if (!_socket) { - return NSAPI_ERROR_NO_SOCKET; + return NSAPI_ERROR_NO_SOCKET; } - return _iface->socket_bind(_socket, port); + return _iface->socket_bind(_socket, address); } int TCPServer::listen(int backlog)
--- a/TCPServer.h Tue Apr 19 18:24:24 2016 -0500 +++ b/TCPServer.h Tue Apr 19 18:24:34 2016 -0500 @@ -36,11 +36,24 @@ */ virtual int open(NetworkInterface *iface); - /** Bind a socket to a specific port - * @param port The port to listen for incoming connections on - * @return 0 on success, negative on failure + /** Bind a TCP Server to a specific port + * @param port The port to listen for incoming connections on + * @return 0 on success, negative on failure. */ int bind(uint16_t port); + + /** Bind a TCP Server to a local address + * @param address The null-terminated address to listen for incoming connections on + * @param port The port to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + int bind(const char *address, uint16_t port); + + /** Bind a TCP Server to a local address + * @param address The SocketAddress to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + int bind(const SocketAddress &address); /** Start listening for incoming connections * @param backlog Number of pending connections that can be queued up at any
--- a/TCPSocket.cpp Tue Apr 19 18:24:24 2016 -0500 +++ b/TCPSocket.cpp Tue Apr 19 18:24:34 2016 -0500 @@ -43,7 +43,7 @@ int TCPSocket::connect(const char *host, uint16_t port) { SocketAddress addr(_iface, host, port); - if (!addr.get_ip_address()) { + if (!addr) { return NSAPI_ERROR_DNS_FAILURE; }
--- a/UDPSocket.cpp Tue Apr 19 18:24:24 2016 -0500 +++ b/UDPSocket.cpp Tue Apr 19 18:24:34 2016 -0500 @@ -33,11 +33,33 @@ int UDPSocket::bind(uint16_t port) { + SocketAddress addr(0, port); + return bind(addr); +} + +int UDPSocket::bind(const char *address, uint16_t port) +{ + SocketAddress addr(address, port); + return bind(addr); +} + +int UDPSocket::bind(const SocketAddress &address) +{ if (!_socket) { return NSAPI_ERROR_NO_SOCKET; } - return _iface->socket_bind(_socket, port); + return _iface->socket_bind(_socket, address); +} + +int UDPSocket::sendto(const char *host, uint16_t port, const void *data, unsigned size) +{ + SocketAddress addr(_iface, host, port); + if (!addr) { + return NSAPI_ERROR_DNS_FAILURE; + } + + return sendto(addr, data, size); } int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned size)
--- a/UDPSocket.h Tue Apr 19 18:24:24 2016 -0500 +++ b/UDPSocket.h Tue Apr 19 18:24:34 2016 -0500 @@ -40,6 +40,29 @@ */ int bind(uint16_t port); + /** Bind a UDP Server Socket to a local address + * @param address The null-terminated address to listen for incoming connections on + * @param port The port to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + int bind(const char *address, uint16_t port); + + /** Bind a UDP Server Socket to a local address + * @param address The SocketAddress to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + int bind(const SocketAddress &address); + + /** Send a packet to a remote endpoint + * @param host The host to connect to. It can either be an IP Address + * or a hostname that will be resolved with DNS + * @param port The remote port + * @param data The packet to be sent + * @param size The length of the packet to be sent + * @return The number of written bytes on success, negative on failure + */ + int sendto(const char *host, uint16_t port, const void *data, unsigned size); + /** Send a packet to a remote endpoint * @param address The remote SocketAddress * @param data The packet to be sent