]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - netconn.c
implemented shownetgraph
[xonotic/darkplaces.git] / netconn.c
index ba857902e35540817b143ccdcb4dce52c3bd4483..833c50a5fb418ec556375f242e4b229fb0194820 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -38,8 +38,7 @@ static cvar_t sv_masters [] =
        {CVAR_SAVE, "sv_master4", "", "user-chosen master server 4"},
        {0, "sv_masterextra1", "ghdigital.com", "default master server 1 (admin: LordHavoc)"}, // admin: LordHavoc
        {0, "sv_masterextra2", "dpmaster.deathmask.net", "default master server 2 (admin: Willis)"}, // admin: Willis
-       {0, "sv_masterextra3", "12.166.196.192", "default master server 3 (admin: Venim)"}, // admin: Venim
-       {0, "sv_masterextra4", "excalibur.nvg.ntnu.no", "default master server 4 (admin: tChr)"}, // admin: tChr
+       {0, "sv_masterextra3", "excalibur.nvg.ntnu.no", "default master server 3 (admin: tChr)"}, // admin: tChr
        {0, NULL, NULL, NULL}
 };
 
@@ -405,8 +404,7 @@ static void _ServerList_Test(void)
 
 void ServerList_QueryList(qboolean querydp, qboolean queryqw)
 {
-       //masterquerytime = realtime;
-       masterquerytime = Sys_DoubleTime();
+       masterquerytime = realtime;
        masterquerycount = 0;
        masterreplycount = 0;
        serverquerycount = 0;
@@ -474,8 +472,31 @@ int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnet
        return NetConn_Write(mysocket, string, (int)strlen(string), peeraddress);
 }
 
-int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol)
+qboolean NetConn_CanSend(netconn_t *conn)
 {
+       conn->outgoing_packetcounter = (conn->outgoing_packetcounter + 1) % NETGRAPH_PACKETS;
+       conn->outgoing_unreliablesize[conn->outgoing_packetcounter] = NETGRAPH_NOPACKET;
+       conn->outgoing_reliablesize[conn->outgoing_packetcounter] = NETGRAPH_NOPACKET;
+       conn->outgoing_acksize[conn->outgoing_packetcounter] = NETGRAPH_NOPACKET;
+       if (realtime > conn->cleartime)
+               return true;
+       else
+       {
+               conn->outgoing_unreliablesize[conn->outgoing_packetcounter] = NETGRAPH_CHOKEDPACKET;
+               return false;
+       }
+}
+
+int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate)
+{
+       int totallen = 0;
+
+       // if this packet was supposedly choked, but we find ourselves sending one
+       // anyway, make sure the size counting starts at zero
+       // (this mostly happens on level changes and disconnects and such)
+       if (conn->outgoing_unreliablesize[conn->outgoing_packetcounter] == NETGRAPH_CHOKEDPACKET)
+               conn->outgoing_unreliablesize[conn->outgoing_packetcounter] = NETGRAPH_NOPACKET;
+
        if (protocol == PROTOCOL_QUAKEWORLD)
        {
                int packetLen;
@@ -516,16 +537,22 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
                        Con_Printf ("NetConn_SendUnreliableMessage: reliable message too big %u\n", data->cursize);
                        return -1;
                }
+
+               conn->outgoing_unreliablesize[conn->outgoing_packetcounter] += packetLen;
+
                // add the reliable message if there is one
                if (sendreliable)
                {
+                       conn->outgoing_reliablesize[conn->outgoing_packetcounter] += conn->sendMessageLength;
                        memcpy(sendbuffer + packetLen, conn->sendMessage, conn->sendMessageLength);
                        packetLen += conn->sendMessageLength;
                        conn->qw.last_reliable_sequence = conn->qw.outgoing_sequence;
                }
+
                // add the unreliable message if possible
                if (packetLen + data->cursize <= 1400)
                {
+                       conn->outgoing_unreliablesize[conn->outgoing_packetcounter] += data->cursize;
                        memcpy(sendbuffer + packetLen, data->data, data->cursize);
                        packetLen += data->cursize;
                }
@@ -535,10 +562,7 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
                packetsSent++;
                unreliableMessagesSent++;
 
-               // delay later packets to obey rate limit
-               conn->qw.cleartime = max(conn->qw.cleartime, realtime) + packetLen * conn->qw.rate;
-
-               return 0;
+               totallen += packetLen + 18;
        }
        else
        {
@@ -568,11 +592,15 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
                        header[1] = BigLong(conn->nq.sendSequence - 1);
                        memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
 
+                       conn->outgoing_reliablesize[conn->outgoing_packetcounter] += packetLen;
+
                        if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) == (int)packetLen)
                        {
                                conn->lastSendTime = realtime;
                                packetsReSent++;
                        }
