ddnet/src/versionsrv/versionsrv.cpp

140 lines
3.2 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-08-15 13:34:55 +00:00
#include <base/system.h>
#include <engine/shared/network.h>
#include <game/version.h>
2010-08-15 13:34:55 +00:00
#include "versionsrv.h"
2013-02-24 17:55:55 +00:00
#include "mapversions.h"
2010-08-15 13:34:55 +00:00
enum {
MAX_MAPS_PER_PACKET=48,
MAX_PACKETS=16,
MAX_MAPS=MAX_MAPS_PER_PACKET*MAX_PACKETS,
};
struct CPacketData
{
int m_Size;
struct {
unsigned char m_aHeader[sizeof(VERSIONSRV_MAPLIST)];
CMapVersion m_aMaplist[MAX_MAPS_PER_PACKET];
} m_Data;
};
CPacketData m_aPackets[MAX_PACKETS];
static int m_NumPackets = 0;
2010-08-15 13:34:55 +00:00
static CNetClient g_NetOp; // main
void BuildPackets()
{
CMapVersion *pCurrent = &s_aMapVersionList[0];
int ServersLeft = s_NumMapVersionItems;
m_NumPackets = 0;
while(ServersLeft && m_NumPackets < MAX_PACKETS)
{
int Chunk = ServersLeft;
if(Chunk > MAX_MAPS_PER_PACKET)
Chunk = MAX_MAPS_PER_PACKET;
ServersLeft -= Chunk;
// copy header
mem_copy(m_aPackets[m_NumPackets].m_Data.m_aHeader, VERSIONSRV_MAPLIST, sizeof(VERSIONSRV_MAPLIST));
// copy map versions
for(int i = 0; i < Chunk; i++)
{
m_aPackets[m_NumPackets].m_Data.m_aMaplist[i] = *pCurrent;
pCurrent++;
}
m_aPackets[m_NumPackets].m_Size = sizeof(VERSIONSRV_MAPLIST) + sizeof(CMapVersion)*Chunk;
m_NumPackets++;
}
}
2010-08-15 13:34:55 +00:00
void SendVer(NETADDR *pAddr)
{
CNetChunk p;
unsigned char aData[sizeof(VERSIONSRV_VERSION) + sizeof(GAME_RELEASE_VERSION)];
2010-08-15 13:34:55 +00:00
mem_copy(aData, VERSIONSRV_VERSION, sizeof(VERSIONSRV_VERSION));
mem_copy(aData + sizeof(VERSIONSRV_VERSION), GAME_RELEASE_VERSION, sizeof(GAME_RELEASE_VERSION));
2010-08-15 13:34:55 +00:00
p.m_ClientID = -1;
p.m_Address = *pAddr;
p.m_Flags = NETSENDFLAG_CONNLESS;
p.m_pData = aData;
p.m_DataSize = sizeof(aData);
g_NetOp.Send(&p);
}
int main(int argc, char **argv) // ignore_convention
{
NETADDR BindAddr;
dbg_logger_stdout();
net_init();
mem_zero(&BindAddr, sizeof(BindAddr));
BindAddr.type = NETTYPE_ALL;
2010-08-15 13:34:55 +00:00
BindAddr.port = VERSIONSRV_PORT;
if(!g_NetOp.Open(BindAddr, 0))
{
dbg_msg("mastersrv", "couldn't start network");
return -1;
}
BuildPackets();
2010-08-15 13:34:55 +00:00
dbg_msg("versionsrv", "started");
2010-08-15 13:34:55 +00:00
while(1)
{
g_NetOp.Update();
2010-08-15 13:34:55 +00:00
// process packets
CNetChunk Packet;
while(g_NetOp.Recv(&Packet))
{
if(Packet.m_DataSize == sizeof(VERSIONSRV_GETVERSION) &&
mem_comp(Packet.m_pData, VERSIONSRV_GETVERSION, sizeof(VERSIONSRV_GETVERSION)) == 0)
{
SendVer(&Packet.m_Address);
2014-01-27 04:33:36 +00:00
char aAddrStr[NETADDR_MAXSTRSIZE];
net_addr_str(&Packet.m_Address, aAddrStr, sizeof(aAddrStr), false);
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "version request by %s", aAddrStr);
dbg_msg("versionsrv", aBuf);
2010-08-15 13:34:55 +00:00
}
if(Packet.m_DataSize == sizeof(VERSIONSRV_GETMAPLIST) &&
mem_comp(Packet.m_pData, VERSIONSRV_GETMAPLIST, sizeof(VERSIONSRV_GETMAPLIST)) == 0)
{
CNetChunk p;
p.m_ClientID = -1;
p.m_Address = Packet.m_Address;
p.m_Flags = NETSENDFLAG_CONNLESS;
for(int i = 0; i < m_NumPackets; i++)
{
p.m_DataSize = m_aPackets[i].m_Size;
p.m_pData = &m_aPackets[i].m_Data;
g_NetOp.Send(&p);
}
}
2010-08-15 13:34:55 +00:00
}
2014-01-14 23:02:19 +00:00
// wait for input
net_socket_read_wait(g_NetOp.m_Socket, 1000);
2010-08-15 13:34:55 +00:00
}
2010-08-15 13:34:55 +00:00
return 0;
}