author | convert-repo |
Mon, 07 Apr 2008 16:21:55 +0000 | |
changeset 10076 | dfd70e42c4ae |
parent 8626 | 440dfcd14c4a |
child 10178 | 8fde48e0ddac |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
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" |
8626
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
7827
diff
changeset
|
13 |
#include "../core/alloc_func.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 |
|
2885
c06f2f3985a0
(svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents:
2881
diff
changeset
|
21 |
/** 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
|
22 |
* 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
|
23 |
* @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
|
24 |
* @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
|
25 |
* @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
|
26 |
NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port) |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
27 |
{ |
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
28 |
NetworkGameList *item, *prev_item; |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
29 |
|
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
30 |
prev_item = NULL; |
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
31 |
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
|
32 |
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
|
33 |
prev_item = item; |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
34 |
} |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
35 |
|
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
|
36 |
item = MallocT<NetworkGameList>(1); |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
37 |
memset(item, 0, sizeof(*item)); |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
38 |
item->next = NULL; |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
39 |
item->ip = ip; |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
40 |
item->port = port; |
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
41 |
|
4077
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
42 |
if (prev_item == NULL) { |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
43 |
_network_game_list = item; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
44 |
} else { |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
45 |
prev_item->next = item; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
46 |
} |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5347
diff
changeset
|
47 |
DEBUG(net, 4, "[gamelist] added server to list"); |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
48 |
|
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
49 |
UpdateNetworkGameWindow(false); |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
50 |
|
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
51 |
return item; |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
52 |
} |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
53 |
|
2885
c06f2f3985a0
(svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents:
2881
diff
changeset
|
54 |
/** 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
|
55 |
* @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
|
56 |
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
|
57 |
{ |
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
58 |
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
|
59 |
|
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
60 |
prev_item = NULL; |
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
61 |
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
|
62 |
if (remove == item) { |
4077
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
63 |
if (prev_item == NULL) { |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
64 |
_network_game_list = remove->next; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
65 |
} else { |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
66 |
prev_item->next = remove->next; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
67 |
} |
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
|
68 |
|
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
|
69 |
/* Remove GRFConfig information */ |
5347
18836f961808
(svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents:
5339
diff
changeset
|
70 |
ClearGRFConfigList(&remove->info.grfconfig); |
18836f961808
(svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents:
5339
diff
changeset
|
71 |
free(remove); |
18836f961808
(svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents:
5339
diff
changeset
|
72 |
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
|
73 |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5347
diff
changeset
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
} |
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
78 |
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
|
79 |
} |
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 |
} |
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 |
|
6148
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
82 |
enum { |
7827
526cc294e6e5
(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:
6573
diff
changeset
|
83 |
MAX_GAME_LIST_REQUERY_COUNT = 5, ///< How often do we requery in number of times per server? |
526cc294e6e5
(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:
6573
diff
changeset
|
84 |
REQUERY_EVERY_X_GAMELOOPS = 60, ///< How often do we requery in time? |
526cc294e6e5
(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:
6573
diff
changeset
|
85 |
REFRESH_GAMEINFO_X_REQUERIES = 50, ///< Refresh the game info itself after REFRESH_GAMEINFO_X_REQUERIES * REQUERY_EVERY_X_GAMELOOPS game loops |
6148
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
86 |
}; |
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
87 |
|
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
88 |
/** Requeries the (game) servers we have not gotten a reply from */ |
6573 | 89 |
void NetworkGameListRequery() |
6148
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 |
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
|
92 |
|
7827
526cc294e6e5
(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:
6573
diff
changeset
|
93 |
if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return; |
6148
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
94 |
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 |
struct in_addr ip; |
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
97 |
NetworkGameList *item; |
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
98 |
|
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
99 |
for (item = _network_game_list; item != NULL; item = item->next) { |
7827
526cc294e6e5
(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:
6573
diff
changeset
|
100 |
item->retries++; |
526cc294e6e5
(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:
6573
diff
changeset
|
101 |
if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue; |
6148
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
102 |
|
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
103 |
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
|
104 |
|
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
105 |
/* 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
|
106 |
uint8 retries = item->retries; |
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
107 |
NetworkUDPQueryServer(inet_ntoa(ip), item->port); |
7827
526cc294e6e5
(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:
6573
diff
changeset
|
108 |
item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries; |
6148
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
109 |
} |
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
110 |
} |
a8cc2e7bf9b6
(svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents:
5860
diff
changeset
|
111 |
|
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
112 |
#endif /* ENABLE_NETWORK */ |