10
10
#include "../stdafx.h"
11
11
#include "../debug.h"
12
#include "../newgrf_config.h"
13
12
#include "../core/alloc_func.hpp"
14
#include "core/game.h"
13
#include "../thread.h"
14
#include "../string_func.h"
15
#include "network_internal.h"
15
16
#include "network_udp.h"
16
17
#include "network_gamelist.h"
17
#include "network_gui.h"
19
19
NetworkGameList *_network_game_list = NULL;
21
ThreadMutex *_network_game_list_mutex = ThreadMutex::New();
22
NetworkGameList *_network_game_delayed_insertion_list = NULL;
24
/** Add a new item to the linked gamelist, but do it delayed in the next tick
25
* or so to prevent race conditions.
26
* @param item the item to add. Will be freed once added.
28
void NetworkGameListAddItemDelayed(NetworkGameList *item)
30
_network_game_list_mutex->BeginCritical();
31
item->next = _network_game_delayed_insertion_list;
32
_network_game_delayed_insertion_list = item;
33
_network_game_list_mutex->EndCritical();
36
/** Perform the delayed (thread safe) insertion into the game list */
37
static void NetworkGameListHandleDelayedInsert()
39
_network_game_list_mutex->BeginCritical();
40
while (_network_game_delayed_insertion_list != NULL) {
41
NetworkGameList *ins_item = _network_game_delayed_insertion_list;
42
_network_game_delayed_insertion_list = ins_item->next;
44
NetworkGameList *item = NetworkGameListAddItem(ins_item->ip, ins_item->port);
47
if (StrEmpty(item->info.server_name)) {
48
memset(&item->info, 0, sizeof(item->info));
49
strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name));
50
strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
53
item->manually = ins_item->manually;
54
UpdateNetworkGameWindow(false);
58
_network_game_list_mutex->EndCritical();
21
61
/** Add a new item to the linked gamelist. If the IP and Port match
22
62
* return the existing item instead of adding it again
23
63
* @param ip the IP-address (inet_addr) of the to-be added item
25
65
* @return a point to the newly added or already existing item */
26
66
NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
68
if (ip == 0) return NULL;
28
70
NetworkGameList *item, *prev_item;
88
129
/** Requeries the (game) servers we have not gotten a reply from */
89
130
void NetworkGameListRequery()
132
NetworkGameListHandleDelayedInsert();
91
134
static uint8 requery_cnt = 0;
93
136
if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
97
NetworkGameList *item;
99
for (item = _network_game_list; item != NULL; item = item->next) {
139
for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
101
141
if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;
103
ip.s_addr = item->ip;
105
143
/* item gets mostly zeroed by NetworkUDPQueryServer */
106
144
uint8 retries = item->retries;
107
NetworkUDPQueryServer(inet_ntoa(ip), item->port);
145
NetworkUDPQueryServer(NetworkAddress(item->ip, item->port));
108
146
item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;