OpenTTD
network_gamelist.cpp
Go to the documentation of this file.
1 /* $Id$ */
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 #include "../stdafx.h"
16 #include "../debug.h"
17 #include "../window_func.h"
18 #include "network_internal.h"
19 #include "network_udp.h"
20 #include "network_gamelist.h"
21 #include <atomic>
22 
23 #include "../safeguards.h"
24 
26 
28 static std::atomic<NetworkGameList *> _network_game_delayed_insertion_list(nullptr);
29 
36 {
37  item->next = _network_game_delayed_insertion_list.load(std::memory_order_relaxed);
38  while (!_network_game_delayed_insertion_list.compare_exchange_weak(item->next, item, std::memory_order_acq_rel)) {}
39 }
40 
43 {
44  while (true) {
45  NetworkGameList *ins_item = _network_game_delayed_insertion_list.load(std::memory_order_relaxed);
46  while (ins_item != nullptr && !_network_game_delayed_insertion_list.compare_exchange_weak(ins_item, ins_item->next, std::memory_order_acq_rel)) {}
47  if (ins_item == nullptr) break; // No item left.
48 
50 
51  if (item != nullptr) {
52  if (StrEmpty(item->info.server_name)) {
54  memset(&item->info, 0, sizeof(item->info));
55  strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name));
56  strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
57  item->online = false;
58  }
59  item->manually |= ins_item->manually;
60  if (item->manually) NetworkRebuildHostList();
62  }
63  free(ins_item);
64  }
65 }
66 
74 {
75  const char *hostname = address.GetHostname();
76 
77  /* Do not query the 'any' address. */
78  if (StrEmpty(hostname) ||
79  strcmp(hostname, "0.0.0.0") == 0 ||
80  strcmp(hostname, "::") == 0) {
81  return nullptr;
82  }
83 
84  NetworkGameList *item, *prev_item;
85 
86  prev_item = nullptr;
87  for (item = _network_game_list; item != nullptr; item = item->next) {
88  if (item->address == address) return item;
89  prev_item = item;
90  }
91 
92  item = CallocT<NetworkGameList>(1);
93  item->next = nullptr;
94  item->address = address;
95 
96  if (prev_item == nullptr) {
97  _network_game_list = item;
98  } else {
99  prev_item->next = item;
100  }
101  DEBUG(net, 4, "[gamelist] added server to list");
102 
104 
105  return item;
106 }
107 
113 {
114  NetworkGameList *prev_item = nullptr;
115  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
116  if (remove == item) {
117  if (prev_item == nullptr) {
118  _network_game_list = remove->next;
119  } else {
120  prev_item->next = remove->next;
121  }
122 
123  /* Remove GRFConfig information */
124  ClearGRFConfigList(&remove->info.grfconfig);
125  free(remove);
126  remove = nullptr;
127 
128  DEBUG(net, 4, "[gamelist] removed server from list");
129  NetworkRebuildHostList();
131  return;
132  }
133  prev_item = item;
134  }
135 }
136 
137 static const uint MAX_GAME_LIST_REQUERY_COUNT = 10;
138 static const uint REQUERY_EVERY_X_GAMELOOPS = 60;
139 static const uint REFRESH_GAMEINFO_X_REQUERIES = 50;
140 
143 {
145 
146  static uint8 requery_cnt = 0;
147 
148  if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
149  requery_cnt = 0;
150 
151  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
152  item->retries++;
153  if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;
154 
155  /* item gets mostly zeroed by NetworkUDPQueryServer */
156  uint8 retries = item->retries;
157  NetworkUDPQueryServer(NetworkAddress(item->address));
158  item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
159  }
160 }
161 
167 {
168  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
169  /* Reset compatibility state */
170  item->info.compatible = item->info.version_compatible;
171 
172  for (GRFConfig *c = item->info.grfconfig; c != nullptr; c = c->next) {
173  assert(HasBit(c->flags, GCF_COPY));
174 
175  const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
176  if (f == nullptr) {
177  /* Don't know the GRF, so mark game incompatible and the (possibly)
178  * already resolved name for this GRF (another server has sent the
179  * name of the GRF already. */
180  c->name->Release();
181  c->name = FindUnknownGRFName(c->ident.grfid, c->ident.md5sum, true);
182  c->name->AddRef();
183  c->status = GCS_NOT_FOUND;
184 
185  /* If we miss a file, we're obviously incompatible. */
186  item->info.compatible = false;
187  } else {
188  c->filename = f->filename;
189  c->name->Release();
190  c->name = f->name;
191  c->name->AddRef();
192  c->info->Release();
193  c->info = f->info;
194  c->info->AddRef();
195  c->status = GCS_UNKNOWN;
196  }
197  }
198  }
199 
201 }
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:29
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
GRF file was not found in the local cache.
Definition: newgrf_config.h:38
Structure with information shown in the game list (GUI)
void NetworkUDPQueryServer(NetworkAddress address, bool manually)
Query a specific server.
Definition: network_udp.cpp:80
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:40
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Definition: network_gui.cpp:84
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:3318
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:28
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:24
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:36
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:37
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:59
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:468
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
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:131
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:41
Only find Grfs matching md5sum.
GRFConfig * grfconfig
List of NewGRF files used.
Definition: game.h:35
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)