]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - netconn.c
-Fixed a minor bug in ResetMasks.
[xonotic/darkplaces.git] / netconn.c
index d6ba818b7f65b8ebf035f7b044b8c5060560c611..e2d34910f8026cec73edfff270c9214ddcc66ac2 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -89,7 +89,9 @@ cvar_t net_address = {0, "net_address", "0.0.0.0"};
 //cvar_t net_netaddress_ipv6 = {0, "net_address_ipv6", "[0:0:0:0:0:0:0:0]"};
 
 // HostCache interface
-hostcache_mask_t               hostcache_currentmask;
+hostcache_mask_t                       hostcache_andmasks[HOSTCACHE_ANDMASKCOUNT];
+hostcache_mask_t                       hostcache_ormasks[HOSTCACHE_ORMASKCOUNT];
+
 hostcache_infofield_t  hostcache_sortbyfield;
 qboolean                               hostcache_sortdescending;
 
@@ -103,18 +105,18 @@ qboolean  hostcache_consoleoutput;
 
 // helper function to insert a value into the viewset
 // spare entries will be removed
-static void _HostCache_VS_Insert( int index, hostcache_t *entry )
+static void _HostCache_ViewSet_InsertBefore( int index, hostcache_t *entry )
 {
-    int start;
-       for( start = index, index = hostcache_viewcount; index > start; index-- )
-               hostcache_viewset[index] = hostcache_viewset[index - 1];
+    int i;
        if( ++hostcache_viewcount > HOSTCACHE_VIEWCACHESIZE )
                hostcache_viewcount = HOSTCACHE_VIEWCACHESIZE;
-       hostcache_viewset[start] = entry;
+       for( i = hostcache_viewcount - 1; i > index; i-- )
+               hostcache_viewset[i] = hostcache_viewset[i - 1];
+       hostcache_viewset[index] = entry;
 }
 
 // we suppose hostcache_viewcount to be valid, ie > 0
-static void _HostCache_VS_Remove( int index )
+static void _HostCache_ViewSet_Remove( int index )
 {
        for( --hostcache_viewcount; index < hostcache_viewcount; index++ )
                hostcache_viewset[index] = hostcache_viewset[index + 1];
@@ -144,78 +146,93 @@ static qboolean _HostCache_SortTest( hostcache_t *A, hostcache_t *B )
        else if( hostcache_sortbyfield == HCIF_NAME )
                result = strcmp( B->info.name, A->info.name );
        
-       if( result > 0 && hostcache_sortdescending)
-               return true;
-       return false;
+       if( hostcache_sortdescending )
+               return result > 0;
+       return result < 0;
 }
 
-static qboolean _hc_testint( int A, hostcache_infofield_t op, int B )
+static qboolean _hc_testint( int A, hostcache_maskop_t op, int B )
 {
-       int diff;
-
-       diff = A - B;
-       switch( op ) {
-                       case HCMO_GREATER:
-                               if( !diff )
-                                       return false;
-                       case HCMO_GREATEREQUAL:
-                               if( diff < 0 )
-                                       return false;
-                               break;
-                       case HCMO_EQUAL:
-                               if( diff )
-                                       return false;
-                               break;
-                       case HCMO_LESS:
-                               if( !diff )
-                                       return false;
-                       case HCMO_LESSEQUAL:
-                               if( diff > 0 )
-                                       return false;
-                               break;
-       }
-       return true;
+       if( op == HCMO_LESS )
+               return A < B;
+       else if( op == HCMO_LESSEQUAL )
+               return A <= B;
+       else if( op == HCMO_EQUAL )
+               return A == B;
+       else if( op == HCMO_GREATER )
+               return A > B;
+       else if( op == HCMO_NOTEQUAL )
+               return A != B;
+       else // HCMO_GREATEREQUAL
+               return A >= B;
+}
+
+static qboolean _hc_teststr( const char *A, hostcache_maskop_t op, const char *B )
+{
+       if( op == HCMO_CONTAINS ) // A info B mask
+               return *A && !!strstr( B, A ); // we want a real bool
+       else if( op == HCMO_NOTCONTAIN )
+               return !*A || !strstr( B, A );
+       else if( op == HCMO_LESS )
+               return strcmp( A, B ) < 0;
+       else if( op == HCMO_LESSEQUAL )
+               return strcmp( A, B ) <= 0;
+       else if( op == HCMO_EQUAL )
+               return strcmp( A, B ) == 0;
+       else if( op == HCMO_GREATER )
+               return strcmp( A, B ) > 0;      
+       else if( op == HCMO_NOTEQUAL )
+               return strcmp( A, B ) != 0;
+       else // HCMO_GREATEREQUAL
+               return strcmp( A, B ) >= 0;
 }
 
