rename files: Address -> Endpoint, SockAddr -> Address
authorterom
Wed, 17 Dec 2008 00:44:46 +0000
changeset 382 190f81d30624
parent 381 9b35bc329d23
child 383 2a57f0a871b0
rename files: Address -> Endpoint, SockAddr -> Address
src/Network/Address.cc
src/Network/Address.hh
src/Network/Endpoint.cc
src/Network/Endpoint.hh
src/Network/SockAddr.cc
src/Network/SockAddr.hh
src/Network/Socket.hh
--- a/src/Network/Address.cc	Wed Dec 17 00:40:22 2008 +0000
+++ b/src/Network/Address.cc	Wed Dec 17 00:44:46 2008 +0000
@@ -1,73 +1,76 @@
 
 #include "Address.hh"
 
-#include <sstream>
+#include <cstdlib>
 
-NetworkEndpoint::NetworkEndpoint (void) :
-    hostname(), service()
+NetworkAddress::NetworkAddress (void) :
+    NetworkEndpoint(), address_length(0)
 {
 
 }
 
-NetworkEndpoint::NetworkEndpoint (std::string service) :
-    hostname(), service(service)
+NetworkAddress::NetworkAddress (const sockaddr *addr, socklen_t len) :
+    NetworkEndpoint(), address_length(0)
 {
-
+    // proxy to set_sockaddr
+    set_sockaddr(addr, len);
 }
