Added better support for SocketAddress/string addresses/ports

pull/2216/head^2
Christopher Haster 2016-03-13 17:25:05 -05:00
parent bd8cbf0fcb
commit 56e11d6709
6 changed files with 82 additions and 12 deletions

View File

@ -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 nsapi_error_t {
};
/** 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 @@ protected:
/** 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

View File

@ -32,12 +32,24 @@ int TCPServer::open(NetworkInterface *iface)
}
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)

View File

@ -36,11 +36,24 @@ public:
*/
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

View File

@ -43,7 +43,7 @@ int TCPSocket::connect(const SocketAddress &addr)
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;
}

View File

@ -32,12 +32,34 @@ int UDPSocket::open(NetworkInterface *iface)
}
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)

View File

@ -40,6 +40,29 @@ public:
*/
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