src/network/network_gamelist.cpp
author peter1138
Sun, 03 Aug 2008 17:35:08 +0000
changeset 9834 7474149531c1
parent 9428 1ba05b499957
permissions -rw-r--r--
(svn r13977) -Codechange: Let ResizeWindowForWidget() handle hidden (zero height or zero width) widgets.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1299
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1299
diff changeset
     2
6121
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5917
diff changeset
     3
/**
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5917
diff changeset
     4
 * @file network_gamelist.cpp This file handles the GameList
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5917
diff changeset
     5
 * Also, it handles the request to a server for data about the server
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5917
diff changeset
     6
 */
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5917
diff changeset
     7
4826
6a545d194528 (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4077
diff changeset
     8
#ifdef ENABLE_NETWORK
6a545d194528 (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4077
diff changeset
     9
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    10
#include "../stdafx.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    11
#include "../debug.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    12
#include "../newgrf_config.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 7331
diff changeset
    13
#include "../core/alloc_func.hpp"
9428
1ba05b499957 (svn r13343) -Codechange: reorder/move variable/functions in the network headers so that nothing from the network directory needs to include basically all network headers.
rubidium
parents: 8937
diff changeset
    14
#include "network_internal.h"
5917
b0a9e5f20dcc (svn r8544) -Codechange: move game list related function/struct declarations to network_gamelist.h
rubidium
parents: 5915
diff changeset
    15
#include "core/game.h"
5897
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    16
#include "network_udp.h"
5917
b0a9e5f20dcc (svn r8544) -Codechange: move game list related function/struct declarations to network_gamelist.h
rubidium
parents: 5915
diff changeset
    17
#include "network_gamelist.h"
b0a9e5f20dcc (svn r8544) -Codechange: move game list related function/struct declarations to network_gamelist.h
rubidium
parents: 5915
diff changeset
    18
#include "network_gui.h"
b0a9e5f20dcc (svn r8544) -Codechange: move game list related function/struct declarations to network_gamelist.h
rubidium
parents: 5915
diff changeset
    19
b0a9e5f20dcc (svn r8544) -Codechange: move game list related function/struct declarations to network_gamelist.h
rubidium
parents: 5915
diff changeset
    20
NetworkGameList *_network_game_list = NULL;
5897
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    21
2885
fc5297214b36 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    22
/** Add a new item to the linked gamelist. If the IP and Port match
fc5297214b36 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    23
 * return the existing item instead of adding it again
fc5297214b36 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    24
 * @param ip the IP-address (inet_addr) of the to-be added item
fc5297214b36 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    25
 * @param port the port the server is running on
fc5297214b36 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    26
 * @return a point to the newly added or already existing item */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    27
NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    28
{
8937
9a4da442dd50 (svn r12709) -Feature: show the last joined server. Heavily based on a patch by Yexo.
rubidium
parents: 8130
diff changeset
    29
	if (ip == 0) return NULL;
9a4da442dd50 (svn r12709) -Feature: show the last joined server. Heavily based on a patch by Yexo.
rubidium
parents: 8130
diff changeset
    30
2881
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    31
	NetworkGameList *item, *prev_item;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    32
2881
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    33
	prev_item = NULL;
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    34
	for (item = _network_game_list; item != NULL; item = item->next) {
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    35
		if (item->ip == ip && item->port == port) return item;
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    36
		prev_item = item;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    37
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    38
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
    39
	item = MallocT<NetworkGameList>(1);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    40
	memset(item, 0, sizeof(*item));
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    41
	item->next = NULL;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    42
	item->ip = ip;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    43
	item->port = port;
2881
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    44
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    45
	if (prev_item == NULL) {
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    46
		_network_game_list = item;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    47
	} else {
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    48
		prev_item->next = item;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    49
	}
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5347
diff changeset
    50
	DEBUG(net, 4, "[gamelist] added server to list");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    51
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    52
	UpdateNetworkGameWindow(false);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    53
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    54
	return item;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    55
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    56
2885
fc5297214b36 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    57
/** Remove an item from the gamelist linked list
fc5297214b36 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    58
 * @param remove pointer to the item to be removed */
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
    59
void NetworkGameListRemoveItem(NetworkGameList *remove)
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
    60
{
2881
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    61
	NetworkGameList *item, *prev_item;
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
    62
2881
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    63
	prev_item = NULL;
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    64
	for (item = _network_game_list; item != NULL; item = item->next) {
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    65
		if (remove == item) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    66
			if (prev_item == NULL) {
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    67
				_network_game_list = remove->next;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    68
			} else {
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    69
				prev_item->next = remove->next;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    70
			}
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
    71
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    72
			/* Remove GRFConfig information */
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5339
diff changeset
    73
			ClearGRFConfigList(&remove->info.grfconfig);
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5339
diff changeset
    74
			free(remove);
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5339
diff changeset
    75
			remove = NULL;
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    76
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5347
diff changeset
    77
			DEBUG(net, 4, "[gamelist] removed server from list");
2881
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    78
			UpdateNetworkGameWindow(false);
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
    79
			return;
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
    80
		}
2881
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    81
		prev_item = item;
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
    82
	}
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
    83
}
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
    84
5897
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    85
enum {
7331
7e3503b13496 (svn r10694) -Codechange: update the server information in the game list every once in a while so the information stays fairly up-to-date when you are looking through the servers.
rubidium
parents: 6247
diff changeset
    86
	MAX_GAME_LIST_REQUERY_COUNT  =  5, ///< How often do we requery in number of times per server?
7e3503b13496 (svn r10694) -Codechange: update the server information in the game list every once in a while so the information stays fairly up-to-date when you are looking through the servers.
rubidium
parents: 6247
diff changeset
    87
	REQUERY_EVERY_X_GAMELOOPS    = 60, ///< How often do we requery in time?
7e3503b13496 (svn r10694) -Codechange: update the server information in the game list every once in a while so the information stays fairly up-to-date when you are looking through the servers.
rubidium
parents: 6247
diff changeset
    88
	REFRESH_GAMEINFO_X_REQUERIES = 50, ///< Refresh the game info itself after REFRESH_GAMEINFO_X_REQUERIES * REQUERY_EVERY_X_GAMELOOPS game loops
5897
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    89
};
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    90
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    91
/** Requeries the (game) servers we have not gotten a reply from */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6121
diff changeset
    92
void NetworkGameListRequery()
5897
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    93
{
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    94
	static uint8 requery_cnt = 0;
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    95
7331
7e3503b13496 (svn r10694) -Codechange: update the server information in the game list every once in a while so the information stays fairly up-to-date when you are looking through the servers.
rubidium
parents: 6247
diff changeset
    96
	if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
5897
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    97
	requery_cnt = 0;
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    98
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
    99
	struct in_addr ip;
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   100
	NetworkGameList *item;
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   101
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   102
	for (item = _network_game_list; item != NULL; item = item->next) {
7331
7e3503b13496 (svn r10694) -Codechange: update the server information in the game list every once in a while so the information stays fairly up-to-date when you are looking through the servers.
rubidium
parents: 6247
diff changeset
   103
		item->retries++;
7e3503b13496 (svn r10694) -Codechange: update the server information in the game list every once in a while so the information stays fairly up-to-date when you are looking through the servers.
rubidium
parents: 6247
diff changeset
   104
		if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;
5897
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   105
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   106
		ip.s_addr = item->ip;
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   107
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   108
		/* item gets mostly zeroed by NetworkUDPQueryServer */
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   109
		uint8 retries = item->retries;
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   110
		NetworkUDPQueryServer(inet_ntoa(ip), item->port);
7331
7e3503b13496 (svn r10694) -Codechange: update the server information in the game list every once in a while so the information stays fairly up-to-date when you are looking through the servers.
rubidium
parents: 6247
diff changeset
   111
		item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
5897
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   112
	}
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   113
}
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5609
diff changeset
   114
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   115
#endif /* ENABLE_NETWORK */