# network_gamelist.cpp

Go to the documentation of this file.

    00001 /* $Id: network_gamelist.cpp 16120 2009-04-22 08:48:08Z rubidium $ */
    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       ClearGRFConfigList(&item->info.grfconfig);
    00048       if (StrEmpty(item->info.server_name)) {
    00049         memset(&item->info, 0, sizeof(item->info));
    00050         strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name));
    00051         strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
    00052         item->online = false;
    00053       }
    00054       item->manually = ins_item->manually;
    00055       UpdateNetworkGameWindow(false);
    00056     }
    00057     free(ins_item);
    00058   }
    00059   _network_game_list_mutex->EndCritical();
    00060 }
    00061
    00067 NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
    00068 {
    00069   if (ip == 0) return NULL;
    00070
    00071   NetworkGameList *item, *prev_item;
    00072
    00073   prev_item = NULL;
    00074   for (item = _network_game_list; item != NULL; item = item->next) {
    00075     if (item->ip == ip && item->port == port) return item;
    00076     prev_item = item;
    00077   }
    00078
    00079   item = CallocT<NetworkGameList>(1);
    00080   item->next = NULL;
    00081   item->ip = ip;
    00082   item->port = port;
    00083
    00084   if (prev_item == NULL) {
    00085     _network_game_list = item;
    00086   } else {
    00087     prev_item->next = item;
    00088   }
    00089   DEBUG(net, 4, "[gamelist] added server to list");
    00090
    00091   UpdateNetworkGameWindow(false);
    00092
    00093   return item;
    00094 }
    00095
    00098 void NetworkGameListRemoveItem(NetworkGameList *remove)
    00099 {
    00100   NetworkGameList *item, *prev_item;
    00101
    00102   prev_item = NULL;
    00103   for (item = _network_game_list; item != NULL; item = item->next) {
    00104     if (remove == item) {
    00105       if (prev_item == NULL) {
    00106         _network_game_list = remove->next;
    00107       } else {
    00108         prev_item->next = remove->next;
    00109       }
    00110
    00111       /* Remove GRFConfig information */
    00112       ClearGRFConfigList(&remove->info.grfconfig);
    00113       free(remove);
    00114       remove = NULL;
    00115
    00116       DEBUG(net, 4, "[gamelist] removed server from list");
    00117       UpdateNetworkGameWindow(false);
    00118       return;
    00119     }
    00120     prev_item = item;
    00121   }
    00122 }
    00123
    00124 enum {
    00125   MAX_GAME_LIST_REQUERY_COUNT  =  5,
    00126   REQUERY_EVERY_X_GAMELOOPS    = 60,
    00127   REFRESH_GAMEINFO_X_REQUERIES = 50,
    00128 };
    00129
    00131 void NetworkGameListRequery()
    00132 {
    00133   NetworkGameListHandleDelayedInsert();
    00134
    00135   static uint8 requery_cnt = 0;
    00136
    00137   if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
    00138   requery_cnt = 0;
    00139
    00140   for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
    00141     item->retries++;
    00142     if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;
    00143
    00144     /* item gets mostly zeroed by NetworkUDPQueryServer */
    00145     uint8 retries = item->retries;
    00146     NetworkUDPQueryServer(NetworkAddress(item->ip, item->port));
    00147     item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
    00148   }
    00149 }
    00150
    00151 #endif /* ENABLE_NETWORK */