Changeset - r662:d42c2c6d320b
[Not reviewed]
master
0 5 0
truelight - 20 years ago 2004-12-15 15:19:51
truelight@openttd.org
(svn r1096) -Fix: [Network] PlayAs is now registered correctly
-Codewise: [Network] Cleaned some code, removed some comment, changed
some wrong comment
5 files changed with 20 insertions and 17 deletions:
0 comments (0 inline, 0 general)
network.c
Show inline comments
 
@@ -383,49 +383,49 @@ static ClientState *AllocClient(SOCKET s
 
	NetworkClientInfo *ci;
 
	byte client_no;
 

	
 
	client_no = 0;
 

	
 
	if (_network_server) {
 
		// Can we handle a new client?
 
		if (_network_clients_connected >= MAX_CLIENTS)
 
			return NULL;
 

	
 
		if (_network_game_info.clients_on >= _network_game_info.clients_max)
 
			return NULL;
 

	
 
		// Register the login
 
		client_no = _network_clients_connected++;
 
	}
 

	
 
	cs = &_clients[client_no];
 
	memset(cs, 0, sizeof(*cs));
 
	cs->socket = s;
 
	cs->last_frame = 0;
 
	cs->quited = false;
 

	
 
	if (_network_server) {
 
		ci = &_network_client_info[client_no];
 
		ci = DEREF_CLIENT_INFO(cs);
 
		memset(ci, 0, sizeof(*ci));
 

	
 
		cs->index = _network_client_index++;
 
		ci->client_index = cs->index;
 
		ci->join_date = _date;
 

	
 
		InvalidateWindow(WC_CLIENT_LIST, 0);
 
	}
 

	
 
	return cs;
 
}
 

	
 
// Close a connection
 
void CloseClient(ClientState *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;
 
@@ -679,48 +679,49 @@ void NetworkClose(void)
 
		if (!_network_server) {
 
			SEND_COMMAND(PACKET_CLIENT_QUIT)("leaving");
 
			NetworkSend_Packets(cs);
 
		}
 
		CloseClient(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)
 
{
 
	ClientState *cs;
 

	
 
	_local_command_queue = NULL;
 

	
 
	// Clean all client-sockets
 
	memset(_clients, 0, sizeof(_clients));
 
	for (cs = _clients; cs != &_clients[MAX_CLIENTS]; cs++) {
 
		cs->socket = INVALID_SOCKET;
 
		cs->status = STATUS_INACTIVE;
 
		cs->command_queue = NULL;
 
	}
 

	
 
	// Clean the client_info memory
 
	memset(_network_client_info, 0, sizeof(_network_client_info));
 
	memset(_network_player_info, 0, sizeof(_network_player_info));
 

	
 
	_sync_frame = 0;
 
	_network_first_time = true;
 

	
 
	_network_reconnect = 0;
 

	
 
	InitPlayerRandoms();
 

	
 
	NetworkUDPInitialize();
 
}
 

	
 
// 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
 
void NetworkQueryServer(const byte* host, unsigned short port, bool game_info)
network.h
Show inline comments
 
@@ -6,49 +6,48 @@
 
#ifdef ENABLE_NETWORK
 

	
 
// If this line is enable, every frame will have a sync test
 
//  this is not needed in normal games. Normal is like 1 sync in 100
 
//  frames. You can enable this if you have a lot of desyncs on a certain
 
//  game.
 
// Remember: both client and server have to be compiled with this
 
//  option enabled to make it to work. If one of the two has it disabled
 
//  nothing will happen.
 
//#define ENABLE_NETWORK_SYNC_EVERY_FRAME
 

	
 
// In theory sending 1 of the 2 seeds is enough to check for desyncs
 
//   so in theory, this next define can be left off.
 
//#define NETWORK_SEND_DOUBLE_SEED
 

	
 
// How many clients can we have? Like.. MAX_PLAYERS - 1 is the amount of
 
//  players that can really play.. so.. a max of 4 spectators.. gives us..
 
//  MAX_PLAYERS + 3
 
#define MAX_CLIENTS (MAX_PLAYERS + 3)
 

	
 

	
 
// Do not change this next line. It should _ALWAYS_ be MAX_CLIENTS + 1
 
#define MAX_CLIENT_INFO (MAX_CLIENTS + 1)
 

	
 
#define NETWORK_DISCOVER_PORT 3978
 
#define NETWORK_DEFAULT_PORT 3979
 

	
 
#define MAX_INTERFACES 9
 

	
 

	
 
// How many vehicle/station types we put over the network
 
#define NETWORK_VEHICLE_TYPES 5
 
#define NETWORK_STATION_TYPES 5
 

	
 
#define NETWORK_NAME_LENGTH 80
 
#define NETWORK_HOSTNAME_LENGTH 80
 
#define NETWORK_REVISION_LENGTH 10
 
#define NETWORK_PASSWORD_LENGTH 20
 
#define NETWORK_PLAYERS_LENGTH 200
 

	
 
// This is the struct used by both client and server
 
//  some fields will be empty on the client (like game_password) by default
 
//  and only filled with data a player enters.
 
typedef struct NetworkGameInfo {
 
	char server_name[NETWORK_NAME_LENGTH];					// Server name
 
	char hostname[NETWORK_HOSTNAME_LENGTH];					// Hostname of the server (if any)
 
	char server_revision[NETWORK_REVISION_LENGTH];	// The SVN version number the server is using (e.g.: 'r304')
 
																									//  It even shows a SVN version in release-version, so
 
																									//  it is easy to compare if a server is of the correct version
network_client.c
Show inline comments
 
@@ -44,49 +44,49 @@ DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_CO
 
}
 

	
 
extern const char _openttd_revision[];
 

	
 
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_JOIN)
 
