(svn r15916) -Codechange: let the network game list use NetworkAddress

This commit is contained in:
rubidium
2009-04-02 20:17:46 +00:00
parent bdf3611d02
commit 785779ca62
7 changed files with 61 additions and 33 deletions

View File

@@ -14,7 +14,9 @@
const char *NetworkAddress::GetHostname()
{
if (this->hostname == NULL) {
this->hostname = strdup(inet_ntoa(((struct sockaddr_in *)&this->address)->sin_addr));
char buf[NETWORK_HOSTNAME_LENGTH] = { '\0' };
getnameinfo((struct sockaddr *)&this->address, sizeof(this->address), buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
this->hostname = strdup(buf);
}
return this->hostname;
}
@@ -41,6 +43,18 @@ uint16 NetworkAddress::GetPort() const
}
}
void NetworkAddress::SetPort(uint16 port)
{
switch (this->address.ss_family) {
case AF_INET:
((struct sockaddr_in*)&this->address)->sin_port = htons(port);
break;
default:
NOT_REACHED();
}
}
const char *NetworkAddress::GetAddressAsString()
{
/* 6 = for the : and 5 for the decimal port number */

View File

@@ -33,7 +33,7 @@ public:
memset(&this->address, 0, sizeof(this->address));
this->address.ss_family = AF_INET;
((struct sockaddr_in*)&this->address)->sin_addr.s_addr = ip;
((struct sockaddr_in*)&this->address)->sin_port = htons(port);
this->SetPort(port);
}
/**
@@ -58,7 +58,7 @@ public:
{
memset(&this->address, 0, sizeof(this->address));
this->address.ss_family = AF_INET;
((struct sockaddr_in*)&this->address)->sin_port = htons(port);
this->SetPort(port);
}
/**
@@ -110,6 +110,12 @@ public:
*/
uint16 GetPort() const;
/**
* Set the port
* @param port set the port number
*/
void SetPort(uint16 port);
/**
* Check whether the IP address has been resolved already
* @return true iff the port has been resolved
@@ -118,6 +124,19 @@ public:
{
return this->resolved;
}
/**
* Compare the address of this class with the address of another.
* @param address the other address.
*/
bool operator == (NetworkAddress &address)
{
if (this->IsResolved() != address.IsResolved()) return false;
if (this->IsResolved()) return memcmp(&this->address, &address.address, sizeof(this->address)) == 0;
return this->GetPort() == address.GetPort() && strcmp(this->GetHostname(), address.GetHostname()) == 0;
}
};
#endif /* ENABLE_NETWORK */