hlswmaster-ng/hlswserver.cpp

126 lines
2.7 KiB
C++
Raw Normal View History

2006-02-02 16:55:44 +01:00
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include "config.h"
#include "logging.h"
#include "hlswserver.h"
2006-02-06 20:58:32 +01:00
static const char hlsw_header[] = "\xFF\xFF\xFF\xFFHLSWLANSEARCH";
2006-02-20 21:58:59 +01:00
struct hlsw_entry {
uint16_t gameid;
uint32_t ip;
uint16_t port1;
uint16_t port2;
} __attribute__ ((packed));
2006-03-06 20:13:26 +01:00
#define HLSW_MASTER_PORT 7140
#define HLSW_MAX_PACKET_SIZE (sizeof(hlsw_header) + 140 * sizeof(struct hlsw_entry))
#define DEFAULT_REBUILD_INTERVAL 5
2006-02-06 20:58:32 +01:00
HlswServer::HlswPacket::HlswPacket(HlswPacket* next)
2006-03-06 20:13:26 +01:00
: NetPkt(HLSW_MAX_PACKET_SIZE), next(next)
2006-02-06 20:58:32 +01:00
{
2006-03-05 02:28:19 +01:00
memcpy((void*)data, hlsw_header, sizeof(hlsw_header));
2006-03-06 20:13:26 +01:00
size = sizeof(hlsw_header);
2006-02-06 20:58:32 +01:00
}
HlswServer::HlswPacket::~HlswPacket()
{
if (next)
delete next;
}
2006-02-19 18:45:56 +01:00
bool HlswServer::HlswPacket::addGame(GameEntry* ge)
2006-02-06 20:58:32 +01:00
{
2006-03-06 20:13:26 +01:00
if (size >= HLSW_MAX_PACKET_SIZE)
2006-02-06 20:58:32 +01:00
return false;
2006-03-05 02:28:19 +01:00
2006-02-20 21:58:59 +01:00
struct hlsw_entry tmp;
tmp.gameid = ge->gameid;
tmp.ip = ge->addr.s_addr;
tmp.port1 = ge->port1;
tmp.port2 = ge->port2;
2006-03-06 20:13:26 +01:00
memcpy((void*)(data + size), &tmp, sizeof(tmp));
size += sizeof(tmp);
2006-02-20 21:58:59 +01:00
return true;
2006-02-06 20:58:32 +01:00
}
2006-03-05 02:28:19 +01:00
void HlswServer::HlswPacket::send(Socket* socket, struct sockaddr_in* dst)
2006-02-06 20:58:32 +01:00
{
if (next)
next->send(socket, dst);
2006-03-05 02:28:19 +01:00
socket->sendto(this, dst);
2006-02-06 20:58:32 +01:00
}
2006-02-02 16:55:44 +01:00
2006-02-20 21:58:59 +01:00
HlswServer::HlswServer(Config& conf, GameList& glist)
: lastUpdate(0)
2006-02-02 16:55:44 +01:00
{
2006-02-20 21:58:59 +01:00
const char *ip = conf.getString("global", "master_ip", "0.0.0.0");
2006-02-02 16:55:44 +01:00
LogSystem::log(LOG_NOTICE, "HlswServer listen on %s:%d", ip, HLSW_MASTER_PORT);
2006-02-20 21:58:59 +01:00
struct sockaddr_in dst;
2006-02-02 16:55:44 +01:00
dst.sin_family = AF_INET;
dst.sin_port = htons(HLSW_MASTER_PORT);
inet_aton(ip, &dst.sin_addr);
2006-03-05 02:28:19 +01:00
socket = Socket::createSocket(&dst);
2006-02-02 16:55:44 +01:00
2006-02-20 21:58:59 +01:00
int interval = conf.getInteger("global", "rebuild_interval", DEFAULT_REBUILD_INTERVAL);
TimerService::registerTimer(new Timer(new RebuildEvent(*this, glist), interval));
2006-03-05 02:28:19 +01:00
2006-02-06 20:58:32 +01:00
pktlist = new HlswPacket(NULL);
2006-02-02 16:55:44 +01:00
}
HlswServer::~HlswServer()
{
2006-02-06 20:58:32 +01:00
delete pktlist;
2006-03-05 02:28:19 +01:00
delete socket;
2006-02-02 16:55:44 +01:00
}
int HlswServer::execute(void* arg)
{
while (1) {
2006-03-05 02:28:19 +01:00
NetPkt* pkt = socket->recv();
if (!pkt->compare(0, hlsw_header, sizeof(hlsw_header))) {
2006-02-05 12:00:47 +01:00
LogSystem::log(LOG_NOTICE, "HlswServer: not a hlsw packet");
2006-02-02 16:55:44 +01:00
continue;
}
2006-02-06 20:58:32 +01:00
mutex.lock();
2006-03-05 02:28:19 +01:00
pktlist->send(socket, pkt->getAddress());
2006-02-06 20:58:32 +01:00
mutex.unlock();
2006-03-05 02:28:19 +01:00
2006-02-02 16:55:44 +01:00
}
return 0;
}
2006-02-06 20:58:32 +01:00
2006-02-19 18:45:56 +01:00
void HlswServer::rebuild(GameList& glist) {
2006-02-20 21:58:59 +01:00
if (glist.getLastUpdate() > lastUpdate) {
HlswPacket* newlist = new HlswPacket(NULL);
2006-03-05 02:28:19 +01:00
2006-02-20 21:58:59 +01:00
Iterator<GameEntry>* it = glist.createIterator();
while (it->hasNext()) {
GameEntry* d = it->next();
if (!newlist->addGame(d)) {
newlist = new HlswPacket(newlist);
newlist->addGame(d);
}
2006-02-06 20:58:32 +01:00
}
2006-02-20 21:58:59 +01:00
lastUpdate = glist.getLastUpdate();
delete it;
2006-03-05 02:28:19 +01:00
2006-02-20 21:58:59 +01:00
mutex.lock();
HlswPacket* oldlist = pktlist;
pktlist = newlist;
mutex.unlock();
2006-03-05 02:28:19 +01:00
2006-02-20 21:58:59 +01:00
delete oldlist;
}
2006-02-19 18:45:56 +01:00
}