]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - netconn.c
Forgot this when doing the embedded conchar commit
[xonotic/darkplaces.git] / netconn.c
index c24fd3dbc5136d7a0150b17d5ac48987a8f4ac35..12c97a2e3a408fa7e9621976ec00dd5cc7769937 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -35,6 +35,7 @@ static cvar_t sv_masters [] =
        {CVAR_SAVE, "sv_master3", ""},
        {CVAR_SAVE, "sv_master4", ""},
        {0, "sv_masterextra1", "69.59.212.88"},
+       {0, "sv_masterextra2", "66.28.32.64"},
        {0, NULL, NULL}
 };
 
@@ -48,12 +49,12 @@ 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"};
+cvar_t cl_netlocalping_min = {0, "cl_netlocalping_min","0"};
+cvar_t cl_netlocalping_max = {0, "cl_netlocalping_max","0"};
+static cvar_t cl_netpacketloss_receive = {0, "cl_netpacketloss_receive","0"};
+static cvar_t cl_netpacketloss_send = {0, "cl_netpacketloss_send","0"};
+static cvar_t sv_netpacketloss_receive = {0, "sv_netpacketloss_receive","0"};
+static cvar_t sv_netpacketloss_send = {0, "sv_netpacketloss_send","0"};
 
 
 /* statistic counters */
@@ -77,8 +78,8 @@ int serverreplycount = 0;
 int hostCacheCount = 0;
 hostcache_t hostcache[HOSTCACHESIZE];
 
-static qbyte sendbuffer[NET_MAXMESSAGE];
-static qbyte readbuffer[NET_MAXMESSAGE];
+static qbyte sendbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
+static qbyte readbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
 
 int cl_numsockets;
 lhnetsocket_t *cl_sockets[16];
@@ -97,13 +98,13 @@ int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddres
 {
        int length = LHNET_Read(mysocket, data, maxlength, peeraddress);
        int i;
-       if (cl_fakepacketloss_receive.integer)
+       if (cl_netpacketloss_receive.integer)
                for (i = 0;i < cl_numsockets;i++)
-                       if (cl_sockets[i] == mysocket && (rand() % 100) < cl_fakepacketloss_receive.integer)
+                       if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss_receive.integer)
                                return 0;
-       if (sv_fakepacketloss_receive.integer)
+       if (sv_netpacketloss_receive.integer)
                for (i = 0;i < cl_numsockets;i++)
-                       if (sv_sockets[i] == mysocket && (rand() % 100) < sv_fakepacketloss_receive.integer)
+                       if (sv_sockets[i] == mysocket && (rand() % 100) < sv_netpacketloss_receive.integer)
                                return 0;
        if (developer_networking.integer && length != 0)
        {
@@ -125,13 +126,13 @@ int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const l
 {
        int ret;
        int i;
-       if (cl_fakepacketloss_send.integer)
+       if (cl_netpacketloss_send.integer)
                for (i = 0;i < cl_numsockets;i++)
-                       if (cl_sockets[i] == mysocket && (rand() % 100) < cl_fakepacketloss_send.integer)
+                       if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss_send.integer)
                                return length;
-       if (sv_fakepacketloss_send.integer)
+       if (sv_netpacketloss_send.integer)
                for (i = 0;i < cl_numsockets;i++)
-                       if (sv_sockets[i] == mysocket && (rand() % 100) < sv_fakepacketloss_send.integer)
+                       if (sv_sockets[i] == mysocket && (rand() % 100) < sv_netpacketloss_send.integer)
                                return length;
        ret = LHNET_Write(mysocket, data, length, peeraddress);
        if (developer_networking.integer)
@@ -162,8 +163,8 @@ int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data)
        if (data->cursize == 0)
                Sys_Error("Datagram_SendMessage: zero length message\n");
 
-       if (data->cursize > NET_MAXMESSAGE)
-               Sys_Error("Datagram_SendMessage: message too big %u\n", data->cursize);
+       if (data->cursize > (int)sizeof(conn->sendMessage))
+               Sys_Error("Datagram_SendMessage: message too big (%u > %u)\n", data->cursize, sizeof(conn->sendMessage));
 
        if (conn->canSend == false)
                Sys_Error("SendMessage: called with canSend == false\n");
@@ -188,7 +189,7 @@ int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data)
        header = (void *)sendbuffer;
        header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
        header[1] = BigLong(conn->sendSequence);
-       memcpy(sendbuffer + 8, conn->sendMessage, dataLen);
+       memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
 
        conn->sendSequence++;
        conn->canSend = false;
