upgraded the master server code so it can handle more servers

This commit is contained in:
Magnus Auvinen 2008-11-15 13:29:56 +00:00
parent 54429b70cf
commit 8acf5ed788
5 changed files with 93 additions and 33 deletions

View file

@ -23,10 +23,10 @@ inick = 0
def get_nick():
global inick, nicks
inick += 1
inick = (inick+1)%len(nicks)
return nicks[inick].replace("`", "\`")
for s in xrange(0, 150):
for s in xrange(0, 350):
cmd = "./fake_server_d_d "
cmd += '-n "%s" ' % (random.choice(servernames) % get_nick())
for m in masterservers:

View file

@ -574,6 +574,7 @@ static int engine_find_datadir(char *argv0)
const char *sdirs[] = {
"/usr/share/teeworlds",
"/usr/local/share/teeworlds"
"/opt/teeworlds"
};
const int sdirs_count = sizeof(sdirs) / sizeof(sdirs[0]);

View file

@ -10,6 +10,9 @@ extern "C" {
enum {
MTU = 1400,
MAX_SERVERS_PER_PACKET=128,
MAX_PACKETS=16,
MAX_SERVERS=MAX_SERVERS_PER_PACKET*MAX_PACKETS,
EXPIRE_TIME = 90
};
@ -22,11 +25,29 @@ static struct CHECK_SERVER
} check_servers[MAX_SERVERS];
static int num_checkservers = 0;
typedef struct NETADDR_IPv4
{
unsigned char ip[4];
unsigned short port;
} NETADDR_IPv4;
static struct SERVER_ENTRY
{
NETADDR address;
int64 expire;
} servers[MAX_SERVERS];
static int num_servers = 0;
static struct PACKET_DATA
{
unsigned char header[sizeof(SERVERBROWSE_LIST)];
NETADDR servers[MAX_SERVERS];
} data;
int size;
struct {
unsigned char header[sizeof(SERVERBROWSE_LIST)];
NETADDR_IPv4 servers[MAX_SERVERS_PER_PACKET];
} data;
} packets[MAX_PACKETS];
static int num_packets = 0;
static struct COUNT_PACKET_DATA
{
@ -35,12 +56,45 @@ static struct COUNT_PACKET_DATA
unsigned char low;
} count_data;
static int64 server_expire[MAX_SERVERS];
static int num_servers = 0;
//static int64 server_expire[MAX_SERVERS];
static net_client net_checker; // NAT/FW checker
static net_client net_op; // main
void build_packets()
{
SERVER_ENTRY *current = &servers[0];
int servers_left = num_servers;
int i;
num_packets = 0;
while(servers_left && num_packets < MAX_PACKETS)
{
int chunk = servers_left;
if(chunk > MAX_SERVERS_PER_PACKET)
chunk = MAX_SERVERS_PER_PACKET;
servers_left -= chunk;
// copy header
mem_copy(packets[num_packets].data.header, SERVERBROWSE_LIST, sizeof(SERVERBROWSE_LIST));
// copy server addresses
for(i = 0; i < chunk; i++)
{
// TODO: ipv6 support
packets[num_packets].data.servers[i].ip[0] = current->address.ip[0];
packets[num_packets].data.servers[i].ip[1] = current->address.ip[1];
packets[num_packets].data.servers[i].ip[2] = current->address.ip[2];
packets[num_packets].data.servers[i].ip[3] = current->address.ip[3];
packets[num_packets].data.servers[i].port = current->address.port;
current++;
}
packets[num_packets].size = sizeof(SERVERBROWSE_LIST) + sizeof(NETADDR_IPv4)*chunk;
num_packets++;
}
}
void send_ok(NETADDR *addr)
{
NETCHUNK p;
@ -102,11 +156,11 @@ void add_server(NETADDR *info)
int i;
for(i = 0; i < num_servers; i++)
{
if(net_addr_comp(&data.servers[i], info) == 0)
if(net_addr_comp(&servers[i].address, info) == 0)
{
dbg_msg("mastersrv", "updated: %d.%d.%d.%d:%d",
info->ip[0], info->ip[1], info->ip[2], info->ip[3], info->port);
server_expire[i] = time_get()+time_freq()*EXPIRE_TIME;
servers[i].expire = time_get()+time_freq()*EXPIRE_TIME;
return;
}
}
@ -120,8 +174,8 @@ void add_server(NETADDR *info)
dbg_msg("mastersrv", "added: %d.%d.%d.%d:%d",
info->ip[0], info->ip[1], info->ip[2], info->ip[3], info->port);
data.servers[num_servers] = *info;
server_expire[num_servers] = time_get()+time_freq()*EXPIRE_TIME;
servers[num_servers].address = *info;
servers[num_servers].expire = time_get()+time_freq()*EXPIRE_TIME;
num_servers++;
}
@ -166,14 +220,13 @@ void purge_servers()
int i = 0;
while(i < num_servers)
{
if(server_expire[i] < now)
if(servers[i].expire < now)
{
// remove server
dbg_msg("mastersrv", "expired: %d.%d.%d.%d:%d",
data.servers[i].ip[0], data.servers[i].ip[1],
data.servers[i].ip[2], data.servers[i].ip[3], data.servers[i].port);
data.servers[i] = data.servers[num_servers-1];
server_expire[i] = server_expire[num_servers-1];
servers[i].address.ip[0], servers[i].address.ip[1],
servers[i].address.ip[2], servers[i].address.ip[3], servers[i].address.port);
servers[i] = servers[num_servers-1];
num_servers--;
}
else
@ -183,6 +236,7 @@ void purge_servers()
int main(int argc, char **argv)
{
int64 last_build = 0;
NETADDR bindaddr;
dbg_logger_stdout();
@ -198,7 +252,7 @@ int main(int argc, char **argv)
net_checker.open(bindaddr, 0);
// TODO: check socket for errors
mem_copy(data.header, SERVERBROWSE_LIST, sizeof(SERVERBROWSE_LIST));
//mem_copy(data.header, SERVERBROWSE_LIST, sizeof(SERVERBROWSE_LIST));
mem_copy(count_data.header, SERVERBROWSE_COUNT, sizeof(SERVERBROWSE_COUNT));
dbg_msg("mastersrv", "started");
@ -249,9 +303,13 @@ int main(int argc, char **argv)
p.client_id = -1;
p.address = packet.address;
p.flags = NETSENDFLAG_CONNLESS;
p.data_size = num_servers*sizeof(NETADDR)+sizeof(SERVERBROWSE_LIST);
p.data = &data;
net_op.send(&p);
for(int i = 0; i < num_packets; i++)
{
p.data_size = packets[i].size;
p.data = &packets[i].data;
net_op.send(&p);
}
}
}
@ -278,9 +336,14 @@ int main(int argc, char **argv)
}
}
// TODO: shouldn't be done every fuckin frame
purge_servers();
update_servers();
if(time_get()-last_build > time_freq()*5)
{
last_build = time_get();
purge_servers();
update_servers();
build_packets();
}
// be nice to the CPU
thread_sleep(1);

