Files @ r23483:3733e6b8ff17
Branch filter:

Location: cpp/openttd-patchpack/source/src/network/network_gamelist.cpp - annotation

Patric Stout
Remove: ENABLE_NETWORK switch

This switch has been a pain for years. Often disabling broke
compilation, as no developer compiles OpenTTD without, neither do
any of our official binaries.

Additionaly, it has grown so hugely in our codebase, that it
clearly shows that the current solution was a poor one. 350+
instances of "#ifdef ENABLE_NETWORK" were in the code, of which
only ~30 in the networking code itself. The rest were all around
the code to do the right thing, from GUI to NewGRF.

A more proper solution would be to stub all the functions, and
make sure the rest of the code can simply assume network is
available. This was also partially done, and most variables were
correct if networking was disabled. Despite that, often the #ifdefs
were still used.

With the recent removal of DOS, there is also no platform anymore
which we support where networking isn't working out-of-the-box.

All in all, it is time to remove the ENABLE_NETWORK switch. No
replacement is planned, but if you feel we really need this option,
we welcome any Pull Request which implements this in a way that
doesn't crawl through the code like this diff shows we used to.
r5584:545d748cc681
r5584:545d748cc681
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r6121:a8ff6abe7fb2
r6121:a8ff6abe7fb2
r6121:a8ff6abe7fb2
r6121:a8ff6abe7fb2
r6121:a8ff6abe7fb2
r5584:545d748cc681
r5584:545d748cc681
r17279:76addfee8711
r12839:daeca5b0e6db
r9428:6800206a6c0d
r5897:753985117bdb
r5917:d8ebeb21f83f
r5917:d8ebeb21f83f
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r5917:d8ebeb21f83f
r5897:753985117bdb
r17629:21e9dfd343cd
r13498:598a539466dc
r17629:21e9dfd343cd
r13498:598a539466dc
r10824:4036289a0b3d
r15610:623a23fb6560
r15610:623a23fb6560
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r11546:a4422f41f18b
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r14186:5477bcdd03f5
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r11560:52827dc8eb76
r12533:86f2de494688
r19182:8f0b5a1acb1c
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r15610:623a23fb6560
r15610:623a23fb6560
r5584:545d748cc681
r11555:622725a26a6d
r15613:193c12018337
r15613:193c12018337
r11546:a4422f41f18b
r5584:545d748cc681
r11610:3e592325d26d
r11610:3e592325d26d
r11610:3e592325d26d
r11610:3e592325d26d
r11610:3e592325d26d
r11610:3e592325d26d
r11610:3e592325d26d
r11610:3e592325d26d
r8937:f2f32ec9acdd
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11546:a4422f41f18b
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10824:4036289a0b3d
r5584:545d748cc681
r11546:a4422f41f18b
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r19182:8f0b5a1acb1c
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15610:623a23fb6560
r15610:623a23fb6560
r15613:193c12018337
r15613:193c12018337
r5584:545d748cc681
r5584:545d748cc681
r11613:7e088130f298
r11613:7e088130f298
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12533:86f2de494688
r19182:8f0b5a1acb1c
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15175:66e0817dc450
r15175:66e0817dc450
r15175:66e0817dc450
r5897:753985117bdb
r5897:753985117bdb
r6247:96e840dbefcc
r5897:753985117bdb
r10824:4036289a0b3d
r10824:4036289a0b3d
r5897:753985117bdb
r5897:753985117bdb
r7331:9df263aec9d0
r5897:753985117bdb
r5897:753985117bdb
r10822:adfba97cb1d1
r7331:9df263aec9d0
r7331:9df263aec9d0
r5897:753985117bdb
r5897:753985117bdb
r5897:753985117bdb
r11546:a4422f41f18b
r7331:9df263aec9d0
r5897:753985117bdb
r5897:753985117bdb
r5897:753985117bdb
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r19821:021f5f9159cc
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r16246:b0e8840a555e
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r19821:021f5f9159cc
r17403:bbaee031fd60
r17403:bbaee031fd60
r17403:bbaee031fd60
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r19821:021f5f9159cc
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r17403:bbaee031fd60
r17403:bbaee031fd60
r17403:bbaee031fd60
r17403:bbaee031fd60
r17403:bbaee031fd60
r17403:bbaee031fd60
r17403:bbaee031fd60
r17403:bbaee031fd60
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r13054:319ef5c8a6c7
r17279:76addfee8711
r17279:76addfee8711
r13054:319ef5c8a6c7
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/**
 * @file network_gamelist.cpp This file handles the GameList
 * Also, it handles the request to a server for data about the server
 */

#include "../stdafx.h"
#include "../debug.h"
#include "../window_func.h"
#include "../thread/thread.h"
#include "network_internal.h"
#include "network_udp.h"
#include "network_gamelist.h"

#include "../safeguards.h"

NetworkGameList *_network_game_list = NULL;

/** Mutex for handling delayed insertion/querying of servers. */
static ThreadMutex *_network_game_list_mutex = ThreadMutex::New();
/** The games to insert when the GUI thread has time for us. */
static NetworkGameList *_network_game_delayed_insertion_list = NULL;

