NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
sam_grove
Date:
Wed Jun 17 23:37:43 2015 +0000
Revision:
8:4b7f97a5597b
Parent:
7:b147c08301be
Child:
9:26b257519de9
Updates to socket base classes. still needs a little work

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:d35446f60233 1 /* SocketInterface Base Class
sam_grove 0:d35446f60233 2 * Copyright (c) 2015 ARM Limited
sam_grove 0:d35446f60233 3 *
sam_grove 0:d35446f60233 4 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:d35446f60233 5 * you may not use this file except in compliance with the License.
sam_grove 0:d35446f60233 6 * You may obtain a copy of the License at
sam_grove 0:d35446f60233 7 *
sam_grove 0:d35446f60233 8 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:d35446f60233 9 *
sam_grove 0:d35446f60233 10 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:d35446f60233 11 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:d35446f60233 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:d35446f60233 13 * See the License for the specific language governing permissions and
sam_grove 0:d35446f60233 14 * limitations under the License.
sam_grove 0:d35446f60233 15 */
sam_grove 8:4b7f97a5597b 16
bridadan 1:291a9d61e58a 17 #ifndef SOCKETINTERFACE_H
bridadan 1:291a9d61e58a 18 #define SOCKETINTERFACE_H
bridadan 2:ce08986b18b5 19
sam_grove 8:4b7f97a5597b 20 #include "stdint.h"
sam_grove 8:4b7f97a5597b 21
sam_grove 8:4b7f97a5597b 22 /** This enum defines the possible socket domain types.
sam_grove 8:4b7f97a5597b 23 */
sam_grove 8:4b7f97a5597b 24 typedef enum {
sam_grove 8:4b7f97a5597b 25 SOCK_IPV4, /*!< IPv4 */
sam_grove 8:4b7f97a5597b 26 SOCK_IPV6, /*!< IPV6 */
sam_grove 8:4b7f97a5597b 27 } socket_domain_t;
sam_grove 8:4b7f97a5597b 28
sam_grove 8:4b7f97a5597b 29 /** This enum defines the possible socket types.
sam_grove 8:4b7f97a5597b 30 */
sam_grove 8:4b7f97a5597b 31 typedef enum {
sam_grove 8:4b7f97a5597b 32 SOCK_STREAM, /*!< Reliable stream-oriented service or Stream Sockets */
sam_grove 8:4b7f97a5597b 33 SOCK_DGRAM, /**< Datagram service or Datagram Sockets */
sam_grove 8:4b7f97a5597b 34 SOCK_SEQPACKET, /*!< Reliable sequenced packet service */
sam_grove 8:4b7f97a5597b 35 SOCK_RAW /*!< Raw protocols atop the network layer */
sam_grove 8:4b7f97a5597b 36 } socket_type_t;
sam_grove 8:4b7f97a5597b 37
sam_grove 8:4b7f97a5597b 38 /** This enum defines the ip protocols
sam_grove 8:4b7f97a5597b 39 */
sam_grove 8:4b7f97a5597b 40 typedef enum {
sam_grove 8:4b7f97a5597b 41 SOCK_TCP, /*!< Socket connection over TCP */
sam_grove 8:4b7f97a5597b 42 SOCK_UDP, /*!< Socket connection over UDP */
sam_grove 8:4b7f97a5597b 43 } socket_protocol_t;
sam_grove 8:4b7f97a5597b 44
sam_grove 8:4b7f97a5597b 45 /** Base class that defines an endpoint (TCP/UDP/Server/Client Socket)
sam_grove 8:4b7f97a5597b 46 */
sam_grove 8:4b7f97a5597b 47 class Endpoint
sam_grove 8:4b7f97a5597b 48 {
sam_grove 8:4b7f97a5597b 49 public:
sam_grove 8:4b7f97a5597b 50
sam_grove 8:4b7f97a5597b 51 /** Get the ip address of a host by DNS lookup
sam_grove 8:4b7f97a5597b 52 @param name The name of a host you need an ip address for
sam_grove 8:4b7f97a5597b 53 @return The ip address of the host otherwise NULL
sam_grove 8:4b7f97a5597b 54 */
sam_grove 8:4b7f97a5597b 55 virtual const char *get_host_by_name(const char *name) const = 0;
sam_grove 8:4b7f97a5597b 56
sam_grove 8:4b7f97a5597b 57 /** Set the address of this endpoint
sam_grove 8:4b7f97a5597b 58 @param addr The endpoint address
sam_grove 8:4b7f97a5597b 59 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 60 */
sam_grove 8:4b7f97a5597b 61 virtual int32_t set_address(const char* addr) const = 0;
sam_grove 8:4b7f97a5597b 62
sam_grove 8:4b7f97a5597b 63 /** Set the port of this endpoint
sam_grove 8:4b7f97a5597b 64 @param port The endpoint port
sam_grove 8:4b7f97a5597b 65 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 66 */
sam_grove 8:4b7f97a5597b 67 virtual int32_t set_port(uint16_t port) const = 0;
sam_grove 8:4b7f97a5597b 68
sam_grove 8:4b7f97a5597b 69 /** Set the address and port of this endpoint
sam_grove 8:4b7f97a5597b 70 @param addr The endpoint address (supplied as an ip address).
sam_grove 8:4b7f97a5597b 71 @param port The endpoint port
sam_grove 8:4b7f97a5597b 72 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 73 */
sam_grove 8:4b7f97a5597b 74 virtual int32_t set_address_port(const char* addr, uint16_t port) const = 0;
sam_grove 8:4b7f97a5597b 75
sam_grove 8:4b7f97a5597b 76 /** Get the IP address of this endpoint
sam_grove 8:4b7f97a5597b 77 @return The IP address of this endpoint.
sam_grove 8:4b7f97a5597b 78 */
sam_grove 8:4b7f97a5597b 79 virtual const char *get_address(void) const = 0;
sam_grove 8:4b7f97a5597b 80
sam_grove 8:4b7f97a5597b 81 /** Get the port of this endpoint
sam_grove 8:4b7f97a5597b 82 @return The port of this socket
sam_grove 8:4b7f97a5597b 83 */
sam_grove 8:4b7f97a5597b 84 virtual uint16_t get_port(void) const = 0;
sam_grove 8:4b7f97a5597b 85
sam_grove 8:4b7f97a5597b 86 };
sam_grove 8:4b7f97a5597b 87
sam_grove 8:4b7f97a5597b 88 /** Base class that defines a TCP/UDPSocket endpoint
sam_grove 8:4b7f97a5597b 89 */
sam_grove 8:4b7f97a5597b 90 class Socket : public Endpoint
sam_grove 8:4b7f97a5597b 91 {
sam_grove 8:4b7f97a5597b 92 public:
sam_grove 8:4b7f97a5597b 93 /** In server mode, set which port to listen on
sam_grove 8:4b7f97a5597b 94 @param port The endpoint port
sam_grove 8:4b7f97a5597b 95 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 96 */
sam_grove 8:4b7f97a5597b 97 virtual int32_t bind(uint16_t port) const = 0;
sam_grove 8:4b7f97a5597b 98
sam_grove 8:4b7f97a5597b 99 /** In server mode, start listening to a port
sam_grove 8:4b7f97a5597b 100 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 101 */
sam_grove 8:4b7f97a5597b 102 virtual int32_t listen(void) const = 0;
sam_grove 8:4b7f97a5597b 103
sam_grove 8:4b7f97a5597b 104 /** In server mode, accept an incoming connection
sam_grove 8:4b7f97a5597b 105 @param endpoint The endpoint we are listening to
sam_grove 8:4b7f97a5597b 106 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 107 */
sam_grove 8:4b7f97a5597b 108 virtual int32_t accept(const Endpoint &endpoint) const = 0;
sam_grove 8:4b7f97a5597b 109
sam_grove 8:4b7f97a5597b 110 /** In client mode, connect to a remote host
sam_grove 8:4b7f97a5597b 111 @param endpoint The endpoint we want to connect to
sam_grove 8:4b7f97a5597b 112 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 113 */
sam_grove 8:4b7f97a5597b 114 virtual int32_t connect(const Endpoint &endpoint) const = 0;
sam_grove 8:4b7f97a5597b 115
sam_grove 8:4b7f97a5597b 116 /** In client or server mode send data
sam_grove 8:4b7f97a5597b 117 @param data A buffer of data to send
sam_grove 8:4b7f97a5597b 118 @param amount The amount of data to send
sam_grove 8:4b7f97a5597b 119 @param timeout_ms The longest amount of time this send can take
sam_grove 8:4b7f97a5597b 120 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 121 */
sam_grove 8:4b7f97a5597b 122 virtual int32_t send(const void *data, uint32_t amount, uint32_t timeout_ms = 15000) const = 0;
sam_grove 8:4b7f97a5597b 123
sam_grove 8:4b7f97a5597b 124 /** In client or server mode receive data
sam_grove 8:4b7f97a5597b 125 @param data a buffer to store the data in
sam_grove 8:4b7f97a5597b 126 @param amount The amount of data to receive
sam_grove 8:4b7f97a5597b 127 @param timeout_ms The longest time to wait for the data
sam_grove 8:4b7f97a5597b 128 @return The amount of data received
sam_grove 8:4b7f97a5597b 129 */
sam_grove 8:4b7f97a5597b 130 virtual uint32_t recv(const void *data, uint32_t amount, uint32_t timeout_ms = 15000) const = 0;
sam_grove 8:4b7f97a5597b 131
sam_grove 8:4b7f97a5597b 132 /** In client or server mode, close an open connection
sam_grove 8:4b7f97a5597b 133 @param endpoint The endpoint we want to connect to
sam_grove 8:4b7f97a5597b 134 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 135 */
sam_grove 8:4b7f97a5597b 136 virtual int32_t close(const Endpoint &endpoint) const = 0;
sam_grove 8:4b7f97a5597b 137
sam_grove 8:4b7f97a5597b 138 };
sam_grove 8:4b7f97a5597b 139
bridadan 2:ce08986b18b5 140 /** SocketInterface class.
bridadan 2:ce08986b18b5 141 * This is a common interface that is shared between all sockets that connect
bridadan 2:ce08986b18b5 142 * using the NetworkInterface.
bridadan 2:ce08986b18b5 143 */
sam_grove 8:4b7f97a5597b 144 class SocketInterface : public Socket
sam_grove 8:4b7f97a5597b 145 {
bridadan 1:291a9d61e58a 146 public:
sam_grove 8:4b7f97a5597b 147 // do something to specify TCP/UDP here
bridadan 1:291a9d61e58a 148 };
bridadan 1:291a9d61e58a 149
bridadan 1:291a9d61e58a 150 #endif