126 lines
2.1 KiB
C++
126 lines
2.1 KiB
C++
#include "logging.h"
|
|
#include "gamelist.h"
|
|
|
|
#define DEFAULT_TIMEOUT 180
|
|
|
|
GameList::GameList(Config& conf)
|
|
{
|
|
timeout = conf.getInteger("global", "game_timeout", DEFAULT_TIMEOUT);
|
|
TimerService::registerTimer(new Timer(new CleanupEvent(*this), timeout));
|
|
}
|
|
|
|
GameList::~GameList()
|
|
{
|
|
Iterator<GameEntry>* it = createIterator();
|
|
while (it->hasNext())
|
|
delete it->next();
|
|
|
|
delete it;
|
|
}
|
|
|
|
void GameList::cleanup()
|
|
{
|
|
long now = time(NULL);
|
|
|
|
Iterator<GameEntry>* it = createIterator();
|
|
while (it->hasNext()) {
|
|
GameEntry* ge = it->next();
|
|
|
|
if (ge->modtime + timeout <= now) {
|
|
it->remove();
|
|
|
|
char buf[64];
|
|
ge->show(buf, sizeof(buf));
|
|
LogSystem::log(LOG_NOTICE, "Game Timeout: %s", buf);
|
|
delete ge;
|
|
}
|
|
}
|
|
delete it;
|
|
}
|
|
|
|
void GameList::addGame(int gameid, NetPkt* pkt, int port2)
|
|
{
|
|
GameEntry* ge = new GameEntry(gameid, pkt, port2);
|
|
int hash = ge->hash(MAX_BUCKETS);
|
|
bool found = false;
|
|
|
|
Iterator<GameEntry>* it = buckets[hash].createIterator();
|
|
while (it->hasNext()) {
|
|
GameEntry* tmp = it->next();
|
|
if (ge->compare(tmp) == 0) {
|
|
tmp->update();
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!found) {
|
|
char buf[64];
|
|
ge->show(buf, sizeof(buf));
|
|
LogSystem::log(LOG_NOTICE, "Adding Game : %s", buf);
|
|
|
|
buckets[hash].add(ge);
|
|
|
|
} else {
|
|
delete ge;
|
|
}
|
|
|
|
// it holds the lock!
|
|
delete it;
|
|
}
|
|
|
|
Iterator<GameEntry>* GameList::createIterator()
|
|
{
|
|
return new GameListIterator(this);
|
|
}
|
|
|
|
|
|
GameList::GameListIterator::GameListIterator(GameList* gl)
|
|
: gl(gl), it(0)
|
|
{
|
|
gl->mutex.lock();
|
|
reset();
|
|
}
|
|
|
|
GameList::GameListIterator::~GameListIterator()
|
|
{
|
|
delete it;
|
|
gl->mutex.unlock();
|
|
}
|
|
|
|
bool GameList::GameListIterator::hasNext()
|
|
{
|
|
if (it->hasNext())
|
|
return true;
|
|
|
|
while (bucket < MAX_BUCKETS -1) {
|
|
delete it;
|
|
bucket++;
|
|
|
|
it = gl->buckets[bucket].createIterator();
|
|
if (it->hasNext())
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
GameEntry* GameList::GameListIterator::next()
|
|
{
|
|
return it->next();
|
|
}
|
|
|
|
void GameList::GameListIterator::remove()
|
|
{
|
|
it->remove();
|
|
}
|
|
|
|
void GameList::GameListIterator::reset()
|
|
{
|
|
if (it != NULL)
|
|
delete it;
|
|
|
|
bucket = 0;
|
|
it = gl->buckets[bucket].createIterator();
|
|
}
|