/**
 * Add a new item to the linked gamelist, but do it delayed in the next tick
 * or so to prevent race conditions.
 * @param item the item to add. Will be freed once added.
 */
void NetworkGameListAddItemDelayed(NetworkGameList *item)
{
	_network_game_list_mutex->BeginCritical();
	item->next = _network_game_delayed_insertion_list;
	_network_game_delayed_insertion_list = item;
	_network_game_list_mutex->EndCritical();
}

/** Perform the delayed (thread safe) insertion into the game list */
static void NetworkGameListHandleDelayedInsert()
{
	_network_game_list_mutex->BeginCritical();
	while (_network_game_delayed_insertion_list != NULL) {
		NetworkGameList *ins_item = _network_game_delayed_insertion_list;
		_network_game_delayed_insertion_list = ins_item->next;

		NetworkGameList *item = NetworkGameListAddItem(ins_item->address);

		if (item != NULL) {
			if (StrEmpty(item->info.server_name)) {
				ClearGRFConfigList(&item->info.grfconfig);
				memset(&item->info, 0, sizeof(item->info));
				strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name));
				strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
				item->online = false;
			}
			item->manually |= ins_item->manually;
			if (item->manually) NetworkRebuildHostList();
			UpdateNetworkGameWindow();
		}
		free(ins_item);
	}
	_network_game_list_mutex->EndCritical();
}

/**
 * Add a new item to the linked gamelist. If the IP and Port match
 * return the existing item instead of adding it again
 * @param address the address of the to-be added item
 * @return a point to the newly added or already existing item
 */
NetworkGameList *NetworkGameListAddItem(NetworkAddress address)
{
	const char *hostname = address.GetHostname();

	/* Do not query the 'any' address. */
	if (StrEmpty(hostname) ||
			strcmp(hostname, "0.0.0.0") == 0 ||
			strcmp(hostname, "::") == 0) {
		return NULL;
	}

	NetworkGameList *item, *prev_item;

	prev_item = NULL;
	for (item = _network_game_list; item != NULL; item = item->next) {
		if (item->address == address) return item;
		prev_item = item;
	}

	item = CallocT<NetworkGameList>(1);
	item->next = NULL;
	item->address = address;

	if (prev_item == NULL) {
		_network_game_list = item;
	} else {
		prev_item->next = item;
	}
	DEBUG(net, 4, "[gamelist] added server to list");

	UpdateNetworkGameWindow();

	return item;
}

/**
 * Remove an item from the gamelist linked list
 * @param remove pointer to the item to be removed
 */
void NetworkGameListRemoveItem(NetworkGameList *remove)
{
	NetworkGameList *prev_item = NULL;
	for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
		if (remove == item) {
			if (prev_item == NULL) {
				_network_game_list = remove->next;
			} else {
				prev_item->next = remove->next;
			}

			/* Remove GRFConfig information */
			ClearGRFConfigList(&remove->info.grfconfig);
			free(remove);
			remove = NULL;

			DEBUG(net, 4, "[gamelist] removed server from list");
			NetworkRebuildHostList();
			UpdateNetworkGameWindow();
			return;
		}
		prev_item = item;
	}
}

static const uint MAX_GAME_LIST_REQUERY_COUNT  = 10; ///< How often do we requery in number of times per server?
static const uint REQUERY_EVERY_X_GAMELOOPS    = 60; ///< How often do we requery in time?
static const uint REFRESH_GAMEINFO_X_REQUERIES = 50; ///< Refresh the game info itself after REFRESH_GAMEINFO_X_REQUERIES * REQUERY_EVERY_X_GAMELOOPS game loops

/** Requeries the (game) servers we have not gotten a reply from */
void NetworkGameListRequery()
{
	NetworkGameListHandleDelayedInsert();

	static uint8 requery_cnt = 0;

	if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
	requery_cnt = 0;

	for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
		item->retries++;
		if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;

		/* item gets mostly zeroed by NetworkUDPQueryServer */
		uint8 retries = item->retries;
		NetworkUDPQueryServer(NetworkAddress(item->address));
		item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
	}
}

/**
 * Rebuild the GRFConfig's of the servers in the game list as we did
 * a rescan and might have found new NewGRFs.
 */
void NetworkAfterNewGRFScan()
{
	for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
		/* Reset compatibility state */
		item->info.compatible = item->info.version_compatible;

		for (GRFConfig *c = item->info.grfconfig; c != NULL; c = c->next) {
			assert(HasBit(c->flags, GCF_COPY));

			const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
			if (f == NULL) {
				/* Don't know the GRF, so mark game incompatible and the (possibly)
				 * already resolved name for this GRF (another server has sent the
				 * name of the GRF already. */
				c->name->Release();
				c->name = FindUnknownGRFName(c->ident.grfid, c->ident.md5sum, true);
				c->name->AddRef();
				c->status = GCS_NOT_FOUND;

				/* If we miss a file, we're obviously incompatible. */
				item->info.compatible = false;
			} else {
				c->filename = f->filename;
				c->name->Release();
				c->name = f->name;
				c->name->AddRef();
				c->info->Release();
				c->info = f->info;
				c->info->AddRef();
				c->status = GCS_UNKNOWN;
			}
		}
	}

	InvalidateWindowClassesData(WC_NETWORK_WINDOW);
}