{
 
	//
 
	// Packet: CLIENT_JOIN
 
	// Function: Try to join the server
 
	// Data:
 
	//    String: OpenTTD Revision (norev000 if no revision)
 
	//    String: Player Name (max NETWORK_NAME_LENGTH)
 
	//    uint8:  Play as Player id (1..MAX_PLAYERS)
 
	//    uint8:  Language ID
 
	//    String: Unique id to find the player back in server-listing
 
	//
 

	
 
	Packet *p;
 
	_network_join_status = NETWORK_JOIN_STATUS_AUTHORIZING;
 
	InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
 

	
 
	p = NetworkSend_Init(PACKET_CLIENT_JOIN);
 
	NetworkSend_string(p, _openttd_revision);
 
	NetworkSend_string(p, _network_player_name); // Player name
 
	NetworkSend_uint8(p, _network_playas); // Password
 
	NetworkSend_uint8(p, _network_playas); // PlayAs
 
	NetworkSend_uint8(p, NETLANG_ANY); // Language
 
	NetworkSend_string(p, _network_unique_id);
 
	NetworkSend_Packet(p, MY_CLIENT);
 
}
 

	
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_PASSWORD)(NetworkPasswordType type, const char *password)
 
{
 
	//
 
	// Packet: CLIENT_PASSWORD
 
	// Function: Send a password to the server to authorize
 
	// Data:
 
	//    uint8:  NetworkPasswordType
 
	//    String: Password
 
	//
 
	Packet *p = NetworkSend_Init(PACKET_CLIENT_PASSWORD);
 
	NetworkSend_uint8(p, type);
 
	NetworkSend_string(p, password);
 
	NetworkSend_Packet(p, MY_CLIENT);
 
}
 

	
 
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_GETMAP)
 
{
 
	//
 
	// Packet: CLIENT_GETMAP
 
@@ -296,77 +296,84 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER
 
		for (i = 0; i < NETWORK_STATION_TYPES; i++)
 
			_network_player_info[current].num_station[i] = NetworkRecv_uint16(p);
 

	
 
		NetworkRecv_string(p, _network_player_info[current].players, sizeof(_network_player_info[current].players));
 

	
 
		InvalidateWindow(WC_NETWORK_WINDOW, 0);
 

	
 
		if (total == current + 1)
 
			// This was the last one
 
			return NETWORK_RECV_STATUS_CLOSE_QUERY;
 
		else
 
			return NETWORK_RECV_STATUS_OKAY;
 
	}
 

	
 
	return NETWORK_RECV_STATUS_CLOSE_QUERY;
 
}
 

	
 
// This packet contains info about the client (playas and name)
 