@@ -227,7 +228,7 @@ static void NetConn_SendMessageNext(netconn_t *conn)
                header = (void *)sendbuffer;
                header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
                header[1] = BigLong(conn->sendSequence);
-               memcpy(sendbuffer + 8, conn->sendMessage, dataLen);
+               memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
 
                conn->sendSequence++;
                conn->sendNext = false;
@@ -265,7 +266,7 @@ static void NetConn_ReSendMessage(netconn_t *conn)
                header = (void *)sendbuffer;
                header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
                header[1] = BigLong(conn->sendSequence - 1);
-               memcpy(sendbuffer + 8, conn->sendMessage, dataLen);
+               memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
 
                conn->sendNext = false;
 
@@ -287,20 +288,20 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data)
        int packetLen;
        int *header;
 
-#ifdef DEBUG
+       packetLen = NET_HEADERSIZE + data->cursize;
+
+//#ifdef DEBUG
        if (data->cursize == 0)
                Sys_Error("Datagram_SendUnreliableMessage: zero length message\n");
 
-       if (data->cursize > MAX_DATAGRAM)
+       if (packetLen > (int)sizeof(sendbuffer))
                Sys_Error("Datagram_SendUnreliableMessage: message too big %u\n", data->cursize);
-#endif
-
-       packetLen = NET_HEADERSIZE + data->cursize;
+//#endif
 
        header = (void *)sendbuffer;
        header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE);
        header[1] = BigLong(conn->unreliableSendSequence);
-       memcpy(sendbuffer + 8, data->data, data->cursize);
+       memcpy(sendbuffer + NET_HEADERSIZE, data->data, data->cursize);
 
        conn->unreliableSendSequence++;
 
@@ -430,6 +431,11 @@ netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
        conn = Mem_Alloc(netconn_mempool, sizeof(*conn));
        conn->mysocket = mysocket;
        conn->peeraddress = *peeraddress;
+       // updated by receiving "rate" command from client
+       conn->rate = NET_MINRATE;
+       // no limits for local player
+       if (LHNETADDRESS_GetAddressType(peeraddress) == LHNETADDRESSTYPE_LOOP)
+               conn->rate = 1000000000;
        conn->canSend = true;
        conn->connecttime = realtime;
        conn->lastMessageTime = realtime;
@@ -477,7 +483,7 @@ static void NetConn_UpdateServerStuff(void)
                {
                        clientport2 = cl_netport.integer;
                        if (cls.state == ca_connected)
-                               Con_Printf("Changing \"cl_port\" will not take effect until you reconnect.\n");
+                               Con_Print("Changing \"cl_port\" will not take effect until you reconnect.\n");
                }
                if (cls.state == ca_disconnected && clientport != clientport2)
                {
@@ -492,7 +498,7 @@ static void NetConn_UpdateServerStuff(void)
        {
                hostport = sv_netport.integer;
                if (sv.active)
-                       Con_Printf("Changing \"port\" will not take effect until \"map\" command is executed.\n");
+                       Con_Print("Changing \"port\" will not take effect until \"map\" command is executed.\n");
        }
 }
 
@@ -537,7 +543,7 @@ int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
                                        }
                                }
                                else
-                                       Con_DPrintf("Got a stale datagram\n");
+                                       Con_DPrint("Got a stale datagram\n");
                                return 1;
                        }
                        else if (flags & NETFLAG_ACK)
@@ -548,7 +554,7 @@ int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
                                        {
                                                conn->ackSequence++;
                                                if (conn->ackSequence != conn->sendSequence)
-                                                       Con_DPrintf("ack sequencing error\n");
+                                                       Con_DPrint("ack sequencing error\n");
                                                conn->lastMessageTime = realtime;
                                                conn->timeout = realtime + net_messagetimeout.value;
                                                conn->sendMessageLength -= MAX_PACKETFRAGMENT;
@@ -565,10 +571,10 @@ int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
                                                }
                                        }
                                        else
-                                               Con_DPrintf("Duplicate ACK received\n");
+                                               Con_DPrint("Duplicate ACK received\n");
                                }
                                else
-                                       Con_DPrintf("Stale ACK received\n");
+                                       Con_DPrint("Stale ACK received\n");
                                return 1;
                        }
                        else if (flags & NETFLAG_DATA)
@@ -759,7 +765,7 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                        length -= 18;
                        masterreplycount++;
                        if (m_state != m_slist)
-                               Con_Printf("received server list...\n");
+                               Con_Print("received server list...\n");
                        while (length >= 7 && data[0] == '\\' && (data[1] != 0xFF || data[2] != 0xFF || data[3] != 0xFF || data[4] != 0xFF) && data[5] * 256 + data[6] != 0)
                        {
                                snprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[1], data[2], data[3], data[4], (data[5] << 8) | data[6]);
