network_gamelist.c
author truelight
Thu, 23 Dec 2004 17:59:37 +0000
changeset 786 aa75e1682d39
parent 738 0b2fb79e64fc
child 1095 90220990fd7c
permissions -rw-r--r--
(svn r1253) -Update: updated the multiplayer doc a bit.
Via this commit I wanted to thank everyone who helped with this new
network, with some people by name: Bjarni, for Endian support,
Hackykid, for Masterserver and ingame server-list, Geniusdex, for
suggestions and bug reporting, Darkvater, for the Windows testing,
Tron, for his comments and suggestions for the network, sign_de, for his
general help around the network, orudge, for his kind offer to host
our masterserver, dominik, for the network GUI, and all the others I
did not mention or forgot. Thank you all :)
#include "stdafx.h"
#include "network_data.h"

#ifdef ENABLE_NETWORK

//
// This file handles the GameList
// Also, it handles the request to a server for data about the server

extern void UpdateNetworkGameWindow(bool unselect);

void NetworkGameListClear(void)
{
	NetworkGameList *item;
	NetworkGameList *next;

	item = _network_game_list;

	while (item != NULL) {
		next = item->next;
		free(item);
		item = next;
	}
	_network_game_list = NULL;
	_network_game_count = 0;

	UpdateNetworkGameWindow(true);

	DEBUG(net, 4)("[NET][GameList] Cleared list");
}

NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
{
	NetworkGameList *item;

	item = _network_game_list;
	if (item != NULL) {
		while (item->next != NULL) {
			if (item->ip == ip && item->port == port)
				return item;
			item = item->next;
		}

		if (item->ip == ip && item->port == port)
			return item;

		item->next = malloc(sizeof(*item));
		item = item->next;
	} else {
		item = malloc(sizeof(*item));
		_network_game_list = item;
	}

	DEBUG(net, 4) ("[NET][GameList] Added server to list");

	memset(item, 0, sizeof(*item));

	item->next = NULL;
	item->ip = ip;
	item->port = port;
	_network_game_count++;

	UpdateNetworkGameWindow(false);

	return item;
}

void NetworkGameListRemoveItem(NetworkGameList *remove)
{
	NetworkGameList *item;

	item = _network_game_list;

	// examine head of the list
	if ( remove == _network_game_list ) {
		_network_game_list = remove->next;
		free(remove);
		DEBUG(net, 4) ("[NET][GameList] Removed server from list");
		return;
	}

	// examine each item
	while ( item->next != NULL ) {
		if ( item->next == remove )
		{
			item->next = remove->next;
			free(remove);
			DEBUG(net, 4) ("[NET][GameList] Removed server from list");
			return;
		}
		item = item->next;
	}
}

void NetworkGameListAddQueriedItem(const NetworkGameInfo *info, bool server_online)
{
	// We queried a server and now we are going to add it to the list
	NetworkGameList *item;

	item = NetworkGameListAddItem(_network_last_host_ip, _network_last_port);
	item->online = server_online;
	memcpy(&item->info, info, sizeof(NetworkGameInfo));
	ttd_strlcpy(item->info.hostname, _network_last_host, sizeof(item->info.hostname));

	UpdateNetworkGameWindow(false);
}

#endif /* ENABLE_NETWORK */