# network_gamelist.cpp

Go to the documentation of this file.

    00001 /* $Id: network_gamelist.cpp 11691 2007-12-25 09:48:53Z rubidium $ */
    00002
    00008 #ifdef ENABLE_NETWORK
    00009
    00010 #include "../stdafx.h"
    00011 #include "../debug.h"
    00012 #include "../newgrf_config.h"
    00013 #include "../core/alloc_func.hpp"
    00014 #include "core/game.h"
    00015 #include "network_udp.h"
    00016 #include "network_gamelist.h"
    00017 #include "network_gui.h"
    00018
    00019 NetworkGameList *_network_game_list = NULL;
    00020
    00026 NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
    00027 {
    00028   NetworkGameList *item, *prev_item;
    00029
    00030   prev_item = NULL;
    00031   for (item = _network_game_list; item != NULL; item = item->next) {
    00032     if (item->ip == ip && item->port == port) return item;
    00033     prev_item = item;
    00034   }
    00035
    00036   item = MallocT<NetworkGameList>(1);
    00037   memset(item, 0, sizeof(*item));
    00038   item->next = NULL;
    00039   item->ip = ip;
    00040   item->port = port;
    00041
    00042   if (prev_item == NULL) {
    00043     _network_game_list = item;
    00044   } else {
    00045     prev_item->next = item;
    00046   }
    00047   DEBUG(net, 4, "[gamelist] added server to list");
    00048
    00049   UpdateNetworkGameWindow(false);
    00050
    00051   return item;
    00052 }
    00053
    00056 void NetworkGameListRemoveItem(NetworkGameList *remove)
    00057 {
    00058   NetworkGameList *item, *prev_item;
    00059
    00060   prev_item = NULL;
    00061   for (item = _network_game_list; item != NULL; item = item->next) {
    00062     if (remove == item) {
    00063       if (prev_item == NULL) {
    00064         _network_game_list = remove->next;
    00065       } else {
    00066         prev_item->next = remove->next;
    00067       }
    00068
    00069       /* Remove GRFConfig information */
    00070       ClearGRFConfigList(&remove->info.grfconfig);
    00071       free(remove);
    00072       remove = NULL;
    00073
    00074       DEBUG(net, 4, "[gamelist] removed server from list");
    00075       UpdateNetworkGameWindow(false);
    00076       return;
    00077     }
    00078     prev_item = item;
    00079   }
    00080 }
    00081
    00082 enum {
    00083   MAX_GAME_LIST_REQUERY_COUNT  =  5,
    00084   REQUERY_EVERY_X_GAMELOOPS    = 60,
    00085   REFRESH_GAMEINFO_X_REQUERIES = 50,
    00086 };
    00087
    00089 void NetworkGameListRequery()
    00090 {
    00091   static uint8 requery_cnt = 0;
    00092
    00093   if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
    00094   requery_cnt = 0;
    00095
    00096   struct in_addr ip;
    00097   NetworkGameList *item;
    00098
    00099   for (item = _network_game_list; item != NULL; item = item->next) {
    00100     item->retries++;
    00101     if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;
    00102
    00103     ip.s_addr = item->ip;
    00104
    00105     /* item gets mostly zeroed by NetworkUDPQueryServer */
    00106     uint8 retries = item->retries;
    00107     NetworkUDPQueryServer(inet_ntoa(ip), item->port);
    00108     item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
    00109   }
    00110 }
    00111
    00112 #endif /* ENABLE_NETWORK */