diff --git a/net/NetworkSocketAPI/Socket.cpp b/net/NetworkSocketAPI/Socket.cpp index 7f90895ede..e52ba47793 100644 --- a/net/NetworkSocketAPI/Socket.cpp +++ b/net/NetworkSocketAPI/Socket.cpp @@ -16,12 +16,12 @@ #include "Socket.h" -Socket::Socket(NetworkInterface *iface, nsapi_protocol_t proto) - : _iface(iface) +Socket::Socket() + : _iface(0) + , _socket(0) , _blocking(true) , _timeout(0) { - _socket = _iface->socket_create(proto); } Socket::~Socket() @@ -31,6 +31,28 @@ Socket::~Socket() } } +int Socket::open(NetworkInterface *iface, nsapi_protocol_t proto) +{ + _iface = iface; + _socket = _iface->socket_create(proto); +} + +int Socket::close(bool shutdown) +{ + if (!_socket) { + return 0; + } + + int err = _iface->socket_close(_socket, shutdown); + if (!err) { + void *socket = _socket; + _socket = 0; + _iface->socket_destroy(socket); + } + + return err; +} + void Socket::set_blocking(bool blocking) { _blocking = blocking; @@ -59,22 +81,6 @@ int Socket::get_option(int optname, void *optval, unsigned int *optlen) return _iface->socket_get_option(_socket, optname, optval, optlen); } -int Socket::close(bool shutdown) -{ - if (!_socket) { - return 0; - } - - int err = _iface->socket_close(_socket, shutdown); - if (!err) { - void *socket = _socket; - _socket = 0; - _iface->socket_destroy(socket); - } - - return err; -} - void Socket::thunk(void *p) { FunctionPointer *fptr = (FunctionPointer *)p; diff --git a/net/NetworkSocketAPI/Socket.h b/net/NetworkSocketAPI/Socket.h index cb359e7040..ebae4440e0 100644 --- a/net/NetworkSocketAPI/Socket.h +++ b/net/NetworkSocketAPI/Socket.h @@ -27,6 +27,11 @@ public: /** Socket lifetime */ virtual ~Socket(); + + /** Open the socket + * @param iface Interface to open socket on + */ + virtual int open(NetworkInterface *iface) = 0; /** Set blocking or non-blocking mode of the socket * @param blocking true for blocking mode, false for non-blocking mode. @@ -60,7 +65,8 @@ public: int close(bool shutdown=true); protected: - Socket(NetworkInterface *iface, nsapi_protocol_t proto); + Socket(); + int open(NetworkInterface *iface, nsapi_protocol_t proto); static void thunk(void *); diff --git a/net/NetworkSocketAPI/TCPServer.cpp b/net/NetworkSocketAPI/TCPServer.cpp index aa3623caa0..c3e9dd6c18 100644 --- a/net/NetworkSocketAPI/TCPServer.cpp +++ b/net/NetworkSocketAPI/TCPServer.cpp @@ -17,11 +17,20 @@ #include "TCPServer.h" #include "Timer.h" -TCPServer::TCPServer(NetworkInterface *iface) - : Socket(iface, NSAPI_TCP) +TCPServer::TCPServer() { } +TCPServer::TCPServer(NetworkInterface *iface) +{ + open(iface); +} + +int TCPServer::open(NetworkInterface *iface) +{ + return Socket::open(iface, NSAPI_TCP); +} + int TCPServer::bind(uint16_t port) { if (!_socket) { @@ -45,15 +54,16 @@ int TCPServer::accept(TCPSocket *connection) mbed::Timer timer; timer.start(); - void *socket = connection->_socket; - connection->_socket = 0; - _iface->socket_destroy(socket); + if (connection->_socket) { + connection->close(); + } while (true) { if (!_socket) { return NSAPI_ERROR_NO_SOCKET; } + void *socket; int err = _iface->socket_accept(_socket, &socket); if (err > 0) { diff --git a/net/NetworkSocketAPI/TCPServer.h b/net/NetworkSocketAPI/TCPServer.h index 0072ec3f1c..8d52c06bd1 100644 --- a/net/NetworkSocketAPI/TCPServer.h +++ b/net/NetworkSocketAPI/TCPServer.h @@ -27,8 +27,14 @@ class TCPServer : public Socket { public: /** TCP Server lifetime */ + TCPServer(); TCPServer(NetworkInterface *iface); virtual ~TCPServer(); + + /** Open the socket + * @param iface Interface to open socket on + */ + virtual int open(NetworkInterface *iface); /** Bind a socket to a specific port * @param port The port to listen for incoming connections on diff --git a/net/NetworkSocketAPI/TCPSocket.cpp b/net/NetworkSocketAPI/TCPSocket.cpp index 13bd8b6c0a..84a86c1ff7 100644 --- a/net/NetworkSocketAPI/TCPSocket.cpp +++ b/net/NetworkSocketAPI/TCPSocket.cpp @@ -17,11 +17,20 @@ #include "TCPSocket.h" #include "Timer.h" -TCPSocket::TCPSocket(NetworkInterface *iface) - : Socket(iface, NSAPI_TCP) +TCPSocket::TCPSocket() { } +TCPSocket::TCPSocket(NetworkInterface *iface) +{ + open(iface); +} + +int TCPSocket::open(NetworkInterface *iface) +{ + return Socket::open(iface, NSAPI_TCP); +} + int TCPSocket::connect(const SocketAddress &addr) { if (!_socket) { diff --git a/net/NetworkSocketAPI/TCPSocket.h b/net/NetworkSocketAPI/TCPSocket.h index b4f7917424..31085919eb 100644 --- a/net/NetworkSocketAPI/TCPSocket.h +++ b/net/NetworkSocketAPI/TCPSocket.h @@ -26,8 +26,14 @@ class TCPSocket : public Socket { public: /** TCP socket lifetime */ + TCPSocket(); TCPSocket(NetworkInterface *iface); virtual ~TCPSocket(); + + /** Open the socket + * @param iface Interface to open socket on + */ + virtual int open(NetworkInterface *iface); /** Connects this TCP socket to the server * @param host The host to connect to. It can either be an IP Address diff --git a/net/NetworkSocketAPI/UDPSocket.cpp b/net/NetworkSocketAPI/UDPSocket.cpp index 90ed74ad39..3025cd93c4 100644 --- a/net/NetworkSocketAPI/UDPSocket.cpp +++ b/net/NetworkSocketAPI/UDPSocket.cpp @@ -17,11 +17,20 @@ #include "UDPSocket.h" #include "Timer.h" -UDPSocket::UDPSocket(NetworkInterface *iface) - : Socket(iface, NSAPI_UDP) +UDPSocket::UDPSocket() { } +UDPSocket::UDPSocket(NetworkInterface *iface) +{ + open(iface); +} + +int UDPSocket::open(NetworkInterface *iface) +{ + return Socket::open(iface, NSAPI_UDP); +} + int UDPSocket::bind(uint16_t port) { if (!_socket) { diff --git a/net/NetworkSocketAPI/UDPSocket.h b/net/NetworkSocketAPI/UDPSocket.h index 1614f8f920..8564713c53 100644 --- a/net/NetworkSocketAPI/UDPSocket.h +++ b/net/NetworkSocketAPI/UDPSocket.h @@ -26,8 +26,14 @@ class UDPSocket : public Socket { public: /** UDPSocket lifetime */ + UDPSocket(); UDPSocket(NetworkInterface *iface); virtual ~UDPSocket(); + + /** Open the socket + * @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