author | glx |
Mon, 24 Sep 2007 03:08:47 +0000 | |
branch | 0.5 |
changeset 5545 | f42dc59a45f5 |
parent 5429 | a1f3d6573141 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
4826
63b1eb7c966b
(svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents:
4077
diff
changeset
|
3 |
#ifdef ENABLE_NETWORK |
63b1eb7c966b
(svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents:
4077
diff
changeset
|
4 |
|
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
5 |
#include "stdafx.h" |
1299
0a6510cc889b
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1122
diff
changeset
|
6 |
#include "debug.h" |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
7 |
#include "network_data.h" |
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
|
8 |
#include "newgrf_config.h" |
5429
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
9 |
#include "network_udp.h" |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
10 |
|
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
11 |
/** Should we stop/contiue requerying of offline servers? */ |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
12 |
static bool _stop_requerying = false; |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
13 |
|
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
14 |
// This file handles the GameList |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
15 |
// Also, it handles the request to a server for data about the server |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
16 |
|
2885
c06f2f3985a0
(svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents:
2881
diff
changeset
|
17 |
/** 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
|
18 |
* 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
|
19 |
* @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
|
20 |
* @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
|
21 |
* @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
|
22 |
NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port) |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
23 |
{ |
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
24 |
NetworkGameList *item, *prev_item; |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
25 |
|
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
26 |
prev_item = NULL; |
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
27 |
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
|
28 |
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
|
29 |
prev_item = item; |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
30 |
} |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
31 |
|
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
32 |
item = malloc(sizeof(*item)); |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
33 |
memset(item, 0, sizeof(*item)); |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
34 |
item->next = NULL; |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
35 |
item->ip = ip; |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
36 |
item->port = port; |
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
37 |
|
4077
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
38 |
if (prev_item == NULL) { |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
39 |
_network_game_list = item; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
40 |
} else { |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
41 |
prev_item->next = item; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
42 |
} |
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
43 |
DEBUG(net, 4) ("[NET][GameList] Added server to list"); |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
44 |
|
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
45 |
UpdateNetworkGameWindow(false); |
5429
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
46 |
_stop_requerying = false; |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
47 |
|
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
48 |
return item; |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
49 |
} |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
50 |
|
2885
c06f2f3985a0
(svn r3435) - Fix a warning, removed unused extern and some comments in network_gamelist.c
Darkvater
parents:
2881
diff
changeset
|
51 |
/** 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
|
52 |
* @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
|
53 |
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
|
54 |
{ |
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
55 |
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
|
56 |
|
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
57 |
prev_item = NULL; |
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
58 |
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
|
59 |
if (remove == item) { |
4077
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
60 |
if (prev_item == NULL) { |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
61 |
_network_game_list = remove->next; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
62 |
} else { |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
63 |
prev_item->next = remove->next; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
2885
diff
changeset
|
64 |
} |
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
|
65 |
|
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
|
66 |
/* Remove GRFConfig information */ |
5347
18836f961808
(svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents:
5339
diff
changeset
|
67 |
ClearGRFConfigList(&remove->info.grfconfig); |
18836f961808
(svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents:
5339
diff
changeset
|
68 |
free(remove); |
18836f961808
(svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents:
5339
diff
changeset
|
69 |
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
|
70 |
|
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
|
71 |
DEBUG(net, 4) ("[NET][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
|
72 |
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
|
73 |
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
|
74 |
} |
2881
1ffbbdbf1685
(svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents:
2186
diff
changeset
|
75 |
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
|
76 |
} |
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 |
} |
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
|
78 |
|
5429
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
79 |
enum { |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
80 |
MAX_GAME_LIST_REQUERY_COUNT = 5, |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
81 |
REQUERY_EVERY_X_GAMELOOPS = 60, |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
82 |
}; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
83 |
|
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
84 |
/** Requeries the (game) servers we have not gotten a reply from */ |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
85 |
void NetworkGameListRequery(void) |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
86 |
{ |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
87 |
static uint8 requery_cnt = 0; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
88 |
struct in_addr ip; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
89 |
NetworkGameList *item; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
90 |
|
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
91 |
if (_stop_requerying || ++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
92 |
|
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
93 |
requery_cnt = 0; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
94 |
_stop_requerying = true; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
95 |
|
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
96 |
for (item = _network_game_list; item != NULL; item = item->next) { |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
97 |
uint8 retries; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
98 |
|
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
99 |
if (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT) continue; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
100 |
|
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
101 |
ip.s_addr = item->ip; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
102 |
|
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
103 |
/* item gets mostly zeroed by NetworkUDPQueryServer */ |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
104 |
retries = item->retries; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
105 |
NetworkUDPQueryServer(inet_ntoa(ip), item->port); |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
106 |
item->retries = retries + 1; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
107 |
|
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
108 |
_stop_requerying = false; |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
109 |
} |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
110 |
|
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
111 |
} |
a1f3d6573141
(svn r8625) [0.5] -Backport from trunk (8253, 8273, 8497, 8520 + 8542):
rubidium
parents:
5347
diff
changeset
|
112 |
|
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
113 |
#endif /* ENABLE_NETWORK */ |