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. */
|
2008-08-14 17:19:13 +00:00
|
|
|
#include <base/system.h>
|
2007-07-13 13:40:04 +00:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
struct CPacket
|
2007-07-13 13:40:04 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
CPacket *m_pPrev;
|
|
|
|
CPacket *m_pNext;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
NETADDR m_SendTo;
|
|
|
|
int64 m_Timestamp;
|
2011-02-12 10:40:36 +00:00
|
|
|
int m_ID;
|
2010-05-29 07:25:38 +00:00
|
|
|
int m_DataSize;
|
|
|
|
char m_aData[1];
|
2007-07-13 13:40:04 +00:00
|
|
|
};
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
static CPacket *m_pFirst = (CPacket *)0;
|
|
|
|
static CPacket *m_pLast = (CPacket *)0;
|
|
|
|
static int m_CurrentLatency = 0;
|
2008-10-20 19:59:33 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
struct CPingConfig
|
2009-01-21 19:12:09 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
int m_Base;
|
|
|
|
int m_Flux;
|
|
|
|
int m_Spike;
|
|
|
|
int m_Loss;
|
|
|
|
int m_Delay;
|
|
|
|
int m_DelayFreq;
|
2009-01-21 19:12:09 +00:00
|
|
|
};
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
static CPingConfig m_aConfigPings[] = {
|
2009-01-24 14:32:12 +00:00
|
|
|
// base flux spike loss delay delayfreq
|
|
|
|
{0, 0, 0, 0, 0, 0},
|
|
|
|
{40, 20, 100, 0, 0, 0},
|
|
|
|
{140, 40, 200, 0, 0, 0},
|
2009-01-21 19:12:09 +00:00
|
|
|
};
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
static int m_ConfigNumpingconfs = sizeof(m_aConfigPings)/sizeof(CPingConfig);
|
|
|
|
static int m_ConfigInterval = 10; // seconds between different pingconfigs
|
|
|
|
static int m_ConfigLog = 0;
|
|
|
|
static int m_ConfigReorder = 0;
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2014-12-31 12:04:31 +00:00
|
|
|
void Run(unsigned short Port, NETADDR Dest)
|
2007-07-13 13:40:04 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
NETADDR Src = {NETTYPE_IPV4, {0,0,0,0}, Port};
|
|
|
|
NETSOCKET Socket = net_udp_create(Src);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
char aBuffer[1024*2];
|
2011-02-12 10:40:36 +00:00
|
|
|
int ID = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
int Delaycounter = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
while(1)
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
static int Lastcfg = 0;
|
|
|
|
int n = ((time_get()/time_freq())/m_ConfigInterval) % m_ConfigNumpingconfs;
|
|
|
|
CPingConfig Ping = m_aConfigPings[n];
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(n != Lastcfg)
|
2009-01-21 19:12:09 +00:00
|
|
|
dbg_msg("crapnet", "cfg = %d", n);
|
2010-05-29 07:25:38 +00:00
|
|
|
Lastcfg = n;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2018-07-25 14:24:12 +00:00
|
|
|
MMSGS m;
|
|
|
|
net_init_mmsgs(&m);
|
|
|
|
|
2018-02-04 15:00:47 +00:00
|
|
|
// handle incoming packets
|
2007-07-13 13:40:04 +00:00
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
// fetch data
|
2010-05-29 07:25:38 +00:00
|
|
|
int DataTrash = 0;
|
|
|
|
NETADDR From;
|
2018-07-25 14:24:12 +00:00
|
|
|
int Bytes = net_udp_recv(Socket, &From, aBuffer, 1024*2, &m);
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Bytes <= 0)
|
2007-07-13 13:40:04 +00:00
|
|
|
break;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if((rand()%100) < Ping.m_Loss) // drop the packet
|
2008-10-20 19:59:33 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_ConfigLog)
|
2008-11-08 14:04:16 +00:00
|
|
|
dbg_msg("crapnet", "dropped packet");
|
2008-10-19 09:10:55 +00:00
|
|
|
continue;
|
2008-10-20 19:59:33 +00:00
|
|
|
}
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
// create new packet
|
2018-04-09 09:56:39 +00:00
|
|
|
CPacket *p = (CPacket *)malloc(sizeof(CPacket) + Bytes);
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(net_addr_comp(&From, &Dest) == 0)
|
|
|
|
p->m_SendTo = Src; // from the server
|
2007-07-13 13:40:04 +00:00
|
|
|
else
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
Src = From; // from the client
|
|
|
|
p->m_SendTo = Dest;
|
2007-07-13 13:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// queue packet
|
2010-05-29 07:25:38 +00:00
|
|
|
p->m_pPrev = m_pLast;
|
|
|
|
p->m_pNext = 0;
|
|
|
|
if(m_pLast)
|
|
|
|
m_pLast->m_pNext = p;
|
2007-07-13 13:40:04 +00:00
|
|
|
else
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pFirst = p;
|
|
|
|
m_pLast = p;
|
2007-07-13 13:40:04 +00:00
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pLast = p;
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2009-01-24 14:32:12 +00:00
|
|
|
// set data in packet
|
2010-05-29 07:25:38 +00:00
|
|
|
p->m_Timestamp = time_get();
|
|
|
|
p->m_DataSize = Bytes;
|
2011-02-12 10:40:36 +00:00
|
|
|
p->m_ID = ID++;
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_copy(p->m_aData, aBuffer, Bytes);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-02-12 10:40:36 +00:00
|
|
|
if(ID > 20 && Bytes > 6 && DataTrash)
|
2008-03-18 01:30:47 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
p->m_aData[6+(rand()%(Bytes-6))] = rand()&255; // modify a byte
|
2008-03-18 01:30:47 +00:00
|
|
|
if((rand()%10) == 0)
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
p->m_DataSize -= rand()%32;
|
|
|
|
if(p->m_DataSize < 6)
|
|
|
|
p->m_DataSize = 6;
|
2008-03-18 01:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Delaycounter <= 0)
|
2009-01-24 14:32:12 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Ping.m_Delay)
|
|
|
|
p->m_Timestamp += (time_freq()*1000)/Ping.m_Delay;
|
|
|
|
Delaycounter = Ping.m_DelayFreq;
|
2009-01-24 14:32:12 +00:00
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
Delaycounter--;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_ConfigLog)
|
2011-03-30 10:08:33 +00:00
|
|
|
{
|
|
|
|
char aAddrStr[NETADDR_MAXSTRSIZE];
|
2011-12-29 22:36:53 +00:00
|
|
|
net_addr_str(&From, aAddrStr, sizeof(aAddrStr), true);
|
2011-03-30 10:08:33 +00:00
|
|
|
dbg_msg("crapnet", "<< %08d %s (%d)", p->m_ID, aAddrStr, p->m_DataSize);
|
|
|
|
}
|
2007-07-13 13:40:04 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
//
|
2009-01-24 14:32:12 +00:00
|
|
|
/*while(1)
|
|
|
|
{*/
|
2010-05-29 07:25:38 +00:00
|
|
|
CPacket *p = 0;
|
|
|
|
CPacket *pNext = m_pFirst;
|
2007-07-13 13:40:04 +00:00
|
|
|
while(1)
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
p = pNext;
|
2009-01-24 14:32:12 +00:00
|
|
|
if(!p)
|
|
|
|
break;
|
2010-05-29 07:25:38 +00:00
|
|
|
pNext = p->m_pNext;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if((time_get()-p->m_Timestamp) > m_CurrentLatency)
|
2007-07-13 13:40:04 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
char aFlags[] = " ";
|
2008-10-20 19:59:33 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_ConfigReorder && (rand()%2) == 0 && p->m_pNext)
|
2007-07-13 13:40:04 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
aFlags[0] = 'R';
|
|
|
|
p = m_pFirst->m_pNext;
|
2007-07-13 13:40:04 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(p->m_pNext)
|
|
|
|
p->m_pNext->m_pPrev = p->m_pPrev;
|
2008-10-20 19:59:33 +00:00
|
|
|
else
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pLast = p->m_pPrev;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(p->m_pPrev)
|
|
|
|
p->m_pPrev->m_pNext = p->m_pNext;
|
2008-10-20 19:59:33 +00:00
|
|
|
else
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pFirst = p->m_pNext;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
/*CPacket *cur = first;
|
2008-10-20 19:59:33 +00:00
|
|
|
while(cur)
|
|
|
|
{
|
|
|
|
dbg_assert(cur != p, "p still in list");
|
|
|
|
cur = cur->next;
|
2009-01-24 14:32:12 +00:00
|
|
|
}*/
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
// send and remove packet
|
2007-10-28 19:28:09 +00:00
|
|
|
//if((rand()%20) != 0) // heavy packetloss
|
2010-05-29 07:25:38 +00:00
|
|
|
net_udp_send(Socket, &p->m_SendTo, p->m_aData, p->m_DataSize);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
// update lag
|
2010-05-29 07:25:38 +00:00
|
|
|
double Flux = rand()/(double)RAND_MAX;
|
|
|
|
int MsSpike = Ping.m_Spike;
|
|
|
|
int MsFlux = Ping.m_Flux;
|
|
|
|
int MsPing = Ping.m_Base;
|
|
|
|
m_CurrentLatency = ((time_freq()*MsPing)/1000) + (int64)(((time_freq()*MsFlux)/1000)*Flux); // 50ms
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-02-12 10:40:36 +00:00
|
|
|
if(MsSpike && (p->m_ID%100) == 0)
|
2008-10-20 19:59:33 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_CurrentLatency += (time_freq()*MsSpike)/1000;
|
|
|
|
aFlags[1] = 'S';
|
2008-10-20 19:59:33 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_ConfigLog)
|
2008-10-20 19:59:33 +00:00
|
|
|
{
|
2011-03-30 10:08:33 +00:00
|
|
|
char aAddrStr[NETADDR_MAXSTRSIZE];
|
2011-12-29 22:36:53 +00:00
|
|
|
net_addr_str(&p->m_SendTo, aAddrStr, sizeof(aAddrStr), true);
|
2011-03-30 10:08:33 +00:00
|
|
|
dbg_msg("crapnet", ">> %08d %s (%d) %s", p->m_ID, aAddrStr, p->m_DataSize, aFlags);
|
2008-10-20 19:59:33 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2018-04-09 09:56:39 +00:00
|
|
|
free(p);
|
2007-07-13 13:40:04 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
thread_sleep(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int main(int argc, char **argv) // ignore_convention
|
2007-07-13 13:40:04 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
NETADDR Addr = {NETTYPE_IPV4, {127,0,0,1},8303};
|
2008-10-20 19:59:33 +00:00
|
|
|
dbg_logger_stdout();
|
2010-05-29 07:25:38 +00:00
|
|
|
Run(8302, Addr);
|
2007-07-13 13:40:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|