2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-02-23 08:37:33 +00:00
|
|
|
/**
|
|
|
|
* @file network_gamelist.cpp This file handles the GameList
|
|
|
|
* Also, it handles the request to a server for data about the server
|
|
|
|
*/
|
|
|
|
|
2006-10-12 14:13:39 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../debug.h"
|
|
|
|
#include "../newgrf_config.h"
|
2007-01-10 18:56:51 +00:00
|
|
|
#include "../helpers.hpp"
|
2007-02-02 21:32:58 +00:00
|
|
|
#include "core/game.h"
|
2007-02-01 21:04:40 +00:00
|
|
|
#include "network_udp.h"
|
2007-02-02 21:32:58 +00:00
|
|
|
#include "network_gamelist.h"
|
|
|
|
#include "network_gui.h"
|
|
|
|
|
|
|
|
NetworkGameList *_network_game_list = NULL;
|
2007-02-01 21:04:40 +00:00
|
|
|
|
|
|
|
/** Should we stop/contiue requerying of offline servers? */
|
|
|
|
static bool _stop_requerying = false;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2006-01-26 13:01:53 +00:00
|
|
|
/** Add a new item to the linked gamelist. If the IP and Port match
|
|
|
|
* return the existing item instead of adding it again
|
|
|
|
* @param ip the IP-address (inet_addr) of the to-be added item
|
|
|
|
* @param port the port the server is running on
|
|
|
|
* @return a point to the newly added or already existing item */
|
2004-12-04 17:54:56 +00:00
|
|
|
NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
|
|
|
|
{
|
2006-01-25 19:03:50 +00:00
|
|
|
NetworkGameList *item, *prev_item;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2006-01-25 19:03:50 +00:00
|
|
|
prev_item = NULL;
|
|
|
|
for (item = _network_game_list; item != NULL; item = item->next) {
|
|
|
|
if (item->ip == ip && item->port == port) return item;
|
|
|
|
prev_item = item;
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
2007-01-11 17:29:39 +00:00
|
|
|
item = MallocT<NetworkGameList>(1);
|
2004-12-04 17:54:56 +00:00
|
|
|
memset(item, 0, sizeof(*item));
|
|
|
|
item->next = NULL;
|
|
|
|
item->ip = ip;
|
|
|
|
item->port = port;
|
2006-01-25 19:03:50 +00:00
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
if (prev_item == NULL) {
|
|
|
|
_network_game_list = item;
|
|
|
|
} else {
|
|
|
|
prev_item->next = item;
|
|
|
|
}
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(net, 4, "[gamelist] added server to list");
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
UpdateNetworkGameWindow(false);
|
2007-02-01 21:04:40 +00:00
|
|
|
_stop_requerying = false;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2006-01-26 13:01:53 +00:00
|
|
|
/** Remove an item from the gamelist linked list
|
|
|
|
* @param remove pointer to the item to be removed */
|
2004-12-20 22:14:39 +00:00
|
|
|
void NetworkGameListRemoveItem(NetworkGameList *remove)
|
|
|
|
{
|
2006-01-25 19:03:50 +00:00
|
|
|
NetworkGameList *item, *prev_item;
|
2004-12-20 22:14:39 +00:00
|
|
|
|
2006-01-25 19:03:50 +00:00
|
|
|
prev_item = NULL;
|
|
|
|
for (item = _network_game_list; item != NULL; item = item->next) {
|
|
|
|
if (remove == item) {
|
2006-06-27 21:25:53 +00:00
|
|
|
if (prev_item == NULL) {
|
|
|
|
_network_game_list = remove->next;
|
|
|
|
} else {
|
|
|
|
prev_item->next = remove->next;
|
|
|
|
}
|
2004-12-20 22:14:39 +00:00
|
|
|
|
2006-12-18 12:26:55 +00:00
|
|
|
/* Remove GRFConfig information */
|
2006-12-20 21:17:33 +00:00
|
|
|
ClearGRFConfigList(&remove->info.grfconfig);
|
2004-12-20 22:14:39 +00:00
|
|
|
free(remove);
|
2006-12-20 21:17:33 +00:00
|
|
|
remove = NULL;
|
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(net, 4, "[gamelist] removed server from list");
|
2006-01-25 19:03:50 +00:00
|
|
|
UpdateNetworkGameWindow(false);
|
2004-12-20 22:14:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-01-25 19:03:50 +00:00
|
|
|
prev_item = item;
|
2004-12-20 22:14:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-01 21:04:40 +00:00
|
|
|
enum {
|
|
|
|
MAX_GAME_LIST_REQUERY_COUNT = 5,
|
2007-02-02 20:57:03 +00:00
|
|
|
REQUERY_EVERY_X_GAMELOOPS = 60,
|
2007-02-01 21:04:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Requeries the (game) servers we have not gotten a reply from */
|
|
|
|
void NetworkGameListRequery(void)
|
|
|
|
{
|
|
|
|
static uint8 requery_cnt = 0;
|
|
|
|
|
|
|
|
if (_stop_requerying || ++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
|
|
|
|
|
|
|
|
requery_cnt = 0;
|
|
|
|
_stop_requerying = true;
|
|
|
|
|
|
|
|
struct in_addr ip;
|
|
|
|
NetworkGameList *item;
|
|
|
|
|
|
|
|
for (item = _network_game_list; item != NULL; item = item->next) {
|
|
|
|
if (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT) continue;
|
|
|
|
|
|
|
|
ip.s_addr = item->ip;
|
|
|
|
|
|
|
|
/* item gets mostly zeroed by NetworkUDPQueryServer */
|
|
|
|
uint8 retries = item->retries;
|
|
|
|
NetworkUDPQueryServer(inet_ntoa(ip), item->port);
|
|
|
|
item->retries = retries + 1;
|
|
|
|
|
|
|
|
_stop_requerying = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|