src/network/network_gamelist.cpp
author celestar
Mon, 19 Mar 2007 12:38:16 +0000
branchgamebalance
changeset 9895 7bd07f43b0e3
parent 6447 3b71e57fd22b
child 6743 cabfaa4a0295
permissions -rw-r--r--
(svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 1299
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 1299
diff changeset
     2
6447
3b71e57fd22b (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: 6168
diff changeset
     3
/**
3b71e57fd22b (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: 6168
diff changeset
     4
 * @file network_gamelist.cpp This file handles the GameList
3b71e57fd22b (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: 6168
diff changeset
     5
 * Also, it handles the request to a server for data about the server
3b71e57fd22b (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: 6168
diff changeset
     6
 */
3b71e57fd22b (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: 6168
diff changeset
     7
4826
63b1eb7c966b (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4077
diff changeset
     8
#ifdef ENABLE_NETWORK
63b1eb7c966b (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4077
diff changeset
     9
5720
cc0ceeafaa55 (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: 5568
diff changeset
    10
#include "../stdafx.h"
cc0ceeafaa55 (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: 5568
diff changeset
    11
#include "../debug.h"
cc0ceeafaa55 (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: 5568
diff changeset
    12
#include "../newgrf_config.h"
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    13
#include "../helpers.hpp"
6168
b587bfc4e44e (svn r8544) -Codechange: move game list related function/struct declarations to network_gamelist.h
rubidium
parents: 6166
diff changeset
    14
#include "core/game.h"
6148
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    15
#include "network_udp.h"
6168
b587bfc4e44e (svn r8544) -Codechange: move game list related function/struct declarations to network_gamelist.h
rubidium
parents: 6166
diff changeset
    16
#include "network_gamelist.h"
b587bfc4e44e (svn r8544) -Codechange: move game list related function/struct declarations to network_gamelist.h
rubidium
parents: 6166
diff changeset
    17
#include "network_gui.h"
b587bfc4e44e (svn r8544) -Codechange: move game list related function/struct declarations to network_gamelist.h
rubidium
parents: 6166
diff changeset
    18
b587bfc4e44e (svn r8544) -Codechange: move game list related function/struct declarations to network_gamelist.h
rubidium
parents: 6166
diff changeset
    19
NetworkGameList *_network_game_list = NULL;
6148
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    20
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    21
/** Should we stop/contiue requerying of offline servers? */
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    22
static bool _stop_requerying = false;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    23
2885
c06f2f3985a0 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    24
/** Add a new item to the linked gamelist. If the IP and Port match
c06f2f3985a0 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    25
 * return the existing item instead of adding it again
c06f2f3985a0 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    26
 * @param ip the IP-address (inet_addr) of the to-be added item
c06f2f3985a0 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    27
 * @param port the port the server is running on
c06f2f3985a0 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    28
 * @return a point to the newly added or already existing item */
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    29
NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    30
{
2881
1ffbbdbf1685 (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
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    32
2881
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    33
	prev_item = NULL;
1ffbbdbf1685 (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) {
1ffbbdbf1685 (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;
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    36
		prev_item = item;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    37
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    38
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
    39
	item = MallocT<NetworkGameList>(1);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    40
	memset(item, 0, sizeof(*item));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    41
	item->next = NULL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    42
	item->ip = ip;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    43
	item->port = port;
2881
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    44
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    45
	if (prev_item == NULL) {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    46
		_network_game_list = item;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    47
	} else {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    48
		prev_item->next = item;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    49
	}
5568
75f13d7bfaed (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
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    51
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    52
	UpdateNetworkGameWindow(false);
6148
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    53
	_stop_requerying = false;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    54
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    55
	return item;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    56
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    57
2885
c06f2f3985a0 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    58
/** Remove an item from the gamelist linked list
c06f2f3985a0 (svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents: 2881
diff changeset
    59
 * @param remove pointer to the item to be removed */
738
0b2fb79e64fc (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
void NetworkGameListRemoveItem(NetworkGameList *remove)
0b2fb79e64fc (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
    61
{
2881
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    62
	NetworkGameList *item, *prev_item;
738
0b2fb79e64fc (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
    63
2881
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    64
	prev_item = NULL;
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    65
	for (item = _network_game_list; item != NULL; item = item->next) {
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    66
		if (remove == item) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    67
			if (prev_item == NULL) {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    68
				_network_game_list = remove->next;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    69
			} else {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    70
				prev_item->next = remove->next;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 2885
diff changeset
    71
			}
738
0b2fb79e64fc (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
    72
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    73
			/* Remove GRFConfig information */
5347
18836f961808 (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5339
diff changeset
    74
			ClearGRFConfigList(&remove->info.grfconfig);
18836f961808 (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5339
diff changeset
    75
			free(remove);
18836f961808 (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5339
diff changeset
    76
			remove = NULL;
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    77
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5347
diff changeset
    78
			DEBUG(net, 4, "[gamelist] removed server from list");
2881
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    79
			UpdateNetworkGameWindow(false);
738
0b2fb79e64fc (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
			return;
0b2fb79e64fc (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
    81
		}
2881
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2186
diff changeset
    82
		prev_item = item;
738
0b2fb79e64fc (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
	}
0b2fb79e64fc (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
}
0b2fb79e64fc (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
    85
6148
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    86
enum {
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    87
	MAX_GAME_LIST_REQUERY_COUNT =  5,
6166
053fd8482b7d (svn r8542) -Codechange (Fix?): tweak the gamelist requery timeout so more servers are found for slow network connections.
rubidium
parents: 6148
diff changeset
    88
	REQUERY_EVERY_X_GAMELOOPS   = 60,
6148
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    89
};
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    90
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    91
/** Requeries the (game) servers we have not gotten a reply from */
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6447
diff changeset
    92
void NetworkGameListRequery()
6148
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    93
{
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    94
	static uint8 requery_cnt = 0;
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    95
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    96
	if (_stop_requerying || ++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    97
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    98
	requery_cnt = 0;
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
    99
	_stop_requerying = true;
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   100
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   101
	struct in_addr ip;
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   102
	NetworkGameList *item;
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   103
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   104
	for (item = _network_game_list; item != NULL; item = item->next) {
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   105
		if (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT) continue;
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   106
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   107
		ip.s_addr = item->ip;
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   108
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   109
		/* item gets mostly zeroed by NetworkUDPQueryServer */
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   110
		uint8 retries = item->retries;
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   111
		NetworkUDPQueryServer(inet_ntoa(ip), item->port);
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   112
		item->retries = retries + 1;
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   113
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   114
		_stop_requerying = false;
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   115
	}
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   116
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   117
}
a8cc2e7bf9b6 (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5860
diff changeset
   118
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   119
#endif /* ENABLE_NETWORK */