Changeset - r9429:9bb0dfa4c67f
[Not reviewed]
master
0 2 0
rubidium - 16 years ago 2008-05-30 20:08:32
rubidium@openttd.org
(svn r13344) -Codechange: remove some code duplication.
2 files changed with 12 insertions and 25 deletions:
0 comments (0 inline, 0 general)
src/network/network.cpp
Show inline comments
 
@@ -868,30 +868,40 @@ static void NetworkClose()
 
{
 
	NetworkTCPSocketHandler *cs;
 

	
 
	FOR_ALL_CLIENTS(cs) {
 
		if (!_network_server) {
 
			SEND_COMMAND(PACKET_CLIENT_QUIT)("leaving");
 
			cs->Send_Packets();
 
		}
 
		NetworkCloseClient(cs);
 
	}
 

	
 
	if (_network_server) {
 
		// We are a server, also close the listensocket
 
		/* We are a server, also close the listensocket */
 
		closesocket(_listensocket);
 
		_listensocket = INVALID_SOCKET;
 
		DEBUG(net, 1, "Closed listener");
 
		NetworkUDPCloseAll();
 
	}
 
	NetworkUDPCloseAll();
 

	
 
	/* Free all queued commands */
 
	while (_local_command_queue != NULL) {
 
		CommandPacket *p = _local_command_queue;
 
		_local_command_queue = _local_command_queue->next;
 
		free(p);
 
	}
 

	
 
	_networking = false;
 
	_network_server = false;
 
}
 

	
 
// Inits the network (cleans sockets and stuff)
 
static void NetworkInitialize()
 
{
 
	NetworkTCPSocketHandler *cs;
 

	
 
	_local_command_queue = NULL;
 

	
 
	// Clean all client-sockets
 
	for (cs = _clients; cs != &_clients[MAX_CLIENTS]; cs++) {
 
		cs->Initialize();
 
@@ -979,25 +989,24 @@ void NetworkRebuildHostList()
 

	
 
// Used by clients, to connect to a server
 
bool NetworkClientConnectGame(const char *host, uint16 port)
 
{
 
	if (!_network_available) return false;
 

	
 
	if (port == 0) return false;
 

	
 
	ttd_strlcpy(_settings_client.network.last_host, host, sizeof(_settings_client.network.last_host));
 
	_settings_client.network.last_port = port;
 

	
 
	NetworkDisconnect();
 
	NetworkUDPCloseAll();
 
	NetworkInitialize();
 

	
 
	// Try to connect
 
	_networking = NetworkConnect(host, port);
 

	
 
	// We are connected
 
	if (_networking) {
 
		IConsoleCmdExec("exec scripts/on_client.scr 0");
 
		NetworkClient_Connected();
 
	} else {
 
		// Connecting failed
 
		NetworkError(STR_NETWORK_ERR_NOCONNECTION);
 
@@ -1105,62 +1114,42 @@ bool NetworkServerStart()
 
// The only difference with NetworkDisconnect, is the packets that is sent
 
void NetworkReboot()
 
{
 
	if (_network_server) {
 
		NetworkTCPSocketHandler *cs;
 
		FOR_ALL_CLIENTS(cs) {
 
			SEND_COMMAND(PACKET_SERVER_NEWGAME)(cs);
 
			cs->Send_Packets();
 
		}
 
	}
 

	
 
	NetworkClose();
 

	
 
	// Free all queued commands
 
	while (_local_command_queue != NULL) {
 
		CommandPacket *p = _local_command_queue;
 
		_local_command_queue = _local_command_queue->next;
 
		free(p);
 
	}
 

	
 
	_networking = false;
 
	_network_server = false;
 
}
 

	
 
// We want to disconnect from the host/clients
 
void NetworkDisconnect()
 
{
 
	if (_network_server) {
 
		NetworkTCPSocketHandler *cs;
 
		FOR_ALL_CLIENTS(cs) {
 
			SEND_COMMAND(PACKET_SERVER_SHUTDOWN)(cs);
 
			cs->Send_Packets();
 
		}
 
	}
 

	
 
	if (_network_advertise) NetworkUDPRemoveAdvertise();
 

	
 
	DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 

	
 
	NetworkClose();
 

	
 
	// Free all queued commands
 
	while (_local_command_queue != NULL) {
 
		CommandPacket *p = _local_command_queue;
 
		_local_command_queue = _local_command_queue->next;
 
		free(p);
 
	}
 

	
 
	_networking = false;
 
	_network_server = false;
 
}
 

	
 
// Receives something from the network
 
static bool NetworkReceive()
 
{
 
	NetworkTCPSocketHandler *cs;
 
	int n;
 
	fd_set read_fd, write_fd;
 
	struct timeval tv;
 

	
 
	FD_ZERO(&read_fd);
 
	FD_ZERO(&write_fd);
src/openttd.cpp
Show inline comments
 
@@ -801,28 +801,26 @@ bool SafeSaveOrLoad(const char *filename
 
	}
 
}
 

	
 
void SwitchMode(int new_mode)
 
{
 
#ifdef ENABLE_NETWORK
 
	/* If we are saving something, the network stays in his current state */
 
	if (new_mode != SM_SAVE) {
 
		/* If the network is active, make it not-active */
 
		if (_networking) {
 
			if (_network_server && (new_mode == SM_LOAD || new_mode == SM_NEWGAME)) {
 
				NetworkReboot();
 
				NetworkUDPCloseAll();
 
			} else {
 
				NetworkDisconnect();
 
				NetworkUDPCloseAll();
 
			}
 
		}
 

	
 
		/* If we are a server, we restart the server */
 
		if (_is_network_server) {
 
			/* But not if we are going to the menu */
 
			if (new_mode != SM_MENU) {
 
				/* check if we should reload the config */
 
				if (_settings_client.network.reload_cfg) {
 
					LoadFromConfig();
 
					_settings_game = _settings_newgame;
 
					ResetGRFConfig(false);
0 comments (0 inline, 0 general)