Base class for IP Based Networking Libraries
Dependencies: DnsQuery
Dependents: TempTower BSDInterfaceTests HelloBSDInterface ESP8266InterfaceTests ... more
For a complete getting started guide see the wiki...
Network Socket API
The Network Socket API provides a common interface for using sockets on network devices. The API provides a simple class-based interface that should be familiar to users experienced with other socket APIs. Additionally, the API provides a simple interface for implementing network devices, making it easy to connect hardware agnostic programs to new devices.
Network Interfaces
The NetworkInterface provides an abstract class for network devices that support sockets. Devices should provide a DeviceInterface class that inherits this interface and adds implementation specific methods for using the device. A NetworkInterface must be provided to a Socket constructor to open a socket on the interface. Currently two subclasses are defined for common devices, EthernetInterface
and WiFiInterface.
Sockets
The Socket class is used for managing network sockets. Once opened, the socket provides a pipe through which data can sent and recieved to a specific endpoint. The socket class can be instantiated as either a TCPSocket
or a UDPSocket
which defines the protocol used for the connection.
Diff: Socket.cpp
- Branch:
- api-changes
- Revision:
- 34:c17745683385
- Parent:
- 32:2c5fc105fc50
- Child:
- 35:838393fbc2ca
--- a/Socket.cpp Tue Feb 23 17:14:20 2016 +0000 +++ b/Socket.cpp Mon Feb 22 19:56:44 2016 -0600 @@ -31,7 +31,7 @@ if (_socket) close(); } -int32_t Socket::setURL(const char *url, uint16_t port) +int32_t Socket::setURL(const char *url) { int32_t err = _iface->getHostByName(url, _ip_address); if (err) return err; @@ -40,24 +40,16 @@ _socket->setIPAddress(_ip_address); } - if (port) { - setPort(port); - } - return 0; } -void Socket::setIPAddress(const char *ip, uint16_t port) +void Socket::setIPAddress(const char *ip) { strcpy(_ip_address, ip); if (_socket) { _socket->setIPAddress(_ip_address); } - - if (port) { - setPort(port); - } } void Socket::setPort(uint16_t port) @@ -79,9 +71,22 @@ return _port; } -int32_t Socket::open() +int32_t Socket::open(const char *url, uint16_t port) { - if (_socket) close(); + if (_socket) { + int32_t err = close(); + if (err) return err; + } + + if (url) { + int32_t err = setURL(url); + if (err) return err; + } + + if (port) { + setPort(port); + } + _socket = _iface->createSocket(_proto); if (!_socket) return -2; @@ -94,8 +99,8 @@ _socket->setPort(_port); } + int32_t err = _socket->open(); - if (err) { _iface->destroySocket(_socket); } @@ -119,14 +124,12 @@ int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms) { if (!_socket) return -2; - return _socket->send(data, len, timeout_ms); } int32_t Socket::recv(void *data, uint32_t len, uint32_t timeout_ms) { if (!_socket) return -2; - return _socket->recv(data, len, timeout_ms); }