Changeset - r716:ae3edbaa50e7
[Not reviewed]
master
0 15 0
truelight - 19 years ago 2004-12-19 10:17:26
truelight@openttd.org
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
and 'void' where needed, prefixed all functions, typedefs and global
vars with 'Network' and organized all externals nicely.
15 files changed with 118 insertions and 116 deletions:
0 comments (0 inline, 0 general)
console_cmds.c
Show inline comments
 
@@ -144,13 +144,13 @@ DEF_CONSOLE_CMD(ConScrollToTile)
 
#ifdef ENABLE_NETWORK
 

	
 
DEF_CONSOLE_CMD(ConStatus)
 
{
 
	const char *status;
 
	int lag;
 
	const ClientState *cs;
 
	const NetworkClientState *cs;
 
	const NetworkClientInfo *ci;
 
	FOR_ALL_CLIENTS(cs) {
 
		lag = NetworkCalculateLag(cs);
 
		ci = DEREF_CLIENT_INFO(cs);
 

	
 
		switch (cs->status) {
 
@@ -214,13 +214,13 @@ DEF_CONSOLE_CMD(ConKick)
 
	return NULL;
 
}
 

	
 
DEF_CONSOLE_CMD(ConResetCompany)
 
{
 
	Player *p;
 
	ClientState *cs;
 
	NetworkClientState *cs;
 
	NetworkClientInfo *ci;
 

	
 
	if (argc == 2) {
 
		uint32 index = atoi(argv[1]);
 

	
 
		/* Check valid range */
economy.c
Show inline comments
 
@@ -399,13 +399,13 @@ static void PlayersCheckBankrupt(Player 
 
			} else {
 
#ifdef ENABLE_NETWORK
 
				if (IS_HUMAN_PLAYER(owner) && _network_server) {
 
					// If we are the server, make sure it is clear that his player is no
 
					//  longer with us!
 
					NetworkClientInfo *ci;
 
					ClientState *cs;
 
					NetworkClientState *cs;
 
					/* Find all clients that were in control of this company */
 
					FOR_ALL_CLIENTS(cs) {
 
						ci = DEREF_CLIENT_INFO(cs);
 
						if ((ci->client_playas-1) == owner) {
 
							ci->client_playas = OWNER_SPECTATOR;
 
							// Send the new info to all the clients
network.c
Show inline comments
 
@@ -32,12 +32,16 @@ static Patches network_tmp_patches;
 

	
 
// The amount of clients connected
 
static byte _network_clients_connected = 0;
 
// The index counter for new clients (is never decreased)
 
static uint16 _network_client_index = NETWORK_SERVER_INDEX + 1;
 

	
 
/* Some externs / forwards */
 
extern void ShowJoinStatusWindow();
 
extern void StateGameLoop();
 

	
 
// Function that looks up the CI for a given client-index
 
NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index)
 
{
 
	NetworkClientInfo *ci;
 

	
 
	for (ci = _network_client_info; ci != &_network_client_info[MAX_CLIENT_INFO]; ci++)
 
@@ -45,26 +49,26 @@ NetworkClientInfo *NetworkFindClientInfo
 
			return ci;
 

	
 
	return NULL;
 
}
 

	
 
// Function that looks up the CS for a given client-index
 
ClientState *NetworkFindClientStateFromIndex(uint16 client_index)
 
NetworkClientState *NetworkFindClientStateFromIndex(uint16 client_index)
 
{
 
	ClientState *cs;
 
	NetworkClientState *cs;
 

	
 
	for (cs = _clients; cs != &_clients[MAX_CLIENT_INFO]; cs++)
 
		if (cs->index == client_index)
 
			return cs;
 

	
 
	return NULL;
 
}
 

	
 
// NetworkGetClientName is a server-safe function to get the name of the client
 
//  if the user did not send it yet, Client #<no> is used.
 
void NetworkGetClientName(char *client_name, size_t size, ClientState *cs)
 
void NetworkGetClientName(char *client_name, size_t size, const NetworkClientState *cs)
 
{
 
	NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
 
	if (ci->client_name[0] == '\0')
 
		snprintf(client_name, size, "Client #%d", cs->index);
 
	else
 
		snprintf(client_name, size, "%s", ci->client_name);
 
@@ -116,13 +120,13 @@ void CDECL NetworkTextMessage(NetworkAct
 
			AddTextMessage(color, duration, "[All] %s: %s", name, buf);
 
			break;
 
	}
 
}
 

	
 
// Calculate the frame-lag of a client
 
uint NetworkCalculateLag(const ClientState *cs)
 
uint NetworkCalculateLag(const NetworkClientState *cs)
 
{
 
	int lag = cs->last_frame_server - cs->last_frame;
 
	// This client has missed his ACK packet after 1 DAY_TICKS..
 
	//  so we increase his lag for every frame that passes!
 
	// The packet can be out by a max of _net_frame_freq
 
	if (cs->last_frame_server + DAY_TICKS + _network_frame_freq < _frame_counter)
 
@@ -147,21 +151,21 @@ void ClientStartError(char *error) {
 

	
 
void ServerStartError(char *error) {
 
	DEBUG(net, 0)("[NET] Server could not start network: %s",error);
 
	NetworkError(STR_NETWORK_ERR_SERVER_START);
 
}
 

	
 
void NetworkClientError(byte res, ClientState *cs) {
 
static void NetworkClientError(byte res, NetworkClientState *cs) {
 
	// First, send a CLIENT_ERROR to the server, so he knows we are
 
	//  disconnection (and why!)
 
	NetworkErrorCode errorno;
 

	
 
	// We just want to close the connection..
 
	if (res == NETWORK_RECV_STATUS_CLOSE_QUERY) {
 
		cs->quited = true;
 
		CloseClient(cs);
 
		NetworkCloseClient(cs);
 
		_networking = false;
 

	
 
		DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 
		return;
 
	}
 

	
 
@@ -176,18 +180,18 @@ void NetworkClientError(byte res, Client
 

	
 
		// Dequeue all commands before closing the socket
 
		NetworkSend_Packets(DEREF_CLIENT(0));
 
	}
 

	
 
	_switch_mode = SM_MENU;
 
	CloseClient(cs);
 
	NetworkCloseClient(cs);
 
	_networking = false;
 
}
 

	
 
// Find all IP-aliases for this host
 
void NetworkFindIPs(void)
 
static void NetworkFindIPs(void)
 
{
 
	int i, last;
 

	
 
#if defined(BEOS_NET_SERVER) /* doesn't have neither getifaddrs or net/if.h */
 
	/* Based on Andrew Bachmann's netstat+.c. Big thanks to him! */
 
	int _netstat(int fd, char **output, int verbose);
 
@@ -385,15 +389,15 @@ void ParseConnectionString(const byte **
 
		}
 
	}
 
}
 

	
 
// Creates a new client from a socket
 
//   Used both by the server and the client
 
static ClientState *AllocClient(SOCKET s)
 
static NetworkClientState *NetworkAllocClient(SOCKET s)
 
{
 
	ClientState *cs;
 
	NetworkClientState *cs;
 
	NetworkClientInfo *ci;
 
	byte client_no;
 

	
 
	client_no = 0;
 

	
 
	if (_network_server) {
 
@@ -426,26 +430,26 @@ static ClientState *AllocClient(SOCKET s
 
	}
 

	
 
	return cs;
 
}
 

	
 
// Close a connection
 
void CloseClient(ClientState *cs)
 
void NetworkCloseClient(NetworkClientState *cs)
 
{
 
	NetworkClientInfo *ci;
 
	// Socket is already dead
 
	if (cs->socket == INVALID_SOCKET) return;
 

	
 
	DEBUG(net, 1) ("[NET] Closed client connection");
 

	
 
	if (!cs->quited && _network_server && cs->status > STATUS_INACTIVE) {
 
		// We did not receive a leave message from this client...
 
		NetworkErrorCode errorno = NETWORK_ERROR_CONNECTION_LOST;
 
		char str1[100], str2[100];
 
		char client_name[NETWORK_NAME_LENGTH];
 
		ClientState *new_cs;
 
		NetworkClientState *new_cs;
 

	
 
		NetworkGetClientName(client_name, sizeof(client_name), cs);
 

	
 
		GetString(str1, STR_NETWORK_ERR_LEFT);
 
		GetString(str2, STR_NETWORK_ERR_CLIENT_GENERAL + errorno);
 

	
 
@@ -500,16 +504,14 @@ void CloseClient(ClientState *cs)
 
	cs->socket = INVALID_SOCKET;
 
	cs->status = STATUS_INACTIVE;
 
	cs->index = NETWORK_EMPTY_INDEX;
 
	ci->client_index = NETWORK_EMPTY_INDEX;
 
}
 

	
 
extern void ShowJoinStatusWindow();
 

	
 
// A client wants to connect to a server
 
bool NetworkConnect(const char *hostname, int port)
 
static bool NetworkConnect(const char *hostname, int port)
 
{
 
	SOCKET s;
 
	struct sockaddr_in sin;
 

	
 
	DEBUG(net, 1) ("[NET] Connecting to %s %d", hostname, port);
 

	
 
@@ -547,13 +549,13 @@ bool NetworkConnect(const char *hostname
 
		if (ioctlsocket(s, FIONBIO, &blocking) != 0)
 
		#endif
 
			DEBUG(net, 0)("[NET] Setting non-blocking failed"); /* XXX should this be an error? */
 
	}
 

	
 
	// in client mode, only the first client field is used. it's pointing to the server.
 
	AllocClient(s);
 
	NetworkAllocClient(s);
 

	
 
	ShowJoinStatusWindow();
 

	
 
	memcpy(&network_tmp_patches, &_patches, sizeof(_patches));
 

	
 
	return true;
 
@@ -561,13 +563,13 @@ bool NetworkConnect(const char *hostname
 

	
 
// For the server, to accept new clients
 
static void NetworkAcceptClients(void)
 
{
 
	struct sockaddr_in sin;
 
	SOCKET s;
 
	ClientState *cs;
 
	NetworkClientState *cs;
 
#ifndef __MORPHOS__
 
	int sin_len;
 
#else
 
	LONG sin_len; // for some reason we need a 'LONG' under MorphOS
 
#endif
 

	
 
@@ -591,13 +593,13 @@ static void NetworkAcceptClients(void)
 
		// set nodelay
 
		#if !defined(BEOS_NET_SERVER) // not implemented on BeOS net_server...
 
		// The (const char*) cast is needed for windows!!
 
		{int b = 1; setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b));}
 
		#endif
 

	
 
		cs = AllocClient(s);
 
		cs = NetworkAllocClient(s);
 
		if (cs == NULL) {
 
			// no more clients allowed?
 
			// Send to the client that we are full!
 
			Packet *p = NetworkSend_Init(PACKET_SERVER_FULL);
 

	
 
			p->buffer[0] = p->size & 0xFF;
 
@@ -623,13 +625,13 @@ static void NetworkAcceptClients(void)
 
			ci->client_ip = sin.sin_addr.s_addr;
 
		}
 
	}
 
}
 

	
 
// Set up the listen socket for the server
 
bool NetworkListen(void)
 
static bool NetworkListen(void)
 
{
 
	SOCKET ls;
 
	struct sockaddr_in sin;
 
	int port;
 

	
 
	port = _network_server_port;
 
@@ -679,37 +681,37 @@ bool NetworkListen(void)
 
	_listensocket = ls;
 

	
 
	return true;
 
}
 

	
 
// Close all current connections
 
void NetworkClose(void)
 
static void NetworkClose(void)
 
{
 
	ClientState *cs;
 
	NetworkClientState *cs;
 

	
 
	FOR_ALL_CLIENTS(cs) {
 
		if (!_network_server) {
 
			SEND_COMMAND(PACKET_CLIENT_QUIT)("leaving");
 
			NetworkSend_Packets(cs);
 
		}
 
		CloseClient(cs);
 
		NetworkCloseClient(cs);
 
	}
 

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

	
 
// Inits the network (cleans sockets and stuff)
 
void NetworkInitialize(void)
 
static void NetworkInitialize(void)
 
{
 
	ClientState *cs;
 
	NetworkClientState *cs;
 
	uint i;
 

	
 
	_local_command_queue = NULL;
 

	
 
	// Clean all client-sockets
 
	memset(_clients, 0, sizeof(_clients));
 
@@ -732,13 +734,13 @@ void NetworkInitialize(void)
 

	
 
	NetworkUDPInitialize();
 

	
 
	// add all servers from the config file to our list
 
	for (i=0; i != lengthof(_network_server_list); i++) {
 
		if (_network_server_list[i] == NULL) break;
 
		AddServer(_network_server_list[i]);
 
		NetworkAddServer(_network_server_list[i]);
 
	}
 
}
 

	
 
// Query a server to fetch his game-info
 
//  If game_info is true, only the gameinfo is fetched,
 
//   else only the client_info is fetched
 
@@ -772,28 +774,31 @@ void NetworkQueryServer(const byte* host
 
	// No networking, close everything down again
 
	NetworkDisconnect();
 
}
 

	
 
// validates an address entered as a string and adds the server to
 
// the list
 
void AddServer(byte *b)
 
void NetworkAddServer(const byte *b)
 
{
 
	if (*b != '\0') {
 
		const byte *port = NULL;
 
		const byte *player = NULL;
 
		byte host[NETWORK_HOSTNAME_LENGTH];
 
		uint16 rport;
 

	
 
		ttd_strlcpy(host, b, lengthof(host));
 

	
 
		ttd_strlcpy(_network_default_ip, b, lengthof(_network_default_ip));
 
		rport = NETWORK_DEFAULT_PORT;
 

	
 
		ParseConnectionString(&player, &port, b);
 
		ParseConnectionString(&player, &port, host);
 

	
 
		if (player != NULL) _network_playas = atoi(player);
 
		if (port != NULL) rport = atoi(port);
 

	
 
		NetworkQueryServer(b, rport, true);
 
		NetworkQueryServer(host, rport, true);
 
	}
 
}
 

	
 
// Used by clients, to connect to a server
 
bool NetworkClientConnectGame(const byte* host, unsigned short port)
 
{
 
@@ -820,13 +825,13 @@ bool NetworkClientConnectGame(const byte
 
		NetworkError(STR_NETWORK_ERR_NOCONNECTION);
 
	}
 

	
 
	return _networking;
 
}
 

	
 
void NetworkInitGameInfo(void)
 
static void NetworkInitGameInfo(void)
 
{
 
	NetworkClientInfo *ci;
 

	
 
	ttd_strlcpy(_network_game_info.server_name, _network_server_name, sizeof(_network_game_info.server_name));
 
	if (_network_game_info.server_name[0] == '\0')
 
		snprintf(_network_game_info.server_name, sizeof(_network_game_info.server_name), "Unnamed Server");
 
@@ -910,13 +915,13 @@ bool NetworkServerStart(void)
 

	
 
// The server is rebooting...
 
// The only difference with NetworkDisconnect, is the packets that is sent
 
void NetworkReboot(void)
 
{
 
	if (_network_server) {
 
		ClientState *cs;
 
		NetworkClientState *cs;
 
		FOR_ALL_CLIENTS(cs) {
 
			SEND_COMMAND(PACKET_SERVER_NEWGAME)(cs);
 
			NetworkSend_Packets(cs);
 
		}
 
	}
 

	
 
@@ -934,13 +939,13 @@ void NetworkReboot(void)
 
}
 

	
 
// We want to disconnect from the host/clients
 
void NetworkDisconnect(void)
 
{
 
	if (_network_server) {
 
		ClientState *cs;
 
		NetworkClientState *cs;
 
		FOR_ALL_CLIENTS(cs) {
 
			SEND_COMMAND(PACKET_SERVER_SHUTDOWN)(cs);
 
			NetworkSend_Packets(cs);
 
		}
 
	}
 

	
 
@@ -961,15 +966,15 @@ void NetworkDisconnect(void)
 

	
 
	_networking = false;
 
	_network_server = false;
 
}
 

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

	
 
	FD_ZERO(&read_fd);
 
	FD_ZERO(&write_fd);
 
@@ -1019,13 +1024,13 @@ bool NetworkReceive(void)
 
	return true;
 
}
 

	
 
// This sends all buffered commands (if possible)
 
static void NetworkSend(void)
 
{
 
	ClientState *cs;
 
	NetworkClientState *cs;
 
	FOR_ALL_CLIENTS(cs) {
 
		if (cs->writable) {
 
			NetworkSend_Packets(cs);
 

	
 
			if (cs->status == STATUS_MAP) {
 
				// This client is in the middle of a map-send, call the function for that
 
@@ -1033,13 +1038,13 @@ static void NetworkSend(void)
 
			}
 
		}
 
	}
 
}
 

	
 
// Handle the local-command-queue
 
void NetworkHandleLocalQueue(void)
 
static void NetworkHandleLocalQueue(void)
 
{
 
	if (_local_command_queue != NULL) {
 
		CommandPacket *cp;
 
		CommandPacket *cp_prev;
 

	
 
		cp = _local_command_queue;
 
@@ -1068,16 +1073,13 @@ void NetworkHandleLocalQueue(void)
 
				cp = cp->next;
 
			}
 
		}
 
	}
 
}
 

	
 

	
 
extern void StateGameLoop();
 

	
 
bool NetworkDoClientLoop(void)
 
static bool NetworkDoClientLoop(void)
 
{
 
	_frame_counter++;
 

	
 
	NetworkHandleLocalQueue();
 

	
 
	StateGameLoop();
 
@@ -1165,13 +1167,13 @@ void NetworkGameLoop(void)
 
		}
 
	}
 

	
 
	NetworkSend();
 
}
 

	
 
void NetworkGenerateUniqueId()
 
static void NetworkGenerateUniqueId(void)
 
{
 
	md5_state_t state;
 
	md5_byte_t digest[16];
 
	char hex_output[16*2 + 1];
 
	char coding_string[NETWORK_NAME_LENGTH];
 
	int di;
network.h
Show inline comments
 
@@ -190,9 +190,9 @@ VARDEF bool _network_available;  // is n
 
VARDEF bool _network_server; // network-server is active
 
VARDEF bool _network_dedicated; // are we a dedicated server?
 
VARDEF byte _network_playas; // an id to play as..
 

	
 
void ParseConnectionString(const byte **player, const byte **port, byte *connection_string);
 
void NetworkUpdateClientInfo(uint16 client_index);
 
void AddServer(byte *b);
 
void NetworkAddServer(const byte *b);
 

	
 
#endif /* NETWORK_H */
network_client.c
Show inline comments
 
@@ -12,12 +12,14 @@
 
#include "settings.h"
 

	
 

	
 
// This file handles all the client-commands
 

	
 

	
 
extern const char _openttd_revision[];
 

	
 
// So we don't make too much typos ;)
 
#define MY_CLIENT DEREF_CLIENT(0)
 

	
 
static uint32 last_ack_frame;
 

	
 
void NetworkRecvPatchSettings(Packet *p);
 
@@ -40,14 +42,12 @@ DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_CO
 
	InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
 

	
 
	p = NetworkSend_Init(PACKET_CLIENT_COMPANY_INFO);
 
	NetworkSend_Packet(p, MY_CLIENT);
 
}
 

	
 
extern const char _openttd_revision[];
 

	
 
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_JOIN)
 
{
 
	//
 
	// Packet: CLIENT_JOIN
 
	// Function: Try to join the server
 
	// Data:
 
@@ -802,13 +802,13 @@ void NetworkClient_Connected(void)
 
	last_ack_frame = 0;
 
	// Request the game-info
 
	SEND_COMMAND(PACKET_CLIENT_JOIN)();
 
}
 

	
 
// Reads the packets from the socket-stream, if available
 
NetworkRecvStatus NetworkClient_ReadPackets(ClientState *cs)
 
NetworkRecvStatus NetworkClient_ReadPackets(NetworkClientState *cs)
 
{
 
	Packet *p;
 
	NetworkRecvStatus res = NETWORK_RECV_STATUS_OKAY;
 

	
 
	while (res == NETWORK_RECV_STATUS_OKAY && (p = NetworkRecv_Packet(cs, &res)) != NULL) {
 
		byte type = NetworkRecv_uint8(p);
network_client.h
Show inline comments
 
@@ -11,12 +11,12 @@ DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLI
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_CHAT)(NetworkAction action, DestType desttype, int dest, const char *msg);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_PASSWORD)(NetworkPasswordType type, const char *password);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_PASSWORD)(const char *password);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_NAME)(const char *name);
 
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_ACK);
 

	
 
NetworkRecvStatus NetworkClient_ReadPackets(ClientState *cs);
 
NetworkRecvStatus NetworkClient_ReadPackets(NetworkClientState *cs);
 
void NetworkClient_Connected(void);
 

	
 
#endif /* ENABLE_NETWORK */
 

	
 
#endif // NETWORK_CLIENT_H
network_data.c
Show inline comments
 
@@ -83,13 +83,13 @@ void NetworkSend_string(Packet *packet, 
 
assert_compile(sizeof(PacketSize) == 2);
 

	
 
// This function puts the packet in the send-queue and it is send
 
//  as soon as possible
 
// (that is: the next tick, or maybe one tick later if the
 
//   OS-network-buffer is full)
 
void NetworkSend_Packet(Packet *packet, ClientState *cs)
 
void NetworkSend_Packet(Packet *packet, NetworkClientState *cs)
 
{
 
	Packet *p;
 
	assert(packet != NULL);
 

	
 
	packet->pos = 0;
 
	packet->next = NULL;
 
@@ -111,15 +111,15 @@ void NetworkSend_Packet(Packet *packet, 
 

	
 
// Functions to help NetworkRecv_Packet/NetworkSend_Packet a bit
 
//  A socket can make errors. When that happens
 
//  this handles what to do.
 
// For clients: close connection and drop back to main-menu
 
// For servers: close connection and that is it
 
NetworkRecvStatus CloseConnection(ClientState *cs)
 
NetworkRecvStatus CloseConnection(NetworkClientState *cs)
 
{
 
	CloseClient(cs);
 
	NetworkCloseClient(cs);
 

	
 
	// Clients drop back to the main menu
 
	if (!_network_server) {
 
		_switch_mode = SM_MENU;
 
		_networking = false;
 
		_switch_mode_errorstr = STR_NETWORK_ERR_LOSTCONNECTION;
 
@@ -133,13 +133,13 @@ NetworkRecvStatus CloseConnection(Client
 
// Sends all the buffered packets out for this client
 
//  it stops when:
 
//   1) all packets are send (queue is empty)
 
//   2) the OS reports back that it can not send any more
 
//        data right now (full network-buffer, it happens ;))
 
//   3) sending took too long
 
bool NetworkSend_Packets(ClientState *cs)
 
bool NetworkSend_Packets(NetworkClientState *cs)
 
{
 
	ssize_t res;
 
	Packet *p;
 

	
 
	// We can not write to this socket!!
 
	if (!cs->writable) return false;
 
@@ -239,13 +239,13 @@ void NetworkRecv_string(Packet *p, char*
 

	
 
// If PacketSize changes of size, you have to change the 2 packet->size
 
//   lines below matching the size of packet->size/PacketSize!
 
// (the line: 'p->size = (uint16)p->buffer[0];' and below)
 
assert_compile(sizeof(PacketSize) == 2);
 

	
 
Packet *NetworkRecv_Packet(ClientState *cs, NetworkRecvStatus *status)
 
Packet *NetworkRecv_Packet(NetworkClientState *cs, NetworkRecvStatus *status)
 
{
 
	ssize_t res;
 
	Packet *p;
 

	
 
	*status = NETWORK_RECV_STATUS_OKAY;
 

	
 
@@ -327,13 +327,13 @@ Packet *NetworkRecv_Packet(ClientState *
 
	cs->packet_recv = NULL;
 

	
 
	return p;
 
}
 

	
 
// Add a command to the local command queue
 
void NetworkAddCommandQueue(ClientState *cs, CommandPacket *cp)
 
void NetworkAddCommandQueue(NetworkClientState *cs, CommandPacket *cp)
 
{
 
	CommandPacket *new_cp = malloc(sizeof(CommandPacket));
 

	
 
	*new_cp = *cp;
 

	
 
	if (cs->command_queue == NULL)
 
@@ -387,13 +387,13 @@ void NetworkSend_Command(uint32 tile, ui
 
	if (_network_server) {
 
		// If we are the server, we queue the command in our 'special' queue.
 
		//   In theory, we could execute the command right away, but then the
 
		//   client on the server can do everything 1 tick faster then others.
 
		//   So to keep the game fair, we delay the command with 1 tick
 
		//   which gives about the same speed as most clients.
 
		ClientState *cs;
 
		NetworkClientState *cs;
 

	
 
		// And we queue it for delivery to the clients
 
		FOR_ALL_CLIENTS(cs) {
 
			if (cs->status > STATUS_AUTH) {
 
				NetworkAddCommandQueue(cs, c);
 
			}
network_data.h
Show inline comments
 
@@ -104,13 +104,13 @@ typedef enum {
 
typedef enum {
 
	NETWORK_GAME_PASSWORD,
 
	NETWORK_COMPANY_PASSWORD,
 
} NetworkPasswordType;
 

	
 
// To keep the clients all together
 
typedef struct ClientState {
 
typedef struct NetworkClientState {
 
	int socket;
 
	uint16 index;
 
	uint32 last_frame;
 
	uint32 last_frame_server;
 
	byte lag_test; // This byte is used for lag-testing the client
 

	
 
@@ -119,13 +119,13 @@ typedef struct ClientState {
 
	bool quited;
 

	
 
	Packet *packet_queue; // Packets that are awaiting delivery
 
	Packet *packet_recv; // Partially received packet
 

	
 
	CommandPacket *command_queue; // The command-queue awaiting delivery
 
} ClientState;
 
} NetworkClientState;
 

	
 
// What packet types are there
 
// WARNING: The first 3 packets can NEVER change order again
 
//   it protects old clients from joining newer servers (because SERVER_ERROR
 
//   is the respond to a wrong revision)
 
typedef enum {
 
@@ -170,23 +170,23 @@ typedef enum {
 
CommandPacket *_local_command_queue;
 

	
 
SOCKET _udp_client_socket; // udp client socket
 

	
 
// Here we keep track of the clients
 
//  (and the client uses [0] for his own communication)
 
ClientState _clients[MAX_CLIENTS];
 
NetworkClientState _clients[MAX_CLIENTS];
 
#define DEREF_CLIENT(i) (&_clients[i])
 
// This returns the NetworkClientInfo from a ClientState
 
// This returns the NetworkClientInfo from a NetworkClientState
 
#define DEREF_CLIENT_INFO(cs) (&_network_client_info[cs - _clients])
 

	
 
// Macros to make life a bit more easier
 
#define DEF_CLIENT_RECEIVE_COMMAND(type) NetworkRecvStatus NetworkPacketReceive_ ## type ## _command(Packet *p)
 
#define DEF_CLIENT_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command(void)
 
#define DEF_CLIENT_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command
 
#define DEF_SERVER_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(ClientState *cs, Packet *p)
 
#define DEF_SERVER_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command(ClientState *cs)
 
#define DEF_SERVER_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(NetworkClientState *cs, Packet *p)
 
#define DEF_SERVER_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command(NetworkClientState *cs)
 
#define DEF_SERVER_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command
 

	
 
#define SEND_COMMAND(type) NetworkPacketSend_ ## type ## _command
 
#define RECEIVE_COMMAND(type) NetworkPacketReceive_ ## type ## _command
 

	
 
#define FOR_ALL_CLIENTS(cs) for (cs = _clients; cs != &_clients[MAX_CLIENTS] && cs->socket != INVALID_SOCKET; cs++)
 
@@ -194,32 +194,32 @@ ClientState _clients[MAX_CLIENTS];
 
Packet *NetworkSend_Init(PacketType type);
 
void NetworkSend_uint8(Packet *packet, uint8 data);
 
void NetworkSend_uint16(Packet *packet, uint16 data);
 
void NetworkSend_uint32(Packet *packet, uint32 data);
 
void NetworkSend_uint64(Packet *packet, uint64 data);
 
void NetworkSend_string(Packet *packet, const char* data);
 
void NetworkSend_Packet(Packet *packet, ClientState *cs);
 
void NetworkSend_Packet(Packet *packet, NetworkClientState *cs);
 

	
 
uint8 NetworkRecv_uint8(Packet *packet);
 
uint16 NetworkRecv_uint16(Packet *packet);
 
uint32 NetworkRecv_uint32(Packet *packet);
 
uint64 NetworkRecv_uint64(Packet *packet);
 
void NetworkRecv_string(Packet *packet, char* buffer, size_t size);
 
Packet *NetworkRecv_Packet(ClientState *cs, NetworkRecvStatus *status);
 
Packet *NetworkRecv_Packet(NetworkClientState *cs, NetworkRecvStatus *status);
 

	
 
bool NetworkSend_Packets(ClientState *cs);
 
bool NetworkSend_Packets(NetworkClientState *cs);
 
void NetworkExecuteCommand(CommandPacket *cp);
 
void NetworkAddCommandQueue(ClientState *cs, CommandPacket *cp);
 
void NetworkAddCommandQueue(NetworkClientState *cs, CommandPacket *cp);
 

	
 
// from network.c
 
void CloseClient(ClientState *cs);
 
void NetworkCloseClient(NetworkClientState *cs);
 
void CDECL NetworkTextMessage(NetworkAction action, uint16 color, const char *name, const char *str, ...);
 
void NetworkGetClientName(char *clientname, size_t size, ClientState *cs);
 
uint NetworkCalculateLag(const ClientState *cs);
 
void NetworkGetClientName(char *clientname, size_t size, const NetworkClientState *cs);
 
uint NetworkCalculateLag(const NetworkClientState *cs);
 
byte NetworkGetCurrentLanguageIndex();
 
NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index);
 
ClientState *NetworkFindClientStateFromIndex(uint16 client_index);
 
NetworkClientState *NetworkFindClientStateFromIndex(uint16 client_index);
 
unsigned long NetworkResolveHost(const char *hostname);
 

	
 
#endif /* ENABLE_NETWORK */
 

	
 
#endif // NETWORK_DATA_H
network_gamelist.c
Show inline comments
 
@@ -62,13 +62,13 @@ NetworkGameList *NetworkGameListAddItem(
 

	
 
	UpdateNetworkGameWindow(false);
 

	
 
	return item;
 
}
 

	
 
void NetworkGameListAddQueriedItem(NetworkGameInfo *info, bool server_online)
 
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;
network_gamelist.h
Show inline comments
 
#ifndef NETWORK_GAMELIST_H
 
#define NETWORK_GAMELIST_H
 

	
 
void NetworkGameListClear(void);
 
NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port);
 
void NetworkGameListAddQueriedItem(NetworkGameInfo *info, bool server_online);
 
void NetworkGameListAddQueriedItem(const NetworkGameInfo *info, bool server_online);
 

	
 
#endif /* NETWORK_GAMELIST_H */
network_gui.c
Show inline comments
 
@@ -314,13 +314,13 @@ static void NetworkGameWindowWndProc(Win
 
		else
 
			ttd_strlcpy(_network_player_name, "Player", lengthof(_network_player_name));
 

	
 
		break;
 

	
 
	case WE_ON_EDIT_TEXT: {
 
		AddServer(e->edittext.str);
 
		NetworkAddServer(e->edittext.str);
 
	} break;
 

	
 
	case WE_CREATE: {
 
		_selected_item = NULL;
 
	} break;
 
	}
network_server.c
Show inline comments
 
@@ -12,24 +12,26 @@
 
#include "vehicle.h"
 
#include "station.h"
 
#include "settings.h"
 

	
 
// This file handles all the server-commands
 

	
 
void NetworkHandleCommandQueue(ClientState *cs);
 
void NetworkHandleCommandQueue(NetworkClientState *cs);
 
void NetworkPopulateCompanyInfo(void);
 
void NetworkSendPatchSettings(ClientState *cs);
 
void NetworkSendPatchSettings(NetworkClientState *cs);
 

	
 
extern const char _openttd_revision[];
 

	
 
// Is the network enabled?
 

	
 
// **********
 
// Sending functions
 
//   DEF_SERVER_SEND_COMMAND has parameter: ClientState *cs
 
//   DEF_SERVER_SEND_COMMAND has parameter: NetworkClientState *cs
 
// **********
 

	
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CLIENT_INFO)(ClientState *cs, NetworkClientInfo *ci)
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CLIENT_INFO)(NetworkClientState *cs, NetworkClientInfo *ci)
 
{
 
	//
 
	// Packet: SERVER_CLIENT_INFO
 
	// Function: Sends info about a client
 
	// Data:
 
	//    uint16:  The index of the client (always unique on a server. 1 = server)
 
@@ -116,22 +118,22 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_CO
 
			NetworkSend_string(p, _network_player_info[player->index].players);
 

	
 
		NetworkSend_Packet(p, cs);
 
	}
 
}
 

	
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(ClientState *cs, NetworkErrorCode error)
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkClientState *cs, NetworkErrorCode error)
 
{
 
	//
 
	// Packet: SERVER_ERROR
 
	// Function: The client made an error
 
	// Data:
 
	//    uint8:  ErrorID (see network_data.h, NetworkErrorCode)
 
	//
 

	
 
	ClientState *new_cs;
 
	NetworkClientState *new_cs;
 
	char str1[100], str2[100];
 
	char client_name[NETWORK_NAME_LENGTH];
 

	
 
	Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR);
 
	NetworkSend_uint8(p, error);
 
	NetworkSend_Packet(p, cs);
 
@@ -164,16 +166,16 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SER
 
	cs->quited = true;
 

	
 
	// Make sure the data get's there before we close the connection
 
	NetworkSend_Packets(cs);
 

	
 
	// The client made a mistake, so drop his connection now!
 
	CloseClient(cs);
 
	NetworkCloseClient(cs);
 
}
 

	
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_NEED_PASSWORD)(ClientState *cs, NetworkPasswordType type)
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_NEED_PASSWORD)(NetworkClientState *cs, NetworkPasswordType type)
 
{
 
	//
 
	// Packet: SERVER_NEED_PASSWORD
 
	// Function: Indication to the client that the server needs a password
 
	// Data:
 
	//    uint8:  Type of password
 
@@ -191,13 +193,13 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WE
 
	// Function: The client is joined and ready to receive his map
 
	// Data:
 
	//    uint16:  Own ClientID
 
	//
 

	
 
	Packet *p;
 
	ClientState *new_cs;
 
	NetworkClientState *new_cs;
 

	
 
	// Invalid packet when status is AUTH or higher
 
	if (cs->status >= STATUS_AUTH)
 
		return;
 

	
 
	cs->status = STATUS_AUTH;
 
@@ -223,13 +225,13 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WA
 
	// Function: The client can not receive the map at the moment because
 
	//             someone else is already receiving the map
 
	// Data:
 
	//    uint8:  Clients awaiting map
 
	//
 
	int waiting = 0;
 
	ClientState *new_cs;
 
	NetworkClientState *new_cs;
 
	Packet *p;
 

	
 
	// Count how many players are waiting in the queue
 
	FOR_ALL_CLIENTS(new_cs) {
 
		if (new_cs->status == STATUS_MAP_WAIT)
 
			waiting++;
 
@@ -324,13 +326,13 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_MA
 
				// Set the status to DONE_MAP, no we will wait for the client
 
				//  to send it is ready (maybe that happens like never ;))
 
				cs->status = STATUS_DONE_MAP;
 
				fclose(file_pointer);
 

	
 
				{
 
					ClientState *new_cs;
 
					NetworkClientState *new_cs;
 
					bool new_map_client = false;
 
					// Check if there is a client waiting for receiving the map
 
					//  and start sending him the map
 
					FOR_ALL_CLIENTS(new_cs) {
 
						if (new_cs->status == STATUS_MAP_WAIT) {
 
							// Check if we already have a new client to send the map to
 
@@ -361,13 +363,13 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_MA
 
			// Not everything is sent, decrease the sent_packets
 
			if (sent_packets > 1) sent_packets /= 2;
 
		}
 
	}
 
}
 

	
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_JOIN)(ClientState *cs, uint16 client_index)
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_JOIN)(NetworkClientState *cs, uint16 client_index)
 
{
 
	//
 
	// Packet: SERVER_JOIN
 
	// Function: A client is joined (all active clients receive this after a
 
	//     PACKET_CLIENT_MAP_OK) Mostly what directly follows is a
 
	//     PACKET_SERVER_CLIENT_INFO
 
@@ -427,13 +429,13 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SY
 
#ifdef NETWORK_SEND_DOUBLE_SEED
 
	NetworkSend_uint32(p, _sync_seed_2);
 
#endif
 
	NetworkSend_Packet(p, cs);
 
}
 

	
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_COMMAND)(ClientState *cs, CommandPacket *cp)
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_COMMAND)(NetworkClientState *cs, CommandPacket *cp)
 
{
 
	//
 
	// Packet: SERVER_COMMAND
 
	// Function: Sends a DoCommand to the client
 
	// Data:
 
	//    uint8:  PlayerID (0..MAX_PLAYERS-1)
 
@@ -467,13 +469,13 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SER
 
	NetworkSend_uint8(p, cp->callback);
 
	NetworkSend_uint32(p, cp->frame);
 

	
 
	NetworkSend_Packet(p, cs);
 
}
 

	
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHAT)(ClientState *cs, NetworkAction action, uint16 client_index, const char *msg)
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHAT)(NetworkClientState *cs, NetworkAction action, uint16 client_index, const char *msg)
 
{
 
	//
 
	// Packet: SERVER_CHAT
 
	// Function: Sends a chat-packet to the client
 
	// Data:
 
	//    uint8:  ActionID (see network_data.h, NetworkAction)
 
@@ -487,13 +489,13 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SER
 
	NetworkSend_uint16(p, client_index);
 
	NetworkSend_string(p, msg);
 

	
 
	NetworkSend_Packet(p, cs);
 
}
 

	
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(ClientState *cs, uint16 client_index, NetworkErrorCode errorno)
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkClientState *cs, uint16 client_index, NetworkErrorCode errorno)
 
{
 
	//
 
	// Packet: SERVER_ERROR_QUIT
 
	// Function: One of the clients made an error and is quiting the game
 
	//      This packet informs the other clients of that.
 
	// Data:
 
@@ -506,13 +508,13 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SER
 
	NetworkSend_uint16(p, client_index);
 
	NetworkSend_uint8(p, errorno);
 

	
 
	NetworkSend_Packet(p, cs);
 
}
 

	
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_QUIT)(ClientState *cs, uint16 client_index, const char *leavemsg)
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_QUIT)(NetworkClientState *cs, uint16 client_index, const char *leavemsg)
 
{
 
	//
 
	// Packet: SERVER_ERROR_QUIT
 
	// Function: A client left the game, and this packets informs the other clients
 
	//      of that.
 
	// Data:
 
@@ -553,22 +555,20 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_NE
 
	Packet *p = NetworkSend_Init(PACKET_SERVER_NEWGAME);
 
	NetworkSend_Packet(p, cs);
 
}
 

	
 
// **********
 
// Receiving functions
 
//   DEF_SERVER_RECEIVE_COMMAND has parameter: ClientState *cs, Packet *p
 
//   DEF_SERVER_RECEIVE_COMMAND has parameter: NetworkClientState *cs, Packet *p
 
// **********
 

	
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO)
 
{
 
	SEND_COMMAND(PACKET_SERVER_COMPANY_INFO)(cs);
 
}
 

	
 
extern const char _openttd_revision[];
 

	
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
 
{
 
	char name[NETWORK_NAME_LENGTH];
 
	char unique_id[NETWORK_NAME_LENGTH];
 
	NetworkClientInfo *ci;
 
	char test_name[NETWORK_NAME_LENGTH];
 
@@ -679,13 +679,13 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
	SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
 
	return;
 
}
 

	
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_GETMAP)
 
{
 
	ClientState *new_cs;
 
	NetworkClientState *new_cs;
 

	
 
	// The client was never joined.. so this is impossible, right?
 
	//  Ignore the packet, give the client a warning, and close his connection
 
	if (cs->status < STATUS_AUTH || cs->quited) {
 
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
 
		return;
 
@@ -708,13 +708,13 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK)
 
{
 
	// Client has the map, now start syncing
 
	if (cs->status == STATUS_DONE_MAP && !cs->quited) {
 
		char client_name[NETWORK_NAME_LENGTH];
 
		char str[100];
 
		ClientState *new_cs;
 
		NetworkClientState *new_cs;
 
		GetString(str, STR_NETWORK_CLIENT_JOINED);
 

	
 
		NetworkGetClientName(client_name, sizeof(client_name), cs);
 

	
 
		NetworkTextMessage(NETWORK_ACTION_JOIN_LEAVE, 1, client_name, str);
 

	
 
@@ -744,13 +744,13 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 

	
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
 
{
 
	// The client has done a command and wants us to handle it
 
	int i;
 
	byte callback;
 
	ClientState *new_cs;
 
	NetworkClientState *new_cs;
 
	NetworkClientInfo *ci;
 
	char *dparam_char;
 

	
 
	CommandPacket *cp = malloc(sizeof(CommandPacket));
 

	
 
	// The client was never joined.. so this is impossible, right?
 
@@ -829,13 +829,13 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
}
 

	
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ERROR)
 
{
 
	// This packets means a client noticed an error and is reporting this
 
	//  to us. Display the error and report it to the other clients
 
	ClientState *new_cs;
 
	NetworkClientState *new_cs;
 
	byte errorno = NetworkRecv_uint8(p);
 
	char str1[100], str2[100];
 
	char client_name[NETWORK_NAME_LENGTH];
 

	
 
	// The client was never joined.. thank the client for the packet, but ignore it
 
	if (cs->status < STATUS_DONE_MAP || cs->quited) {
 
@@ -862,13 +862,13 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
}
 

	
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_QUIT)
 
{
 
	// The client wants to leave. Display this and report it to the other
 
	//  clients.
 
	ClientState *new_cs;
 
	NetworkClientState *new_cs;
 
	char str1[100], str2[100];
 
	char client_name[NETWORK_NAME_LENGTH];
 

	
 
	// The client was never joined.. thank the client for the packet, but ignore it
 
	if (cs->status < STATUS_DONE_MAP || cs->quited) {
 
		cs->quited = true;
 
@@ -905,13 +905,13 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
}
 

	
 

	
 

	
 
void NetworkServer_HandleChat(NetworkAction action, DestType desttype, int dest, const char *msg, byte from_index)
 
{
 
	ClientState *cs;
 
	NetworkClientState *cs;
 
	NetworkClientInfo *ci, *ci_own, *ci_to;
 

	
 
	switch (desttype) {
 
	case DESTTYPE_CLIENT:
 
		if (dest == 1) {
 
			ci = NetworkFindClientInfoFromIndex(from_index);
 
@@ -1033,13 +1033,13 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
			NetworkUpdateClientInfo(ci->client_index);
 
		}
 
	}
 
}
 

	
 
// The layout for the receive-functions by the server
 
typedef void NetworkServerPacket(ClientState *cs, Packet *p);
 
typedef void NetworkServerPacket(NetworkClientState *cs, Packet *p);
 

	
 

	
 
// This array matches PacketType. At an incoming
 
//  packet it is matches against this array
 
//  and that way the right function to handle that
 
//  packet is found.
 
@@ -1081,13 +1081,13 @@ assert_compile(lengthof(_network_server_
 

	
 
extern const SettingDesc patch_settings[];
 

	
 
// This is a TEMPORARY solution to get the patch-settings
 
//  to the client. When the patch-settings are saved in the savegame
 
//  this should be removed!!
 
void NetworkSendPatchSettings(ClientState *cs)
 
void NetworkSendPatchSettings(NetworkClientState *cs)
 
{
 
	const SettingDesc *item;
 
	Packet *p = NetworkSend_Init(PACKET_SERVER_MAP);
 
	NetworkSend_uint8(p, MAP_PACKET_PATCH);
 
	// Now send all the patch-settings in a pretty order..
 

	
 
@@ -1119,13 +1119,13 @@ void NetworkSendPatchSettings(ClientStat
 
void NetworkPopulateCompanyInfo(void)
 
{
 
	char password[NETWORK_PASSWORD_LENGTH];
 
	Player *p;
 
	Vehicle *v;
 
	Station *s;
 
	ClientState *cs;
 
	NetworkClientState *cs;
 
	NetworkClientInfo *ci;
 
	int i;
 
	uint16 months_empty;
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (!p->is_active) {
 
@@ -1219,13 +1219,13 @@ void NetworkPopulateCompanyInfo(void)
 
	}
 
}
 

	
 
// Send a packet to all clients with updated info about this client_index
 
void NetworkUpdateClientInfo(uint16 client_index)
 
{
 
	ClientState *cs;
 
	NetworkClientState *cs;
 
	NetworkClientInfo *ci;
 

	
 
	ci = NetworkFindClientInfoFromIndex(client_index);
 

	
 
	if (ci == NULL)
 
		return;
 
@@ -1239,13 +1239,13 @@ void NetworkUpdateClientInfo(uint16 clie
 
    Two things happen:
 
      1) If a company is not protected, it is closed after 1 year (for example)
 
      2) If a company is protected, protection is disabled after 3 years (for example)
 
           (and item 1. happens a year later) */
 
static void NetworkAutoCleanCompanies()
 
{
 
	ClientState *cs;
 
	NetworkClientState *cs;
 
	NetworkClientInfo *ci;
 
	Player *p;
 
	bool clients_in_company[MAX_PLAYERS];
 

	
 
	if (!_network_autoclean_companies)
 
		return;
 
@@ -1297,13 +1297,13 @@ static void NetworkAutoCleanCompanies()
 
}
 

	
 
// This function changes new_name to a name that is unique (by adding #1 ...)
 
//  and it returns true if that succeeded.
 
bool NetworkFindName(char new_name[NETWORK_NAME_LENGTH])
 
{
 
	ClientState *new_cs;
 
	NetworkClientState *new_cs;
 
	NetworkClientInfo *ci;
 
	bool found_name = false;
 
	byte number = 0;
 
	char original_name[NETWORK_NAME_LENGTH];
 

	
 
	// We use NETWORK_NAME_LENGTH in here, because new_name is really a pointer
 
@@ -1338,13 +1338,13 @@ bool NetworkFindName(char new_name[NETWO
 
	}
 

	
 
	return found_name;
 
}
 

	
 
// Reads a packet from the stream
 
bool NetworkServer_ReadPackets(ClientState *cs)
 
bool NetworkServer_ReadPackets(NetworkClientState *cs)
 
{
 
	Packet *p;
 
	NetworkRecvStatus res;
 
	while((p = NetworkRecv_Packet(cs, &res)) != NULL) {
 
		byte type = NetworkRecv_uint8(p);
 
		if (type < PACKET_END && _network_server_packet[type] != NULL)
 
@@ -1355,13 +1355,13 @@ bool NetworkServer_ReadPackets(ClientSta
 
	}
 

	
 
	return true;
 
}
 

	
 
// Handle the local command-queue
 
void NetworkHandleCommandQueue(ClientState *cs) {
 
void NetworkHandleCommandQueue(NetworkClientState *cs) {
 
	if (cs->command_queue != NULL) {
 
		CommandPacket *cp;
 
		CommandPacket *cp_prev;
 

	
 
		cp = cs->command_queue;
 
		cp_prev = NULL;
 
@@ -1386,13 +1386,13 @@ void NetworkHandleCommandQueue(ClientSta
 
// This is called every tick if this is a _network_server
 
void NetworkServer_Tick(void)
 
{
 
#ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
 
	static uint32 last_sync_frame = 0;
 
#endif
 
	ClientState *cs;
 
	NetworkClientState *cs;
 
	bool send_frame = false;
 

	
 
	// Update max-frame-counter
 
	if (_frame_counter > _frame_counter_max) {
 
		_frame_counter_max = _frame_counter + _network_frame_freq;
 
		send_frame = true;
 
@@ -1407,13 +1407,13 @@ void NetworkServer_Tick(void)
 
			int lag = NetworkCalculateLag(cs) / DAY_TICKS;
 
			if (lag > 0) {
 
				if (lag > 3) {
 
					// Client did still not report in after 4 game-day, drop him
 
					//  (that is, the 3 of above, + 1 before any lag is counted)
 
					IConsolePrintF(_iconsole_color_error,"Client #%d is dropped because the client did not respond for more then 4 game-days", cs->index);
 
					CloseClient(cs);
 
					NetworkCloseClient(cs);
 
					continue;
 
				}
 

	
 
				// Report once per time we detect the lag
 
				if (cs->lag_test == 0) {
 
					IConsolePrintF(_iconsole_color_warning,"[%d] Client #%d is slow, try increasing *net_frame_freq to a higher value!", _frame_counter, cs->index);
 
@@ -1450,12 +1450,12 @@ void NetworkServer_Tick(void)
 
#endif
 

	
 
	/* See if we need to advertise */
 
	NetworkUDPAdvertise();
 
}
 

	
 
void NetworkServerMonthlyLoop()
 
void NetworkServerMonthlyLoop(void)
 
{
 
	NetworkAutoCleanCompanies();
 
}
 

	
 
#endif /* ENABLE_NETWORK */
network_server.h
Show inline comments
 
#ifndef NETWORK_SERVER_H
 
#define NETWORK_SERVER_H
 

	
 
#ifdef ENABLE_NETWORK
 

	
 
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_MAP);
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(ClientState *cs, uint16 client_index, NetworkErrorCode errorno);
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(ClientState *cs, NetworkErrorCode error);
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkClientState *cs, uint16 client_index, NetworkErrorCode errorno);
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkClientState *cs, NetworkErrorCode error);
 
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SHUTDOWN);
 
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_NEWGAME);
 

	
 
bool NetworkFindName(char new_name[NETWORK_NAME_LENGTH]);
 
void NetworkServer_HandleChat(NetworkAction action, DestType desttype, int dest, const char *msg, byte from_index);
 

	
 
bool NetworkServer_ReadPackets(ClientState *cs);
 
void NetworkServer_Tick();
 
void NetworkServerMonthlyLoop();
 
bool NetworkServer_ReadPackets(NetworkClientState *cs);
 
void NetworkServer_Tick(void);
 
void NetworkServerMonthlyLoop(void);
 

	
 
#endif /* ENABLE_NETWORK */
 

	
 
#endif // NETWORK_SERVER_H
network_udp.c
Show inline comments
 
@@ -103,13 +103,13 @@ DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVE
 

	
 
	UpdateNetworkGameWindow(false);
 
}
 

	
 
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO)
 
{
 
	ClientState *cs;
 
	NetworkClientState *cs;
 
	NetworkClientInfo *ci;
 
	Packet *packet;
 
	Player *player;
 
	byte active = 0;
 
	byte current = 0;
 
	int i;
 
@@ -468,13 +468,13 @@ void NetworkUDPQueryServer(const byte* h
 

	
 
	UpdateNetworkGameWindow(false);
 
}
 

	
 
/* Register us to the master server
 
     This function checks if it needs to send an advertise */
 
void NetworkUDPAdvertise()
 
void NetworkUDPAdvertise(void)
 
{
 
	struct sockaddr_in out_addr;
 
	Packet *p;
 

	
 
	/* Check if we should send an advertise */
 
	if (!_networking || !_network_server || !_network_udp_server || !_network_advertise)
network_udp.h
Show inline comments
 
@@ -3,9 +3,9 @@
 

	
 
void NetworkUDPInitialize(void);
 
bool NetworkUDPListen(uint32 host, uint16 port);
 
void NetworkUDPReceive(void);
 
void NetworkUDPSearchGame(void);
 
void NetworkUDPQueryServer(const byte* host, unsigned short port);
 
void NetworkUDPAdvertise();
 
void NetworkUDPAdvertise(void);
 

	
 
#endif /* NETWORK_LAN_H */
0 comments (0 inline, 0 general)