diff options
author | rubidium <rubidium@openttd.org> | 2009-01-20 11:28:18 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2009-01-20 11:28:18 +0000 |
commit | 206841ba5f526270a37ab002ebbae6bd533dedad (patch) | |
tree | 6b344cc0bfeff2bffba2764265342a0ed65238d2 /src/network/core/address.h | |
parent | 12ff4cf083704088ef88ec178d81c37ca60327e0 (diff) | |
download | openttd-206841ba5f526270a37ab002ebbae6bd533dedad.tar.xz |
(svn r15163) -Change/Fix: use a non-blocking method to resolve the hostname and connect to game servers.
Diffstat (limited to 'src/network/core/address.h')
-rw-r--r-- | src/network/core/address.h | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/network/core/address.h b/src/network/core/address.h new file mode 100644 index 000000000..62e79f3de --- /dev/null +++ b/src/network/core/address.h @@ -0,0 +1,103 @@ +/* $Id$ */ + +/** @file core/address.h Wrapper for network addresses. */ + +#ifndef NETWORK_ADDRESS_H +#define NETWORK_ADDRESS_H + +#ifdef ENABLE_NETWORK + +#include "os_abstraction.h" + +/** + * 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_ADDRESS_H */ |