src/network/network_server.cpp
changeset 5838 9c3129cb019b
parent 5835 e0ff603ae0b7
child 5860 7fdc9b423ba1
equal deleted inserted replaced
5837:96b4b92b86ae 5838:9c3129cb019b
    20 #include "../saveload.h"
    20 #include "../saveload.h"
    21 #include "../vehicle.h"
    21 #include "../vehicle.h"
    22 #include "../station.h"
    22 #include "../station.h"
    23 #include "../variables.h"
    23 #include "../variables.h"
    24 #include "../genworld.h"
    24 #include "../genworld.h"
       
    25 #include "../helpers.hpp"
    25 
    26 
    26 // This file handles all the server-commands
    27 // This file handles all the server-commands
    27 
    28 
    28 static void NetworkHandleCommandQueue(NetworkClientState* cs);
    29 static void NetworkHandleCommandQueue(NetworkClientState* cs);
    29 
    30 
   569 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
   570 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
   570 {
   571 {
   571 	char name[NETWORK_CLIENT_NAME_LENGTH];
   572 	char name[NETWORK_CLIENT_NAME_LENGTH];
   572 	char unique_id[NETWORK_NAME_LENGTH];
   573 	char unique_id[NETWORK_NAME_LENGTH];
   573 	NetworkClientInfo *ci;
   574 	NetworkClientInfo *ci;
   574 	byte playas;
   575 	PlayerID playas;
   575 	NetworkLanguage client_lang;
   576 	NetworkLanguage client_lang;
   576 	char client_revision[NETWORK_REVISION_LENGTH];
   577 	char client_revision[NETWORK_REVISION_LENGTH];
   577 
   578 
   578 	NetworkRecv_string(cs, p, client_revision, sizeof(client_revision));
   579 	NetworkRecv_string(cs, p, client_revision, sizeof(client_revision));
   579 
   580 
   586 		return;
   587 		return;
   587 	}
   588 	}
   588 #endif
   589 #endif
   589 
   590 
   590 	NetworkRecv_string(cs, p, name, sizeof(name));
   591 	NetworkRecv_string(cs, p, name, sizeof(name));
   591 	playas = NetworkRecv_uint8(cs, p);
   592 	playas = (Owner)NetworkRecv_uint8(cs, p);
   592 	client_lang = NetworkRecv_uint8(cs, p);
   593 	client_lang = (NetworkLanguage)NetworkRecv_uint8(cs, p);
   593 	NetworkRecv_string(cs, p, unique_id, sizeof(unique_id));
   594 	NetworkRecv_string(cs, p, unique_id, sizeof(unique_id));
   594 
   595 
   595 	if (cs->has_quit) return;
   596 	if (cs->has_quit) return;
   596 
   597 
   597 	// join another company does not affect these values
   598 	// join another company does not affect these values
   652 {
   653 {
   653 	NetworkPasswordType type;
   654 	NetworkPasswordType type;
   654 	char password[NETWORK_PASSWORD_LENGTH];
   655 	char password[NETWORK_PASSWORD_LENGTH];
   655 	const NetworkClientInfo *ci;
   656 	const NetworkClientInfo *ci;
   656 
   657 
   657 	type = NetworkRecv_uint8(cs, p);
   658 	type = (NetworkPasswordType)NetworkRecv_uint8(cs, p);
   658 	NetworkRecv_string(cs, p, password, sizeof(password));
   659 	NetworkRecv_string(cs, p, password, sizeof(password));
   659 
   660 
   660 	if (cs->status == STATUS_INACTIVE && type == NETWORK_GAME_PASSWORD) {
   661 	if (cs->status == STATUS_INACTIVE && type == NETWORK_GAME_PASSWORD) {
   661 		// Check game-password
   662 		// Check game-password
   662 		if (strcmp(password, _network_game_info.server_password) != 0) {
   663 		if (strcmp(password, _network_game_info.server_password) != 0) {
   790 {
   791 {
   791 	NetworkClientState *new_cs;
   792 	NetworkClientState *new_cs;
   792 	const NetworkClientInfo *ci;
   793 	const NetworkClientInfo *ci;
   793 	byte callback;
   794 	byte callback;
   794 
   795 
   795 	CommandPacket *cp = malloc(sizeof(CommandPacket));
   796 	CommandPacket *cp;
       
   797 	MallocT(&cp, 1);
   796 
   798 
   797 	// The client was never joined.. so this is impossible, right?
   799 	// The client was never joined.. so this is impossible, right?
   798 	//  Ignore the packet, give the client a warning, and close his connection
   800 	//  Ignore the packet, give the client a warning, and close his connection
   799 	if (cs->status < STATUS_DONE_MAP || cs->has_quit) {
   801 	if (cs->status < STATUS_DONE_MAP || cs->has_quit) {
   800 		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
   802 		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
   801 		return;
   803 		return;
   802 	}
   804 	}
   803 
   805 
   804 	cp->player = NetworkRecv_uint8(cs, p);
   806 	cp->player = (Owner)NetworkRecv_uint8(cs, p);
   805 	cp->cmd    = NetworkRecv_uint32(cs, p);
   807 	cp->cmd    = NetworkRecv_uint32(cs, p);
   806 	cp->p1     = NetworkRecv_uint32(cs, p);
   808 	cp->p1     = NetworkRecv_uint32(cs, p);
   807 	cp->p2     = NetworkRecv_uint32(cs, p);
   809 	cp->p2     = NetworkRecv_uint32(cs, p);
   808 	cp->tile   = NetworkRecv_uint32(cs, p);
   810 	cp->tile   = NetworkRecv_uint32(cs, p);
   809 	NetworkRecv_string(cs, p, cp->text, lengthof(cp->text));
   811 	NetworkRecv_string(cs, p, cp->text, lengthof(cp->text));
   849 		}
   851 		}
   850 
   852 
   851 		/* XXX - Execute the command as a valid player. Normally this would be done by a
   853 		/* XXX - Execute the command as a valid player. Normally this would be done by a
   852 		 * spectator, but that is not allowed any commands. So do an impersonation. The drawback
   854 		 * spectator, but that is not allowed any commands. So do an impersonation. The drawback
   853 		 * of this is that the first company's last_built_tile is also updated... */
   855 		 * of this is that the first company's last_built_tile is also updated... */
   854 		cp->player = 0;
   856 		cp->player = OWNER_BEGIN;
   855 		cp->p2 = cs - _clients; // XXX - UGLY! p2 is mis-used to get the client-id in CmdPlayerCtrl
   857 		cp->p2 = cs - _clients; // XXX - UGLY! p2 is mis-used to get the client-id in CmdPlayerCtrl
   856 	}
   858 	}
   857 
   859 
   858 	// The frame can be executed in the same frame as the next frame-packet
   860 	// The frame can be executed in the same frame as the next frame-packet
   859 	//  That frame just before that frame is saved in _frame_counter_max
   861 	//  That frame just before that frame is saved in _frame_counter_max
   888 	// This packets means a client noticed an error and is reporting this
   890 	// This packets means a client noticed an error and is reporting this
   889 	//  to us. Display the error and report it to the other clients
   891 	//  to us. Display the error and report it to the other clients
   890 	NetworkClientState *new_cs;
   892 	NetworkClientState *new_cs;
   891 	char str[100];
   893 	char str[100];
   892 	char client_name[NETWORK_CLIENT_NAME_LENGTH];
   894 	char client_name[NETWORK_CLIENT_NAME_LENGTH];
   893 	NetworkErrorCode errorno = NetworkRecv_uint8(cs, p);
   895 	NetworkErrorCode errorno = (NetworkErrorCode)NetworkRecv_uint8(cs, p);
   894 
   896 
   895 	// The client was never joined.. thank the client for the packet, but ignore it
   897 	// The client was never joined.. thank the client for the packet, but ignore it
   896 	if (cs->status < STATUS_DONE_MAP || cs->has_quit) {
   898 	if (cs->status < STATUS_DONE_MAP || cs->has_quit) {
   897 		cs->has_quit = true;
   899 		cs->has_quit = true;
   898 		return;
   900 		return;
  1042 
  1044 
  1043 		// Display the message locally (so you know you have sent it)
  1045 		// Display the message locally (so you know you have sent it)
  1044 		if (ci != NULL && show_local) {
  1046 		if (ci != NULL && show_local) {
  1045 			if (from_index == NETWORK_SERVER_INDEX) {
  1047 			if (from_index == NETWORK_SERVER_INDEX) {
  1046 				char name[NETWORK_NAME_LENGTH];
  1048 				char name[NETWORK_NAME_LENGTH];
  1047 				StringID str = IsValidPlayer(ci_to->client_playas) ? GetPlayer(ci_to->client_playas)->name_1 : STR_NETWORK_SPECTATORS;
  1049 				StringID str = IsValidPlayer(ci_to->client_playas) ? GetPlayer(ci_to->client_playas)->name_1 : (uint16)STR_NETWORK_SPECTATORS;
  1048 				GetString(name, str, lastof(name));
  1050 				GetString(name, str, lastof(name));
  1049 				NetworkTextMessage(action, GetDrawStringPlayerColor(ci_own->client_playas), true, name, "%s", msg);
  1051 				NetworkTextMessage(action, GetDrawStringPlayerColor(ci_own->client_playas), true, name, "%s", msg);
  1050 			} else {
  1052 			} else {
  1051 				FOR_ALL_CLIENTS(cs) {
  1053 				FOR_ALL_CLIENTS(cs) {
  1052 					if (cs->index == from_index) {
  1054 					if (cs->index == from_index) {
  1071 	}
  1073 	}
  1072 }
  1074 }
  1073 
  1075 
  1074 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_CHAT)
  1076 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_CHAT)
  1075 {
  1077 {
  1076 	NetworkAction action = NetworkRecv_uint8(cs, p);
  1078 	NetworkAction action = (NetworkAction)NetworkRecv_uint8(cs, p);
  1077 	DestType desttype = NetworkRecv_uint8(cs, p);
  1079 	DestType desttype = (DestType)NetworkRecv_uint8(cs, p);
  1078 	int dest = NetworkRecv_uint8(cs, p);
  1080 	int dest = NetworkRecv_uint8(cs, p);
  1079 	char msg[MAX_TEXT_MSG_LEN];
  1081 	char msg[MAX_TEXT_MSG_LEN];
  1080 
  1082 
  1081 	NetworkRecv_string(cs, p, msg, MAX_TEXT_MSG_LEN);
  1083 	NetworkRecv_string(cs, p, msg, MAX_TEXT_MSG_LEN);
  1082 
  1084