hlswmaster-ng/gameentry.cpp

69 lines
1.3 KiB
C++

#include "gameentry.h"
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include "gameentry.h"
GameEntry::GameEntry(int gameid, NetPkt* pkt, int port2)
: port2(port2), gameid(gameid)
{
memcpy(&addr, &pkt->getAddress()->sin_addr, sizeof(addr));
this->port1 = pkt->getPort();
if (port2 != 0) {
this->port2 = this->port1;
this->port1 = port2;
}
update();
}
int GameEntry::compare(const GameEntry* ge)
{
if (this->addr.s_addr > ge->addr.s_addr)
return -1;
if (this->addr.s_addr < ge->addr.s_addr)
return 1;
if (this->port1 > ge->port1)
return -1;
if (this->port1 < ge->port1)
return 1;
// only compare IP:port
// gameid and port2 are useless
return 0;
}
int GameEntry::hash(int max)
{
unsigned int hash = 0x12345678;
// IP
hash = ((hash<<5) ^ (hash>>27)) ^ ((addr.s_addr>>0) & 0xFF);
hash = ((hash<<5) ^ (hash>>27)) ^ ((addr.s_addr>>8) & 0xFF);
hash = ((hash<<5) ^ (hash>>27)) ^ ((addr.s_addr>>16) & 0xFF);
hash = ((hash<<5) ^ (hash>>27)) ^ ((addr.s_addr>>24) & 0xFF);
// port
hash = ((hash<<5) ^ (hash>>27)) ^ ((port1>>0) & 0xFF);
hash = ((hash<<5) ^ (hash>>27)) ^ ((port1>>8) & 0xFF);
return hash % max;
}
void GameEntry::update()
{
modtime = time(NULL);
}
int GameEntry::show(char* buf, int size)
{
return snprintf(buf, size, "(%2d:%15s:%5d:%5d)",
this->gameid, inet_ntoa(this->addr),
this->port1, this->port2);
}