modified by ohneta

Dependents:   HelloESP8266Interface_mine

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
sam_grove
Date:
Thu Jul 16 05:19:17 2015 +0000
Revision:
12:ab3679eb4d9d
Parent:
11:47c32687a44c
Child:
13:f84e69b3fdd3
Update comments

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