improved crapnet to beable to do packet reodering /home/kma/code/teeworlds/trunk/src/tools/crapnet.cpp

This commit is contained in:
Magnus Auvinen 2008-10-20 19:59:33 +00:00
parent d9e34b6cfc
commit 9cbfe9c462
2 changed files with 50 additions and 19 deletions

View file

@ -213,8 +213,10 @@ void inp_mouse_mode_absolute()
void inp_mouse_mode_relative()
{
/*
SDL_ShowCursor(0);
SDL_WM_GrabInput(SDL_GRAB_ON);
*/
}
#endif

View file

@ -18,7 +18,12 @@ struct PACKET
static PACKET *first = (PACKET *)0;
static PACKET *last = (PACKET *)0;
static int current_latency = 0;
static int debug = 0;
static int debug = 1;
static int config_ping = 100;
static int config_pingflux = 100;
static int config_pingspike = 0;
static int config_packetloss = 0; // in percent
int run(int port, NETADDR dest)
{
@ -40,16 +45,17 @@ int run(int port, NETADDR dest)
if(bytes <= 0)
break;
if((rand()%2) == 0) // drop the packet
if((rand()%100) < config_packetloss) // drop the packet
{
dbg_msg("crapnet", "dropped packet");
continue;
}
// create new packet
PACKET *p = (PACKET *)mem_alloc(sizeof(PACKET)+bytes, 1);
if(net_addr_comp(&from, &dest) == 0)
{
p->send_to = src; // from the server
}
else
{
src = from; // from the client
@ -92,37 +98,59 @@ int run(int port, NETADDR dest)
//
while(1)
{
//dbg_msg("crapnet", "%p", first);
if(first && (time_get()-first->timestamp) > current_latency)
{
PACKET *p = first;
first = first->next;
if(first)
first->prev = 0;
else
last = 0;
if(debug)
char flags[] = " ";
if((rand()%2) == 0 && first->next)
{
dbg_msg("crapnet", ">> %08d %d.%d.%d.%d:%5d (%d)", p->id,
p->send_to.ip[0], p->send_to.ip[1],
p->send_to.ip[2], p->send_to.ip[3],
p->send_to.port, p->data_size);
flags[0] = 'R';
p = first->next;
}
if(p->next)
p->next->prev = 0;
else
last = p->prev;
if(p->prev)
p->prev->next = p->next;
else
first = p->next;
PACKET *cur = first;
while(cur)
{
dbg_assert(cur != p, "p still in list");
cur = cur->next;
}
// send and remove packet
//if((rand()%20) != 0) // heavy packetloss
net_udp_send(socket, &p->send_to, p->data, p->data_size);
// update lag
double flux = rand()/(double)RAND_MAX;
int ms_spike = 0;
int ms_flux = 100;
int ms_ping = 100;
int ms_spike = config_pingspike;
int ms_flux = config_pingflux;
int ms_ping = config_ping;
current_latency = ((time_freq()*ms_ping)/1000) + (int64)(((time_freq()*ms_flux)/1000)*flux); // 50ms
if(ms_spike && (p->id%100) == 0)
{
current_latency += (time_freq()*ms_spike)/1000;
flags[1] = 'S';
}
if(debug)
{
dbg_msg("crapnet", ">> %08d %d.%d.%d.%d:%5d (%d) %s", p->id,
p->send_to.ip[0], p->send_to.ip[1],
p->send_to.ip[2], p->send_to.ip[3],
p->send_to.port, p->data_size, flags);
}
mem_free(p);
}
@ -137,6 +165,7 @@ int run(int port, NETADDR dest)
int main(int argc, char **argv)
{
NETADDR a = {NETTYPE_IPV4, {127,0,0,1},8303};
dbg_logger_stdout();
run(8302, a);
return 0;
}