OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

network_gamelist.cpp
Go to the documentation of this file.
1 /* $Id: network_gamelist.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
15 #ifdef ENABLE_NETWORK
16
17 #include "../stdafx.h"
18 #include "../debug.h"
19 #include "../window_func.h"
20 #include "../thread/thread.h"
21 #include "network_internal.h"
22 #include "network_udp.h"
23 #include "network_gamelist.h"
24
25 #include "../safeguards.h"
26
27 NetworkGameList *_network_game_list = NULL;
28
30 static ThreadMutex *_network_game_list_mutex = ThreadMutex::New();
32 static NetworkGameList *_network_game_delayed_insertion_list = NULL;
33
39 void NetworkGameListAddItemDelayed(NetworkGameList *item)
40 {
41 _network_game_list_mutex->BeginCritical();
42 item->next = _network_game_delayed_insertion_list;
43 _network_game_delayed_insertion_list = item;
44 _network_game_list_mutex->EndCritical();
45 }
46
48 static void NetworkGameListHandleDelayedInsert()
49 {
50 _network_game_list_mutex->BeginCritical();
51 while (_network_game_delayed_insertion_list != NULL) {
52 NetworkGameList *ins_item = _network_game_delayed_insertion_list;
53 _network_game_delayed_insertion_list = ins_item->next;
54
55 NetworkGameList *item = NetworkGameListAddItem(ins_item->address);
56
57 if (item != NULL) {
58 if (StrEmpty(item->info.server_name)) {
59 ClearGRFConfigList(&item->info.grfconfig);
60 memset(&item->info, 0, sizeof(item->info));
61 strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name));
62 strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
63 item->online = false;
64 }
65 item->manually |= ins_item->manually;
66 if (item->manually) NetworkRebuildHostList();
67 UpdateNetworkGameWindow();
68 }
69 free(ins_item);
70 }
71 _network_game_list_mutex->EndCritical();
72 }
73
80 NetworkGameList *NetworkGameListAddItem(NetworkAddress address)
81 {
82 const char *hostname = address.GetHostname();
83
84 /* Do not query the 'any' address. */
85 if (StrEmpty(hostname) ||
86 strcmp(hostname, "0.0.0.0") == 0 ||
87 strcmp(hostname, "::") == 0) {
88 return NULL;
89 }
90
91 NetworkGameList *item, *prev_item;
92
93 prev_item = NULL;
94 for (item = _network_game_list; item != NULL; item = item->next) {
95 if (item->address == address) return item;
96 prev_item = item;
97 }
98
99 item = CallocT<NetworkGameList>(1);
100 item->next = NULL;
101 item->address = address;
102
103 if (prev_item == NULL) {
104 _network_game_list = item;
105 } else {
106 prev_item->next = item;
107 }
108 DEBUG(net, 4, "[gamelist] added server to list");
109
110 UpdateNetworkGameWindow();
111
112 return item;
113 }
114
119 void NetworkGameListRemoveItem(NetworkGameList *remove)
120 {
121 NetworkGameList *prev_item = NULL;
122 for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
123 if (remove == item) {
124 if (prev_item == NULL) {
125 _network_game_list = remove->next;
126 } else {
127 prev_item->next = remove->next;
128 }
129
130 /* Remove GRFConfig information */
131 ClearGRFConfigList(&remove->info.grfconfig);
132 free(remove);
133 remove = NULL;
134
135 DEBUG(net, 4, "[gamelist] removed server from list");
136 NetworkRebuildHostList();
137 UpdateNetworkGameWindow();
138 return;
139 }
140 prev_item = item;
141 }
142 }
143
144 static const uint MAX_GAME_LIST_REQUERY_COUNT = 10;
145 static const uint REQUERY_EVERY_X_GAMELOOPS = 60;
146 static const uint REFRESH_GAMEINFO_X_REQUERIES = 50;
147
149 void NetworkGameListRequery()
150 {
151 NetworkGameListHandleDelayedInsert();
152
153 static uint8 requery_cnt = 0;
154
155 if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
156 requery_cnt = 0;
157
158 for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
159 item->retries++;
160 if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;
161
162 /* item gets mostly zeroed by NetworkUDPQueryServer */
163 uint8 retries = item->retries;
164 NetworkUDPQueryServer(NetworkAddress(item->address));
165 item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
166 }
167 }
168
173 void NetworkAfterNewGRFScan()
174 {
175 for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
176 /* Reset compatibility state */
177 item->info.compatible = item->info.version_compatible;
178
179 for (GRFConfig *c = item->info.grfconfig; c != NULL; c = c->next) {
180 assert(HasBit(c->flags, GCF_COPY));
181
182 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
183 if (f == NULL) {
184 /* Don't know the GRF, so mark game incompatible and the (possibly)
185 * already resolved name for this GRF (another server has sent the
186 * name of the GRF already. */
187 c->name->Release();
188 c->name = FindUnknownGRFName(c->ident.grfid, c->ident.md5sum, true);
189 c->name->AddRef();
190 c->status = GCS_NOT_FOUND;
191
192 /* If we miss a file, we're obviously incompatible. */
193 item->info.compatible = false;
194 } else {
195 c->filename = f->filename;
196 c->name->Release();
197 c->name = f->name;
198 c->name->AddRef();
199 c->info->Release();
200 c->info = f->info;
201 c->info->AddRef();
202 c->status = GCS_UNKNOWN;
203 }
204 }
205 }
206
207 InvalidateWindowClassesData(WC_NETWORK_WINDOW);
208 }
209
210 #endif /* ENABLE_NETWORK */