+
+                       totallen += packetLen + 18;
                }
 
                // if we have a new reliable message to send, do so
@@ -615,15 +643,19 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
 
                        conn->nq.sendSequence++;
 
+                       conn->outgoing_reliablesize[conn->outgoing_packetcounter] += packetLen;
+
                        NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
 
                        conn->lastSendTime = realtime;
                        packetsSent++;
                        reliableMessagesSent++;
+
+                       totallen += packetLen + 18;
                }
 
                // if we have an unreliable message to send, do so
-               if (data->cursize)
+               //if (data->cursize)
                {
                        packetLen = NET_HEADERSIZE + data->cursize;
 
@@ -640,13 +672,26 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
 
                        conn->nq.unreliableSendSequence++;
 
+                       conn->outgoing_unreliablesize[conn->outgoing_packetcounter] += packetLen;
+
                        NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
 
                        packetsSent++;
                        unreliableMessagesSent++;
+
+                       if (data->cursize)
+                               totallen += packetLen + 18;
                }
-               return 0;
        }
+
+       // delay later packets to obey rate limit
+       if (conn->cleartime < realtime - 0.1)
+               conn->cleartime = realtime - 0.1;
+       conn->cleartime = conn->cleartime + (double)totallen / (double)rate;
+       if (conn->cleartime < realtime)
+               conn->cleartime = realtime;
+
+       return 0;
 }
 
 void NetConn_CloseClientPorts(void)
@@ -852,6 +897,7 @@ void NetConn_UpdateSockets(void)
 
 static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int length, protocolversion_t protocol, double newtimeout)
 {
+       int originallength = length;
        if (length < 8)
                return 0;
 
@@ -897,12 +943,16 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
                        //Con_DPrintf("Dropped %u datagram(s)\n", count);
                        while (count--)
                        {
-                               conn->packetlost[conn->packetlostcounter] = true;
-                               conn->packetlostcounter = (conn->packetlostcounter + 1) % 100;
+                               conn->incoming_packetcounter = (conn->incoming_packetcounter + 1) % NETGRAPH_PACKETS;
+                               conn->incoming_unreliablesize[conn->incoming_packetcounter] = NETGRAPH_LOSTPACKET;
+                               conn->incoming_reliablesize[conn->incoming_packetcounter] = NETGRAPH_NOPACKET;
+                               conn->incoming_acksize[conn->incoming_packetcounter] = NETGRAPH_NOPACKET;
                        }
                }
-               conn->packetlost[conn->packetlostcounter] = false;
-               conn->packetlostcounter = (conn->packetlostcounter + 1) % 100;
+               conn->incoming_packetcounter = (conn->incoming_packetcounter + 1) % NETGRAPH_PACKETS;
+               conn->incoming_unreliablesize[conn->incoming_packetcounter] = originallength;
+               conn->incoming_reliablesize[conn->incoming_packetcounter] = NETGRAPH_NOPACKET;
+               conn->incoming_acksize[conn->incoming_packetcounter] = NETGRAPH_NOPACKET;
                if (reliable_ack == conn->qw.reliable_sequence)
                {
                        // received, now we will be able to send another reliable message
@@ -952,12 +1002,16 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
                                                //Con_DPrintf("Dropped %u datagram(s)\n", count);
                                                while (count--)
                                                {
-                                                       conn->packetlost[conn->packetlostcounter] = true;
-                                                       conn->packetlostcounter = (conn->packetlostcounter + 1) % 100;
+                                                       conn->incoming_packetcounter = (conn->incoming_packetcounter + 1) % NETGRAPH_PACKETS;
+                                                       conn->incoming_unreliablesize[conn->incoming_packetcounter] = NETGRAPH_LOSTPACKET;
+                                                       conn->incoming_reliablesize[conn->incoming_packetcounter] = NETGRAPH_NOPACKET;
+                                                       conn->incoming_acksize[conn->incoming_packetcounter] = NETGRAPH_NOPACKET;
                                                }
                                        }
