src/Network/Endpoint.hh
changeset 382 190f81d30624
parent 381 9b35bc329d23
child 399 c7295b72731a
equal deleted inserted replaced
381:9b35bc329d23 382:190f81d30624
       
     1 #ifndef NETWORK_ENDPOINT_HH
       
     2 #define NETWORK_ENDPOINT_HH
       
     3 
       
     4 #include "../Error.hh"
       
     5 
       
     6 /*
       
     7  * Platform-specific includes
       
     8  */
       
     9 #ifndef WIN32
       
    10     // linux
       
    11     #include <sys/types.h>
       
    12     #include <netdb.h>
       
    13 #else
       
    14     #error "This network code won't compile on win32 :)"
       
    15 #endif
       
    16 
       
    17 #include <string>
       
    18 
       
    19 // assume...
       
    20 #if INET6_ADDRSTRLEN < INET_ADDRSTRLEN
       
    21     #error INET6_ADDRSTRLEN is smaller than INET_ADDRSTRLEN
       
    22 #endif
       
    23 
       
    24 /**
       
    25  * Length of a network address
       
    26  */
       
    27 const socklen_t NETWORK_ADDRESS_LENGTH = INET6_ADDRSTRLEN;
       
    28 
       
    29 /**
       
    30  * We use ClanLib's IPAddress API, but with our own name
       
    31  */ 
       
    32 class NetworkEndpoint {
       
    33     protected:
       
    34         /**
       
    35          * Our human-readable hostname
       
    36          */
       
    37         std::string hostname;
       
    38 
       
    39         /**
       
    40          * Our human-readable service
       
    41          */
       
    42         std::string service;
       
    43 
       
    44     public:
       
    45         /**
       
    46          * Construct an empty NetworkEndpoint
       
    47          */
       
    48         NetworkEndpoint (void);
       
    49 
       
    50         /**
       
    51          * Construct a NetworkEndpoint with a NULL hostname, and a specific service
       
    52          */
       
    53         explicit NetworkEndpoint (std::string service);
       
    54 
       
    55         /**
       
    56          * Construct a NetworkEndpoint on a specific hostname and service
       
    57          */
       
    58         NetworkEndpoint (std::string hostname, std::string service);
       
    59         
       
    60         /*
       
    61          * We can use the default copy-constructor and assignment operator
       
    62          */
       
    63    
       
    64     public:    
       
    65         /**
       
    66          * Get a addrinfo* for this address using the given family/type/protocol/flags.
       
    67          *
       
    68          * Remember to free the returned pointer using freeaddrinfo() after use.
       
    69          *
       
    70          * @param family the socket family for hints.ai_family
       
    71          * @param socktype the socket type for hints.ai_socktype
       
    72          * @param protoocl the socket protocol for hints.ai_protocol
       
    73          * @param flags the flags for hints.ai_flags
       
    74          * @return linked list of addrinfo's
       
    75          * @throw NetworkAddressError if resolving this address fails
       
    76          */
       
    77         virtual addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const;
       
    78         
       
    79         /**
       
    80          * Free an addrinfo returned by get_addrinfo
       
    81          */
       
    82         virtual void free_addrinfo (addrinfo *info) const;
       
    83 
       
    84         /**
       
    85          * Get the human-readable hostname
       
    86          */
       
    87         virtual std::string get_hostname (void) const { return hostname; }
       
    88 
       
    89         /**
       
    90          * Get the human-readable service name
       
    91          */
       
    92         virtual std::string get_service (void) const { return service; }
       
    93 };
       
    94 
       
    95 /**
       
    96  * Formatted as  [<addr>:<port>]
       
    97  */
       
    98 std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr);
       
    99 
       
   100 /**
       
   101  *
       
   102  */
       
   103 class NetworkAddressError : public Error {
       
   104     protected:
       
   105         static std::string build_str (const NetworkEndpoint &addr, const char *op, const char *msg);
       
   106 
       
   107     public:
       
   108         NetworkAddressError (const NetworkEndpoint &addr, const char *op, const char *msg);
       
   109 };
       
   110 
       
   111 #endif /* NETWORK_ADDRESS_HH */