DongEun Koak / NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Revision:
8:4b7f97a5597b
Parent:
7:b147c08301be
Child:
9:26b257519de9
--- a/SocketInterface.h	Wed Jun 17 20:56:15 2015 +0000
+++ b/SocketInterface.h	Wed Jun 17 23:37:43 2015 +0000
@@ -13,76 +13,138 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
- 
+
 #ifndef SOCKETINTERFACE_H
 #define SOCKETINTERFACE_H
 
+#include "stdint.h"
+
+/** This enum defines the possible socket domain types.
+ */
+typedef enum {
+    SOCK_IPV4,      /*!< IPv4 */
+    SOCK_IPV6,      /*!< IPV6 */
+} socket_domain_t;
+
+/** This enum defines the possible socket types.
+ */
+typedef enum {
+    SOCK_STREAM,    /*!< Reliable stream-oriented service or Stream Sockets */
+    SOCK_DGRAM,     /**< Datagram service or Datagram Sockets */
+    SOCK_SEQPACKET, /*!< Reliable sequenced packet service */
+    SOCK_RAW        /*!< Raw protocols atop the network layer */
+} socket_type_t;
+
+/** This enum defines the ip protocols
+ */
+typedef enum {
+    SOCK_TCP,    /*!< Socket connection over TCP */
+    SOCK_UDP,    /*!< Socket connection over UDP */
+} socket_protocol_t;
+
+/** Base class that defines an endpoint (TCP/UDP/Server/Client Socket)
+ */
+class Endpoint
+{
+public:
+
+    /** Get the ip address of a host by DNS lookup
+        @param name The name of a host you need an ip address for
+        @return The ip address of the host otherwise NULL
+     */
+    virtual const char *get_host_by_name(const char *name) const = 0;
+    
+    /** Set the address of this endpoint
+        @param addr The endpoint address
+        @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+     */
+    virtual int32_t set_address(const char* addr) const = 0;
+
+    /** Set the port of this endpoint
+        @param port The endpoint port
+        @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+     */
+    virtual int32_t set_port(uint16_t port) const = 0;
+
+    /** Set the address and port of this endpoint
+        @param addr The endpoint address (supplied as an ip address).
+        @param port The endpoint port
+        @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+     */
+    virtual int32_t set_address_port(const char* addr, uint16_t port) const = 0;
+
+    /** Get the IP address of this endpoint
+        @return The IP address of this endpoint.
+     */
+    virtual const char *get_address(void) const = 0;
+
+    /** Get the port of this endpoint
+        @return The port of this socket
+     */
+    virtual uint16_t get_port(void) const = 0;
+
+};
+
+/** Base class that defines a TCP/UDPSocket endpoint
+ */
+class Socket : public Endpoint
+{
+public:
+    /** In server mode, set which port to listen on
+        @param port The endpoint port
+        @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+     */
+    virtual int32_t bind(uint16_t port) const = 0;
+    
+    /** In server mode, start listening to a port
+        @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+     */
+    virtual int32_t listen(void) const = 0;
+    
+    /** In server mode, accept an incoming connection
+        @param endpoint The endpoint we are listening to
+        @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+     */
+    virtual int32_t accept(const Endpoint &endpoint) const = 0;
+
+    /** In client mode, connect to a remote host
+        @param endpoint The endpoint we want to connect to
+        @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+     */
+    virtual int32_t connect(const Endpoint &endpoint) const = 0;
+    
+    /** In client or server mode send data
+        @param data A buffer of data to send
+        @param amount The amount of data to send
+        @param timeout_ms The longest amount of time this send can take
+        @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+     */
+    virtual int32_t send(const void *data, uint32_t amount, uint32_t timeout_ms = 15000) const = 0;
+    
+    /** In client or server mode receive data
+        @param data a buffer to store the data in
+        @param amount The amount of data to receive
+        @param timeout_ms The longest time to wait for the data
+        @return The amount of data received
+     */
+    virtual uint32_t recv(const void *data, uint32_t amount, uint32_t timeout_ms = 15000) const = 0;
+    
+    /** In client or server mode, close an open connection
+        @param endpoint The endpoint we want to connect to 
+        @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+     */
+    virtual int32_t close(const Endpoint &endpoint) const = 0;
+
+};
+
 /** SocketInterface class.
  *   This is a common interface that is shared between all sockets that connect
  *   using the NetworkInterface.
  */
-class SocketInterface {
-
+class SocketInterface : public Socket
+{
 public:
-//    /** This enum defines the possible socket domain types.
-//     */
-//    typedef enum {
-//        AF_INET,        /*!< IPv4 */
-//        AF_INET6,       /*!< IPV6 */
-//        AF_UNIX         /*!< Local socket (using a file) */
-//    } socket_domain_t;
-//    
-//    /** This enum defines the possible socket types.
-//     */
-//    typedef enum {
-//        SOCK_STREAM,    /*!< Reliable stream-oriented service or Stream Sockets */
-//        SOCK_DGRAM,     /**< Datagram service or Datagram Sockets */
-//        SOCK_SEQPACKET, /*!< Reliable sequenced packet service */
-//        SOCK_RAW        /*!< Raw protocols atop the network layer */
-//    } socket_type_t;
-//
-//    /** This enum defines the ip protocols
-//     */
-//    typedef enum {
-//        IPPROTO_TCP,    /*!< Socket connection over TCP */
-//        IPPROTO_UDP,    /*!< Socket connection over UDP */
-//        IPPROTO_SCTP    /*!< Socket connection over SCTP */
-//        IPPROTO_DCCP,   /*!< Socket connection over DCCP */
-//    } socket_type_t;
-//
-//    /** Configure the socket's protocol and type.
-//        @param protocol The protocol to use.
-//        @param type The type of socket to use.
-//        @returns 0 on success, a negative number on failure
-//     */
-//    virtual int32_t socket(socket_domain_t protocol, socket_type_t type, ) const = 0;
-//    
-//    /** Configure the socket's protocol and type.
-//        @param protocol The protocol to use.
-//        @param type The type of socket to use.
-//        @returns 0 on success, a negative number on failure
-//     */
-//    virtual int bind(const struct sockaddr *my_addr, socklen_t addrlen);
-//    
-//    /** 
-//     *    Set blocking or non-blocking mode of the socket and a timeout on
-//     *    blocking socket operations.
-//     *    
-//     *    @param blocking true for blocking mode, false for non-blocking mode.
-//     *    @param timeout timeout in ms [Default: (1500)ms].
-//     */
-//    virtual void setBlocking(bool blocking, unsigned int timeout=1500) = 0;
-//    
-//    /*
-//        "options" functions here? Not familiar with this, need to discuss
-//    */
-//    
-//    /**
-//     *    Close the socket
-//     */
-//    virtual void close();
-
+    // do something to specify TCP/UDP here
 };
 
 #endif
- 
\ No newline at end of file