-        
-NetworkEndpoint::NetworkEndpoint (std::string hostname, std::string service) :
-    hostname(hostname), service(service)
-{
+ 
+void NetworkAddress::set_sockaddr (const sockaddr *addr, socklen_t len) {
+    // invalid length?
+    if (len <= 0 || len > sizeof(address))
+        throw NetworkAddressError(*this, "set_sockaddr", "invalid sockaddr length");
 
-}
-       
-addrinfo* NetworkEndpoint::get_addrinfo (int family, int socktype, int protocol, int flags) const {
-    addrinfo hints, *results;
-    int err;
+    // copy over to address
+    memcpy(&address, addr, len);
     
-    // initialize flags
-    hints.ai_flags = flags;
-    hints.ai_family = family;
-    hints.ai_socktype = socktype;
-    hints.ai_protocol = protocol;
-    hints.ai_addrlen = 0;
-    hints.ai_canonname = NULL;
-    hints.ai_next = NULL;
-
-    // hostname + service may be NULL
-    const char *hostname = this->hostname.empty() ? NULL : this->hostname.c_str();
-    const char *service = this->service.empty() ? NULL : this->service.c_str();
-
-    // do getaddrinfo()
-    if ((err = getaddrinfo(hostname, service, &hints, &results)))
-        throw NetworkAddressError(*this, "getaddrinfo", gai_strerror(err));
-
-    // done
-    return results;
-}
-        
-void NetworkEndpoint::free_addrinfo (addrinfo *info) const {
-    freeaddrinfo(info);
+    // set address_length
+    address_length = len;
+    
+   // update
+   update();
 }
 
-std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr) {
-    s << "[" << addr.get_hostname() << ":" << addr.get_service() << "]";
-
-    return s;
+void NetworkAddress::update (void) {
+    char host_buf[NI_MAXHOST], serv_buf[NI_MAXSERV];
+    int err;
 
-}
- 
-std::string NetworkAddressError::build_str (const NetworkEndpoint &addr, const char *op, const char *msg) {
-    std::stringstream ss;
-
-    ss << op << ": " << addr << ": " << msg;
-
-    return ss.str();
+    // do getnameinfo()
+    if ((err = getnameinfo((sockaddr *) &address, address_length, host_buf, NI_MAXHOST, serv_buf, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV)))
+        throw NetworkAddressError(*this, "getnameinfo", gai_strerror(err));
+    
+    // update hostname + service
+    hostname = host_buf;
+    service = serv_buf;
 }
 
-NetworkAddressError::NetworkAddressError (const NetworkEndpoint &addr, const char *op, const char *msg) :
-    Error(build_str(addr, op, msg))
-{
+addrinfo* NetworkAddress::get_addrinfo (int family, int socktype, int protocol, int flags) const {
+    (void) flags;
 
+    // get my family from address
+    int my_family = address.ss_family;
+    
+    // right family?
+    if (family && family != my_family)
+        throw NetworkAddressError(*this, "fake_getaddrinfo", "ai_family mismatch");
+    
+    // alllocate new addrinfo
+    addrinfo *r = new addrinfo;
+
+    // set it up
+    r->ai_flags = 0;
+    r->ai_family = my_family;
+    r->ai_socktype = socktype;
+    r->ai_protocol = protocol;
+    r->ai_addrlen = get_socklen();
+    r->ai_addr = const_cast<sockaddr *>(get_sockaddr());
+    r->ai_canonname = const_cast<char *>(get_hostname().c_str());
+    r->ai_next = NULL;
+
+    // return it
+    return r;
 }
 
+void NetworkAddress::free_addrinfo (addrinfo *info) const {
+    delete info;
+}
--- a/src/Network/Address.hh	Wed Dec 17 00:40:22 2008 +0000
+++ b/src/Network/Address.hh	Wed Dec 17 00:44:46 2008 +0000
@@ -1,111 +1,121 @@
-#ifndef NETWORK_ADDRESS_HH
-#define NETWORK_ADDRESS_HH
+#ifndef NETWORK_ADDRESS_H
+#define NETWORK_ADDRESS_H
 
-#include "../Error.hh"
+#include "Endpoint.hh"
 
 /*
  * Platform-specific includes
  */
 #ifndef WIN32
     // linux
-    #include <sys/types.h>
-    #include <netdb.h>
+    #include <netinet/in.h>
 #else
     #error "This network code won't compile on win32 :)"
 #endif
 
-#include <string>
-
-// assume...
-#if INET6_ADDRSTRLEN < INET_ADDRSTRLEN
-    #error INET6_ADDRSTRLEN is smaller than INET_ADDRSTRLEN
-#endif
-
 /**
- * Length of a network address
+ * This represents a `struct sockaddr` as used by the socket API.
+ *
+ * It can be used like a NetworkEndpoint, but it's also suitable for use with recvfrom/sendto
  */
-const socklen_t NETWORK_ADDRESS_LENGTH = INET6_ADDRSTRLEN;
-
-/**
- * We use ClanLib's IPAddress API, but with our own name
- */ 
-class NetworkEndpoint {
+class NetworkAddress : public NetworkEndpoint {
     protected:
         /**
-         * Our human-readable hostname
+         * The machine-readable address
          */
-        std::string hostname;
+        sockaddr_storage address;
 
         /**
-         * Our human-readable service
+         * The address length
          */
-        std::string service;
+        socklen_t address_length;
 
     public:
         /**
-         * Construct an empty NetworkEndpoint
+         * Construct an empty SockAddr for later update()
          */
-        NetworkEndpoint (void);
-
-        /**
-         * Construct a NetworkEndpoint with a NULL hostname, and a specific service
-         */
-        explicit NetworkEndpoint (std::string service);
+        NetworkAddress (void);
 
         /**
-         * Construct a NetworkEndpoint on a specific hostname and service
+         * Construct a NetworkAddress from a machine-readable address of the given length
          */
-        NetworkEndpoint (std::string hostname, std::string service);
-        
-        /*
-         * We can use the default copy-constructor and assignment operator
-         */
-   
-    public:    
+        NetworkAddress (const sockaddr *addr, socklen_t len);
+
         /**
-         * Get a addrinfo* for this address using the given family/type/protocol/flags.
-         *
-         * Remember to free the returned pointer using freeaddrinfo() after use.
+         * Get a const sockaddr* in this address
          *
-         * @param family the socket family for hints.ai_family
-         * @param socktype the socket type for hints.ai_socktype
-         * @param protoocl the socket protocol for hints.ai_protocol
-         * @param flags the flags for hints.ai_flags
-         * @return linked list of addrinfo's
-         * @throw NetworkAddressError if resolving this address fails
+         * @return read-only sockaddr pointer
+         */
+        const sockaddr* get_sockaddr (void) const { return (const sockaddr *) &address; }
+
+        /**
+         * Get a mutable sockaddr* in this address
+         *
+         * @return writeable sockaddr pointer
+         */
+        sockaddr* get_sockaddr (void) { return (sockaddr *) &address; }
+
+        /**
+         * Get the current address length
+         *
+         * @return address length
+         */
+        socklen_t get_socklen (void) const { return address_length; }
+
+        /**
+         * Get a the address length pointer, initialized to the size of our sockaddr_storage.
+         *
+         * @return address length pointer
+         */
+        socklen_t* get_socklen_ptr (void) { address_length = sizeof(address); return &address_length; }
+
+        /**
+         * Copy given sockaddr/len + update
+         */
+        void set_sockaddr (const sockaddr *addr, socklen_t len);
+
+        /**
+         * Update internal state for NetworkAddress after sockaddr/socklen_ptr have been modified.
+         */
+        void update (void);
+        
+        /**
+         * Returns a "fake" addrinfo
          */
         virtual addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const;
-        
+
         /**
          * Free an addrinfo returned by get_addrinfo
          */
         virtual void free_addrinfo (addrinfo *info) const;
 
         /**
-         * Get the human-readable hostname
+         * Equal-to comparison operator. Invalid addresses compare equal and are always smaller
          */
-        virtual std::string get_hostname (void) const { return hostname; }
+        bool operator== (const NetworkAddress &other) const {
+            return (address_length == other.address_length) && memcmp(&address, &other.address, address_length) == 0;
+        }
 
         /**
-         * Get the human-readable service name
+         * Not-equal-to comparison operator. Invalid addresses compare equal and are always smaller
          */
-        virtual std::string get_service (void) const { return service; }
+        bool operator!= (const NetworkAddress &other) const {
+            return (address_length != other.address_length) || memcmp(&address, &other.address, address_length) != 0;
+        }
+
+        /**
+         * Less-than comparison operator. Smaller addresses are always lesser.
+         */
+        bool operator< (const NetworkAddress &other) const {
+            return (address_length < other.address_length) || memcmp(&address, &other.address, other.address_length) < 0;
+        }
+
+        /**
+         * Greater-than comparison operator. Bigger addresses are always greater.
+         */
+        bool operator> (const NetworkAddress &other) const {
+            return (address_length > other.address_length) || memcmp(&address, &other.address, address_length) > 0;
+        }
 };
 
-/**
- * Formatted as  [<addr>:<port>]
- */
-std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr);
-
-/**
- *
- */
-class NetworkAddressError : public Error {
-    protected:
-        static std::string build_str (const NetworkEndpoint &addr, const char *op, const char *msg);
-
-    public:
-        NetworkAddressError (const NetworkEndpoint &addr, const char *op, const char *msg);
-};
-
-#endif /* NETWORK_ADDRESS_HH */
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Network/Endpoint.cc	Wed Dec 17 00:44:46 2008 +0000
@@ -0,0 +1,73 @@
+
+#include "Endpoint.hh"
+
+#include <sstream>
+
+NetworkEndpoint::NetworkEndpoint (void) :
+    hostname(), service()
+{
+
+}
+
+NetworkEndpoint::NetworkEndpoint (std::string service) :
+    hostname(), service(service)
+{
+
+}
+        
+NetworkEndpoint::NetworkEndpoint (std::string hostname, std::string service) :
+    hostname(hostname), service(service)
+{
+
+}
+       
+addrinfo* NetworkEndpoint::get_addrinfo (int family, int socktype, int protocol, int flags) const {
+    addrinfo hints, *results;
+    int err;
+    
+    // initialize flags
+    hints.ai_flags = flags;
+    hints.ai_family = family;
+    hints.ai_socktype = socktype;
+    hints.ai_protocol = protocol;
+    hints.ai_addrlen = 0;
+    hints.ai_canonname = NULL;
+    hints.ai_next = NULL;
+
+    // hostname + service may be NULL
+    const char *hostname = this->hostname.empty() ? NULL : this->hostname.c_str();
+    const char *service = this->service.empty() ? NULL : this->service.c_str();
+
+    // do getaddrinfo()
+    if ((err = getaddrinfo(hostname, service, &hints, &results)))
+        throw NetworkAddressError(*this, "getaddrinfo", gai_strerror(err));
+
+    // done
+    return results;
+}
+        
+void NetworkEndpoint::free_addrinfo (addrinfo *info) const {
+    freeaddrinfo(info);
+}
+
+std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr) {
+    s << "[" << addr.get_hostname() << ":" << addr.get_service() << "]";
+
+    return s;
+
+}
+ 
+std::string NetworkAddressError::build_str (const NetworkEndpoint &addr, const char *op, const char *msg) {
+    std::stringstream ss;
+
+    ss << op << ": " << addr << ": " << msg;
+
+    return ss.str();
+}
+
+NetworkAddressError::NetworkAddressError (const NetworkEndpoint &addr, const char *op, const char *msg) :
+    Error(build_str(addr, op, msg))
+{
+
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Network/Endpoint.hh	Wed Dec 17 00:44:46 2008 +0000
@@ -0,0 +1,111 @@
+#ifndef NETWORK_ENDPOINT_HH
+#define NETWORK_ENDPOINT_HH
+
+#include "../Error.hh"
+
+/*
+ * Platform-specific includes
+ */
+#ifndef WIN32
+    // linux
+    #include <sys/types.h>
+    #include <netdb.h>
+#else
+    #error "This network code won't compile on win32 :)"
+#endif
+
+#include <string>
+
+// assume...
+#if INET6_ADDRSTRLEN < INET_ADDRSTRLEN
+    #error INET6_ADDRSTRLEN is smaller than INET_ADDRSTRLEN
+#endif
+
+/**
+ * Length of a network address
+ */
+const socklen_t NETWORK_ADDRESS_LENGTH = INET6_ADDRSTRLEN;
+
+/**
+ * We use ClanLib's IPAddress API, but with our own name
+ */ 
+class NetworkEndpoint {
+    protected:
+        /**
+         * Our human-readable hostname
+         */
+        std::string hostname;
+
+        /**
+         * Our human-readable service
+         */
+        std::string service;
+
+    public:
+        /**
+         * Construct an empty NetworkEndpoint
+         */
+        NetworkEndpoint (void);
+
+        /**
+         * Construct a NetworkEndpoint with a NULL hostname, and a specific service
+         */
+        explicit NetworkEndpoint (std::string service);
+
+        /**
+         * Construct a NetworkEndpoint on a specific hostname and service
+         */
+        NetworkEndpoint (std::string hostname, std::string service);
+        
+        /*
+         * We can use the default copy-constructor and assignment operator
+         */
+   
+    public:    
+        /**
+         * Get a addrinfo* for this address using the given family/type/protocol/flags.
+         *
+         * Remember to free the returned pointer using freeaddrinfo() after use.
+         *
+         * @param family the socket family for hints.ai_family
+         * @param socktype the socket type for hints.ai_socktype
+         * @param protoocl the socket protocol for hints.ai_protocol
+         * @param flags the flags for hints.ai_flags
+         * @return linked list of addrinfo's
+         * @throw NetworkAddressError if resolving this address fails
+         */
+        virtual addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const;
+        
+        /**
+         * Free an addrinfo returned by get_addrinfo
+         */
+        virtual void free_addrinfo (addrinfo *info) const;
+
+        /**
+         * Get the human-readable hostname
+         */
+        virtual std::string get_hostname (void) const { return hostname; }
+
+        /**
+         * Get the human-readable service name
+         */
+        virtual std::string get_service (void) const { return service; }
+};
+
+/**
+ * Formatted as  [<addr>:<port>]
+ */
+std::ostream& operator<< (std::ostream &s, const NetworkEndpoint &addr);
+
+/**
+ *
+ */
+class NetworkAddressError : public Error {
+    protected:
+        static std::string build_str (const NetworkEndpoint &addr, const char *op, const char *msg);
+
+    public:
+        NetworkAddressError (const NetworkEndpoint &addr, const char *op, const char *msg);
+};
+
+#endif /* NETWORK_ADDRESS_HH */
--- a/src/Network/SockAddr.cc	Wed Dec 17 00:40:22 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-
-#include "SockAddr.hh"
-
-#include <cstdlib>
-
-NetworkAddress::NetworkAddress (void) :
-    NetworkEndpoint(), address_length(0)
-{
-
-}
-
-NetworkAddress::NetworkAddress (const sockaddr *addr, socklen_t len) :
-    NetworkEndpoint(), address_length(0)
-{
-    // proxy to set_sockaddr
-    set_sockaddr(addr, len);
-}
- 
-void NetworkAddress::set_sockaddr (const sockaddr *addr, socklen_t len) {
-    // invalid length?
-    if (len <= 0 || len > sizeof(address))
-        throw NetworkAddressError(*this, "set_sockaddr", "invalid sockaddr length");
-
-    // copy over to address
-    memcpy(&address, addr, len);
-    
-    // set address_length
-    address_length = len;
-    
-   // update
-   update();
-}
-
-void NetworkAddress::update (void) {
-    char host_buf[NI_MAXHOST], serv_buf[NI_MAXSERV];
-    int err;
-
-    // do getnameinfo()
-    if ((err = getnameinfo((sockaddr *) &address, address_length, host_buf, NI_MAXHOST, serv_buf, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV)))
-        throw NetworkAddressError(*this, "getnameinfo", gai_strerror(err));
-    
-    // update hostname + service
-    hostname = host_buf;
-    service = serv_buf;
-}
-
-addrinfo* NetworkAddress::get_addrinfo (int family, int socktype, int protocol, int flags) const {
-    (void) flags;
-
-    // get my family from address
-    int my_family = address.ss_family;
-    
-    // right family?
-    if (family && family != my_family)
-        throw NetworkAddressError(*this, "fake_getaddrinfo", "ai_family mismatch");
-    
-    // alllocate new addrinfo
-    addrinfo *r = new addrinfo;
-
-    // set it up
-    r->ai_flags = 0;
-    r->ai_family = my_family;
-    r->ai_socktype = socktype;
-    r->ai_protocol = protocol;
-    r->ai_addrlen = get_socklen();
-    r->ai_addr = const_cast<sockaddr *>(get_sockaddr());
-    r->ai_canonname = const_cast<char *>(get_hostname().c_str());
-    r->ai_next = NULL;
-
-    // return it
-    return r;
-}
-
-void NetworkAddress::free_addrinfo (addrinfo *info) const {
-    delete info;
-}
--- a/src/Network/SockAddr.hh	Wed Dec 17 00:40:22 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,122 +0,0 @@
-#ifndef NETWORK_SOCKADDR_H
-#define NETWORK_SOCKADDR_H
-
-#include "Address.hh"
-
-/*
- * Platform-specific includes
- */
-#ifndef WIN32
-    // linux
-    #include <netinet/in.h>
-#else
-    #error "This network code won't compile on win32 :)"
-#endif
-
-
-/**
- * This represents a `struct sockaddr` as used by the socket API.
- *
- * It can be used like a NetworkEndpoint, but it's also suitable for use with recvfrom/sendto
- */
-class NetworkAddress : public NetworkEndpoint {
-    protected:
-        /**
-         * The machine-readable address
-         */
-        sockaddr_storage address;
-
-        /**
-         * The address length
-         */
-        socklen_t address_length;
-
-    public:
-        /**
-         * Construct an empty SockAddr for later update()
-         */
-        NetworkAddress (void);
-
-        /**
-         * Construct a NetworkAddress from a machine-readable address of the given length
-         */
-        NetworkAddress (const sockaddr *addr, socklen_t len);
-
-        /**
-         * Get a const sockaddr* in this address
-         *
-         * @return read-only sockaddr pointer
-         */
-        const sockaddr* get_sockaddr (void) const { return (const sockaddr *) &address; }
-
-        /**
-         * Get a mutable sockaddr* in this address
-         *
-         * @return writeable sockaddr pointer
-         */
-        sockaddr* get_sockaddr (void) { return (sockaddr *) &address; }
-
-        /**
-         * Get the current address length
-         *
-         * @return address length
-         */
-        socklen_t get_socklen (void) const { return address_length; }
-
-        /**
-         * Get a the address length pointer, initialized to the size of our sockaddr_storage.
-         *
-         * @return address length pointer
-         */
-        socklen_t* get_socklen_ptr (void) { address_length = sizeof(address); return &address_length; }
-
-        /**
-         * Copy given sockaddr/len + update
-         */
-        void set_sockaddr (const sockaddr *addr, socklen_t len);
-
-        /**
-         * Update internal state for NetworkAddress after sockaddr/socklen_ptr have been modified.
-         */
-        void update (void);
-        
-        /**
-         * Returns a "fake" addrinfo
-         */
-        virtual addrinfo* get_addrinfo (int family, int socktype, int protocol = 0, int flags = 0) const;
-
-        /**
-         * Free an addrinfo returned by get_addrinfo
-         */
-        virtual void free_addrinfo (addrinfo *info) const;
-
-        /**
-         * Equal-to comparison operator. Invalid addresses compare equal and are always smaller
-         */
-        bool operator== (const NetworkAddress &other) const {
-            return (address_length == other.address_length) && memcmp(&address, &other.address, address_length) == 0;
-        }
-
-        /**
-         * Not-equal-to comparison operator. Invalid addresses compare equal and are always smaller
-         */
-        bool operator!= (const NetworkAddress &other) const {
-            return (address_length != other.address_length) || memcmp(&address, &other.address, address_length) != 0;
-        }
-
-        /**
-         * Less-than comparison operator. Smaller addresses are always lesser.
-         */
-        bool operator< (const NetworkAddress &other) const {
-            return (address_length < other.address_length) || memcmp(&address, &other.address, other.address_length) < 0;
-        }
-
-        /**
-         * Greater-than comparison operator. Bigger addresses are always greater.
-         */
-        bool operator> (const NetworkAddress &other) const {
-            return (address_length > other.address_length) || memcmp(&address, &other.address, address_length) > 0;
-        }
-};
-
-#endif
--- a/src/Network/Socket.hh	Wed Dec 17 00:40:22 2008 +0000
+++ b/src/Network/Socket.hh	Wed Dec 17 00:44:46 2008 +0000
@@ -6,7 +6,6 @@
 
 #include "../Error.hh"
 #include "Address.hh"
-#include "SockAddr.hh"
 #include "Reactor.hh"
 
 /*