From 63725d653c788bda054e0b4cf582606a2182cd8e Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sun, 13 Mar 2016 17:57:17 -0500 Subject: [PATCH] Move bind to Socket Bind can operate on any IP socket and is not specific to a protocol --- Socket.cpp | 21 +++++++++++++++++++++ Socket.h | 19 +++++++++++++++++++ TCPServer.cpp | 21 --------------------- TCPServer.h | 19 ------------------- UDPSocket.cpp | 21 --------------------- UDPSocket.h | 19 ------------------- 6 files changed, 40 insertions(+), 80 deletions(-) diff --git a/Socket.cpp b/Socket.cpp index 1b3858aa91..0d6e652993 100644 --- a/Socket.cpp +++ b/Socket.cpp @@ -58,6 +58,27 @@ int Socket::close() return _iface->socket_close(socket); } +int Socket::bind(uint16_t port) +{ + SocketAddress addr(0, port); + return bind(addr); +} + +int Socket::bind(const char *address, uint16_t port) +{ + SocketAddress addr(address, port); + return bind(addr); +} + +int Socket::bind(const SocketAddress &address) +{ + if (!_socket) { + return NSAPI_ERROR_NO_SOCKET; + } + + return _iface->socket_bind(_socket, address); +} + void Socket::set_blocking(bool blocking) { _blocking = blocking; diff --git a/Socket.h b/Socket.h index e8b96dd8c5..68353f1b12 100644 --- a/Socket.h +++ b/Socket.h @@ -33,6 +33,25 @@ public: */ virtual int open(NetworkInterface *iface) = 0; + /** Bind a socket 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 socket to a specific port + * @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 socket to a specific port + * @param address The SocketAddress to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + int bind(const SocketAddress &address); + /** Set blocking or non-blocking mode of the socket * @param blocking true for blocking mode, false for non-blocking mode. */ diff --git a/TCPServer.cpp b/TCPServer.cpp index b2b7e57a6a..6c513f4be0 100644 --- a/TCPServer.cpp +++ b/TCPServer.cpp @@ -31,27 +31,6 @@ int TCPServer::open(NetworkInterface *iface) return Socket::open(iface, NSAPI_TCP); } -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 _iface->socket_bind(_socket, address); -} - int TCPServer::listen(int backlog) { if (!_socket) { diff --git a/TCPServer.h b/TCPServer.h index 17b2a48c7b..59fbbac5fb 100644 --- a/TCPServer.h +++ b/TCPServer.h @@ -36,25 +36,6 @@ public: */ virtual int open(NetworkInterface *iface); - /** 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 * one time [Default: 1] diff --git a/UDPSocket.cpp b/UDPSocket.cpp index 6e210946e9..9177de60c8 100644 --- a/UDPSocket.cpp +++ b/UDPSocket.cpp @@ -31,27 +31,6 @@ int UDPSocket::open(NetworkInterface *iface) return Socket::open(iface, NSAPI_UDP); } -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, address); -} - int UDPSocket::sendto(const char *host, uint16_t port, const void *data, unsigned size) { SocketAddress addr(_iface, host, port); diff --git a/UDPSocket.h b/UDPSocket.h index f17ad6c8a3..4fecb7cf18 100644 --- a/UDPSocket.h +++ b/UDPSocket.h @@ -33,25 +33,6 @@ public: * @param iface Interface to open socket on */ virtual int open(NetworkInterface *iface); - - /** Bind a UDP Server Socket 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 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