src/network/network_server.cpp
changeset 9428 1ba05b499957
parent 9420 8a38703928e8
child 9451 0d6806ba5504
equal deleted inserted replaced
9427:af652de004a0 9428:1ba05b499957
     6 
     6 
     7 #include "../stdafx.h"
     7 #include "../stdafx.h"
     8 #include "../openttd.h" // XXX StringID
     8 #include "../openttd.h" // XXX StringID
     9 #include "../debug.h"
     9 #include "../debug.h"
    10 #include "../strings_func.h"
    10 #include "../strings_func.h"
    11 #include "network_data.h"
    11 #include "network_internal.h"
    12 #include "core/tcp.h"
    12 #include "core/tcp.h"
    13 #include "../vehicle_base.h"
    13 #include "../vehicle_base.h"
    14 #include "../vehicle_func.h"
    14 #include "../vehicle_func.h"
    15 #include "../date_func.h"
    15 #include "../date_func.h"
    16 #include "network_server.h"
    16 #include "network_server.h"
   802 
   802 
   803 		if (_settings_client.network.pause_on_join) {
   803 		if (_settings_client.network.pause_on_join) {
   804 			/* Now pause the game till the client is in sync */
   804 			/* Now pause the game till the client is in sync */
   805 			DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
   805 			DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
   806 
   806 
   807 			NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (incoming client)", NETWORK_SERVER_INDEX);
   807 			NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (incoming client)", NETWORK_SERVER_INDEX);
   808 		}
   808 		}
   809 	} else {
   809 	} else {
   810 		// Wrong status for this packet, give a warning to client, and close connection
   810 		// Wrong status for this packet, give a warning to client, and close connection
   811 		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
   811 		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
   812 	}
   812 	}
  1021 		/* Now he is! Unpause the game */
  1021 		/* Now he is! Unpause the game */
  1022 		cs->status = STATUS_ACTIVE;
  1022 		cs->status = STATUS_ACTIVE;
  1023 
  1023 
  1024 		if (_settings_client.network.pause_on_join) {
  1024 		if (_settings_client.network.pause_on_join) {
  1025 			DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
  1025 			DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
  1026 			NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (client connected)", NETWORK_SERVER_INDEX);
  1026 			NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (client connected)", NETWORK_SERVER_INDEX);
  1027 		}
  1027 		}
  1028 
  1028 
  1029 		CheckMinPlayers();
  1029 		CheckMinPlayers();
  1030 
  1030 
  1031 		/* Execute script for, e.g. MOTD */
  1031 		/* Execute script for, e.g. MOTD */
  1038 	cs->last_frame_server = _frame_counter;
  1038 	cs->last_frame_server = _frame_counter;
  1039 }
  1039 }
  1040 
  1040 
  1041 
  1041 
  1042 
  1042 
  1043 void NetworkServer_HandleChat(NetworkAction action, DestType desttype, int dest, const char *msg, uint16 from_index)
  1043 void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const char *msg, uint16 from_index)
  1044 {
  1044 {
  1045 	NetworkTCPSocketHandler *cs;
  1045 	NetworkTCPSocketHandler *cs;
  1046 	const NetworkClientInfo *ci, *ci_own, *ci_to;
  1046 	const NetworkClientInfo *ci, *ci_own, *ci_to;
  1047 
  1047 
  1048 	switch (desttype) {
  1048 	switch (desttype) {
  1144 	int dest = p->Recv_uint16();
  1144 	int dest = p->Recv_uint16();
  1145 	char msg[MAX_TEXT_MSG_LEN];
  1145 	char msg[MAX_TEXT_MSG_LEN];
  1146 
  1146 
  1147 	p->Recv_string(msg, MAX_TEXT_MSG_LEN);
  1147 	p->Recv_string(msg, MAX_TEXT_MSG_LEN);
  1148 
  1148 
  1149 	NetworkServer_HandleChat(action, desttype, dest, msg, cs->index);
  1149 	NetworkServerSendChat(action, desttype, dest, msg, cs->index);
  1150 }
  1150 }
  1151 
  1151 
  1152 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD)
  1152 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD)
  1153 {
  1153 {
  1154 	char password[NETWORK_PASSWORD_LENGTH];
  1154 	char password[NETWORK_PASSWORD_LENGTH];
  1580 void NetworkServerMonthlyLoop()
  1580 void NetworkServerMonthlyLoop()
  1581 {
  1581 {
  1582 	NetworkAutoCleanCompanies();
  1582 	NetworkAutoCleanCompanies();
  1583 }
  1583 }
  1584 
  1584 
       
  1585 void NetworkServerChangeOwner(PlayerID current_player, PlayerID new_player)
       
  1586 {
       
  1587 	/* The server has to handle all administrative issues, for example
       
  1588 	 * updating and notifying all clients of what has happened */
       
  1589 	NetworkTCPSocketHandler *cs;
       
  1590 	NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
       
  1591 
       
  1592 	/* The server has just changed from player */
       
  1593 	if (current_player == ci->client_playas) {
       
  1594 		ci->client_playas = new_player;
       
  1595 		NetworkUpdateClientInfo(NETWORK_SERVER_INDEX);
       
  1596 	}
       
  1597 
       
  1598 	/* Find all clients that were in control of this company, and mark them as new_player */
       
  1599 	FOR_ALL_CLIENTS(cs) {
       
  1600 		ci = DEREF_CLIENT_INFO(cs);
       
  1601 		if (current_player == ci->client_playas) {
       
  1602 			ci->client_playas = new_player;
       
  1603 			NetworkUpdateClientInfo(ci->client_index);
       
  1604 		}
       
  1605 	}
       
  1606 }
       
  1607 
       
  1608 const char* GetPlayerIP(const NetworkClientInfo* ci)
       
  1609 {
       
  1610 	struct in_addr addr;
       
  1611 
       
  1612 	addr.s_addr = ci->client_ip;
       
  1613 	return inet_ntoa(addr);
       
  1614 }
       
  1615 
       
  1616 void NetworkServerShowStatusToConsole()
       
  1617 {
       
  1618 	static const char* const stat_str[] = {
       
  1619 		"inactive",
       
  1620 		"authorizing",
       
  1621 		"authorized",
       
  1622 		"waiting",
       
  1623 		"loading map",
       
  1624 		"map done",
       
  1625 		"ready",
       
  1626 		"active"
       
  1627 	};
       
  1628 
       
  1629 	NetworkTCPSocketHandler *cs;
       
  1630 	FOR_ALL_CLIENTS(cs) {
       
  1631 		int lag = NetworkCalculateLag(cs);
       
  1632 		const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
       
  1633 		const char* status;
       
  1634 
       
  1635 		status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
       
  1636 		IConsolePrintF(CC_INFO, "Client #%1d  name: '%s'  status: '%s'  frame-lag: %3d  company: %1d  IP: %s  unique-id: '%s'",
       
  1637 			cs->index, ci->client_name, status, lag,
       
  1638 			ci->client_playas + (IsValidPlayer(ci->client_playas) ? 1 : 0),
       
  1639 			GetPlayerIP(ci), ci->unique_id);
       
  1640 	}
       
  1641 }
       
  1642 
       
  1643 void NetworkServerSendRcon(uint16 client_index, ConsoleColour colour_code, const char *string)
       
  1644 {
       
  1645 	SEND_COMMAND(PACKET_SERVER_RCON)(NetworkFindClientStateFromIndex(client_index), colour_code, string);
       
  1646 }
       
  1647 
       
  1648 void NetworkServerSendError(uint16 client_index, NetworkErrorCode error)
       
  1649 {
       
  1650 	SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(client_index), error);
       
  1651 }
       
  1652 
       
  1653 bool NetworkCompanyHasPlayers(PlayerID company)
       
  1654 {
       
  1655 	const NetworkTCPSocketHandler *cs;
       
  1656 	const NetworkClientInfo *ci;
       
  1657 	FOR_ALL_CLIENTS(cs) {
       
  1658 		ci = DEREF_CLIENT_INFO(cs);
       
  1659 		if (ci->client_playas == company) return true;
       
  1660 	}
       
  1661 	return false;
       
  1662 }
  1585 #endif /* ENABLE_NETWORK */
  1663 #endif /* ENABLE_NETWORK */