ddnet/src/twping/twping.cpp

75 lines
1.7 KiB
C++
Raw Normal View History

2014-11-24 20:16:03 +00:00
#include <base/math.h>
#include <base/system.h>
2022-02-15 00:07:19 +00:00
#include <cstdio>
2014-11-24 20:16:03 +00:00
#include <engine/shared/network.h>
#include <engine/shared/packer.h>
#include <mastersrv/mastersrv.h>
2014-11-24 20:16:03 +00:00
static CNetClient g_NetOp; // main
int main(int argc, const char **argv)
2014-11-24 20:16:03 +00:00
{
cmdline_fix(&argc, &argv);
2014-11-24 20:16:03 +00:00
NETADDR BindAddr;
2014-11-24 20:21:16 +00:00
mem_zero(&BindAddr, sizeof(BindAddr));
2014-11-24 20:16:03 +00:00
BindAddr.type = NETTYPE_ALL;
g_NetOp.Open(BindAddr, 0);
if(argc != 2)
{
2014-11-24 21:37:13 +00:00
fprintf(stderr, "usage: %s server[:port] (default port: 8303)\n", argv[0]);
2014-11-24 20:16:03 +00:00
return 1;
}
NETADDR Addr;
if(net_host_lookup(argv[1], &Addr, NETTYPE_ALL))
2014-11-24 20:16:03 +00:00
{
2014-11-24 21:32:24 +00:00
fprintf(stderr, "host lookup failed\n");
2014-11-24 20:16:03 +00:00
return 1;
}
2014-11-24 21:37:13 +00:00
if(Addr.port == 0)
Addr.port = 8303;
2014-11-24 20:16:03 +00:00
unsigned char Buffer[sizeof(SERVERBROWSE_GETINFO) + 1];
2014-11-24 20:16:03 +00:00
CNetChunk Packet;
mem_copy(Buffer, SERVERBROWSE_GETINFO, sizeof(SERVERBROWSE_GETINFO));
int CurToken = rand() % 256;
Buffer[sizeof(SERVERBROWSE_GETINFO)] = CurToken;
Packet.m_ClientID = -1;
Packet.m_Address = Addr;
Packet.m_Flags = NETSENDFLAG_CONNLESS;
Packet.m_DataSize = sizeof(Buffer);
Packet.m_pData = Buffer;
g_NetOp.Send(&Packet);
2021-06-23 05:05:49 +00:00
int64_t startTime = time_get();
2014-11-24 20:16:03 +00:00
net_socket_read_wait(g_NetOp.m_Socket, 1000000);
g_NetOp.Update();
while(g_NetOp.Recv(&Packet))
{
if(Packet.m_DataSize >= (int)sizeof(SERVERBROWSE_INFO) && mem_comp(Packet.m_pData, SERVERBROWSE_INFO, sizeof(SERVERBROWSE_INFO)) == 0)
{
// we got ze info
CUnpacker Up;
Up.Reset((unsigned char *)Packet.m_pData + sizeof(SERVERBROWSE_INFO), Packet.m_DataSize - sizeof(SERVERBROWSE_INFO));
2014-11-24 20:16:03 +00:00
int Token = str_toint(Up.GetString());
if(Token != CurToken)
continue;
2021-06-23 05:05:49 +00:00
int64_t endTime = time_get();
2020-06-17 17:56:29 +00:00
printf("%g ms\n", (double)(endTime - startTime) / time_freq() * 1000);
2014-11-24 20:16:03 +00:00
}
}
cmdline_free(argc, argv);
return 0;
2014-11-24 20:16:03 +00:00
}