//  as client we save this in NetworkClientInfo, linked via 'index'
 
//  which is always an unique number on a server.
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO)
 
{
 
	NetworkClientInfo *ci;
 
	uint16 index = NetworkRecv_uint16(p);
 
	byte playas = NetworkRecv_uint8(p);
 
	char name[NETWORK_NAME_LENGTH];
 
	char unique_id[NETWORK_NAME_LENGTH];
 

	
 
	NetworkRecv_string(p, name, sizeof(name));
 
	NetworkRecv_string(p, unique_id, sizeof(unique_id));
 

	
 
	/* Do we receive a change of data? Most likely we changed playas */
 
	if (index == _network_own_client_index)
 
		_network_playas = playas;
 

	
 
	ci = NetworkFindClientInfoFromIndex(index);
 
	if (ci != NULL) {
 
		byte playas;
 
		char name[NETWORK_NAME_LENGTH];
 

	
 
		playas = NetworkRecv_uint8(p);
 
		NetworkRecv_string(p, name, sizeof(name));
 

	
 
		if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
 
			// Client name changed, display the change
 
			NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, 1, ci->client_name, name);
 
		} else if (playas != ci->client_playas) {
 
			// The player changed from client-player..
 
			// Do not display that for now
 
		}
 

	
 
		ci->client_playas = playas;
 
		ttd_strlcpy(ci->client_name, name, sizeof(ci->client_name));
 

	
 
		return NETWORK_RECV_STATUS_OKAY;
 
	}
 

	
 
	// We don't have this index yet, find an empty index, and put the data there
 
	ci = NetworkFindClientInfoFromIndex(NETWORK_EMPTY_INDEX);
 
	if (ci != NULL) {
 
		ci->client_index = index;
 
		ci->client_playas = NetworkRecv_uint8(p);
 
		NetworkRecv_string(p, ci->client_name, sizeof(ci->client_name));
 
		NetworkRecv_string(p, ci->unique_id, sizeof(ci->unique_id));
 
		ci->client_playas = playas;
 

	
 
		ttd_strlcpy(ci->client_name, name, sizeof(ci->client_name));
 
		ttd_strlcpy(ci->unique_id, unique_id, sizeof(ci->unique_id));
 

	
 
		return NETWORK_RECV_STATUS_OKAY;
 
	}
 

	
 
	// Here the program should never ever come.....
 
	return NETWORK_RECV_STATUS_MALFORMED_PACKET;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR)
 
{
 
	NetworkErrorCode error = NetworkRecv_uint8(p);
 

	
 
	if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED ||
 
			error == NETWORK_ERROR_PLAYER_MISMATCH) {
 
		// We made an error in the protocol, and our connection is closed.... :(
 
		_switch_mode_errorstr = STR_NETWORK_ERR_SERVER_ERROR;
 
	} else if (error == NETWORK_ERROR_WRONG_REVISION) {
 
		// Wrong revision :(
 
		_switch_mode_errorstr = STR_NETWORK_ERR_WRONG_REVISION;
 
	} else if (error == NETWORK_ERROR_WRONG_PASSWORD) {
 
		// Wrong password
 
		_switch_mode_errorstr = STR_NETWORK_ERR_WRONG_PASSWORD;
 
	} else if (error == NETWORK_ERROR_KICKED) {
 
		_switch_mode_errorstr = STR_NETWORK_ERR_KICKED;
 
	}
network_server.c
Show inline comments
 
