summaryrefslogtreecommitdiff
path: root/src/network/network_type.h
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-01-20 01:32:06 +0000
committerrubidium <rubidium@openttd.org>2009-01-20 01:32:06 +0000
commit5f3772a42c322a99d2ea57b6c97b76fceccbb781 (patch)
treef14ce239c04d4b6e3d7f4dbe6723e777a42cc03f /src/network/network_type.h
parent6e1af6a051b5129c0703dd654794427094d672c8 (diff)
downloadopenttd-5f3772a42c322a99d2ea57b6c97b76fceccbb781.tar.xz
(svn r15157) -Codechange: wrap the hostname/ip and port into a single structure so we can pass either one of them and not convert an ip to a string and then back again.
Diffstat (limited to 'src/network/network_type.h')
-rw-r--r--src/network/network_type.h91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/network/network_type.h b/src/network/network_type.h
index 9104e2f0f..4c6c3e829 100644
--- a/src/network/network_type.h
+++ b/src/network/network_type.h
@@ -11,6 +11,7 @@
#include "../economy_type.h"
#include "core/config.h"
#include "core/game.h"
+#include "core/os_abstraction.h"
enum {
/** How many clients can we have */
@@ -114,5 +115,95 @@ enum NetworkErrorCode {
NETWORK_ERROR_FULL,
};
+/**
+ * Wrapper for (un)resolved network addresses; there's no reason to transform
+ * a numeric IP to a string and then back again to pass it to functions. It
+ * furthermore allows easier delaying of the hostname lookup.
+ */
+class NetworkAddress {
+private:
+ bool resolved; ///< Has the IP address been resolved
+ char *hostname; ///< The hostname, NULL if there isn't one
+ uint32 ip; ///< The resolved IP address
+ uint16 port; ///< The port associated with the address
+
+public:
+ /**
+ * Create a network address based on a resolved IP and port
+ * @param ip the resolved ip
+ * @param port the port
+ */
+ NetworkAddress(in_addr_t ip, uint16 port) :
+ resolved(true),
+ hostname(NULL),
+ ip(ip),
+ port(port)
+ {
+ }
+
+ /**
+ * Create a network address based on a unresolved host and port
+ * @param ip the unresolved hostname
+ * @param port the port
+ */
+ NetworkAddress(const char *hostname, uint16 port) :
+ resolved(false),
+ hostname(strdup(hostname)),
+ ip(0),
+ port(port)
+ {
+ }
+
+ /**
+ * Make a clone of another address
+ * @param address the address to clone
+ */
+ NetworkAddress(const NetworkAddress &address) :
+ resolved(address.resolved),
+ hostname(address.hostname == NULL ? NULL : strdup(address.hostname)),
+ ip(address.ip),
+ port(address.port)
+ {
+ }
+
+ /** Clean up our mess */
+ ~NetworkAddress()
+ {
+ free(hostname);
+ }
+
+ /**
+ * Get the hostname; in case it wasn't given the
+ * IPv4 dotted representation is given.
+ * @return the hostname
+ */
+ const char *GetHostname() const;
+
+ /**
+ * Get the IP address. If the IP has not been resolved yet this will resolve
+ * it possibly blocking this function for a while
+ * @return the IP address
+ */
+ uint32 GetIP();
+
+ /**
+ * Get the port
+ * @return the port
+ */
+ uint16 GetPort() const
+ {
+ return this->port;
+ }
+
+ /**
+ * Check whether the IP address has been resolved already
+ * @return true iff the port has been resolved
+ */
+ bool IsResolved() const
+ {
+ return this->resolved;
+ }
+};
+
#endif /* ENABLE_NETWORK */
#endif /* NETWORK_TYPE_H */