@@ -794,8 +800,8 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                hostcache[n].ping = 100000;
                                hostcache[n].querytime = realtime;
                                // build description strings for the things users care about
-                               snprintf(hostcache[n].line1, sizeof(hostcache[n].line1), "?");
-                               snprintf(hostcache[n].line2, sizeof(hostcache[n].line2), "%s", ipstring);
+                               strlcpy (hostcache[n].line1, "?", sizeof (hostcache[n].line1));
+                               strlcpy (hostcache[n].line2, ipstring, sizeof (hostcache[n].line2));
                                // if not in the slist menu we should print the server to console
                                if (m_state != m_slist)
                                        Con_Printf("querying %s\n", ipstring);
@@ -941,7 +947,7 @@ void NetConn_ClientFrame(void)
                        if (m_state == m_slist)
                                strcpy (m_return_reason, "Connect: Failed");
                        else
-                               Con_Printf("Connect failed\n");
+                               Con_Print("Connect failed\n");
                        return;
                }
                if (cls.connect_nextsendtime)
@@ -949,14 +955,14 @@ void NetConn_ClientFrame(void)
                        if (m_state == m_slist)
                                strcpy (m_return_reason, "Connect: Still trying");
                        else
-                               Con_Printf("Still trying...\n");
+                               Con_Print("Still trying...\n");
                }
                else
                {
                        if (m_state == m_slist)
                                strcpy (m_return_reason, "Connect: Trying");
                        else
-                               Con_Printf("Trying...\n");
+                               Con_Print("Trying...\n");
                }
                cls.connect_nextsendtime = realtime + 1;
                cls.connect_remainingtries--;
@@ -978,8 +984,9 @@ void NetConn_ClientFrame(void)
                        NetConn_ClientParsePacket(cl_sockets[i], readbuffer, length, &peeraddress);
        if (cls.netcon && realtime > cls.netcon->timeout)
        {
-               Con_Printf("Connection timed out\n");
+               Con_Print("Connection timed out\n");
                CL_Disconnect();
+               Host_ShutdownServer (false);
        }
        for (conn = netconn_list;conn;conn = conn->next)
                NetConn_ReSendMessage(conn);
@@ -1463,7 +1470,7 @@ void NetConn_QueryMasters(void)
        }
        if (!masterquerycount)
        {
-               Con_Printf("Unable to query master servers, no suitable network sockets active.\n");
+               Con_Print("Unable to query master servers, no suitable network sockets active.\n");
                strcpy(m_return_reason, "No network");
        }
 }
@@ -1541,7 +1548,7 @@ static void Net_Heartbeat_f(void)
        if (sv.active)
                NetConn_Heartbeat(2);
        else
-               Con_Printf("No server running, can not heartbeat to master server.\n");
+               Con_Print("No server running, can not heartbeat to master server.\n");
 }
 
 void PrintStats(netconn_t *conn)
@@ -1561,7 +1568,7 @@ void Net_Stats_f(void)
        Con_Printf("packetsReceived            = %i\n", packetsReceived);
        Con_Printf("receivedDuplicateCount     = %i\n", receivedDuplicateCount);
        Con_Printf("droppedDatagrams           = %i\n", droppedDatagrams);
-       Con_Printf("connections                =\n");
+       Con_Print("connections                =\n");
        for (conn = netconn_list;conn;conn = conn->next)
                PrintStats(conn);
 }
@@ -1576,10 +1583,10 @@ void Net_Slist_f(void)
        hostCacheCount = 0;
        memset(&pingcache, 0, sizeof(pingcache));
        if (m_state != m_slist)
-               Con_Printf("Sending requests to master servers\n");
+               Con_Print("Sending requests to master servers\n");
        NetConn_QueryMasters();
        if (m_state != m_slist)
-               Con_Printf("Listening for replies...\n");
+               Con_Print("Listening for replies...\n");
 }
 
 void NetConn_Init(void)
@@ -1593,12 +1600,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(&cl_netlocalping_min);
+       Cvar_RegisterVariable(&cl_netlocalping_max);
+       Cvar_RegisterVariable(&cl_netpacketloss_receive);
+       Cvar_RegisterVariable(&cl_netpacketloss_send);
+       Cvar_RegisterVariable(&sv_netpacketloss_receive);
+       Cvar_RegisterVariable(&sv_netpacketloss_send);
        Cvar_RegisterVariable(&hostname);
        Cvar_RegisterVariable(&developer_networking);
        Cvar_RegisterVariable(&cl_netport);