simulated packet loss (cl_fakepacketloss_receive, cl_fakepacketloss_send, sv_fakepack...
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 7 Aug 2003 10:44:20 +0000 (10:44 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 7 Aug 2003 10:44:20 +0000 (10:44 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@3371 d7cf8633-e32d-0410-b094-e92efae38249

lhnet.c
netconn.c
netconn.h

diff --git a/lhnet.c b/lhnet.c
index d9597f2..bdb21c5 100644 (file)
--- a/lhnet.c
+++ b/lhnet.c
 
 // for Z_Malloc/Z_Free in quake
 #ifndef STANDALONETEST
+#include "quakedef.h"
 #include "zone.h"
+#include "sys.h"
+#include "netconn.h"
 #else
 #define Z_Malloc malloc
 #define Z_Free free
@@ -296,6 +299,9 @@ typedef struct lhnetpacket_s
        int sourceport;
        int destinationport;
        time_t timeout;
+#ifndef STANDALONETEST
+       double sentdoubletime;
+#endif
        struct lhnetpacket_s *next, *prev;
 }
 lhnetpacket_t;
@@ -481,6 +487,18 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                for (p = lhnet_packetlist.next;p != &lhnet_packetlist;p = pnext)
                {
                        pnext = p->next;
+                       if (p->timeout < currenttime)
+                       {
+                               // unlink and free
+                               p->next->prev = p->prev;
+                               p->prev->next = p->next;
+                               Z_Free(p);
+                               continue;
+                       }
+#ifndef STANDALONETEST
+                       if (p->sentdoubletime && Sys_DoubleTime() < p->sentdoubletime)
+                               continue;
+#endif
                        if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port)
                        {
                                if (p->length <= maxcontentlength)
@@ -497,13 +515,6 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                                p->prev->next = p->next;
                                Z_Free(p);
                        }
-                       else if (p->timeout < currenttime)
-                       {
-                               // unlink and free
-                               p->next->prev = p->prev;
-                               p->prev->next = p->next;
-                               Z_Free(p);
-                       }
                }
        }
        else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
@@ -576,6 +587,10 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng
                p->prev = p->next->prev;
                p->next->prev = p;
                p->prev->next = p;
+#ifndef STANDALONETEST
+               if (cl_fakelocalping_min.integer || cl_fakelocalping_max.integer)
+                       p->sentdoubletime = Sys_DoubleTime() + (cl_fakelocalping_min.integer + ((cl_fakelocalping_max.integer - cl_fakelocalping_min.integer) * (rand() & 255) / 256)) / 1000.0;
+#endif
                value = contentlength;
        }
        else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
index c4bd8ac..a297a05 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -49,6 +49,14 @@ cvar_t net_connecttimeout = {0, "net_connecttimeout","10"};
 cvar_t hostname = {CVAR_SAVE, "hostname", "UNNAMED"};
 cvar_t developer_networking = {0, "developer_networking", "0"};
 
+cvar_t cl_fakelocalping_min = {0, "cl_fakelocalping_min","0"};
+cvar_t cl_fakelocalping_max = {0, "cl_fakelocalping_max","0"};
+static cvar_t cl_fakepacketloss_receive = {0, "cl_fakepacketloss_receive","0"};
+static cvar_t cl_fakepacketloss_send = {0, "cl_fakepacketloss_send","0"};
+static cvar_t sv_fakepacketloss_receive = {0, "sv_fakepacketloss_receive","0"};
+static cvar_t sv_fakepacketloss_send = {0, "sv_fakepacketloss_send","0"};
+
+
 /* statistic counters */
 static int packetsSent = 0;
 static int packetsReSent = 0;
@@ -86,6 +94,15 @@ cvar_t sv_netaddress_ipv6 = {0, "sv_netaddress_ipv6", "[0:0:0:0:0:0:0:0]:26000"}
 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress)
 {
        int length = LHNET_Read(mysocket, data, maxlength, peeraddress);
+       int i;
+       if (cl_fakepacketloss_receive.integer)
+               for (i = 0;i < cl_numsockets;i++)
+                       if (cl_sockets[i] == mysocket && (rand() % 100) < cl_fakepacketloss_receive.integer)
+                               return 0;
+       if (sv_fakepacketloss_receive.integer)
+               for (i = 0;i < cl_numsockets;i++)
+                       if (sv_sockets[i] == mysocket && (rand() % 100) < sv_fakepacketloss_receive.integer)
+                               return 0;
        if (developer_networking.integer && length != 0)
        {
                char addressstring[128], addressstring2[128];
@@ -104,7 +121,17 @@ int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddres
 
 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress)
 {
-       int ret = LHNET_Write(mysocket, data, length, peeraddress);
+       int ret;
+       int i;
+       if (cl_fakepacketloss_send.integer)
+               for (i = 0;i < cl_numsockets;i++)
+                       if (cl_sockets[i] == mysocket && (rand() % 100) < cl_fakepacketloss_send.integer)
+                               return length;
+       if (sv_fakepacketloss_send.integer)
+               for (i = 0;i < cl_numsockets;i++)
+                       if (sv_sockets[i] == mysocket && (rand() % 100) < sv_fakepacketloss_send.integer)
+                               return length;
+       ret = LHNET_Write(mysocket, data, length, peeraddress);
        if (developer_networking.integer)
        {
                char addressstring[128], addressstring2[128];
@@ -1502,6 +1529,12 @@ void NetConn_Init(void)
        Cvar_RegisterVariable(&net_messagetimeout);
        Cvar_RegisterVariable(&net_messagerejointimeout);
        Cvar_RegisterVariable(&net_connecttimeout);
+       Cvar_RegisterVariable(&cl_fakelocalping_min);
+       Cvar_RegisterVariable(&cl_fakelocalping_max);
+       Cvar_RegisterVariable(&cl_fakepacketloss_receive);
+       Cvar_RegisterVariable(&cl_fakepacketloss_send);
+       Cvar_RegisterVariable(&sv_fakepacketloss_receive);
+       Cvar_RegisterVariable(&sv_fakepacketloss_send);
        Cvar_RegisterVariable(&hostname);
        Cvar_RegisterVariable(&developer_networking);
        Cvar_RegisterVariable(&cl_netport);
index 9586fdc..c1d1e67 100755 (executable)
--- a/netconn.h
+++ b/netconn.h
@@ -193,6 +193,9 @@ extern unsigned short ntohs (unsigned short netshort);
 
 extern sizebuf_t net_message;
 
+extern cvar_t cl_fakelocalping_min;
+extern cvar_t cl_fakelocalping_max;
+
 int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data);
 //void NetConn_SendMessageNext(netconn_t *conn);
 //void NetConn_ReSendMessage(netconn_t *conn);