-                                       conn->packetlost[conn->packetlostcounter] = false;
-                                       conn->packetlostcounter = (conn->packetlostcounter + 1) % 100;
+                                       conn->incoming_packetcounter = (conn->incoming_packetcounter + 1) % NETGRAPH_PACKETS;
+                                       conn->incoming_unreliablesize[conn->incoming_packetcounter] = originallength;
+                                       conn->incoming_reliablesize[conn->incoming_packetcounter] = NETGRAPH_NOPACKET;
+                                       conn->incoming_acksize[conn->incoming_packetcounter] = NETGRAPH_NOPACKET;
                                        conn->nq.unreliableReceiveSequence = sequence + 1;
                                        conn->lastMessageTime = realtime;
                                        conn->timeout = realtime + newtimeout;
@@ -976,6 +1030,7 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
                        }
                        else if (flags & NETFLAG_ACK)
                        {
+                               conn->incoming_acksize[conn->incoming_packetcounter] += originallength;
                                if (sequence == (conn->nq.sendSequence - 1))
                                {
                                        if (sequence == conn->nq.ackSequence)
@@ -1034,6 +1089,8 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
                        else if (flags & NETFLAG_DATA)
                        {
                                unsigned int temppacket[2];
+                               conn->incoming_reliablesize[conn->incoming_packetcounter] += originallength;
+                               conn->outgoing_acksize[conn->outgoing_packetcounter] += 8;
                                temppacket[0] = BigLong(8 | NETFLAG_ACK);
                                temppacket[1] = BigLong(sequence);
                                NetConn_Write(conn->mysocket, (unsigned char *)temppacket, 8, &conn->peeraddress);
@@ -1092,6 +1149,9 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer
        cls.state = ca_connected;
        cls.signon = 0;                         // need all the signon messages before playing
        cls.protocol = initialprotocol;
+       // reset move sequence numbering on this new connection
+       cls.movesequence = 0;
+       cls.servermovesequence = 0;
        if (cls.protocol == PROTOCOL_QUAKEWORLD)
                Cmd_ForwardStringToServer("new");
        if (cls.protocol == PROTOCOL_QUAKE)
@@ -1104,7 +1164,7 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer
                msg.data = buf;
                msg.maxsize = sizeof(buf);
                MSG_WriteChar(&msg, clc_nop);
-               NetConn_SendUnreliableMessage(cls.netcon, &msg, cls.protocol);
+               NetConn_SendUnreliableMessage(cls.netcon, &msg, cls.protocol, 10000);
        }
 }
 
@@ -1136,8 +1196,7 @@ static int NetConn_ClientParsePacket_ServerList_ProcessReply(const char *address
                // store the data the engine cares about (address and ping)
                strlcpy(serverlist_cache[serverlist_cachecount].info.cname, addressstring, sizeof(serverlist_cache[serverlist_cachecount].info.cname));
                serverlist_cache[serverlist_cachecount].info.ping = 100000;
-               //serverlist_cache[serverlist_cachecount].querytime = realtime;
-               serverlist_cache[serverlist_cachecount].querytime = Sys_DoubleTime();
+               serverlist_cache[serverlist_cachecount].querytime = realtime;
                // if not in the slist menu we should print the server to console
                if (serverlist_consoleoutput)
                        Con_Printf("querying %s\n", addressstring);
@@ -1146,7 +1205,7 @@ static int NetConn_ClientParsePacket_ServerList_ProcessReply(const char *address
        // if this is the first reply from this server, count it as having replied
        if (serverlist_cache[n].info.ping == 100000)
                serverreplycount++;
-       pingtime = (int)((Sys_DoubleTime() - serverlist_cache[n].querytime) * 1000.0 + 0.5);
+       pingtime = (int)((realtime - serverlist_cache[n].querytime) * 1000.0 + 0.5);
        pingtime = bound(0, pingtime, 9999);
        // update the ping
        serverlist_cache[n].info.ping = min(serverlist_cache[n].info.ping, pingtime);
@@ -1353,7 +1412,7 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        if(net_extresponse_count > NET_EXTRESPONSE_MAX)
                                net_extresponse_count = NET_EXTRESPONSE_MAX;
                        net_extresponse_last = (net_extresponse_last + 1) % NET_EXTRESPONSE_MAX;
-                       dpsnprintf(net_extresponse[net_extresponse_last], sizeof(net_extresponse[net_extresponse_last]), "%s %s", addressstring2, string + 12);
+                       dpsnprintf(net_extresponse[net_extresponse_last], sizeof(net_extresponse[net_extresponse_last]), "'%s' %s", addressstring2, string + 12);
                        return true;
                }
                if (!strncmp(string, "ping", 4))
@@ -1452,9 +1511,12 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                int n;
                serverlist_info_t *info;
 
-               c = data[4];
-               data += 5;
-               length -= 5;
+               data += 4;
+               length -= 4;
+               SZ_Clear(&net_message);
+               SZ_Write(&net_message, data, length);
+               MSG_BeginReading();
+               c = MSG_ReadByte();
                switch (c)
                {
                case CCREP_ACCEPT:
@@ -1464,13 +1526,7 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        {
                                lhnetaddress_t clientportaddress;
                                clientportaddress = *peeraddress;
-                               if (length >= 4)
-                               {
-                                       unsigned int port = (data[0] << 0) | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
-                                       data += 4;
-                                       length -= 4;
-                                       LHNETADDRESS_SetPort(&clientportaddress, port);
-                               }
+                               LHNETADDRESS_SetPort(&clientportaddress, MSG_ReadLong());
                                // update the server IP in the userinfo (QW servers expect this, and it is used by the reconnect command)
                                InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "*ip", addressstring2);
                                M_Update_Return_Reason("Accepted");
@@ -1481,7 +1537,7 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        if (developer.integer >= 10)
                                Con_Printf("Datagram_ParseConnectionless: received CCREP_REJECT from %s.\n", addressstring2);
                        cls.connect_trying = false;
-                       M_Update_Return_Reason((char *)data);
+                       M_Update_Return_Reason((char *)MSG_ReadString());
                        break;
                case CCREP_SERVER_INFO:
                        if (developer.integer >= 10)
@@ -1519,6 +1575,7 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                default:
                        break;
                }
+               SZ_Clear(&net_message);
                // we may not have liked the packet, but it was a valid control
                // packet, so we're done processing this packet now
                return true;
@@ -1590,8 +1647,7 @@ void NetConn_QueryQueueFrame(void)
                                        NetConn_WriteString(cl_sockets[socket], "\377\377\377\377getinfo", &address);
                        }
 
-                       //entry->querytime = realtime;
-                       entry->querytime = Sys_DoubleTime();
+                       entry->querytime = realtime;
                        entry->querycounter++;
 
                        // if not in the slist menu we should print the server to console
@@ -2048,20 +2104,26 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
        //  Quake packet flood Denial Of Service attacks)
        if (length >= 5 && (i = BigLong(*((int *)data))) && (i & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (i & NETFLAG_LENGTH_MASK) == length && (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3))
        {
-               int c = data[4];
-               data += 5;
-               length -= 5;
+               int c;
+               int protocolnumber;
+               const char *protocolname;
+               data += 4;
+               length -= 4;
+               SZ_Clear(&net_message);
+               SZ_Write(&net_message, data, length);
+               MSG_BeginReading();
+               c = MSG_ReadByte();
                switch (c)
                {
                case CCREQ_CONNECT:
                        if (developer.integer >= 10)
                                Con_Printf("Datagram_ParseConnectionless: received CCREQ_CONNECT from %s.\n", addressstring2);
-                       if (length < (int)strlen("QUAKE") + 1 + 1)
-                               break;
                        if(sv_public.integer <= -2)
                                break;
 
-                       if (memcmp(data, "QUAKE", strlen("QUAKE") + 1) != 0 || (int)data[strlen("QUAKE") + 1] != NET_PROTOCOL_VERSION)
+                       protocolname = MSG_ReadString();
+                       protocolnumber = MSG_ReadByte();
+                       if (strcmp(protocolname, "QUAKE") || protocolnumber != NET_PROTOCOL_VERSION)
                        {
                                if (developer.integer >= 10)
                                        Con_Printf("Datagram_ParseConnectionless: sending CCREP_REJECT \"Incompatible version.\" to %s.\n", addressstring2);
@@ -2209,7 +2271,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                        MSG_WriteByte(&net_message, playerNumber);
                                        MSG_WriteString(&net_message, client->name);
                                        MSG_WriteLong(&net_message, client->colors);
-                                       MSG_WriteLong(&net_message, (int)client->edict->fields.server->frags);
+                                       MSG_WriteLong(&net_message, client->frags);
                                        MSG_WriteLong(&net_message, (int)(realtime - client->connecttime));
                                        MSG_WriteString(&net_message, client->netconnection ? client->netconnection->address : "botclient");
                                        *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
@@ -2250,6 +2312,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                default:
                        break;
                }
+               SZ_Clear(&net_message);
                // we may not have liked the packet, but it was a valid control
                // packet, so we're done processing this packet now
                return true;
@@ -2287,6 +2350,11 @@ void NetConn_ServerFrame(void)
        }
 }
 
+void NetConn_SleepMicroseconds(int microseconds)
+{
+       LHNET_SleepUntilPacket_Microseconds(microseconds);
+}
+
 void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
 {
        int i;