-static qboolean _HostCache_TestMask( hostcache_info_t *info )
+static qboolean _HostCache_TestMask( hostcache_mask_t *mask, hostcache_info_t *info )
 {
-       if( !_hc_testint( info->ping, hostcache_currentmask.pingtest, hostcache_currentmask.info.ping ) )
+       if( !_hc_testint( info->ping, mask->tests[HCIF_PING], mask->info.ping ) )
                return false;
-       if( !_hc_testint( info->maxplayers, hostcache_currentmask.maxplayerstest, hostcache_currentmask.info.maxplayers ) )
+       if( !_hc_testint( info->maxplayers, mask->tests[HCIF_MAXPLAYERS], mask->info.maxplayers ) )
                return false;
-       if( !_hc_testint( info->numplayers, hostcache_currentmask.numplayerstest, hostcache_currentmask.info.numplayers ) )
+       if( !_hc_testint( info->numplayers, mask->tests[HCIF_NUMPLAYERS], mask->info.numplayers ) )
                return false;
-       if( !_hc_testint( info->protocol, hostcache_currentmask.protocoltest, hostcache_currentmask.info.protocol ))
+       if( !_hc_testint( info->protocol, mask->tests[HCIF_PROTOCOL], mask->info.protocol ))
                return false;
-       if( *hostcache_currentmask.info.cname
-               && !strstr( info->cname, hostcache_currentmask.info.cname ) )
+       if( *mask->info.cname
+               && !_hc_teststr( info->cname, mask->tests[HCIF_CNAME], mask->info.cname ) )
                return false;
-       if( *hostcache_currentmask.info.game
-               && !strstr( info->game, hostcache_currentmask.info.game ) )
+       if( *mask->info.game
+               && !_hc_teststr( info->game, mask->tests[HCIF_GAME], mask->info.game ) )
                return false;
-       if( *hostcache_currentmask.info.mod
-               && !strstr( info->mod, hostcache_currentmask.info.mod ) )
+       if( *mask->info.mod
+               && !_hc_teststr( info->mod, mask->tests[HCIF_MOD], mask->info.mod ) )
                return false;
-       if( *hostcache_currentmask.info.map
-               && !strstr( info->map, hostcache_currentmask.info.map ) )
+       if( *mask->info.map
+               && !_hc_teststr( info->map, mask->tests[HCIF_MAP], mask->info.map ) )
                return false;
-       if( *hostcache_currentmask.info.name
-               && !strstr( info->name, hostcache_currentmask.info.name ) )
+       if( *mask->info.name
+               && !_hc_teststr( info->name, mask->tests[HCIF_NAME], mask->info.name ) )
                return false;
        return true;
 }
 
 static void _HostCache_Insert( hostcache_t *entry )
 {
-       int start, end, size;
+       int start, end, mid;
        if( hostcache_viewcount == HOSTCACHE_VIEWCACHESIZE )
                return;
-       // now check whether it passes through mask
-       if( !_HostCache_TestMask( &entry->info ) )
+       // now check whether it passes through the masks mask
+       for( start = 0 ; hostcache_andmasks[start].active && start < HOSTCACHE_ANDMASKCOUNT ; start++ ) 
+               if( !_HostCache_TestMask( &hostcache_andmasks[start], &entry->info ) )
+                       return;
+
+       for( start = 0 ; hostcache_ormasks[start].active && start < HOSTCACHE_ORMASKCOUNT ; start++ )
+               if( _HostCache_TestMask( &hostcache_ormasks[start], &entry->info ) )
+                       break;
+       if( start == HOSTCACHE_ORMASKCOUNT || (start > 0 && !hostcache_ormasks[start].active) )
                return;
 
        if( !hostcache_viewcount ) {
-               _HostCache_VS_Insert( 0, entry );
+               _HostCache_ViewSet_InsertBefore( 0, entry );
                return;
        }
        // ok, insert it, we just need to find out where exactly:
@@ -223,24 +240,40 @@ static void _HostCache_Insert( hostcache_t *entry )
        // two special cases
        // check whether to insert it as new first item
        if( _HostCache_SortTest( entry, hostcache_viewset[0] ) ) {
-               _HostCache_VS_Insert( 0, entry );
+               _HostCache_ViewSet_InsertBefore( 0, entry );
                return;
        } // check whether to insert it as new last item
        else if( !_HostCache_SortTest( entry, hostcache_viewset[hostcache_viewcount - 1] ) ) {
-               _HostCache_VS_Insert( hostcache_viewcount, entry );
+               _HostCache_ViewSet_InsertBefore( hostcache_viewcount, entry );
                return;
        }
-       start = 1;
+       start = 0;
        end = hostcache_viewcount - 1;
-       while( (size = start - end) > 0 )
+       while( end > start + 1 )
+       {
+               mid = (start + end) / 2;
                // test the item that lies in the middle between start and end
-               if( _HostCache_SortTest( entry, hostcache_viewset[(start + end) / 2] ) )
+               if( _HostCache_SortTest( entry, hostcache_viewset[mid] ) )
                        // the item has to be in the upper half
-                       end = (start + end) / 2 - 1;
+                       end = mid;
                else 
                        // the item has to be in the lower half
-                       start = (start + end) / 2 + 1;
-       _HostCache_VS_Insert( start + 1, entry );
+                       start = mid;
+       }
+       _HostCache_ViewSet_InsertBefore( start + 1, entry );
+}
+
+static void _HostCache_Remove( hostcache_t *entry )
+{
+       int i;
+       for( i = 0; i < hostcache_viewcount; i++ )
+       {
+               if (hostcache_viewset[i] == entry)
+               {
+                       _HostCache_ViewSet_Remove(i);
+                       break;
+               }
+       }
 }
 
 void HostCache_RebuildViewSet(void)
