NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Revision 11:47c32687a44c, committed 2015-07-15
- Comitter:
- bridadan
- Date:
- Wed Jul 15 23:21:17 2015 +0000
- Parent:
- 10:50b0a3f840df
- Child:
- 12:ab3679eb4d9d
- Commit message:
- Moved underscored_functions to camelCase, added allocate/deallocate socket functions, added data structures to keep track of multiple sockets.
Changed in this revision
NetworkInterface.h | Show annotated file Show diff for this revision Revisions of this file |
SocketInterface.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/NetworkInterface.h Wed Jul 15 15:41:11 2015 +0000 +++ b/NetworkInterface.h Wed Jul 15 23:21:17 2015 +0000 @@ -19,6 +19,9 @@ #include "stdint.h" +#include "SocketInterface.h" +#include <map> + /** NetworkInterface class. This is a common interface that is shared between all hardware that connect to a network over IP. @@ -77,6 +80,25 @@ @returns true if connected, a negative number on failure */ virtual int32_t isConnected(void) const = 0; + + /** Allocate space for a socket. + @returns a pointer to the allocated socket. + */ + virtual SocketInterface* allocateSocket(socket_protocol_t socketProtocol) const = 0; + + /** Deallocate space for a socket. + @returns a pointer to the deallocated socket. + */ + virtual int deallocateSocket(SocketInterface* socket) const = 0; + +protected: + /** Map used to keep track of all SocketInterface instances */ + std::map<int, SocketInterface*> sockets; + + /** Counter used to create unique handles for new sockets. + Should be incremented whenever a new socket is created. + */ + uint32_t uuidCounter; }; #endif
--- a/SocketInterface.h Wed Jul 15 15:41:11 2015 +0000 +++ b/SocketInterface.h Wed Jul 15 23:21:17 2015 +0000 @@ -52,42 +52,44 @@ @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; + virtual const char *getHostByName(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; + virtual int32_t setAddress(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; + virtual int32_t setPort(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; + virtual int32_t setAddressPort(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; + virtual const char *getAddress(void) const = 0; /** Get the port of this endpoint @return The port of this socket */ - virtual uint16_t get_port(void) const = 0; + virtual uint16_t getPort(void) const = 0; }; -/** Base class that defines a TCP/UDPSocket endpoint +/** SocketInterface class. + * This is a common interface that is shared between all sockets that connect + * using the NetworkInterface. */ -class Socket : public Endpoint +class SocketInterface : public Endpoint { public: /** In server mode, set which port to listen on @@ -105,13 +107,13 @@ @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; + virtual int32_t accept() const = 0; /** In client mode, open a connection 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 open(const Endpoint &endpoint) const = 0; + virtual int32_t open() const = 0; /** In client or server mode send data @param data A buffer of data to send @@ -133,18 +135,11 @@ @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 : public Socket -{ -public: - // do something to specify TCP/UDP here + virtual int32_t close() const = 0; + +protected: + /** The socket's unique handle number. Used to quickly create and destroy sockets within NetworkInterfaces. */ + uint32_t handle; }; #endif