View file

@ -1,10 +1,6 @@
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
static const int MASTERSERVER_PORT = 8300;
enum {
MAX_SERVERS = 200
};
typedef struct MASTERSRV_ADDR
{
unsigned char ip[4];

View file

@ -91,8 +91,8 @@ static void writeint(int i)
static void build_infomessage()
{
int i;
infomsg_size = sizeof(SERVERBROWSE_INFO);
memcpy(infomsg, SERVERBROWSE_INFO, infomsg_size);
infomsg_size = sizeof(SERVERBROWSE_OLD_INFO);
memcpy(infomsg, SERVERBROWSE_OLD_INFO, infomsg_size);
writestr(version);
writestr(server_name);
@ -147,8 +147,8 @@ static int run()
{
if(p.client_id == -1)
{
if(p.data_size == sizeof(SERVERBROWSE_GETINFO) &&
memcmp(p.data, SERVERBROWSE_GETINFO, sizeof(SERVERBROWSE_GETINFO)) == 0)
if(p.data_size == sizeof(SERVERBROWSE_OLD_GETINFO) &&
memcmp(p.data, SERVERBROWSE_OLD_GETINFO, sizeof(SERVERBROWSE_OLD_GETINFO)) == 0)
{
send_serverinfo(&p.address);
}
@ -163,11 +163,11 @@ static int run()
/* send heartbeats if needed */
if(next_heartbeat < time_get())
{
next_heartbeat = time_get()+time_freq()*30;
next_heartbeat = time_get()+time_freq()*(15+(rand()%15));
send_heartbeats();
}
thread_sleep(10);
thread_sleep(100);
}
}