Renamed NetworkInterface create/destroy methods to match Socket methods

- socket_create -> socket_open
- socket_destroy -> socket_close
pull/2216/head^2
Christopher Haster 2016-03-13 14:37:55 -05:00
parent dfc1ca4cef
commit 1b368cf525
3 changed files with 30 additions and 28 deletions

View File

@ -89,16 +89,20 @@ protected:
friend class TCPSocket; friend class TCPSocket;
friend class TCPServer; friend class TCPServer;
/** Create a socket /** Open a socket
* @param proto The type of socket to open, TCP or UDP * @param handle Handle in which to store new socket
* @return The alocated socket or null on failure * @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 /** Close the socket
* @param socket Previously allocated 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 /** Set socket options
* @param handle Socket handle * @param handle Socket handle
@ -147,13 +151,13 @@ protected:
virtual bool socket_is_connected(void *handle) = 0; virtual bool socket_is_connected(void *handle) = 0;
/** Accept a new connection. /** Accept a new connection.
* @param handle Socket handle * @param handle Handle in which to store new socket
* @param socket A TCPSocket instance that will handle the incoming connection. * @param server Socket handle to server to accept from
* @return 0 on success, negative on failure. * @return 0 on success, negative on failure
* @note This call is not-blocking, if this call would block, must * @note This call is not-blocking, if this call would block, must
* immediately return NSAPI_ERROR_WOULD_WAIT * 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 /** Send data to the remote host
* @param handle Socket handle * @param handle Socket handle
@ -199,11 +203,6 @@ protected:
*/ */
virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0; 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 /** Register a callback on state change of the socket
* @param handle Socket handle * @param handle Socket handle
* @param callback Function to call on state change * @param callback Function to call on state change

View File

@ -34,8 +34,17 @@ Socket::~Socket()
int Socket::open(NetworkInterface *iface, nsapi_protocol_t proto) int Socket::open(NetworkInterface *iface, nsapi_protocol_t proto)
{ {
_iface = iface; _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); _iface->socket_attach(_socket, &Socket::thunk, this);
return 0;
} }
int Socket::close() int Socket::close()
@ -44,14 +53,9 @@ int Socket::close()
return 0; return 0;
} }
int err = _iface->socket_close(_socket); void *socket = _socket;
if (!err) { _socket = 0;
void *socket = _socket; return _iface->socket_close(socket);
_socket = 0;
_iface->socket_destroy(socket);
}
return err;
} }
void Socket::set_blocking(bool blocking) void Socket::set_blocking(bool blocking)

View File

@ -64,9 +64,8 @@ int TCPServer::accept(TCPSocket *connection)
} }
void *socket; void *socket;
int err = _iface->socket_accept(_socket, &socket); int err = _iface->socket_accept(&socket, _socket);
if (!err) {
if (err > 0) {
connection->_socket = socket; connection->_socket = socket;
} }