OpenTTD
network_gamelist.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
13 #include "../stdafx.h"
14 #include "../debug.h"
15 #include "../window_func.h"
16 #include "network_internal.h"
17 #include "network_udp.h"
18 #include "network_gamelist.h"
19 #include <atomic>
20 
21 #include "../safeguards.h"
22 
24 
26 static std::atomic<NetworkGameList *> _network_game_delayed_insertion_list(nullptr);
27 
34 {
35  item->next = _network_game_delayed_insertion_list.load(std::memory_order_relaxed);
36  while (!_network_game_delayed_insertion_list.compare_exchange_weak(item->next, item, std::memory_order_acq_rel)) {}
37 }
38 
41 {
42  while (true) {
43  NetworkGameList *ins_item = _network_game_delayed_insertion_list.load(std::memory_order_relaxed);
44  while (ins_item != nullptr && !_network_game_delayed_insertion_list.compare_exchange_weak(ins_item, ins_item->next, std::memory_order_acq_rel)) {}
45  if (ins_item == nullptr) break; // No item left.
46 
48 
49  if (item != nullptr) {
50  if (StrEmpty(item->info.server_name)) {
52  memset(&item->info, 0, sizeof(item->info));
53  strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name));
54  strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
55  item->online = false;
56  }
57  item->manually |= ins_item->manually;
58  if (item->manually) NetworkRebuildHostList();
60  }
61  free(ins_item);
62  }
63 }
64 
72 {
73  const char *hostname = address.GetHostname();
74 
75  /* Do not query the 'any' address. */
76  if (StrEmpty(hostname) ||
77  strcmp(hostname, "0.0.0.0") == 0 ||
78  strcmp(hostname, "::") == 0) {
79  return nullptr;
80  }
81 
82  NetworkGameList *item, *prev_item;
83 
84  prev_item = nullptr;
85  for (item = _network_game_list; item != nullptr; item = item->next) {
86  if (item->address == address) return item;
87  prev_item = item;
88  }
89 
90  item = CallocT<NetworkGameList>(1);
91  item->next = nullptr;
92  item->address = address;
93 
94  if (prev_item == nullptr) {
95  _network_game_list = item;
96  } else {
97  prev_item->next = item;
98  }
99  DEBUG(net, 4, "[gamelist] added server to list");
100 
102 
103  return item;
104 }
105 
111 {
112  NetworkGameList *prev_item = nullptr;
113  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
114  if (remove == item) {
115  if (prev_item == nullptr) {
116  _network_game_list = remove->next;
117  } else {
118  prev_item->next = remove->next;
119  }
120 
121  /* Remove GRFConfig information */
122  ClearGRFConfigList(&remove->info.grfconfig);
123  free(remove);
124  remove = nullptr;
125 
126  DEBUG(net, 4, "[gamelist] removed server from list");
127  NetworkRebuildHostList();
129  return;
130  }
131  prev_item = item;
132  }
133 }
134 
135 static const uint MAX_GAME_LIST_REQUERY_COUNT = 10;
136 static const uint REQUERY_EVERY_X_GAMELOOPS = 60;
137 static const uint REFRESH_GAMEINFO_X_REQUERIES = 50;
138 
141 {
143 
144  static uint8 requery_cnt = 0;
145 
146  if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
147  requery_cnt = 0;
148 
149  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
150  item->retries++;
151  if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;
152 
153  /* item gets mostly zeroed by NetworkUDPQueryServer */
154  uint8 retries = item->retries;
155  NetworkUDPQueryServer(NetworkAddress(item->address));
156  item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
157  }
158 }
159 
165 {
166  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
167  /* Reset compatibility state */
168  item->info.compatible = item->info.version_compatible;
169 
170  for (GRFConfig *c = item->info.grfconfig; c != nullptr; c = c->next) {
171  assert(HasBit(c->flags, GCF_COPY));
172 
173  const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
174  if (f == nullptr) {
175  /* Don't know the GRF, so mark game incompatible and the (possibly)
176  * already resolved name for this GRF (another server has sent the
177  * name of the GRF already. */
178  c->name->Release();
179  c->name = FindUnknownGRFName(c->ident.grfid, c->ident.md5sum, true);
180  c->name->AddRef();
181  c->status = GCS_NOT_FOUND;
182 
183  /* If we miss a file, we're obviously incompatible. */
184  item->info.compatible = false;
185  } else {
186  c->filename = f->filename;
187  c->name->Release();
188  c->name = f->name;
189  c->name->AddRef();
190  c->info->Release();
191  c->info = f->info;
192  c->info->AddRef();
193  c->status = GCS_UNKNOWN;
194  }
195  }
196  }
197 
199 }
NetworkGameList * next
Next pointer to make a linked game list.
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
static std::atomic< NetworkGameList * > _network_game_delayed_insertion_list(nullptr)
The games to insert when the GUI thread has time for us.
Sending and receiving UDP messages.
void NetworkGameListRemoveItem(NetworkGameList *remove)
Remove an item from the gamelist linked list.
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:27
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
GRF file was not found in the local cache.
Definition: newgrf_config.h:36
Structure with information shown in the game list (GUI)
void NetworkUDPQueryServer(NetworkAddress address, bool manually)
Query a specific server.
Definition: network_udp.cpp:78
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
char server_name[NETWORK_NAME_LENGTH]
Server name.
Definition: game.h:38
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Definition: network_gui.cpp:82
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition: window.cpp:3334
static void NetworkGameListHandleDelayedInsert()
Perform the delayed (thread safe) insertion into the game list.
static const uint REQUERY_EVERY_X_GAMELOOPS
How often do we requery in time?
NetworkGameList * NetworkGameListAddItem(NetworkAddress address)
Add a new item to the linked gamelist.
GRFTextWrapper * FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
Finds the name of a NewGRF in the list of names for unknown GRFs.
Information about GRF, used in the game and (part of it) in savegames.
The data is copied from a grf in _all_grfs.
Definition: newgrf_config.h:26
NetworkAddress address
The connection info of the game server.
const char * GetHostname()
Get the hostname; in case it wasn&#39;t given the IPv4 dotted representation is given.
Definition: address.cpp:22
NetworkGameInfo info
The game information of this server.
NetworkGameList * _network_game_list
Game list of this client.
void NetworkGameListRequery()
Requeries the (game) servers we have not gotten a reply from.
void NetworkAfterNewGRFScan()
Rebuild the GRFConfig&#39;s of the servers in the game list as we did a rescan and might have found new N...
The status of this grf file is unknown.
Definition: newgrf_config.h:34
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
bool online
False if the server did not respond (default status)
static const uint MAX_GAME_LIST_REQUERY_COUNT
How often do we requery in number of times per server?
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:57
Variables and function used internally.
Handling of the list of games.
void NetworkGameListAddItemDelayed(NetworkGameList *item)
Add a new item to the linked gamelist, but do it delayed in the next tick or so to prevent race condi...
Network window; Window numbers:
Definition: window_type.h:466
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
char * filename
Filename - either with or without full path.
GRFTextWrapper * name
NOSAVE: GRF name (Action 0x08)
bool manually
True if the server was added manually.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
char hostname[NETWORK_HOSTNAME_LENGTH]
Hostname of the server (if any)
Definition: game.h:39
Only find Grfs matching md5sum.
GRFConfig * grfconfig
List of NewGRF files used.
Definition: game.h:33
static const uint REFRESH_GAMEINFO_X_REQUERIES
Refresh the game info itself after REFRESH_GAMEINFO_X_REQUERIES * REQUERY_EVERY_X_GAMELOOPS game loop...
GRFTextWrapper * info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)