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/>.
11 * @file network_gamelist.cpp This file handles the GameList
12 * Also, it handles the request to a server for data about the server
17 #include "../stdafx.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"
25 #include "../safeguards.h"
27 NetworkGameList
*_network_game_list
= NULL
;
29 /** Mutex for handling delayed insertion/querying of servers. */
30 static ThreadMutex
*_network_game_list_mutex
= ThreadMutex::New();
31 /** The games to insert when the GUI thread has time for us. */
32 static NetworkGameList
*_network_game_delayed_insertion_list
= NULL
;
35 * Add a new item to the linked gamelist, but do it delayed in the next tick
36 * or so to prevent race conditions.
37 * @param item the item to add. Will be freed once added.
39 void NetworkGameListAddItemDelayed(NetworkGameList
*item
)
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();
47 /** Perform the delayed (thread safe) insertion into the game list */
48 static void NetworkGameListHandleDelayedInsert()
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
;
55 NetworkGameList
*item
= NetworkGameListAddItem(ins_item
->address
);
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
));
65 item
->manually
|= ins_item
->manually
;
66 if (item
->manually
) NetworkRebuildHostList();
67 UpdateNetworkGameWindow();
71 _network_game_list_mutex
->EndCritical();
75 * Add a new item to the linked gamelist. If the IP and Port match
76 * return the existing item instead of adding it again
77 * @param address the address of the to-be added item
78 * @return a point to the newly added or already existing item
80 NetworkGameList
*NetworkGameListAddItem(NetworkAddress address
)
82 const char *hostname
= address
.GetHostname();
84 /* Do not query the 'any' address. */
85 if (StrEmpty(hostname
) ||
86 strcmp(hostname
, "0.0.0.0") == 0 ||
87 strcmp(hostname
, "::") == 0) {
91 NetworkGameList
*item
, *prev_item
;
94 for (item
= _network_game_list
; item
!= NULL
; item
= item
->next
) {
95 if (item
->address
== address
) return item
;
99 item
= CallocT
<NetworkGameList
>(1);
101 item
->address
= address
;
103 if (prev_item
== NULL
) {
104 _network_game_list
= item
;
106 prev_item
->next
= item
;
108 DEBUG(net
, 4, "[gamelist] added server to list");
110 UpdateNetworkGameWindow();
116 * Remove an item from the gamelist linked list
117 * @param remove pointer to the item to be removed
119 void NetworkGameListRemoveItem(NetworkGameList
*remove
)
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
;
127 prev_item
->next
= remove
->next
;
130 /* Remove GRFConfig information */
131 ClearGRFConfigList(&remove
->info
.grfconfig
);
135 DEBUG(net
, 4, "[gamelist] removed server from list");
136 NetworkRebuildHostList();
137 UpdateNetworkGameWindow();
144 static const uint MAX_GAME_LIST_REQUERY_COUNT
= 10; ///< How often do we requery in number of times per server?
145 static const uint REQUERY_EVERY_X_GAMELOOPS
= 60; ///< How often do we requery in time?
146 static const uint REFRESH_GAMEINFO_X_REQUERIES
= 50; ///< Refresh the game info itself after REFRESH_GAMEINFO_X_REQUERIES * REQUERY_EVERY_X_GAMELOOPS game loops
148 /** Requeries the (game) servers we have not gotten a reply from */
149 void NetworkGameListRequery()
151 NetworkGameListHandleDelayedInsert();
153 static uint8 requery_cnt
= 0;
155 if (++requery_cnt
< REQUERY_EVERY_X_GAMELOOPS
) return;
158 for (NetworkGameList
*item
= _network_game_list
; item
!= NULL
; item
= item
->next
) {
160 if (item
->retries
< REFRESH_GAMEINFO_X_REQUERIES
&& (item
->online
|| item
->retries
>= MAX_GAME_LIST_REQUERY_COUNT
)) continue;
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
;
170 * Rebuild the GRFConfig's of the servers in the game list as we did
171 * a rescan and might have found new NewGRFs.
173 void NetworkAfterNewGRFScan()
175 for (NetworkGameList
*item
= _network_game_list
; item
!= NULL
; item
= item
->next
) {
176 /* Reset compatibility state */
177 item
->info
.compatible
= item
->info
.version_compatible
;
179 for (GRFConfig
*c
= item
->info
.grfconfig
; c
!= NULL
; c
= c
->next
) {
180 assert(HasBit(c
->flags
, GCF_COPY
));
182 const GRFConfig
*f
= FindGRFConfig(c
->ident
.grfid
, FGCM_EXACT
, c
->ident
.md5sum
);
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. */
188 c
->name
= FindUnknownGRFName(c
->ident
.grfid
, c
->ident
.md5sum
, true);
190 c
->status
= GCS_NOT_FOUND
;
192 /* If we miss a file, we're obviously incompatible. */
193 item
->info
.compatible
= false;
195 c
->filename
= f
->filename
;
202 c
->status
= GCS_UNKNOWN
;
207 InvalidateWindowClassesData(WC_NETWORK_WINDOW
);
210 #endif /* ENABLE_NETWORK */