NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Revision 93:65a9f84862f0, committed 2016-04-19
- Comitter:
- Christopher Haster
- Date:
- Tue Apr 19 18:23:42 2016 -0500
- Parent:
- 92:dd5f19874adf
- Child:
- 94:644df37bb05b
- Commit message:
- Renamed NetworkInterface create/destroy methods to match Socket methods
- socket_create -> socket_open
- socket_destroy -> socket_close
Changed in this revision
--- a/NetworkInterface.h Tue Apr 19 18:23:29 2016 -0500 +++ b/NetworkInterface.h Tue Apr 19 18:23:42 2016 -0500 @@ -89,16 +89,20 @@ friend class TCPSocket; friend class TCPServer; - /** Create a socket - * @param proto The type of socket to open, TCP or UDP - * @return The alocated socket or null on failure + /** Open a socket + * @param handle Handle in which to store new socket + * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP + * @return 0 on success, negative on failure */ - virtual void *socket_create(nsapi_protocol_t proto) = 0; + virtual int socket_open(void **handle, nsapi_protocol_t proto) = 0; - /** Destroy a socket - * @param socket Previously allocated socket + /** Close the socket + * @param handle Socket handle + * @return 0 on success, negative on failure + * @note On failure, any memory associated with the socket must still + * be cleaned up */ - virtual void socket_destroy(void *handle) = 0; + virtual int socket_close(void *handle) = 0; /** Set socket options * @param handle Socket handle @@ -147,13 +151,13 @@ virtual bool socket_is_connected(void *handle) = 0; /** Accept a new connection. - * @param handle Socket handle - * @param socket A TCPSocket instance that will handle the incoming connection. - * @return 0 on success, negative on failure. + * @param handle Handle in which to store new socket + * @param server Socket handle to server to accept from + * @return 0 on success, negative on failure * @note This call is not-blocking, if this call would block, must * immediately return NSAPI_ERROR_WOULD_WAIT */ - virtual int socket_accept(void *handle, void **connection) = 0; + virtual int socket_accept(void **handle, void *server) = 0; /** Send data to the remote host * @param handle Socket handle @@ -199,11 +203,6 @@ */ virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0; - /** Close the socket - * @param handle Socket handle - */ - virtual int socket_close(void *handle) = 0; - /** Register a callback on state change of the socket * @param handle Socket handle * @param callback Function to call on state change
--- a/Socket.cpp Tue Apr 19 18:23:29 2016 -0500 +++ b/Socket.cpp Tue Apr 19 18:23:42 2016 -0500 @@ -34,8 +34,17 @@ int Socket::open(NetworkInterface *iface, nsapi_protocol_t proto) { _iface = iface; - _socket = _iface->socket_create(proto); + + void *socket; + int err = _iface->socket_open(&socket, proto); + if (err) { + return err; + } + + _socket = socket; _iface->socket_attach(_socket, &Socket::thunk, this); + + return 0; } int Socket::close() @@ -44,14 +53,9 @@ return 0; } - int err = _iface->socket_close(_socket); - if (!err) { - void *socket = _socket; - _socket = 0; - _iface->socket_destroy(socket); - } - - return err; + void *socket = _socket; + _socket = 0; + return _iface->socket_close(socket); } void Socket::set_blocking(bool blocking)
--- a/TCPServer.cpp Tue Apr 19 18:23:29 2016 -0500 +++ b/TCPServer.cpp Tue Apr 19 18:23:42 2016 -0500 @@ -64,9 +64,8 @@ } void *socket; - int err = _iface->socket_accept(_socket, &socket); - - if (err > 0) { + int err = _iface->socket_accept(&socket, _socket); + if (!err) { connection->_socket = socket; }