OpenTTD Source: src/network/network_gamelist.cpp Source File

OpenTTD Source 1.11.0-RC1

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
23 NetworkGameList *_network_game_list = nullptr;
24
26 static std::atomic<NetworkGameList *> _network_game_delayed_insertion_list(nullptr);
27
33 void NetworkGameListAddItemDelayed(NetworkGameList *item)
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
40 static void NetworkGameListHandleDelayedInsert()
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
47 NetworkGameList *item = NetworkGameListAddItem(ins_item->address);
48
49 if (item != nullptr) {
50 if (StrEmpty(item->info.server_name)) {
51 ClearGRFConfigList(&item->info.grfconfig);
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();
59 UpdateNetworkGameWindow();
60 }
61 free(ins_item);
62 }
63 }
64
71 NetworkGameList *NetworkGameListAddItem(NetworkAddress address)
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
101 UpdateNetworkGameWindow();
102
103 return item;
104 }
105
110 void NetworkGameListRemoveItem(NetworkGameList *remove)
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();
128 UpdateNetworkGameWindow();
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
140 void NetworkGameListRequery()
141 {
142 NetworkGameListHandleDelayedInsert();
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
164 void NetworkAfterNewGRFScan()
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 = FindUnknownGRFName(c->ident.grfid, c->ident.md5sum, true);
179 c->status = GCS_NOT_FOUND;
180
181 /* If we miss a file, we're obviously incompatible. */
182 item->info.compatible = false;
183 } else {
184 c->filename = f->filename;
185 c->name = f->name;
186 c->info = f->info;
187 c->status = GCS_UNKNOWN;
188 }
189 }
190 }
191
192 InvalidateWindowClassesData(WC_NETWORK_WINDOW);
193 }
GRFConfig::info
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
Definition: newgrf_config.h:161
NetworkGameListHandleDelayedInsert
static void NetworkGameListHandleDelayedInsert()
Perform the delayed (thread safe) insertion into the game list.
Definition: network_gamelist.cpp:40
FindGRFConfig
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
Definition: newgrf_config.cpp:737
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:401
NetworkGameListAddItem
NetworkGameList * NetworkGameListAddItem(NetworkAddress address)
Add a new item to the linked gamelist.
Definition: network_gamelist.cpp:71
REFRESH_GAMEINFO_X_REQUERIES
static const uint REFRESH_GAMEINFO_X_REQUERIES
Refresh the game info itself after REFRESH_GAMEINFO_X_REQUERIES * REQUERY_EVERY_X_GAMELOOPS game loop...
Definition: network_gamelist.cpp:137
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
NetworkGameInfo::hostname
char hostname[NETWORK_HOSTNAME_LENGTH]
Hostname of the server (if any)
Definition: game.h:39
GCS_NOT_FOUND
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
Definition: newgrf_config.h:37
GCF_COPY
@ GCF_COPY
The data is copied from a grf in _all_grfs.
Definition: newgrf_config.h:27
UpdateNetworkGameWindow
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Definition: network_gui.cpp:77
NetworkGameList::address
NetworkAddress address
The connection info of the game server.
Definition: network_gamelist.h:19
NetworkAfterNewGRFScan
void NetworkAfterNewGRFScan()
Rebuild the GRFConfig's of the servers in the game list as we did a rescan and might have found new N...
Definition: network_gamelist.cpp:164
_network_game_list
NetworkGameList * _network_game_list
Game list of this client.
Definition: network_gamelist.cpp:23
NetworkUDPQueryServer
void NetworkUDPQueryServer(NetworkAddress address, bool manually)
Query a specific server.
Definition: network_udp.cpp:78
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:152
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
NetworkAddress::GetHostname
const char * GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition: address.cpp:22
NetworkGameList::manually
bool manually
True if the server was added manually.
Definition: network_gamelist.h:21
REQUERY_EVERY_X_GAMELOOPS
static const uint REQUERY_EVERY_X_GAMELOOPS
How often do we requery in time?
Definition: network_gamelist.cpp:136
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
NetworkGameInfo::server_name
char server_name[NETWORK_NAME_LENGTH]
Server name.
Definition: game.h:38
GCS_UNKNOWN
@ GCS_UNKNOWN
The status of this grf file is unknown.
Definition: newgrf_config.h:35
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:29
GRFConfig::name
GRFTextWrapper name
NOSAVE: GRF name (Action 0x08)
Definition: newgrf_config.h:160
network_udp.h
WC_NETWORK_WINDOW
@ WC_NETWORK_WINDOW
Network window; Window numbers:
Definition: window_type.h:466
InvalidateWindowClassesData
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:3339
network_internal.h
NetworkGameList
Structure with information shown in the game list (GUI)
Definition: network_gamelist.h:17
NetworkGameListRequery
void NetworkGameListRequery()
Requeries the (game) servers we have not gotten a reply from.
Definition: network_gamelist.cpp:140
NetworkGameList::next
NetworkGameList * next
Next pointer to make a linked game list.
Definition: network_gamelist.h:23
network_gamelist.h
MAX_GAME_LIST_REQUERY_COUNT
static const uint MAX_GAME_LIST_REQUERY_COUNT
How often do we requery in number of times per server?
Definition: network_gamelist.cpp:135
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition: newgrf_config.h:193
GRFConfig::filename
char * filename
Filename - either with or without full path.
Definition: newgrf_config.h:159
NetworkGameList::info
NetworkGameInfo info
The game information of this server.
Definition: network_gamelist.h:18
FindUnknownGRFName
GRFTextWrapper FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
Finds the name of a NewGRF in the list of names for unknown GRFs.
Definition: newgrf_config.cpp:784
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
NetworkGameInfo::grfconfig
GRFConfig * grfconfig
List of NewGRF files used.
Definition: game.h:33
_network_game_delayed_insertion_list
static std::atomic< NetworkGameList * > _network_game_delayed_insertion_list(nullptr)
The games to insert when the GUI thread has time for us.
NetworkGameListRemoveItem
void NetworkGameListRemoveItem(NetworkGameList *remove)
Remove an item from the gamelist linked list.
Definition: network_gamelist.cpp:110
NetworkGameList::online
bool online
False if the server did not respond (default status)
Definition: network_gamelist.h:20
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
NetworkGameListAddItemDelayed
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...
Definition: network_gamelist.cpp:33