X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=netconn.c;h=9ef5455065994d6af59dbccaa307afa8347e1a84;hb=0144ec3bdff2b7e8397be064cb7850a1ece7bce9;hp=1ade12c331a31c390ae00667a88d7bbada744888;hpb=1147e4d621c0a3a82ca50a6f97a1caa6febfa121;p=xonotic%2Fdarkplaces.git diff --git a/netconn.c b/netconn.c index 1ade12c3..9ef54550 100755 --- 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} }; @@ -77,7 +76,8 @@ cvar_t hostname = {CVAR_SAVE, "hostname", "UNNAMED", "server message to show in cvar_t developer_networking = {0, "developer_networking", "0", "prints all received and sent packets (recommended only for debugging)"}; cvar_t cl_netlocalping = {0, "cl_netlocalping","0", "lags local loopback connection by this much ping time (useful to play more fairly on your own server with people with higher pings)"}; -static cvar_t cl_netpacketloss = {0, "cl_netpacketloss","0", "drops this percentage of packets (incoming and outgoing), useful for testing network protocol robustness (effects failing to start, sounds failing to play, etc)"}; +static cvar_t cl_netpacketloss_send = {0, "cl_netpacketloss_send","0", "drops this percentage of outgoing packets, useful for testing network protocol robustness (jerky movement, prediction errors, etc)"}; +static cvar_t cl_netpacketloss_receive = {0, "cl_netpacketloss_receive","0", "drops this percentage of incoming packets, useful for testing network protocol robustness (jerky movement, effects failing to start, sounds failing to play, etc)"}; static cvar_t net_slist_queriespersecond = {0, "net_slist_queriespersecond", "20", "how many server information requests to send per second"}; static cvar_t net_slist_queriesperframe = {0, "net_slist_queriesperframe", "4", "maximum number of server information requests to send each rendered frame (guards against low framerates causing problems)"}; static cvar_t net_slist_timeout = {0, "net_slist_timeout", "4", "how long to listen for a server information response before giving up"}; @@ -185,6 +185,15 @@ static qboolean _ServerList_Entry_Compare( serverlist_entry_t *A, serverlist_ent case SLIF_NUMPLAYERS: result = A->info.numplayers - B->info.numplayers; break; + case SLIF_NUMBOTS: + result = A->info.numbots - B->info.numbots; + break; + case SLIF_NUMHUMANS: + result = A->info.numhumans - B->info.numhumans; + break; + case SLIF_FREESLOTS: + result = A->info.freeslots - B->info.freeslots; + break; case SLIF_PROTOCOL: result = A->info.protocol - B->info.protocol; break; @@ -286,6 +295,12 @@ static qboolean _ServerList_Entry_Mask( serverlist_mask_t *mask, serverlist_info return false; if( !_ServerList_CompareInt( info->numplayers, mask->tests[SLIF_NUMPLAYERS], mask->info.numplayers ) ) return false; + if( !_ServerList_CompareInt( info->numbots, mask->tests[SLIF_NUMBOTS], mask->info.numbots ) ) + return false; + if( !_ServerList_CompareInt( info->numhumans, mask->tests[SLIF_NUMHUMANS], mask->info.numhumans ) ) + return false; + if( !_ServerList_CompareInt( info->freeslots, mask->tests[SLIF_FREESLOTS], mask->info.freeslots ) ) + return false; if( !_ServerList_CompareInt( info->protocol, mask->tests[SLIF_PROTOCOL], mask->info.protocol )) return false; if( *mask->info.cname @@ -376,15 +391,25 @@ void ServerList_RebuildViewList(void) int i; serverlist_viewcount = 0; - for( i = 0 ; i < serverlist_cachecount ; i++ ) - if( serverlist_cache[i].query == SQS_QUERIED ) - ServerList_ViewList_Insert( &serverlist_cache[i] ); + for( i = 0 ; i < serverlist_cachecount ; i++ ) { + serverlist_entry_t *entry = &serverlist_cache[i]; + // also display entries that are currently being refreshed [11/8/2007 Black] + if( entry->query == SQS_QUERIED || entry->query == SQS_REFRESHING ) + ServerList_ViewList_Insert( entry ); + } } void ServerList_ResetMasks(void) { + int i; + memset( &serverlist_andmasks, 0, sizeof( serverlist_andmasks ) ); memset( &serverlist_ormasks, 0, sizeof( serverlist_ormasks ) ); + // numbots needs to be compared to -1 to always succeed + for(i = 0; i < SERVERLIST_ANDMASKCOUNT; ++i) + serverlist_andmasks[i].info.numbots = -1; + for(i = 0; i < SERVERLIST_ORMASKCOUNT; ++i) + serverlist_ormasks[i].info.numbots = -1; } #if 0 @@ -403,17 +428,26 @@ static void _ServerList_Test(void) } #endif -void ServerList_QueryList(qboolean querydp, qboolean queryqw) +void ServerList_QueryList(qboolean resetcache, qboolean querydp, qboolean queryqw, qboolean consoleoutput) { - //masterquerytime = realtime; - masterquerytime = Sys_DoubleTime(); + masterquerytime = realtime; masterquerycount = 0; masterreplycount = 0; - serverquerycount = 0; - serverreplycount = 0; - serverlist_cachecount = 0; - serverlist_viewcount = 0; - serverlist_consoleoutput = false; + if( resetcache ) { + serverquerycount = 0; + serverreplycount = 0; + serverlist_cachecount = 0; + serverlist_viewcount = 0; + } else { + // refresh all entries + int n; + for( n = 0 ; n < serverlist_cachecount ; n++ ) { + serverlist_entry_t *entry = &serverlist_cache[ n ]; + entry->query = SQS_REFRESHING; + entry->querycounter = 0; + } + } + serverlist_consoleoutput = consoleoutput; //_ServerList_Test(); @@ -428,9 +462,9 @@ int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddres int i; if (length == 0) return 0; - if (cl_netpacketloss.integer) + if (cl_netpacketloss_receive.integer) for (i = 0;i < cl_numsockets;i++) - if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss.integer) + if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss_receive.integer) return 0; if (developer_networking.integer) { @@ -452,9 +486,9 @@ int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const l { int ret; int i; - if (cl_netpacketloss.integer) + if (cl_netpacketloss_send.integer) for (i = 0;i < cl_numsockets;i++) - if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss.integer) + if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss_send.integer) return length; ret = LHNET_Write(mysocket, data, length, peeraddress); if (developer_networking.integer) @@ -474,8 +508,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, qboolean quakesignon_suppressreliables) +{ + 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 +573,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 +598,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 + 28; } else { @@ -568,15 +628,19 @@ 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 + 28; } // if we have a new reliable message to send, do so - if (!conn->sendMessageLength && conn->message.cursize) + if (!conn->sendMessageLength && conn->message.cursize && !quakesignon_suppressreliables) { if (conn->message.cursize > (int)sizeof(conn->sendMessage)) { @@ -615,11 +679,15 @@ 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 + 28; } // if we have an unreliable message to send, do so @@ -640,13 +708,35 @@ 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++; + + totallen += packetLen + 28; } - 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; +} + +qboolean NetConn_HaveClientPorts(void) +{ + return !!cl_numsockets; +} + +qboolean NetConn_HaveServerPorts(void) +{ + return !!sv_numsockets; } void NetConn_CloseClientPorts(void) @@ -852,11 +942,10 @@ 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; - // TODO: add netgraph stuff rather than just packetloss counting... - if (protocol == PROTOCOL_QUAKEWORLD) { int sequence, sequence_ack; @@ -897,12 +986,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 +1045,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 +1073,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) @@ -993,7 +1091,7 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len unsigned int *header; conn->sendMessageLength -= MAX_PACKETFRAGMENT; - memcpy(conn->sendMessage, conn->sendMessage+MAX_PACKETFRAGMENT, conn->sendMessageLength); + memmove(conn->sendMessage, conn->sendMessage+MAX_PACKETFRAGMENT, conn->sendMessageLength); if (conn->sendMessageLength <= MAX_PACKETFRAGMENT) { @@ -1034,6 +1132,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 +1192,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 = 1; + cls.servermovesequence = 0; if (cls.protocol == PROTOCOL_QUAKEWORLD) Cmd_ForwardStringToServer("new"); if (cls.protocol == PROTOCOL_QUAKE) @@ -1104,7 +1207,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, false); } } @@ -1119,10 +1222,15 @@ static int NetConn_ClientParsePacket_ServerList_ProcessReply(const char *address { int n; int pingtime; + serverlist_entry_t *entry = NULL; + // search the cache for this server and update it - for (n = 0;n < serverlist_cachecount;n++) - if (!strcmp(addressstring, serverlist_cache[n].info.cname)) + for (n = 0;n < serverlist_cachecount;n++) { + entry = &serverlist_cache[ n ]; + if (!strcmp(addressstring, entry->info.cname)) break; + } + if (n == serverlist_cachecount) { // LAN search doesnt require an answer from the master server so we wont @@ -1132,44 +1240,89 @@ static int NetConn_ClientParsePacket_ServerList_ProcessReply(const char *address if (serverlist_cachecount == SERVERLIST_TOTALSIZE) return -1; - memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount])); + entry = &serverlist_cache[n]; + + memset(entry, 0, sizeof(*entry)); // 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(); + strlcpy(entry->info.cname, addressstring, sizeof(entry->info.cname)); + entry->info.ping = 100000; + entry->querytime = realtime; // if not in the slist menu we should print the server to console if (serverlist_consoleoutput) Con_Printf("querying %s\n", addressstring); ++serverlist_cachecount; } // 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 - entry->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); + if (entry->query == SQS_REFRESHING) { + entry->info.ping = pingtime; + entry->query = SQS_QUERIED; + } else { + // convert to unsigned to catch the -1 + // I still dont like this but its better than the old 10000 magic ping number - as in easier to type and read :( [11/8/2007 Black] + entry->info.ping = min((unsigned) entry->info.ping, (unsigned) pingtime); + serverreplycount++; + } + // other server info is updated by the caller return n; } static void NetConn_ClientParsePacket_ServerList_UpdateCache(int n) { - serverlist_info_t *info = &serverlist_cache[n].info; + serverlist_entry_t *entry = &serverlist_cache[n]; + serverlist_info_t *info = &entry->info; // update description strings for engine menu and console output - dpsnprintf(serverlist_cache[n].line1, sizeof(serverlist_cache[n].line1), "^%c%5d^7 ^%c%3u^7/%3u %-65.65s", info->ping >= 300 ? '1' : (info->ping >= 200 ? '3' : '7'), (int)info->ping, ((info->numplayers > 0 && info->numplayers < info->maxplayers) ? (info->numplayers >= 4 ? '7' : '3') : '1'), info->numplayers, info->maxplayers, info->name); - dpsnprintf(serverlist_cache[n].line2, sizeof(serverlist_cache[n].line2), "^4%-21.21s %-19.19s ^%c%-17.17s^4 %-20.20s", info->cname, info->game, (info->gameversion != gameversion.integer) ? '1' : '4', info->mod, info->map); - if (serverlist_cache[n].query == SQS_QUERIED) - ServerList_ViewList_Remove(&serverlist_cache[n]); + dpsnprintf(entry->line1, sizeof(serverlist_cache[n].line1), "^%c%5d^7 ^%c%3u^7/%3u %-65.65s", info->ping >= 300 ? '1' : (info->ping >= 200 ? '3' : '7'), (int)info->ping, ((info->numhumans > 0 && info->numhumans < info->maxplayers) ? (info->numhumans >= 4 ? '7' : '3') : '1'), info->numplayers, info->maxplayers, info->name); + dpsnprintf(entry->line2, sizeof(serverlist_cache[n].line2), "^4%-21.21s %-19.19s ^%c%-17.17s^4 %-20.20s", info->cname, info->game, (info->gameversion != gameversion.integer) ? '1' : '4', info->mod, info->map); + if (entry->query == SQS_QUERIED) + ServerList_ViewList_Remove(entry); // if not in the slist menu we should print the server to console (if wanted) else if( serverlist_consoleoutput ) Con_Printf("%s\n%s\n", serverlist_cache[n].line1, serverlist_cache[n].line2); // and finally, update the view set - ServerList_ViewList_Insert( &serverlist_cache[n] ); + ServerList_ViewList_Insert( entry ); + // update the entry's state serverlist_cache[n].query = SQS_QUERIED; } +// returns true, if it's sensible to continue the processing +static qboolean NetConn_ClientParsePacket_ServerList_PrepareQuery( int protocol, const char *ipstring ) { + int n; + serverlist_entry_t *entry; + + // ignore the rest of the message if the serverlist is full + if( serverlist_cachecount == SERVERLIST_TOTALSIZE ) + return false; + // also ignore it if we have already queried it (other master server response) + for( n = 0 ; n < serverlist_cachecount ; n++ ) + if( !strcmp( ipstring, serverlist_cache[ n ].info.cname ) ) + break; + + entry = &serverlist_cache[n]; + + if( n < serverlist_cachecount ) { + // the entry has already been queried once or + return true; + } + + memset(entry, 0, sizeof(entry)); + entry->protocol = protocol; + // store the data the engine cares about (address and ping) + strlcpy (entry->info.cname, ipstring, sizeof(entry->info.cname)); + + // no, then reset the ping right away + entry->info.ping = -1; + // we also want to increase the serverlist_cachecount then + serverlist_cachecount++; + serverquerycount++; + + entry->query = SQS_QUERYING; + + return true; +} + static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress) { qboolean fromserver; @@ -1244,14 +1397,26 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat return true; info = &serverlist_cache[n].info; - if ((s = SearchInfostring(string, "gamename" )) != NULL) strlcpy(info->game, s, sizeof (info->game));else info->game[0] = 0; - if ((s = SearchInfostring(string, "modname" )) != NULL) strlcpy(info->mod , s, sizeof (info->mod ));else info->mod[0] = 0; - if ((s = SearchInfostring(string, "mapname" )) != NULL) strlcpy(info->map , s, sizeof (info->map ));else info->map[0] = 0; - if ((s = SearchInfostring(string, "hostname" )) != NULL) strlcpy(info->name, s, sizeof (info->name));else info->name[0] = 0; - if ((s = SearchInfostring(string, "protocol" )) != NULL) info->protocol = atoi(s);else info->protocol = -1; - if ((s = SearchInfostring(string, "clients" )) != NULL) info->numplayers = atoi(s);else info->numplayers = 0; - if ((s = SearchInfostring(string, "sv_maxclients")) != NULL) info->maxplayers = atoi(s);else info->maxplayers = 0; - if ((s = SearchInfostring(string, "gameversion" )) != NULL) info->gameversion = atoi(s);else info->gameversion = 0; + info->game[0] = 0; + info->mod[0] = 0; + info->map[0] = 0; + info->name[0] = 0; + info->protocol = -1; + info->numplayers = 0; + info->numbots = -1; + info->maxplayers = 0; + info->gameversion = 0; + if ((s = SearchInfostring(string, "gamename" )) != NULL) strlcpy(info->game, s, sizeof (info->game)); + if ((s = SearchInfostring(string, "modname" )) != NULL) strlcpy(info->mod , s, sizeof (info->mod )); + if ((s = SearchInfostring(string, "mapname" )) != NULL) strlcpy(info->map , s, sizeof (info->map )); + if ((s = SearchInfostring(string, "hostname" )) != NULL) strlcpy(info->name, s, sizeof (info->name)); + if ((s = SearchInfostring(string, "protocol" )) != NULL) info->protocol = atoi(s); + if ((s = SearchInfostring(string, "clients" )) != NULL) info->numplayers = atoi(s); + if ((s = SearchInfostring(string, "bots" )) != NULL) info->numbots = atoi(s); + if ((s = SearchInfostring(string, "sv_maxclients")) != NULL) info->maxplayers = atoi(s); + if ((s = SearchInfostring(string, "gameversion" )) != NULL) info->gameversion = atoi(s); + info->numhumans = info->numplayers - max(0, info->numbots); + info->freeslots = info->maxplayers - info->numplayers; NetConn_ClientParsePacket_ServerList_UpdateCache(n); @@ -1267,30 +1432,12 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat Con_Print("received DarkPlaces 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) { - int n; - dpsnprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[1], data[2], data[3], data[4], data[5] * 256 + data[6]); if (serverlist_consoleoutput && developer_networking.integer) Con_Printf("Requesting info from DarkPlaces server %s\n", ipstring); - // ignore the rest of the message if the serverlist is full - if( serverlist_cachecount == SERVERLIST_TOTALSIZE ) + + if( !NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_DARKPLACES7, ipstring ) ) { break; - // also ignore it if we have already queried it (other master server response) - for( n = 0 ; n < serverlist_cachecount ; n++ ) - if( !strcmp( ipstring, serverlist_cache[ n ].info.cname ) ) - break; - if( n >= serverlist_cachecount ) - { - serverquerycount++; - - memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount])); - serverlist_cache[serverlist_cachecount].protocol = PROTOCOL_DARKPLACES7; - // store the data the engine cares about (address and ping) - strlcpy (serverlist_cache[serverlist_cachecount].info.cname, ipstring, sizeof (serverlist_cache[serverlist_cachecount].info.cname)); - serverlist_cache[serverlist_cachecount].info.ping = 100000; - serverlist_cache[serverlist_cachecount].query = SQS_QUERYING; - - ++serverlist_cachecount; } // move on to next address in packet @@ -1312,30 +1459,12 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat Con_Printf("received QuakeWorld server list from %s...\n", addressstring2); while (length >= 6 && (data[0] != 0xFF || data[1] != 0xFF || data[2] != 0xFF || data[3] != 0xFF) && data[4] * 256 + data[5] != 0) { - int n; - dpsnprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[0], data[1], data[2], data[3], data[4] * 256 + data[5]); if (serverlist_consoleoutput && developer_networking.integer) Con_Printf("Requesting info from QuakeWorld server %s\n", ipstring); - // ignore the rest of the message if the serverlist is full - if( serverlist_cachecount == SERVERLIST_TOTALSIZE ) + + if( !NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_QUAKEWORLD, ipstring ) ) { break; - // also ignore it if we have already queried it (other master server response) - for( n = 0 ; n < serverlist_cachecount ; n++ ) - if( !strcmp( ipstring, serverlist_cache[ n ].info.cname ) ) - break; - if( n >= serverlist_cachecount ) - { - serverquerycount++; - - memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount])); - serverlist_cache[serverlist_cachecount].protocol = PROTOCOL_QUAKEWORLD; - // store the data the engine cares about (address and ping) - strlcpy (serverlist_cache[serverlist_cachecount].info.cname, ipstring, sizeof (serverlist_cache[serverlist_cachecount].info.cname)); - serverlist_cache[serverlist_cachecount].info.ping = 100000; - serverlist_cache[serverlist_cachecount].query = SQS_QUERYING; - - ++serverlist_cachecount; } // move on to next address in packet @@ -1452,9 +1581,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 +1596,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 +1607,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 +1645,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; @@ -1555,43 +1682,43 @@ void NetConn_QueryQueueFrame(void) return; } - // scan serverlist and issue queries as needed - serverlist_querysleep = true; + // scan serverlist and issue queries as needed + serverlist_querysleep = true; - timeouttime = realtime - net_slist_timeout.value; - for( index = 0, queries = 0 ; index < serverlist_cachecount && queries < maxqueries ; index++ ) + timeouttime = realtime - net_slist_timeout.value; + for( index = 0, queries = 0 ; index < serverlist_cachecount && queries < maxqueries ; index++ ) { serverlist_entry_t *entry = &serverlist_cache[ index ]; - if( entry->query != SQS_QUERYING ) + if( entry->query != SQS_QUERYING && entry->query != SQS_REFRESHING ) { continue; } - serverlist_querysleep = false; - if( entry->querycounter != 0 && entry->querytime > timeouttime ) + serverlist_querysleep = false; + if( entry->querycounter != 0 && entry->querytime > timeouttime ) { continue; } - if( entry->querycounter != (unsigned) net_slist_maxtries.integer ) + if( entry->querycounter != (unsigned) net_slist_maxtries.integer ) { - lhnetaddress_t address; + lhnetaddress_t address; int socket; LHNETADDRESS_FromString(&address, entry->info.cname, 0); - if (entry->protocol == PROTOCOL_QUAKEWORLD) + if (entry->protocol == PROTOCOL_QUAKEWORLD) { - for (socket = 0; socket < cl_numsockets ; socket++) + for (socket = 0; socket < cl_numsockets ; socket++) NetConn_WriteString(cl_sockets[socket], "\377\377\377\377status\n", &address); } else { - for (socket = 0; socket < cl_numsockets ; socket++) + for (socket = 0; socket < cl_numsockets ; socket++) NetConn_WriteString(cl_sockets[socket], "\377\377\377\377getinfo", &address); } - //entry->querytime = realtime; - entry->querytime = Sys_DoubleTime(); + // update the entry fields + entry->querytime = realtime; entry->querycounter++; // if not in the slist menu we should print the server to console @@ -1602,6 +1729,12 @@ void NetConn_QueryQueueFrame(void) } else { + // have we tried to refresh this server? + if( entry->query == SQS_REFRESHING ) { + // yes, so update the reply count (since its not responding anymore) + serverreplycount--; + ServerList_ViewList_Remove(entry); + } entry->query = SQS_TIMEDOUT; } } @@ -1641,7 +1774,7 @@ void NetConn_ClientFrame(void) while (cl_sockets[i] && (length = NetConn_Read(cl_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0) NetConn_ClientParsePacket(cl_sockets[i], readbuffer, length, &peeraddress); NetConn_QueryQueueFrame(); - if (cls.netcon && realtime > cls.netcon->timeout) + if (cls.netcon && realtime > cls.netcon->timeout && !sv.active) { Con_Print("Connection timed out\n"); CL_Disconnect(); @@ -1675,24 +1808,30 @@ static void NetConn_BuildChallengeString(char *buffer, int bufferlength) static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg, size_t out_size, qboolean fullstatus) { - unsigned int nb_clients = 0, i; + unsigned int nb_clients = 0, nb_bots = 0, i; int length; // How many clients are there? for (i = 0;i < (unsigned int)svs.maxclients;i++) + { if (svs.clients[i].active) + { nb_clients++; + if (!svs.clients[i].netconnection) + nb_bots++; + } + } // TODO: we should add more information for the full status string length = dpsnprintf(out_msg, out_size, "\377\377\377\377%s\x0A" "\\gamename\\%s\\modname\\%s\\gameversion\\%d\\sv_maxclients\\%d" - "\\clients\\%d\\mapname\\%s\\hostname\\%s\\protocol\\%d" + "\\clients\\%d\\bots\\%d\\mapname\\%s\\hostname\\%s\\protocol\\%d" "%s%s" "%s", fullstatus ? "statusResponse" : "infoResponse", gamename, com_modname, gameversion.integer, svs.maxclients, - nb_clients, sv.name, hostname.string, NET_PROTOCOL_VERSION, + nb_clients, nb_bots, sv.name, hostname.string, NET_PROTOCOL_VERSION, challenge ? "\\challenge\\" : "", challenge ? challenge : "", fullstatus ? "\n" : ""); @@ -1818,6 +1957,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat double besttime; client_t *client; char *s, *string, response[1400], addressstring2[128], stringbuf[16384]; + qboolean islocal = (LHNETADDRESS_GetAddressType(peeraddress) == LHNETADDRESSTYPE_LOOP); if (!sv.active) return false; @@ -1851,7 +1991,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat Com_HexDumpToConsole(data, length); } - if (length >= 12 && !memcmp(string, "getchallenge", 12) && sv_public.integer > -2) + if (length >= 12 && !memcmp(string, "getchallenge", 12) && (islocal || sv_public.integer > -2)) { for (i = 0, best = 0, besttime = realtime;i < MAX_CHALLENGES;i++) { @@ -1873,7 +2013,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat NetConn_WriteString(mysocket, va("\377\377\377\377challenge %s", challenge[i].string), peeraddress); return true; } - if (length > 8 && !memcmp(string, "connect\\", 8) && sv_public.integer > -2) + if (length > 8 && !memcmp(string, "connect\\", 8) && (islocal || sv_public.integer > -2)) { string += 7; length -= 7; @@ -1956,7 +2096,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat return true; } - if (length >= 7 && !memcmp(string, "getinfo", 7) && sv_public.integer > -1) + if (length >= 7 && !memcmp(string, "getinfo", 7) && (islocal || sv_public.integer > -1)) { const char *challenge = NULL; @@ -1972,7 +2112,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat } return true; } - if (length >= 9 && !memcmp(string, "getstatus", 9) && sv_public.integer > -1) + if (length >= 9 && !memcmp(string, "getstatus", 9) && (islocal || sv_public.integer > -1)) { const char *challenge = NULL; @@ -1992,18 +2132,36 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat { int i; char *s = string + 5; + char *endpos = string + length + 1; // one behind the NUL, so adding strlen+1 will eventually reach it char password[64]; for (i = 0;*s > ' ';s++) if (i < (int)sizeof(password) - 1) password[i++] = *s; + if(*s <= ' ' && s != endpos) // skip leading ugly space + ++s; password[i] = 0; if (password[0] > ' ' && !strcmp(rcon_password.string, password)) { // looks like a legitimate rcon command with the correct password - Con_Printf("server received rcon command from %s:\n%s\n", host_client ? host_client->name : addressstring2, s); + char *s_ptr = s; + Con_Printf("server received rcon command from %s:\n", host_client ? host_client->name : addressstring2); + while(s_ptr != endpos) + { + size_t l = strlen(s_ptr); + if(l) + Con_Printf(" %s;", s_ptr); + s_ptr += l + 1; + } + Con_Printf("\n"); rcon_redirect = true; rcon_redirect_bufferpos = 0; - Cmd_ExecuteString(s, src_command); + while(s != endpos) + { + size_t l = strlen(s); + if(l) + Cmd_ExecuteString(s, src_command); + s += l + 1; + } rcon_redirect_buffer[rcon_redirect_bufferpos] = 0; rcon_redirect = false; // print resulting text to client @@ -2046,22 +2204,28 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat // protocol // (this protects more modern protocols against being used for // 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)) + 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_NEHAHRABJP || sv.protocol == PROTOCOL_NEHAHRABJP2 || sv.protocol == PROTOCOL_NEHAHRABJP3 || 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) + if(!islocal && 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); @@ -2157,7 +2321,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat case CCREQ_SERVER_INFO: if (developer.integer >= 10) Con_Printf("Datagram_ParseConnectionless: received CCREQ_SERVER_INFO from %s.\n", addressstring2); - if(sv_public.integer <= -1) + if(!islocal && sv_public.integer <= -1) break; if (sv.active && !strcmp(MSG_ReadString(), "QUAKE")) { @@ -2188,7 +2352,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat case CCREQ_PLAYER_INFO: if (developer.integer >= 10) Con_Printf("Datagram_ParseConnectionless: received CCREQ_PLAYER_INFO from %s.\n", addressstring2); - if(sv_public.integer <= -1) + if(!islocal && sv_public.integer <= -1) break; if (sv.active) { @@ -2209,7 +2373,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)); @@ -2221,7 +2385,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat case CCREQ_RULE_INFO: if (developer.integer >= 10) Con_Printf("Datagram_ParseConnectionless: received CCREQ_RULE_INFO from %s.\n", addressstring2); - if(sv_public.integer <= -1) + if(!islocal && sv_public.integer <= -1) break; if (sv.active) { @@ -2250,6 +2414,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 +2452,11 @@ void NetConn_ServerFrame(void) } } +void NetConn_SleepMicroseconds(int microseconds) +{ + LHNET_SleepUntilPacket_Microseconds(microseconds); +} + void NetConn_QueryMasters(qboolean querydp, qboolean queryqw) { int i; @@ -2442,6 +2612,16 @@ void Net_Stats_f(void) PrintStats(conn); } +void Net_Refresh_f(void) +{ + if (m_state != m_slist) { + Con_Print("Sending new requests to master servers\n"); + ServerList_QueryList(false, true, false, true); + Con_Print("Listening for replies...\n"); + } else + ServerList_QueryList(false, true, false, false); +} + void Net_Slist_f(void) { ServerList_ResetMasks(); @@ -2449,11 +2629,10 @@ void Net_Slist_f(void) serverlist_sortdescending = false; if (m_state != m_slist) { Con_Print("Sending requests to master servers\n"); - ServerList_QueryList(true, false); - serverlist_consoleoutput = true; + ServerList_QueryList(true, true, false, true); Con_Print("Listening for replies...\n"); } else - ServerList_QueryList(true, false); + ServerList_QueryList(true, true, false, false); } void Net_SlistQW_f(void) @@ -2463,11 +2642,11 @@ void Net_SlistQW_f(void) serverlist_sortdescending = false; if (m_state != m_slist) { Con_Print("Sending requests to master servers\n"); - ServerList_QueryList(false, true); + ServerList_QueryList(true, false, true, true); serverlist_consoleoutput = true; Con_Print("Listening for replies...\n"); } else - ServerList_QueryList(false, true); + ServerList_QueryList(true, false, true, false); } void NetConn_Init(void) @@ -2478,6 +2657,7 @@ void NetConn_Init(void) Cmd_AddCommand("net_stats", Net_Stats_f, "print network statistics"); Cmd_AddCommand("net_slist", Net_Slist_f, "query dp master servers and print all server information"); Cmd_AddCommand("net_slistqw", Net_SlistQW_f, "query qw master servers and print all server information"); + Cmd_AddCommand("net_refresh", Net_Refresh_f, "query dp master servers and refresh all server information"); Cmd_AddCommand("heartbeat", Net_Heartbeat_f, "send a heartbeat to the master server (updates your server information)"); Cvar_RegisterVariable(&net_slist_queriespersecond); Cvar_RegisterVariable(&net_slist_queriesperframe); @@ -2487,7 +2667,8 @@ void NetConn_Init(void) Cvar_RegisterVariable(&net_connecttimeout); Cvar_RegisterVariable(&net_connectfloodblockingtimeout); Cvar_RegisterVariable(&cl_netlocalping); - Cvar_RegisterVariable(&cl_netpacketloss); + Cvar_RegisterVariable(&cl_netpacketloss_send); + Cvar_RegisterVariable(&cl_netpacketloss_receive); Cvar_RegisterVariable(&hostname); Cvar_RegisterVariable(&developer_networking); Cvar_RegisterVariable(&cl_netport);