]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - lhnet.c
move two #defined to quakedef.h, and always include quakedef.h first before any other...
[xonotic/darkplaces.git] / lhnet.c
diff --git a/lhnet.c b/lhnet.c
index 4f820099e2e6e2efe3e6707a359fa6ea064d9552..d72589d1da946794cd7f9d1e6eae691592f8520b 100644 (file)
--- a/lhnet.c
+++ b/lhnet.c
@@ -1,6 +1,10 @@
 
 // Written by Forest Hale 2003-06-15 and placed into public domain.
 
+#ifndef STANDALONETEST
+#include "quakedef.h"
+#endif
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
@@ -23,7 +27,6 @@
 
 // for Z_Malloc/Z_Free in quake
 #ifndef STANDALONETEST
-#include "quakedef.h"
 #include "zone.h"
 #include "sys.h"
 #include "netconn.h"
 
 #define SOCKETERRNO WSAGetLastError()
 
+#define IOC_VENDOR 0x18000000
+#define _WSAIOW(x,y) (IOC_IN|(x)|(y))
+#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
+
 #define SOCKLEN_T int
 #elif defined(__MORPHOS__)
 #define ioctlsocket IoctlSocket
@@ -157,7 +164,7 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
 #ifdef STANDALONETEST
        if (i < MAX_NAMECACHE)
 #else
-       if (i < MAX_NAMECACHE && Sys_DoubleTime() < namecache[i].expirationtime)
+       if (i < MAX_NAMECACHE && realtime < namecache[i].expirationtime)
 #endif
        {
                *address = namecache[i].address;
@@ -188,7 +195,7 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
                                namecache[namecacheposition].name[i] = name[i];
                        namecache[namecacheposition].name[i] = 0;
 #ifndef STANDALONETEST
-                       namecache[namecacheposition].expirationtime = Sys_DoubleTime() + 12 * 3600; // 12 hours
+                       namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
 #endif
                        namecache[namecacheposition].address = *address;
                        namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
@@ -208,7 +215,7 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
                                namecache[namecacheposition].name[i] = name[i];
                        namecache[namecacheposition].name[i] = 0;
 #ifndef STANDALONETEST
-                       namecache[namecacheposition].expirationtime = Sys_DoubleTime() + 12 * 3600; // 12 hours
+                       namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
 #endif
                        namecache[namecacheposition].address = *address;
                        namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
@@ -225,7 +232,7 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
                namecache[namecacheposition].name[i] = name[i];
        namecache[namecacheposition].name[i] = 0;
 #ifndef STANDALONETEST
-       namecache[namecacheposition].expirationtime = Sys_DoubleTime() + 12 * 3600; // 12 hours
+       namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
 #endif
        namecache[namecacheposition].address.addresstype = LHNETADDRESSTYPE_NONE;
        namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
@@ -495,6 +502,28 @@ static const char *LHNETPRIVATE_StrError(void)
 #endif
 }
 
+void LHNET_SleepUntilPacket_Microseconds(int microseconds)
+{
+       fd_set fdreadset;
+       struct timeval tv;
+       int lastfd;
+       lhnetsocket_t *s;
+       FD_ZERO(&fdreadset);
+       lastfd = 0;
+       for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
+       {
+               if (s->address.addresstype == LHNETADDRESSTYPE_INET4 || s->address.addresstype == LHNETADDRESSTYPE_INET6)
+               {
+                       if (lastfd < s->inetsocket)
+                               lastfd = s->inetsocket;
+                       FD_SET(s->inetsocket, &fdreadset);
+               }
+       }
+       tv.tv_sec = microseconds / 1000000;
+       tv.tv_usec = microseconds % 1000000;
+       select(lastfd + 1, &fdreadset, NULL, NULL, &tv);
+}
+
 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
 {
        lhnetsocket_t *lhnetsocket, *s;
@@ -549,6 +578,7 @@ lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
                                {
 #ifdef WIN32
                                        u_long _true = 1;
+                                       u_long _false = 0;
 #else
                                        char _true = 1;
 #endif
@@ -566,6 +596,10 @@ lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
                                                        lhnetsocket->prev = lhnetsocket->next->prev;
                                                        lhnetsocket->next->prev = lhnetsocket;
                                                        lhnetsocket->prev->next = lhnetsocket;
+#ifdef WIN32
+                                                       if (ioctlsocket(lhnetsocket->inetsocket, SIO_UDP_CONNRESET, &_false) == -1)
+                                                               Con_DPrintf("LHNET_OpenSocket_Connectionless: ioctlsocket SIO_UDP_CONNRESET returned error: %s\n", LHNETPRIVATE_StrError());
+#endif
                                                        return lhnetsocket;
                                                }
                                                else
@@ -644,7 +678,7 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                                continue;
                        }
 #ifndef STANDALONETEST
-                       if (cl_netlocalping.value && (Sys_DoubleTime() - cl_netlocalping.value * (1.0 / 2000.0)) < p->sentdoubletime)
+                       if (cl_netlocalping.value && (realtime - cl_netlocalping.value * (1.0 / 2000.0)) < p->sentdoubletime)
                                continue;
 #endif
                        if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port)
@@ -740,7 +774,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng
                p->next->prev = p;
                p->prev->next = p;
 #ifndef STANDALONETEST
-               p->sentdoubletime = Sys_DoubleTime();
+               p->sentdoubletime = realtime;
 #endif
                value = contentlength;
        }
@@ -751,7 +785,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng
                {
                        if (SOCKETERRNO == EWOULDBLOCK)
                                return 0;
-                       Con_Printf("LHNET_Read: sendto returned error: %s\n", LHNETPRIVATE_StrError());
+                       Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
                }
        }
        else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
@@ -761,7 +795,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng
                {
                        if (SOCKETERRNO == EWOULDBLOCK)
                                return 0;
-                       Con_Printf("LHNET_Read: sendto returned error: %s\n", LHNETPRIVATE_StrError());
+                       Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
                }
        }
        return value;