(svn r15914) -Codechange: let the content handling make use of NetworkAddress.

This commit is contained in:
rubidium 2009-04-02 18:35:59 +00:00
parent b2e7941783
commit 804370d964
5 changed files with 29 additions and 12 deletions

View File

@ -7,7 +7,9 @@
#ifdef ENABLE_NETWORK #ifdef ENABLE_NETWORK
#include "address.h" #include "address.h"
#include "config.h"
#include "host.h" #include "host.h"
#include "../../string_func.h"
const char *NetworkAddress::GetHostname() const const char *NetworkAddress::GetHostname() const
{ {
@ -27,4 +29,13 @@ uint32 NetworkAddress::GetIP()
return this->ip; 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 */ #endif /* ENABLE_NETWORK */

View File

@ -40,9 +40,9 @@ public:
* @param ip the unresolved hostname * @param ip the unresolved hostname
* @param port the port * @param port the port
*/ */
NetworkAddress(const char *hostname, uint16 port) : NetworkAddress(const char *hostname = NULL, uint16 port = 0) :
resolved(false), resolved(false),
hostname(strdup(hostname)), hostname(hostname == NULL ? NULL : strdup(hostname)),
ip(0), ip(0),
port(port) port(port)
{ {
@ -73,6 +73,12 @@ public:
*/ */
const char *GetHostname() const; 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 * Get the IP address. If the IP has not been resolved yet this will resolve
* it possibly blocking this function for a while * it possibly blocking this function for a while

View File

@ -84,9 +84,9 @@ bool NetworkContentSocketHandler::HandlePacket(Packet *p)
default: default:
if (this->HasClientQuit()) { 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 { } 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; return false;
} }
@ -114,8 +114,8 @@ void NetworkContentSocketHandler::Recv_Packets()
*/ */
#define DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(type) \ #define DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(type) \
bool NetworkContentSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) { \ bool NetworkContentSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) { \
DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s:%d", \ DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s", \
type, inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port)); \ type, this->client_addr.GetAddressAsString()); \
return false; \ return false; \
} }

View File

@ -101,7 +101,7 @@ struct ContentInfo {
/** Base socket handler for all Content TCP sockets */ /** Base socket handler for all Content TCP sockets */
class NetworkContentSocketHandler : public NetworkTCPSocketHandler { class NetworkContentSocketHandler : public NetworkTCPSocketHandler {
protected: 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(); virtual void Close();
/** /**
@ -187,12 +187,12 @@ public:
/** /**
* Create a new cs socket handler for a given cs * Create a new cs socket handler for a given cs
* @param s the socket we are connected with * @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) : NetworkContentSocketHandler(SOCKET s = INVALID_SOCKET, const NetworkAddress &address = NetworkAddress()) :
NetworkTCPSocketHandler(s) NetworkTCPSocketHandler(s),
client_addr(address)
{ {
if (sin != NULL) this->client_addr = *sin;
} }
/** On destructing of this class, the socket needs to be closed */ /** On destructing of this class, the socket needs to be closed */

View File

@ -426,7 +426,7 @@ DEF_CONTENT_RECEIVE_COMMAND(Client, PACKET_CONTENT_SERVER_CONTENT)
* @param sin the IP/port of the server * @param sin the IP/port of the server
*/ */
ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() : ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
NetworkContentSocketHandler(INVALID_SOCKET, NULL), NetworkContentSocketHandler(),
curFile(NULL), curFile(NULL),
curInfo(NULL), curInfo(NULL),
isConnecting(false) isConnecting(false)