@@ -757,49 +757,49 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
	cp->cmd = NetworkRecv_uint32(p);
 
	cp->p1 = NetworkRecv_uint32(p);
 
	cp->p2 = NetworkRecv_uint32(p);
 
	cp->tile = NetworkRecv_uint32(p);
 
	/* We are going to send them byte by byte, because dparam is misused
 
	    for chars (if it is used), and else we have a BigEndian / LittleEndian
 
	    problem.. we should fix the misuse of dparam... -- TrueLight */
 
	dparam_char = (char *)&cp->dp[0];
 
	for (i = 0; i < lengthof(cp->dp) * 4; i++) {
 
		*dparam_char = NetworkRecv_uint8(p);
 
		dparam_char++;
 
	}
 

	
 
	callback = NetworkRecv_uint8(p);
 

	
 
	ci = DEREF_CLIENT_INFO(cs);
 
	// Only CMD_PLAYER_CTRL is always allowed, for the rest, playas needs
 
	//  to match the player in the packet
 
	if (cp->cmd != CMD_PLAYER_CTRL && ci->client_playas-1 != cp->player) {
 
		// The player did a command with the wrong player_id.. bad!!
 
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_PLAYER_MISMATCH);
 
		return;
 
	}
 
	if (cp->cmd == CMD_PLAYER_CTRL) {
 
		// UGLY! p1 is mis-used to get the client-id in CmdPlayerCtrl
 
		// UGLY! p2 is mis-used to get the client-id in CmdPlayerCtrl
 
		cp->p2 = cs - _clients;
 
	}
 

	
 

	
 
	// The frame can be executed in the same frame as the next frame-packet
 
	//  That frame just before that frame is saved in _frame_counter_max
 
	cp->frame = _frame_counter_max + 1;
 
	cp->next = NULL;
 

	
 
	// Queue the command for the clients (are send at the end of the frame
 
	//   if they can handle it ;))
 
	FOR_ALL_CLIENTS(new_cs) {
 
		if (new_cs->status > STATUS_AUTH) {
 
			// Callbacks are only send back to the client who sent them in the
 
			//  first place. This filters that out.
 
			if (new_cs != cs)
 
				cp->callback = 0;
 
			else
 
				cp->callback = callback;
 
			NetworkAddCommandQueue(new_cs, cp);
 
		}
 
	}
 

	
 
	cp->callback = 0;
players.c
Show inline comments
 
@@ -641,60 +641,56 @@ int32 CmdPlayerCtrl(int x, int y, uint32
 
	_current_player = OWNER_NONE;
 

	
 
	switch(p1 & 0xff) {
 
	case 0: // make new player
 
		p = DoStartupNewPlayer(false);
 
		if (p != NULL) {
 
			if (_local_player == OWNER_SPECTATOR) {
 
				_local_player = p->index;
 
				MarkWholeScreenDirty();
 
			}
 
#ifdef ENABLE_NETWORK
 
			if (_network_server) {
 
				NetworkClientInfo *ci;
 
				// UGLY! p2 is mis-used to fetch the client-id
 
				ci = &_network_client_info[p2];
 
				ci->client_playas = p->index + 1;
 
				NetworkUpdateClientInfo(ci->client_index);
 

	
 
				if (ci->client_playas != 0 && ci->client_playas <= MAX_PLAYERS) {
 
					memcpy(_decode_parameters, ci->client_name, 32);
 
					/* XXX - What are the consequents of this? It is needed, but is it bad? */
 
					_docommand_recursive = 0;
 
					DoCommandP(0, ci->client_playas-1, 0, NULL, CMD_CHANGE_PRESIDENT_NAME | CMD_MSG(STR_700D_CAN_T_CHANGE_PRESIDENT));
 
				}
 
			} else {
 
				_network_playas = p->index + 1;
 
			}
 
		} else {
 
			if (_network_server) {
 
				NetworkClientInfo *ci;
 
				// UGLY! p2 is mis-used to fetch the client-id
 
				ci = &_network_client_info[p2];
 
				ci->client_playas = OWNER_SPECTATOR;
 
				NetworkUpdateClientInfo(ci->client_index);
 
			} else {
 
				_network_playas = OWNER_SPECTATOR;
 
			}
 
#endif /* ENABLE_NETWORK */
 
		}
 
		break;
 
	case 1: // make new ai player
 
		DoStartupNewPlayer(true);
 
		break;
 
	case 2: // delete player
 
		pi = (byte)(p1 >> 8);
 
		ChangeOwnershipOfPlayerItems(pi, 255);
 
		DeletePlayerStuff(pi);
 
		break;
 

	
 
	case 3: // join player
 
		pi = (byte)(p1 >> 8);
 
		ChangeOwnershipOfPlayerItems(pi, (byte)(p1 >> 16));
 
		DeletePlayerStuff(pi);
 
		break;
 
	}
 

	
 

	
 
	return 0;
 
}
 

	
0 comments (0 inline, 0 general)