@@ -253,9 +286,10 @@ void HostCache_RebuildViewSet(void)
                        _HostCache_Insert( &hostcache_cache[i] );
 }
 
-void HostCache_ResetMask(void)
+void HostCache_ResetMasks(void)
 {
-       memset( &hostcache_currentmask, 0, sizeof( hostcache_mask_t ) );
+       memset( &hostcache_andmasks, 0, sizeof( hostcache_andmasks ) );
+       memset( &hostcache_ormasks, 0, sizeof( hostcache_ormasks ) );
 }
 
 
@@ -270,8 +304,25 @@ void HostCache_QueryList(void)
        hostcache_viewcount = 0;
        hostcache_consoleoutput = false;
        NetConn_QueryMasters();
+       
+       //_HostCache_PingTest(); 
 }
 
+#if 0
+static void _HostCache_PingTest(void)
+{
+       int i;
+       for( i = 0 ; i < 50 ; i++ ) {
+               memset( &hostcache_cache[hostcache_cachecount], 0, sizeof( hostcache_t ) );
+               hostcache_cache[hostcache_cachecount].info.ping = rand() % 450;
+               hostcache_cache[hostcache_cachecount].finished = true;
+               sprintf( hostcache_cache[hostcache_cachecount].line1, "%i", hostcache_cache[hostcache_cachecount].info.ping );
+               _HostCache_Insert( &hostcache_cache[hostcache_cachecount] );
+               hostcache_cachecount++;
+       }
+}
+#endif
+
 // rest
 
 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress)
