changeset 11549:df3afb578e51 draft

(svn r15914) -Codechange: let the content handling make use of NetworkAddress.
author rubidium <rubidium@openttd.org>
date Thu, 02 Apr 2009 18:35:59 +0000
parents 9c985010bebe
children 1449b6fb6ce6
files src/network/core/address.cpp src/network/core/address.h src/network/core/tcp_content.cpp src/network/core/tcp_content.h src/network/network_content.cpp
diffstat 5 files changed, 29 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/network/core/address.cpp
+++ b/src/network/core/address.cpp
@@ -7,7 +7,9 @@
 #ifdef ENABLE_NETWORK
 
 #include "address.h"
+#include "config.h"
 #include "host.h"
+#include "../../string_func.h"
 
 const char *NetworkAddress::GetHostname() const
 {
@@ -27,4 +29,13 @@
 	return this->ip;
 }
 
+const char *NetworkAddress::GetAddressAsString() const
+{
+	/* 6 = for the : and 5 for the decimal port number */
+	static char buf[NETWORK_HOSTNAME_LENGTH + 6];
+
+	seprintf(buf, lastof(buf), "%s:%d", this->GetHostname(), this->GetPort());
+	return buf;
+}
+
 #endif /* ENABLE_NETWORK */
--- a/src/network/core/address.h
+++ b/src/network/core/address.h
@@ -40,9 +40,9 @@
 	 * @param ip the unresolved hostname
 	 * @param port the port
 	 */
-	NetworkAddress(const char *hostname, uint16 port) :
+	NetworkAddress(const char *hostname = NULL, uint16 port = 0) :
 		resolved(false),
-		hostname(strdup(hostname)),
+		hostname(hostname == NULL ? NULL : strdup(hostname)),
 		ip(0),
 		port(port)
 	{
@@ -74,6 +74,12 @@
 	const char *GetHostname() const;
 
 	/**
+	 * Get the address as a string, e.g. 127.0.0.1:12345.
+	 * @return the address
+	 */
+	const char *GetAddressAsString() 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
--- a/src/network/core/tcp_content.cpp
+++ b/src/network/core/tcp_content.cpp
@@ -84,9 +84,9 @@
 
 		default:
 			if (this->HasClientQuit()) {
-				DEBUG(net, 0, "[tcp/content] received invalid packet type %d from %s:%d", type,  inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port));
+				DEBUG(net, 0, "[tcp/content] received invalid packet type %d from %s", type, this->client_addr.GetAddressAsString());
 			} else {
-				DEBUG(net, 0, "[tcp/content] received illegal packet from %s:%d", inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port));
+				DEBUG(net, 0, "[tcp/content] received illegal packet from %s", this->client_addr.GetAddressAsString());
 			}
 			return false;
 	}
@@ -114,8 +114,8 @@
  */
 #define DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(type) \
 bool NetworkContentSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) { \
-	DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s:%d", \
-			type, inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port)); \
+	DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s", \
+			type, this->client_addr.GetAddressAsString()); \
 	return false; \
 }
 
--- a/src/network/core/tcp_content.h
+++ b/src/network/core/tcp_content.h
@@ -101,7 +101,7 @@
 /** Base socket handler for all Content TCP sockets */
 class NetworkContentSocketHandler : public NetworkTCPSocketHandler {
 protected:
-	struct sockaddr_in client_addr; ///< The address we're connected to.
+	struct NetworkAddress client_addr; ///< The address we're connected to.
 	virtual void Close();
 
 	/**
@@ -187,12 +187,12 @@
 	/**
 	 * Create a new cs socket handler for a given cs
 	 * @param s  the socket we are connected with
-	 * @param sin IP etc. of the client
+	 * @param address IP etc. of the client
 	 */
-	NetworkContentSocketHandler(SOCKET s, const struct sockaddr_in *sin) :
-		NetworkTCPSocketHandler(s)
+	NetworkContentSocketHandler(SOCKET s = INVALID_SOCKET, const NetworkAddress &address = NetworkAddress()) :
+		NetworkTCPSocketHandler(s),
+		client_addr(address)
 	{
-		if (sin != NULL) this->client_addr = *sin;
 	}
 
 	/** On destructing of this class, the socket needs to be closed */
--- a/src/network/network_content.cpp
+++ b/src/network/network_content.cpp
@@ -426,7 +426,7 @@
  * @param sin the IP/port of the server
  */
 ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
-	NetworkContentSocketHandler(INVALID_SOCKET, NULL),
+	NetworkContentSocketHandler(),
 	curFile(NULL),
 	curInfo(NULL),
 	isConnecting(false)