# network_gamelist.cpp

Go to the documentation of this file.

    00001 /* $Id: network_gamelist.cpp 15299 2009-01-31 20:16:06Z smatz $ */
    00002
    00008 #ifdef ENABLE_NETWORK
    00009
    00010 #include "../stdafx.h"
    00011 #include "../debug.h"
    00012 #include "../core/alloc_func.hpp"
    00013 #include "../thread.h"
    00014 #include "../string_func.h"
    00015 #include "network_internal.h"
    00016 #include "network_udp.h"
    00017 #include "network_gamelist.h"
    00018
    00019 NetworkGameList *_network_game_list = NULL;
    00020
    00021 ThreadMutex *_network_game_list_mutex = ThreadMutex::New();
    00022 NetworkGameList *_network_game_delayed_insertion_list = NULL;
    00023
    00028 void NetworkGameListAddItemDelayed(NetworkGameList *item)
    00029 {
    00030   _network_game_list_mutex->BeginCritical();
    00031   item->next = _network_game_delayed_insertion_list;
    00032   _network_game_delayed_insertion_list = item;
    00033   _network_game_list_mutex->EndCritical();
    00034 }
    00035
    00037 static void NetworkGameListHandleDelayedInsert()
    00038 {
    00039   _network_game_list_mutex->BeginCritical();
    00040   while (_network_game_delayed_insertion_list != NULL) {
    00041     NetworkGameList *ins_item = _network_game_delayed_insertion_list;
    00042     _network_game_delayed_insertion_list = ins_item->next;
    00043
    00044     NetworkGameList *item = NetworkGameListAddItem(ins_item->ip, ins_item->port);
    00045
    00046     if (item != NULL) {
    00047       if (StrEmpty(item->info.server_name)) {
    00048         memset(&item->info, 0, sizeof(item->info));
    00049         strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name));
    00050         strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
    00051         item->online = false;
    00052       }
    00053       item->manually = ins_item->manually;
    00054       UpdateNetworkGameWindow(false);
    00055     }
    00056     free(ins_item);
    00057   }
    00058   _network_game_list_mutex->EndCritical();
    00059 }
    00060
    00066 NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
    00067 {
    00068   if (ip == 0) return NULL;
    00069
    00070   NetworkGameList *item, *prev_item;
    00071
    00072   prev_item = NULL;
    00073   for (item = _network_game_list; item != NULL; item = item->next) {
    00074     if (item->ip == ip && item->port == port) return item;
    00075     prev_item = item;
    00076   }
    00077
    00078   item = CallocT<NetworkGameList>(1);
    00079   item->next = NULL;
    00080   item->ip = ip;
    00081   item->port = port;
    00082
    00083   if (prev_item == NULL) {
    00084     _network_game_list = item;
    00085   } else {
    00086     prev_item->next = item;
    00087   }
    00088   DEBUG(net, 4, "[gamelist] added server to list");
    00089
    00090   UpdateNetworkGameWindow(false);
    00091
    00092   return item;
    00093 }
    00094
    00097 void NetworkGameListRemoveItem(NetworkGameList *remove)
    00098 {
    00099   NetworkGameList *item, *prev_item;
    00100
    00101   prev_item = NULL;
    00102   for (item = _network_game_list; item != NULL; item = item->next) {
    00103     if (remove == item) {
    00104       if (prev_item == NULL) {
    00105         _network_game_list = remove->next;
    00106       } else {
    00107         prev_item->next = remove->next;
    00108       }
    00109
    00110       /* Remove GRFConfig information */
    00111       ClearGRFConfigList(&remove->info.grfconfig);
    00112       free(remove);
    00113       remove = NULL;
    00114
    00115       DEBUG(net, 4, "[gamelist] removed server from list");
    00116       UpdateNetworkGameWindow(false);
    00117       return;
    00118     }
    00119     prev_item = item;
    00120   }
    00121 }
    00122
    00123 enum {
    00124   MAX_GAME_LIST_REQUERY_COUNT  =  5,
    00125   REQUERY_EVERY_X_GAMELOOPS    = 60,
    00126   REFRESH_GAMEINFO_X_REQUERIES = 50,
    00127 };
    00128
    00130 void NetworkGameListRequery()
    00131 {
    00132   NetworkGameListHandleDelayedInsert();
    00133
    00134   static uint8 requery_cnt = 0;
    00135
    00136   if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
    00137   requery_cnt = 0;
    00138
    00139   for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
    00140     item->retries++;
    00141     if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;
    00142
    00143     /* item gets mostly zeroed by NetworkUDPQueryServer */
    00144     uint8 retries = item->retries;
    00145     NetworkUDPQueryServer(NetworkAddress(item->ip, item->port));
    00146     item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
    00147   }
    00148 }
    00149
    00150 #endif /* ENABLE_NETWORK */