modified by ohneta

Dependents:   HelloESP8266Interface_mine

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
bridadan
Date:
Wed Jul 22 20:16:57 2015 +0000
Revision:
14:9e1bd182ef07
Parent:
13:f84e69b3fdd3
Child:
16:658d4943c753
Adding handle to SocketInterface and updating map key type

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 */
bridadan 11:47c32687a44c 55 virtual const char *getHostByName(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 */
sarahmarshy 13:f84e69b3fdd3 61 virtual void setAddress(const char* addr) = 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 */
sarahmarshy 13:f84e69b3fdd3 67 virtual void setPort(uint16_t port) = 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 */
sarahmarshy 13:f84e69b3fdd3 74 virtual void setAddressPort(const char* addr, uint16_t port) = 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 */
bridadan 11:47c32687a44c 79 virtual const char *getAddress(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 */
bridadan 11:47c32687a44c 84 virtual uint16_t getPort(void) const = 0;
sarahmarshy 13:f84e69b3fdd3 85 protected:
sarahmarshy 13:f84e69b3fdd3 86 char* _addr;
sarahmarshy 13:f84e69b3fdd3 87 uint16_t _port;
sam_grove 8:4b7f97a5597b 88 };
sam_grove 8:4b7f97a5597b 89
bridadan 11:47c32687a44c 90 /** SocketInterface class.
bridadan 11:47c32687a44c 91 * This is a common interface that is shared between all sockets that connect
bridadan 11:47c32687a44c 92 * using the NetworkInterface.
sam_grove 8:4b7f97a5597b 93 */
bridadan 11:47c32687a44c 94 class SocketInterface : public Endpoint
sam_grove 8:4b7f97a5597b 95 {
sam_grove 8:4b7f97a5597b 96 public:
sam_grove 8:4b7f97a5597b 97 /** In server mode, set which port to listen on
sam_grove 8:4b7f97a5597b 98 @param port The endpoint port
sam_grove 8:4b7f97a5597b 99 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 100 */
sam_grove 8:4b7f97a5597b 101 virtual int32_t bind(uint16_t port) const = 0;
sam_grove 8:4b7f97a5597b 102
sam_grove 8:4b7f97a5597b 103 /** In server mode, start listening to a port
sam_grove 8:4b7f97a5597b 104 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 105 */
sam_grove 8:4b7f97a5597b 106 virtual int32_t listen(void) const = 0;
sam_grove 8:4b7f97a5597b 107
sam_grove 8:4b7f97a5597b 108 /** In server mode, accept an incoming connection
sam_grove 8:4b7f97a5597b 109 @param endpoint The endpoint we are listening to
sam_grove 8:4b7f97a5597b 110 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 111 */
bridadan 11:47c32687a44c 112 virtual int32_t accept() const = 0;
sam_grove 8:4b7f97a5597b 113
sam_grove 9:26b257519de9 114 /** In client mode, open a connection to a remote host
sam_grove 8:4b7f97a5597b 115 @param endpoint The endpoint we want to connect to
sam_grove 8:4b7f97a5597b 116 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 117 */
sarahmarshy 13:f84e69b3fdd3 118 virtual int32_t open() = 0;
sam_grove 8:4b7f97a5597b 119
sam_grove 8:4b7f97a5597b 120 /** In client or server mode send data
sam_grove 8:4b7f97a5597b 121 @param data A buffer of data to send
sam_grove 8:4b7f97a5597b 122 @param amount The amount of data to send
sam_grove 8:4b7f97a5597b 123 @param timeout_ms The longest amount of time this send can take
sam_grove 8:4b7f97a5597b 124 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 125 */
sarahmarshy 13:f84e69b3fdd3 126 virtual int32_t send(const void *data, uint32_t amount, uint32_t timeout_ms = 15000) = 0;
sam_grove 8:4b7f97a5597b 127
sam_grove 8:4b7f97a5597b 128 /** In client or server mode receive data
sam_grove 8:4b7f97a5597b 129 @param data a buffer to store the data in
sam_grove 8:4b7f97a5597b 130 @param amount The amount of data to receive
sam_grove 8:4b7f97a5597b 131 @param timeout_ms The longest time to wait for the data
sam_grove 8:4b7f97a5597b 132 @return The amount of data received
sam_grove 8:4b7f97a5597b 133 */
sarahmarshy 13:f84e69b3fdd3 134 virtual uint32_t recv(void *data, uint32_t amount, uint32_t timeout_ms = 15000) = 0;
sam_grove 8:4b7f97a5597b 135
sam_grove 8:4b7f97a5597b 136 /** In client or server mode, close an open connection
sam_grove 8:4b7f97a5597b 137 @param endpoint The endpoint we want to connect to
sam_grove 8:4b7f97a5597b 138 @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
sam_grove 8:4b7f97a5597b 139 */
bridadan 11:47c32687a44c 140 virtual int32_t close() const = 0;
bridadan 14:9e1bd182ef07 141
bridadan 14:9e1bd182ef07 142 /** Get the socket's unique handle
bridadan 14:9e1bd182ef07 143 @return socket's handle number
bridadan 14:9e1bd182ef07 144 */
bridadan 14:9e1bd182ef07 145 virtual uint32_t getHandle() const = 0;
bridadan 14:9e1bd182ef07 146
sarahmarshy 13:f84e69b3fdd3 147 protected:
sarahmarshy 13:f84e69b3fdd3 148 socket_protocol_t _type;
bridadan 14:9e1bd182ef07 149 uint32_t _handle;
bridadan 1:291a9d61e58a 150 };
bridadan 1:291a9d61e58a 151
bridadan 1:291a9d61e58a 152 #endif