@@ -864,10 +915,9 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                        // hostcache only uses text addresses
                        LHNETADDRESS_ToString(peeraddress, cname, sizeof(cname), true);
                        // search the cache for this server and update it
-                       for( n = 0; n < hostcache_cachecount; n++ ) {
+                       for( n = 0; n < hostcache_cachecount; n++ )
                                if( !strcmp( cname, hostcache_cache[n].info.cname ) )
                                        break;
-                       }
                        if( n == hostcache_cachecount )
                                return true;
 
@@ -882,14 +932,14 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
 
                        if (info->ping == 100000)
                                        serverreplycount++;
-       
+
                        pingtime = (int)((realtime - hostcache_cache[n].querytime) * 1000.0);
                        pingtime = bound(0, pingtime, 9999);
                        // update the ping
                        info->ping = pingtime;
 
                        // legacy/old stuff move it to the menu ASAP
-                       
+
                        // build description strings for the things users care about
                        snprintf(hostcache_cache[n].line1, sizeof(hostcache_cache[n].line1), "%5d%c%3u/%3u %-65.65s", (int)pingtime, info->protocol != NET_PROTOCOL_VERSION ? '*' : ' ', info->numplayers, info->maxplayers, info->name);
                        snprintf(hostcache_cache[n].line2, sizeof(hostcache_cache[n].line2), "%-21.21s %-19.19s %-17.17s %-20.20s", info->cname, info->game, info->mod, info->map);
@@ -912,13 +962,11 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                        if( hostcache_consoleoutput )
                                Con_Printf("%s\n%s\n", hostcache_cache[n].line1, hostcache_cache[n].line2);
                        // and finally, update the view set
-                       if( hostcache_cache[n].finished ) {
-                _HostCache_VS_Remove( n );
-                               _HostCache_Insert( &hostcache_cache[n] );
-                       } else
-                               _HostCache_Insert( &hostcache_cache[n] );
+                       if( hostcache_cache[n].finished )
+                _HostCache_Remove( &hostcache_cache[n] );
+                       _HostCache_Insert( &hostcache_cache[n] );
                        hostcache_cache[n].finished = true;
-       
+
                        return true;
                }
                if (!strncmp(string, "getserversResponse\\", 19) && hostcache_cachecount < HOSTCACHE_TOTALSIZE)
@@ -936,13 +984,13 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                snprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[1], data[2], data[3], data[4], (data[5] << 8) | data[6]);
                                if (developer.integer)
                                        Con_Printf("Requesting info from server %s\n", ipstring);                               
+                               // ignore the rest of the message if the hostcache is full
                                if( hostcache_cachecount == HOSTCACHE_TOTALSIZE )
-                                               // ignore the rest of the message since there wont magically disappear entries
-                                               break;
-                                               
+                                       break;
+
                                LHNETADDRESS_FromString(&svaddress, ipstring, 0);
                                NetConn_WriteString(mysocket, "\377\377\377\377getinfo", &svaddress);
-                
+
                                memset(&hostcache_cache[hostcache_cachecount], 0, sizeof(hostcache_cache[hostcache_cachecount]));
                                // store the data the engine cares about (address and ping)
                                strlcpy (hostcache_cache[hostcache_cachecount].info.cname, ipstring, sizeof (hostcache_cache[hostcache_cachecount].info.cname));
@@ -953,7 +1001,7 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                        Con_Printf("querying %s\n", ipstring);
 
                                ++hostcache_cachecount;
-                               
+
                                // move on to next address in packet
                                data += 7;
                                length -= 7;
@@ -1684,7 +1732,7 @@ void Net_Stats_f(void)
 
 void Net_Slist_f(void)
 {
-       HostCache_ResetMask();
+       HostCache_ResetMasks();
        hostcache_sortbyfield = HCIF_PING;
        hostcache_sortdescending = false;
     if (m_state != m_slist) {