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.

Committer:
Christopher Haster
Date:
Tue Apr 05 12:52:07 2016 -0500
Revision:
81:1600369a29dd
Parent:
79:43a7e8c0d6cc
Child:
86:7ca9776b9cc0
Added proper forwarding for interrupt driven callbacks

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 78:0914f9b9b24b 1 /* Socket
Christopher Haster 78:0914f9b9b24b 2 * Copyright (c) 2015 ARM Limited
Christopher Haster 72:6a8b52ee83ed 3 *
Christopher Haster 78:0914f9b9b24b 4 * Licensed under the Apache License, Version 2.0 (the "License");
Christopher Haster 78:0914f9b9b24b 5 * you may not use this file except in compliance with the License.
Christopher Haster 78:0914f9b9b24b 6 * You may obtain a copy of the License at
Christopher Haster 72:6a8b52ee83ed 7 *
Christopher Haster 78:0914f9b9b24b 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 72:6a8b52ee83ed 9 *
Christopher Haster 78:0914f9b9b24b 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 78:0914f9b9b24b 11 * distributed under the License is distributed on an "AS IS" BASIS,
Christopher Haster 78:0914f9b9b24b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 78:0914f9b9b24b 13 * See the License for the specific language governing permissions and
Christopher Haster 78:0914f9b9b24b 14 * limitations under the License.
Christopher Haster 72:6a8b52ee83ed 15 */
Christopher Haster 78:0914f9b9b24b 16
Christopher Haster 72:6a8b52ee83ed 17 #ifndef SOCKET_H
Christopher Haster 72:6a8b52ee83ed 18 #define SOCKET_H
Christopher Haster 72:6a8b52ee83ed 19
Christopher Haster 79:43a7e8c0d6cc 20 #include "SocketAddress.h"
Christopher Haster 79:43a7e8c0d6cc 21 #include "NetworkInterface.h"
Christopher Haster 74:ef2470ca328b 22
Christopher Haster 72:6a8b52ee83ed 23 /** Abstract socket class
Christopher Haster 74:ef2470ca328b 24 */
Christopher Haster 72:6a8b52ee83ed 25 class Socket {
Christopher Haster 72:6a8b52ee83ed 26 public:
Christopher Haster 72:6a8b52ee83ed 27 /** Socket lifetime
Christopher Haster 72:6a8b52ee83ed 28 */
Christopher Haster 81:1600369a29dd 29 virtual ~Socket();
Christopher Haster 72:6a8b52ee83ed 30
Christopher Haster 72:6a8b52ee83ed 31 /** Set blocking or non-blocking mode of the socket
Christopher Haster 72:6a8b52ee83ed 32 \param blocking true for blocking mode, false for non-blocking mode.
Christopher Haster 72:6a8b52ee83ed 33 */
Christopher Haster 72:6a8b52ee83ed 34 void set_blocking(bool blocking);
Christopher Haster 72:6a8b52ee83ed 35
Christopher Haster 74:ef2470ca328b 36 /** Set timeout on a socket operation if blocking behaviour is enabled
Christopher Haster 72:6a8b52ee83ed 37 \param timeout timeout in ms
Christopher Haster 72:6a8b52ee83ed 38 */
Christopher Haster 72:6a8b52ee83ed 39 void set_timeout(unsigned int timeout);
Christopher Haster 72:6a8b52ee83ed 40
Christopher Haster 72:6a8b52ee83ed 41 /** Set socket options
Christopher Haster 76:bbe51641f405 42 \param optname Option ID
Christopher Haster 76:bbe51641f405 43 \param optval Option value
Christopher Haster 76:bbe51641f405 44 \param optlen Length of the option value
Christopher Haster 76:bbe51641f405 45 \return 0 on success, negative on failure
Christopher Haster 72:6a8b52ee83ed 46 */
Christopher Haster 74:ef2470ca328b 47 int set_option(int optname, const void *optval, unsigned int optlen);
Christopher Haster 72:6a8b52ee83ed 48
Christopher Haster 72:6a8b52ee83ed 49 /** Get socket options
Christopher Haster 76:bbe51641f405 50 \param optname Option ID
Christopher Haster 76:bbe51641f405 51 \param optval Buffer pointer where to write the option value
Christopher Haster 76:bbe51641f405 52 \param optlen Length of the option value
Christopher Haster 76:bbe51641f405 53 \return 0 on success, negative on failure
Christopher Haster 72:6a8b52ee83ed 54 */
Christopher Haster 74:ef2470ca328b 55 int get_option(int optname, void *optval, unsigned int *optlen);
Christopher Haster 72:6a8b52ee83ed 56
Christopher Haster 72:6a8b52ee83ed 57 /** Close the socket
Christopher Haster 72:6a8b52ee83ed 58 \param shutdown free the left-over data in message queues
Christopher Haster 72:6a8b52ee83ed 59 */
Christopher Haster 72:6a8b52ee83ed 60 int close(bool shutdown=true);
Christopher Haster 79:43a7e8c0d6cc 61
Christopher Haster 79:43a7e8c0d6cc 62 protected:
Christopher Haster 79:43a7e8c0d6cc 63 Socket(NetworkInterface *iface, NetworkInterface::protocol_t proto);
Christopher Haster 79:43a7e8c0d6cc 64
Christopher Haster 81:1600369a29dd 65 static void thunk(void *);
Christopher Haster 81:1600369a29dd 66
Christopher Haster 79:43a7e8c0d6cc 67 NetworkInterface *_iface;
Christopher Haster 79:43a7e8c0d6cc 68 void *_socket;
Christopher Haster 79:43a7e8c0d6cc 69 bool _blocking;
Christopher Haster 79:43a7e8c0d6cc 70 unsigned _timeout;
Christopher Haster 72:6a8b52ee83ed 71 };
Christopher Haster 72:6a8b52ee83ed 72
Christopher Haster 72:6a8b52ee83ed 73 #endif