Added better support for SocketAddress/string addresses/ports

Christopher Haster 2016-03-13 17:25:05 -05:00 committed by Russ Butler
parent d488f02f5e
commit 141b245dfc
10 changed files with 97 additions and 27 deletions

View File

@ -138,7 +138,7 @@ int ESP8266Interface::socket_get_option(void *handle, int optname, void *optval,
return NSAPI_ERROR_UNSUPPORTED;
}
int ESP8266Interface::socket_bind(void *handle, int port)
int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address)
{
return NSAPI_ERROR_UNSUPPORTED;
}

View File

@ -96,11 +96,11 @@ protected:
virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen);
/** Bind a server socket to a specific port
\param handle Socket handle
\param port The port to listen for incoming connections on
\return 0 on success, negative on failure.
*/
virtual int socket_bind(void *handle, int port);
* @param handle Socket handle
* @param address Local address to listen for incoming connections on
* @return 0 on success, negative on failure.
*/
virtual int socket_bind(void *handle, const SocketAddress &address);
/** Start listening for incoming connections
\param handle Socket handle

View File

@ -223,20 +223,20 @@ int LWIPInterface::socket_get_option(void *handle, int optname, void *optval, un
return NSAPI_ERROR_UNSUPPORTED;
}
int LWIPInterface::socket_bind(void *handle, int port)
int LWIPInterface::socket_bind(void *handle, const SocketAddress &address)
{
struct lwip_socket *s = (struct lwip_socket *)handle;
ip_addr_t ip_addr = ip_addr_any;
ip_addr_t ip_addr = ip_addr_any; // TODO use address
switch (s->proto) {
case NSAPI_UDP:
if (udp_bind(s->udp, &ip_addr, port)) {
if (udp_bind(s->udp, &ip_addr, address.get_port())) {
return NSAPI_ERROR_DEVICE_ERROR;
}
return 0;
case NSAPI_TCP:
if (tcp_bind(s->tcp, &ip_addr, port)) {
if (tcp_bind(s->tcp, &ip_addr, address.get_port())) {
return NSAPI_ERROR_DEVICE_ERROR;
}
return 0;

View File

@ -83,11 +83,11 @@ protected:
virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen);
/** Bind a server socket to a specific port
\param handle Socket handle
\param port The port to listen for incoming connections on
\return 0 on success, negative on failure.
*/
virtual int socket_bind(void *handle, int port);
* @param handle Socket handle
* @param address Local address to listen for incoming connections on
* @return 0 on success, negative on failure.
*/
virtual int socket_bind(void *handle, const SocketAddress &address);
/** Start listening for incoming connections
\param handle Socket handle

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