Changeset - r23964:f1693194d4bf
[Not reviewed]
master
0 12 0
glx - 5 years ago 2019-12-16 19:56:10
glx@openttd.org
Codechange: Replace network related FOR_ALL with range-based for loops
12 files changed with 89 insertions and 191 deletions:
0 comments (0 inline, 0 general)
src/network/core/tcp_listen.h
Show inline comments
 
@@ -99,14 +99,13 @@ public:
 
		struct timeval tv;
 

	
 
		FD_ZERO(&read_fd);
 
		FD_ZERO(&write_fd);
 

	
 

	
 
		Tsocket *cs;
 
		FOR_ALL_ITEMS_FROM(Tsocket, idx, cs, 0) {
 
		for (Tsocket *cs : Tsocket::Iterate()) {
 
			FD_SET(cs->sock, &read_fd);
 
			FD_SET(cs->sock, &write_fd);
 
		}
 

	
 
		/* take care of listener port */
 
		for (auto &s : sockets) {
 
@@ -119,13 +118,13 @@ public:
 
		/* accept clients.. */
 
		for (auto &s : sockets) {
 
			if (FD_ISSET(s.second, &read_fd)) AcceptClient(s.second);
 
		}
 

	
 
		/* read stuff from clients */
 
		FOR_ALL_ITEMS_FROM(Tsocket, idx, cs, 0) {
 
		for (Tsocket *cs : Tsocket::Iterate()) {
 
			cs->writable = !!FD_ISSET(cs->sock, &write_fd);
 
			if (FD_ISSET(cs->sock, &read_fd)) {
 
				cs->ReceivePackets();
 
			}
 
		}
 
		return _networking;
src/network/network.cpp
Show inline comments
 
@@ -96,14 +96,13 @@ extern void StateGameLoop();
 
/**
 
 * Return whether there is any client connected or trying to connect at all.
 
 * @return whether we have any client activity
 
 */
 
bool HasClients()
 
{
 
	NetworkClientSocket *cs;
 
	FOR_ALL_CLIENT_SOCKETS(cs) return true;
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) return true;
 

	
 
	return false;
 
}
 

	
 
/**
 
 * Basically a client is leaving us right now.
 
@@ -118,15 +117,13 @@ NetworkClientInfo::~NetworkClientInfo()
 
 * Return the CI given it's client-identifier
 
 * @param client_id the ClientID to search for
 
 * @return return a pointer to the corresponding NetworkClientInfo struct or nullptr when not found
 
 */
 
/* static */ NetworkClientInfo *NetworkClientInfo::GetByClientID(ClientID client_id)
 
{
 
	NetworkClientInfo *ci;
 

	
 
	FOR_ALL_CLIENT_INFOS(ci) {
 
	for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
		if (ci->client_id == client_id) return ci;
 
	}
 

	
 
	return nullptr;
 
}
 

	
 
@@ -134,27 +131,24 @@ NetworkClientInfo::~NetworkClientInfo()
 
 * Return the client state given it's client-identifier
 
 * @param client_id the ClientID to search for
 
 * @return return a pointer to the corresponding NetworkClientSocket struct or nullptr when not found
 
 */
 
/* static */ ServerNetworkGameSocketHandler *ServerNetworkGameSocketHandler::GetByClientID(ClientID client_id)
 
{
 
	NetworkClientSocket *cs;
 

	
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		if (cs->client_id == client_id) return cs;
 
	}
 

	
 
	return nullptr;
 
}
 

	
 
byte NetworkSpectatorCount()
 
{
 
	const NetworkClientInfo *ci;
 
	byte count = 0;
 

	
 
	FOR_ALL_CLIENT_INFOS(ci) {
 
	for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
		if (ci->client_playas == COMPANY_SPECTATOR) count++;
 
	}
 

	
 
	/* Don't count a dedicated server as spectator */
 
	if (_network_dedicated) count--;
 

	
 
@@ -405,16 +399,15 @@ static void CheckPauseHelper(bool pause,
 
 * Counts the number of active clients connected.
 
 * It has to be in STATUS_ACTIVE and not a spectator
 
 * @return number of active clients
 
 */
 
static uint NetworkCountActiveClients()
 
{
 
	const NetworkClientSocket *cs;
 
	uint count = 0;
 

	
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		if (cs->status != NetworkClientSocket::STATUS_ACTIVE) continue;
 
		if (!Company::IsValidID(cs->GetInfo()->client_playas)) continue;
 
		count++;
 
	}
 

	
 
	return count;
 
@@ -436,14 +429,13 @@ static void CheckMinActiveClients()
 
/**
 
 * Checks whether there is a joining client
 
 * @return true iff one client is joining (but not authorizing)
 
 */
 
static bool NetworkHasJoiningClient()
 
{
 
	const NetworkClientSocket *cs;
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		if (cs->status >= NetworkClientSocket::STATUS_AUTHORIZED && cs->status < NetworkClientSocket::STATUS_ACTIVE) return true;
 
	}
 

	
 
	return false;
 
}
 

	
 
@@ -524,20 +516,18 @@ static void InitializeNetworkPools(bool 
 
 * @param close_admins Whether the admin connections have to be closed as well.
 
 */
 
void NetworkClose(bool close_admins)
 
{
 
	if (_network_server) {
 
		if (close_admins) {
 
			ServerNetworkAdminSocketHandler *as;
 
			FOR_ALL_ADMIN_SOCKETS(as) {
 
			for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::Iterate()) {
 
				as->CloseConnection(true);
 
			}
 
		}
 

	
 
		NetworkClientSocket *cs;
 
		FOR_ALL_CLIENT_SOCKETS(cs) {
 
		for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
			cs->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
 
		}
 
		ServerNetworkGameSocketHandler::CloseListeners();
 
		ServerNetworkAdminSocketHandler::CloseListeners();
 
	} else if (MyClient::my_client != nullptr) {
 
		MyClient::SendQuit();
 
@@ -765,20 +755,18 @@ bool NetworkServerStart()
 

	
 
/* The server is rebooting...
 
 * The only difference with NetworkDisconnect, is the packets that is sent */
 
void NetworkReboot()
 
{
 
	if (_network_server) {
 
		NetworkClientSocket *cs;
 
		FOR_ALL_CLIENT_SOCKETS(cs) {
 
		for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
			cs->SendNewGame();
 
			cs->SendPackets();
 
		}
 

	
 
		ServerNetworkAdminSocketHandler *as;
 
		FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
		for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
			as->SendNewGame();
 
			as->SendPackets();
 
		}
 
	}
 

	
 
	/* For non-dedicated servers we have to kick the admins as we are not
 
@@ -791,21 +779,19 @@ void NetworkReboot()
 
 * @param blocking whether to wait till everything has been closed.
 
 * @param close_admins Whether the admin sockets need to be closed as well.
 
 */
 
void NetworkDisconnect(bool blocking, bool close_admins)
 
{
 
	if (_network_server) {
 
		NetworkClientSocket *cs;
 
		FOR_ALL_CLIENT_SOCKETS(cs) {
 
		for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
			cs->SendShutdown();
 
			cs->SendPackets();
 
		}
 

	
 
		if (close_admins) {
 
			ServerNetworkAdminSocketHandler *as;
 
			FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
			for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
				as->SendShutdown();
 
				as->SendPackets();
 
			}
 
		}
 
	}
 

	
src/network/network_admin.cpp
Show inline comments
 
@@ -91,14 +91,13 @@ ServerNetworkAdminSocketHandler::~Server
 
	return accept;
 
}
 

	
 
/** Send the packets for the server sockets. */
 
/* static */ void ServerNetworkAdminSocketHandler::Send()
 
{
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::Iterate()) {
 
		if (as->status == ADMIN_STATUS_INACTIVE && as->realtime_connect + ADMIN_AUTHORISATION_TIMEOUT < _realtime_tick) {
 
			DEBUG(net, 1, "[admin] Admin did not send its authorisation within %d seconds", ADMIN_AUTHORISATION_TIMEOUT / 1000);
 
			as->CloseConnection(true);
 
			continue;
 
		}
 
		if (as->writable) {
 
@@ -724,23 +723,22 @@ NetworkRecvStatus ServerNetworkAdminSock
 
			/* The admin is requesting the current date. */
 
			this->SendDate();
 
			break;
 

	
 
		case ADMIN_UPDATE_CLIENT_INFO:
 
			/* The admin is requesting client info. */
 
			const NetworkClientSocket *cs;
 
			if (d1 == UINT32_MAX) {
 
				this->SendClientInfo(nullptr, NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER));
 
				FOR_ALL_CLIENT_SOCKETS(cs) {
 
				for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
					this->SendClientInfo(cs, cs->GetInfo());
 
				}
 
			} else {
 
				if (d1 == CLIENT_ID_SERVER) {
 
					this->SendClientInfo(nullptr, NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER));
 
				} else {
 
					cs = NetworkClientSocket::GetByClientID((ClientID)d1);
 
					const NetworkClientSocket *cs = NetworkClientSocket::GetByClientID((ClientID)d1);
 
					if (cs != nullptr) this->SendClientInfo(cs, cs->GetInfo());
 
				}
 
			}
 
			break;
 

	
 
		case ADMIN_UPDATE_COMPANY_INFO:
 
@@ -814,14 +812,13 @@ NetworkRecvStatus ServerNetworkAdminSock
 
 * Notify the admin network of a new client (if they did opt in for the respective update).
 
 * @param cs the client info.
 
 * @param new_client if this is a new client, send the respective packet too.
 
 */
 
void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client)
 
{
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) {
 
			as->SendClientInfo(cs, cs->GetInfo());
 
			if (new_client) {
 
				as->SendClientJoin(cs->client_id);
 
			}
 
		}
 
@@ -831,28 +828,26 @@ void NetworkAdminClientInfo(const Networ
 
/**
 
 * Notify the admin network of a client update (if they did opt in for the respective update).
 
 * @param ci the client info.
 
 */
 
void NetworkAdminClientUpdate(const NetworkClientInfo *ci)
 
{
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) {
 
			as->SendClientUpdate(ci);
 
		}
 
	}
 
}
 

	
 
/**
 
 * Notify the admin network that a client quit (if they have opt in for the respective update).
 
 * @param client_id of the client that quit.
 
 */
 
void NetworkAdminClientQuit(ClientID client_id)
 
{
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) {
 
			as->SendClientQuit(client_id);
 
		}
 
	}
 
}
 

	
 
@@ -860,14 +855,13 @@ void NetworkAdminClientQuit(ClientID cli
 
 * Notify the admin network of a client error (if they have opt in for the respective update).
 
 * @param client_id the client that made the error.
 
 * @param error_code the error that was caused.
 
 */
 
void NetworkAdminClientError(ClientID client_id, NetworkErrorCode error_code)
 
{
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) {
 
			as->SendClientError(client_id, error_code);
 
		}
 
	}
 
}
 

	
 
@@ -880,14 +874,13 @@ void NetworkAdminCompanyInfo(const Compa
 
{
 
	if (company == nullptr) {
 
		DEBUG(net, 1, "[admin] Empty company given for update");
 
		return;
 
	}
 

	
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		if (as->update_frequency[ADMIN_UPDATE_COMPANY_INFO] != ADMIN_FREQUENCY_AUTOMATIC) continue;
 

	
 
		as->SendCompanyInfo(company);
 
		if (new_company) {
 
			as->SendCompanyNew(company->index);
 
		}
 
@@ -899,14 +892,13 @@ void NetworkAdminCompanyInfo(const Compa
 
 * @param company company of which updates are going to be sent into the admin network.
 
 */
 
void NetworkAdminCompanyUpdate(const Company *company)
 
{
 
	if (company == nullptr) return;
 

	
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		if (as->update_frequency[ADMIN_UPDATE_COMPANY_INFO] != ADMIN_FREQUENCY_AUTOMATIC) continue;
 

	
 
		as->SendCompanyUpdate(company);
 
	}
 
}
 

	
 
@@ -914,28 +906,26 @@ void NetworkAdminCompanyUpdate(const Com
 
 * Notify the admin network of a company to be removed (including the reason why).
 
 * @param company_id ID of the company that got removed.
 
 * @param bcrr the reason why the company got removed (e.g. bankruptcy).
 
 */
 
void NetworkAdminCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
 
{
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		as->SendCompanyRemove(company_id, bcrr);
 
	}
 
}
 

	
 

	
 
/**
 
 * Send chat to the admin network (if they did opt in for the respective update).
 
 */
 
void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_id, const char *msg, int64 data, bool from_admin)
 
{
 
	if (from_admin) return;
 

	
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		if (as->update_frequency[ADMIN_UPDATE_CHAT] & ADMIN_FREQUENCY_AUTOMATIC) {
 
			as->SendChat(action, desttype, client_id, msg, data);
 
		}
 
	}
 
}
 

	
 
@@ -954,28 +944,26 @@ void NetworkServerSendAdminRcon(AdminInd
 
 * Send console to the admin network (if they did opt in for the respective update).
 
 * @param origin the origin of the message.
 
 * @param string the message as printed on the console.
 
 */
 
void NetworkAdminConsole(const char *origin, const char *string)
 
{
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		if (as->update_frequency[ADMIN_UPDATE_CONSOLE] & ADMIN_FREQUENCY_AUTOMATIC) {
 
			as->SendConsole(origin, string);
 
		}
 
	}
 
}
 

	
 
/**
 
 * Send GameScript JSON to the admin network (if they did opt in for the respective update).
 
 * @param json The JSON data as received from the GameScript.
 
 */
 
void NetworkAdminGameScript(const char *json)
 
{
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		if (as->update_frequency[ADMIN_UPDATE_GAMESCRIPT] & ADMIN_FREQUENCY_AUTOMATIC) {
 
			as->SendGameScript(json);
 
		}
 
	}
 
}
 

	
 
@@ -985,39 +973,36 @@ void NetworkAdminGameScript(const char *
 
 * @param cp    The CommandPacket to be distributed.
 
 */
 
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp)
 
{
 
	ClientID client_id = owner == nullptr ? _network_own_client_id : owner->client_id;
 

	
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		if (as->update_frequency[ADMIN_UPDATE_CMD_LOGGING] & ADMIN_FREQUENCY_AUTOMATIC) {
 
			as->SendCmdLogging(client_id, cp);
 
		}
 
	}
 
}
 

	
 
/**
 
 * Send a Welcome packet to all connected admins
 
 */
 
void ServerNetworkAdminSocketHandler::WelcomeAll()
 
{
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		as->SendWelcome();
 
	}
 
}
 

	
 
/**
 
 * Send (push) updates to the admin network as they have registered for these updates.
 
 * @param freq the frequency to be processed.
 
 */
 
void NetworkAdminUpdate(AdminUpdateFrequency freq)
 
{
 
	ServerNetworkAdminSocketHandler *as;
 
	FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		for (int i = 0; i < ADMIN_UPDATE_END; i++) {
 
			if (as->update_frequency[i] & freq) {
 
				/* Update the admin for the required details */
 
				switch (i) {
 
					case ADMIN_UPDATE_DATE:
 
						as->SendDate();
src/network/network_admin.h
Show inline comments
 
@@ -79,35 +79,25 @@ public:
 
	 * @return the name to show in debug logs and the like.
 
	 */
 
	static const char *GetName()
 
	{
 
		return "admin";
 
	}
 

	
 
	/**
 
	 * Returns an iterable ensemble of all active admin sockets
 
	 * @param from index of the first socket to consider
 
	 * @return an iterable ensemble of all active admin sockets
 
	 */
 
	static Pool::IterateWrapper<ServerNetworkAdminSocketHandler> IterateActive(size_t from = 0)
 
	{
 
		return Pool::IterateWrapper<ServerNetworkAdminSocketHandler>(from,
 
			[](size_t index) { return ServerNetworkAdminSocketHandler::Get(index)->GetAdminStatus() == ADMIN_STATUS_ACTIVE; });
 
	}
 
};
 

	
 
/**
 
 * Iterate over all the sockets from a given starting point.
 
 * @param var The variable to iterate with.
 
 * @param start The start of the iteration.
 
 */
 
#define FOR_ALL_ADMIN_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(ServerNetworkAdminSocketHandler, adminsocket_index, var, start)
 

	
 
/**
 
 * Iterate over all the sockets.
 
 * @param var The variable to iterate with.
 
 */
 
#define FOR_ALL_ADMIN_SOCKETS(var) FOR_ALL_ADMIN_SOCKETS_FROM(var, 0)
 

	
 
/**
 
 * Iterate over all the active sockets.
 
 * @param var The variable to iterate with.
 
 */
 
#define FOR_ALL_ACTIVE_ADMIN_SOCKETS(var) \
 
	FOR_ALL_ADMIN_SOCKETS(var) \
 
		if (var->GetAdminStatus() == ADMIN_STATUS_ACTIVE)
 

	
 
void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client = false);
 
void NetworkAdminClientUpdate(const NetworkClientInfo *ci);
 
void NetworkAdminClientQuit(ClientID client_id);
 
void NetworkAdminClientError(ClientID client_id, NetworkErrorCode error_code);
 
void NetworkAdminCompanyInfo(const Company *company, bool new_company);
 
void NetworkAdminCompanyUpdate(const Company *company);
src/network/network_base.h
Show inline comments
 
@@ -34,20 +34,7 @@ struct NetworkClientInfo : NetworkClient
 
	NetworkClientInfo(ClientID client_id = INVALID_CLIENT_ID) : client_id(client_id) {}
 
	~NetworkClientInfo();
 

	
 
	static NetworkClientInfo *GetByClientID(ClientID client_id);
 
};
 

	
 
/**
 
 * Iterate over all the clients from a given index.
 
 * @param var The variable to iterate with.
 
 * @param start The location to start the iteration from.
 
 */
 
#define FOR_ALL_CLIENT_INFOS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientInfo, clientinfo_index, var, start)
 

	
 
/**
 
 * Iterate over all the clients.
 
 * @param var The variable to iterate with.
 
 */
 
#define FOR_ALL_CLIENT_INFOS(var) FOR_ALL_CLIENT_INFOS_FROM(var, 0)
 

	
 
#endif /* NETWORK_BASE_H */
src/network/network_chat_gui.cpp
Show inline comments
 
@@ -333,14 +333,13 @@ struct NetworkChatWindow : public Window
 
	{
 
		static char chat_tab_temp_buffer[64];
 

	
 
		/* First, try clients */
 
		if (*item < MAX_CLIENT_SLOTS) {
 
			/* Skip inactive clients */
 
			NetworkClientInfo *ci;
 
			FOR_ALL_CLIENT_INFOS_FROM(ci, *item) {
 
			for (NetworkClientInfo *ci : NetworkClientInfo::Iterate(*item)) {
 
				*item = ci->index;
 
				return ci->client_name;
 
			}
 
			*item = MAX_CLIENT_SLOTS;
 
		}
 

	
src/network/network_client.cpp
Show inline comments
 
@@ -1231,14 +1231,13 @@ void NetworkClientRequestMove(CompanyID 
 
void NetworkClientsToSpectators(CompanyID cid)
 
{
 
	Backup<CompanyID> cur_company(_current_company, FILE_LINE);
 
	/* If our company is changing owner, go to spectators */
 
	if (cid == _local_company) SetLocalCompany(COMPANY_SPECTATOR);
 

	
 
	NetworkClientInfo *ci;
 
	FOR_ALL_CLIENT_INFOS(ci) {
 
	for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
		if (ci->client_playas != cid) continue;
 
		NetworkTextMessage(NETWORK_ACTION_COMPANY_SPECTATOR, CC_DEFAULT, false, ci->client_name);
 
		ci->client_playas = COMPANY_SPECTATOR;
 
	}
 

	
 
	cur_company.Restore();
 
@@ -1296,14 +1295,13 @@ void NetworkClientSetCompanyPassword(con
 
 */
 
bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio)
 
{
 
	/* Only companies actually playing can speak to team. Eg spectators cannot */
 
	if (!_settings_client.gui.prefer_teamchat || !Company::IsValidID(cio->client_playas)) return false;
 

	
 
	const NetworkClientInfo *ci;
 
	FOR_ALL_CLIENT_INFOS(ci) {
 
	for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
		if (ci->client_playas == cio->client_playas && ci != cio) return true;
 
	}
 

	
 
	return false;
 
}
 

	
src/network/network_command.cpp
Show inline comments
 
@@ -235,25 +235,24 @@ void NetworkFreeLocalCommandQueue()
 
 */
 
static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
 
{
 
	CommandCallback *callback = cp.callback;
 
	cp.frame = _frame_counter_max + 1;
 

	
 
	NetworkClientSocket *cs;
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		if (cs->status >= NetworkClientSocket::STATUS_MAP) {
 
			/* Callbacks are only send back to the client who sent them in the
 
			 *  first place. This filters that out. */
 
			cp.callback = (cs != owner) ? nullptr : callback;
 
			cp.my_cmd = (cs == owner);
 
			cs->outgoing_queue.Append(&cp);
 
		}
 
	}
 

	
 
	cp.callback = (cs != owner) ? nullptr : callback;
 
	cp.my_cmd = (cs == owner);
 
	cp.callback = (nullptr != owner) ? nullptr : callback;
 
	cp.my_cmd = (nullptr == owner);
 
	_local_execution_queue.Append(&cp);
 
}
 

	
 
/**
 
 * "Send" a particular CommandQueue to all clients.
 
 * @param queue The queue of commands that has to be distributed.
 
@@ -280,14 +279,13 @@ static void DistributeQueue(CommandQueue
 
void NetworkDistributeCommands()
 
{
 
	/* First send the server's commands. */
 
	DistributeQueue(&_local_wait_queue, nullptr);
 

	
 
	/* Then send the queues of the others. */
 
	NetworkClientSocket *cs;
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		DistributeQueue(&cs->incoming_queue, cs);
 
	}
 
}
 

	
 
/**
 
 * Receives a command from the network.
src/network/network_gui.cpp
Show inline comments
 
@@ -1880,16 +1880,15 @@ struct NetworkClientListWindow : Window 
 
	/**
 
	 * Finds the amount of clients and set the height correct
 
	 */
 
	bool CheckClientListHeight()
 
	{
 
		int num = 0;
 
		const NetworkClientInfo *ci;
 

	
 
		/* Should be replaced with a loop through all clients */
 
		FOR_ALL_CLIENT_INFOS(ci) {
 
		for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
			if (ci->client_playas != COMPANY_INACTIVE_CLIENT) num++;
 
		}
 

	
 
		num *= this->line_height;
 

	
 
		int diff = (num + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM) - (this->GetWidget<NWidgetBase>(WID_CL_PANEL)->current_y);
 
@@ -1907,14 +1906,13 @@ struct NetworkClientListWindow : Window 
 

	
 
		this->server_client_width = max(GetStringBoundingBox(STR_NETWORK_SERVER).width, GetStringBoundingBox(STR_NETWORK_CLIENT).width) + WD_FRAMERECT_RIGHT;
 
		this->icon_size = GetSpriteSize(SPR_COMPANY_ICON);
 
		this->line_height = max(this->icon_size.height + 2U, (uint)FONT_HEIGHT_NORMAL);
 

	
 
		uint width = 100; // Default width
 
		const NetworkClientInfo *ci;
 
		FOR_ALL_CLIENT_INFOS(ci) {
 
		for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
			width = max(width, GetStringBoundingBox(ci->client_name).width);
 
		}
 

	
 
		size->width = WD_FRAMERECT_LEFT + this->server_client_width + this->icon_size.width + WD_FRAMERECT_LEFT + width + WD_FRAMERECT_RIGHT;
 
	}
 

	
 
@@ -1944,14 +1942,13 @@ struct NetworkClientListWindow : Window 
 
		uint type_right = rtl ? right : left + this->server_client_width - 1;
 
		uint icon_left  = rtl ? right - type_icon_width + WD_FRAMERECT_LEFT : left + this->server_client_width;
 
		uint name_left  = rtl ? left : left + type_icon_width;
 
		uint name_right = rtl ? right - type_icon_width : right;
 

	
 
		int i = 0;
 
		const NetworkClientInfo *ci;
 
		FOR_ALL_CLIENT_INFOS(ci) {
 
		for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
			TextColour colour;
 
			if (this->selected_item == i++) { // Selected item, highlight it
 
				GfxFillRect(r.left + 1, y, r.right - 1, y + this->line_height - 1, PC_BLACK);
 
				colour = TC_WHITE;
 
			} else {
 
				colour = TC_BLACK;
 
@@ -1973,21 +1970,20 @@ struct NetworkClientListWindow : Window 
 
	}
 

	
 
	void OnClick(Point pt, int widget, int click_count) override
 
	{
 
		/* Show the popup with option */
 
		if (this->selected_item != -1) {
 
			NetworkClientInfo *ci;
 

	
 
			int client_no = this->selected_item;
 
			FOR_ALL_CLIENT_INFOS(ci) {
 
				if (client_no == 0) break;
 
			for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
				if (client_no == 0) {
 
					PopupClientList(ci->client_id, pt.x + this->left, pt.y + this->top);
 
					break;
 
				}
 
				client_no--;
 
			}
 

	
 
			if (ci != nullptr) PopupClientList(ci->client_id, pt.x + this->left, pt.y + this->top);
 
		}
 
	}
 

	
 
	void OnMouseOver(Point pt, int widget) override
 
	{
 
		/* -1 means we left the current window */
src/network/network_server.cpp
Show inline comments
 
@@ -255,20 +255,19 @@ NetworkRecvStatus ServerNetworkGameSocke
 
	 */
 
	if (this->sock == INVALID_SOCKET) return status;
 

	
 
	if (status != NETWORK_RECV_STATUS_CONN_LOST && !this->HasClientQuit() && this->status >= STATUS_AUTHORIZED) {
 
		/* We did not receive a leave message from this client... */
 
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
 
		NetworkClientSocket *new_cs;
 

	
 
		this->GetClientName(client_name, lastof(client_name));
 

	
 
		NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, nullptr, STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST);
 

	
 
		/* Inform other clients of this... strange leaving ;) */
 
		FOR_ALL_CLIENT_SOCKETS(new_cs) {
 
		for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
			if (new_cs->status > STATUS_AUTHORIZED && this != new_cs) {
 
				new_cs->SendErrorQuit(this->client_id, NETWORK_ERROR_CONNECTION_LOST);
 
			}
 
		}
 
	}
 

	
 
@@ -307,14 +306,13 @@ NetworkRecvStatus ServerNetworkGameSocke
 
	return accept;
 
}
 

	
 
/** Send the packets for the server sockets. */
 
/* static */ void ServerNetworkGameSocketHandler::Send()
 
{
 
	NetworkClientSocket *cs;
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		if (cs->writable) {
 
			if (cs->SendPackets() != SPS_CLOSED && cs->status == STATUS_MAP) {
 
				/* This client is in the middle of a map-send, call the function for that */
 
				cs->SendMap();
 
			}
 
		}
 
@@ -351,22 +349,21 @@ NetworkRecvStatus ServerNetworkGameSocke
 
	/* Fetch the latest version of the stats */
 
	NetworkCompanyStats company_stats[MAX_COMPANIES];
 
	NetworkPopulateCompanyStats(company_stats);
 

	
 
	/* Make a list of all clients per company */
 
	char clients[MAX_COMPANIES][NETWORK_CLIENTS_LENGTH];
 
	NetworkClientSocket *csi;
 
	memset(clients, 0, sizeof(clients));
 

	
 
	/* Add the local player (if not dedicated) */
 
	const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
 
	if (ci != nullptr && Company::IsValidID(ci->client_playas)) {
 
		strecpy(clients[ci->client_playas], ci->client_name, lastof(clients[ci->client_playas]));
 
	}
 

	
 
	FOR_ALL_CLIENT_SOCKETS(csi) {
 
	for (NetworkClientSocket *csi : NetworkClientSocket::Iterate()) {
 
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
 

	
 
		((ServerNetworkGameSocketHandler*)csi)->GetClientName(client_name, lastof(client_name));
 

	
 
		ci = csi->GetInfo();
 
		if (ci != nullptr && Company::IsValidID(ci->client_playas)) {
 
@@ -421,22 +418,21 @@ NetworkRecvStatus ServerNetworkGameSocke
 

	
 
	StringID strid = GetNetworkErrorMsg(error);
 
	GetString(str, strid, lastof(str));
 

	
 
	/* Only send when the current client was in game */
 
	if (this->status > STATUS_AUTHORIZED) {
 
		NetworkClientSocket *new_cs;
 
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
 

	
 
		this->GetClientName(client_name, lastof(client_name));
 

	
 
		DEBUG(net, 1, "'%s' made an error and has been disconnected. Reason: '%s'", client_name, str);
 

	
 
		NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, nullptr, strid);
 

	
 
		FOR_ALL_CLIENT_SOCKETS(new_cs) {
 
		for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
			if (new_cs->status > STATUS_AUTHORIZED && new_cs != this) {
 
				/* Some errors we filter to a more general error. Clients don't have to know the real
 
				 *  reason a joining failed. */
 
				if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION) {
 
					error = NETWORK_ERROR_ILLEGAL_PACKET;
 
				}
 
@@ -506,13 +502,12 @@ NetworkRecvStatus ServerNetworkGameSocke
 
}
 

	
 
/** Send the client a welcome message with some basic information. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendWelcome()
 
{
 
	Packet *p;
 
	NetworkClientSocket *new_cs;
 

	
 
	/* Invalid packet when status is AUTH or higher */
 
	if (this->status >= STATUS_AUTHORIZED) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET);
 

	
 
	this->status = STATUS_AUTHORIZED;
 
	/* Reset 'lag' counters */
 
@@ -524,30 +519,29 @@ NetworkRecvStatus ServerNetworkGameSocke
 
	p->Send_uint32(this->client_id);
 
	p->Send_uint32(_settings_game.game_creation.generation_seed);
 
	p->Send_string(_settings_client.network.network_id);
 
	this->SendPacket(p);
 

	
 
	/* Transmit info about all the active clients */
 
	FOR_ALL_CLIENT_SOCKETS(new_cs) {
 
	for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
		if (new_cs != this && new_cs->status > STATUS_AUTHORIZED) {
 
			this->SendClientInfo(new_cs->GetInfo());
 
		}
 
	}
 
	/* Also send the info of the server */
 
	return this->SendClientInfo(NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER));
 
}
 

	
 
/** Tell the client that its put in a waiting queue. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendWait()
 
{
 
	int waiting = 0;
 
	NetworkClientSocket *new_cs;
 
	Packet *p;
 

	
 
	/* Count how many clients are waiting in the queue, in front of you! */
 
	FOR_ALL_CLIENT_SOCKETS(new_cs) {
 
	for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
		if (new_cs->status != STATUS_MAP_WAIT) continue;
 
		if (new_cs->GetInfo()->join_date < this->GetInfo()->join_date || (new_cs->GetInfo()->join_date == this->GetInfo()->join_date && new_cs->client_id < this->client_id)) waiting++;
 
	}
 

	
 
	p = new Packet(PACKET_SERVER_WAIT);
 
	p->Send_uint8(waiting);
 
@@ -608,15 +602,14 @@ NetworkRecvStatus ServerNetworkGameSocke
 

	
 
			/* Set the status to DONE_MAP, no we will wait for the client
 
			 *  to send it is ready (maybe that happens like never ;)) */
 
			this->status = STATUS_DONE_MAP;
 

	
 
			/* Find the best candidate for joining, i.e. the first joiner. */
 
			NetworkClientSocket *new_cs;
 
			NetworkClientSocket *best = nullptr;
 
			FOR_ALL_CLIENT_SOCKETS(new_cs) {
 
			for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
				if (new_cs->status == STATUS_MAP_WAIT) {
 
					if (best == nullptr || best->GetInfo()->join_date > new_cs->GetInfo()->join_date || (best->GetInfo()->join_date == new_cs->GetInfo()->join_date && best->client_id > new_cs->client_id)) {
 
						best = new_cs;
 
					}
 
				}
 
			}
 
@@ -625,13 +618,13 @@ NetworkRecvStatus ServerNetworkGameSocke
 
			if (best != nullptr) {
 
				/* Let the first start joining. */
 
				best->status = STATUS_AUTHORIZED;
 
				best->SendMap();
 

	
 
				/* And update the rest. */
 
				FOR_ALL_CLIENT_SOCKETS(new_cs) {
 
				for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
					if (new_cs->status == STATUS_MAP_WAIT) new_cs->SendWait();
 
				}
 
			}
 
		}
 

	
 
		switch (this->SendPackets()) {
 
@@ -997,21 +990,20 @@ NetworkRecvStatus ServerNetworkGameSocke
 

	
 
	return this->SendWelcome();
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GETMAP(Packet *p)
 
{
 
	NetworkClientSocket *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 (this->status < STATUS_AUTHORIZED || this->HasClientQuit()) {
 
		return this->SendError(NETWORK_ERROR_NOT_AUTHORIZED);
 
	}
 

	
 
	/* Check if someone else is receiving the map */
 
	FOR_ALL_CLIENT_SOCKETS(new_cs) {
 
	for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
		if (new_cs->status == STATUS_MAP) {
 
			/* Tell the new client to wait */
 
			this->status = STATUS_MAP_WAIT;
 
			return this->SendWait();
 
		}
 
	}
 
@@ -1022,13 +1014,12 @@ NetworkRecvStatus ServerNetworkGameSocke
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK(Packet *p)
 
{
 
	/* Client has the map, now start syncing */
 
	if (this->status == STATUS_DONE_MAP && !this->HasClientQuit()) {
 
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
 
		NetworkClientSocket *new_cs;
 

	
 
		this->GetClientName(client_name, lastof(client_name));
 

	
 
		NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, client_name, nullptr, this->client_id);
 

	
 
		/* Mark the client as pre-active, and wait for an ACK
 
@@ -1040,13 +1031,13 @@ NetworkRecvStatus ServerNetworkGameSocke
 

	
 
		/* This is the frame the client receives
 
		 *  we need it later on to make sure the client is not too slow */
 
		this->last_frame = _frame_counter;
 
		this->last_frame_server = _frame_counter;
 

	
 
		FOR_ALL_CLIENT_SOCKETS(new_cs) {
 
		for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
			if (new_cs->status > STATUS_AUTHORIZED) {
 
				new_cs->SendClientInfo(this->GetInfo());
 
				new_cs->SendJoin(this->client_id);
 
			}
 
		}
 

	
 
@@ -1132,13 +1123,12 @@ NetworkRecvStatus ServerNetworkGameSocke
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet *p)
 
{
 
	/* This packets means a client noticed an error and is reporting this
 
	 *  to us. Display the error and report it to the other clients */
 
	NetworkClientSocket *new_cs;
 
	char str[100];
 
	char client_name[NETWORK_CLIENT_NAME_LENGTH];
 
	NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8();
 

	
 
	/* The client was never joined.. thank the client for the packet, but ignore it */
 
	if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
 
@@ -1151,13 +1141,13 @@ NetworkRecvStatus ServerNetworkGameSocke
 
	GetString(str, strid, lastof(str));
 

	
 
	DEBUG(net, 2, "'%s' reported an error and is closing its connection (%s)", client_name, str);
 

	
 
	NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, nullptr, strid);
 

	
 
	FOR_ALL_CLIENT_SOCKETS(new_cs) {
 
	for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
		if (new_cs->status > STATUS_AUTHORIZED) {
 
			new_cs->SendErrorQuit(this->client_id, errorno);
 
		}
 
	}
 

	
 
	NetworkAdminClientError(this->client_id, errorno);
 
@@ -1166,25 +1156,24 @@ NetworkRecvStatus ServerNetworkGameSocke
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_QUIT(Packet *p)
 
{
 
	/* The client wants to leave. Display this and report it to the other
 
	 *  clients. */
 
	NetworkClientSocket *new_cs;
 
	char client_name[NETWORK_CLIENT_NAME_LENGTH];
 

	
 
	/* The client was never joined.. thank the client for the packet, but ignore it */
 
	if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
 
		return this->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
 
	}
 

	
 
	this->GetClientName(client_name, lastof(client_name));
 

	
 
	NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, nullptr, STR_NETWORK_MESSAGE_CLIENT_LEAVING);
 

	
 
	FOR_ALL_CLIENT_SOCKETS(new_cs) {
 
	for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
		if (new_cs->status > STATUS_AUTHORIZED && new_cs != this) {
 
			new_cs->SendQuit(this->client_id);
 
		}
 
	}
 

	
 
	NetworkAdminClientQuit(this->client_id);
 
@@ -1247,13 +1236,12 @@ NetworkRecvStatus ServerNetworkGameSocke
 
 * @param from_id The origin of the message.
 
 * @param data Arbitrary data.
 
 * @param from_admin Whether the origin is an admin or not.
 
 */
 
void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const char *msg, ClientID from_id, int64 data, bool from_admin)
 
{
 
	NetworkClientSocket *cs;
 
	const NetworkClientInfo *ci, *ci_own, *ci_to;
 

	
 
	switch (desttype) {
 
		case DESTTYPE_CLIENT:
 
			/* Are we sending to the server? */
 
			if ((ClientID)dest == CLIENT_ID_SERVER) {
 
@@ -1265,13 +1253,13 @@ void NetworkServerSendChat(NetworkAction
 
					if (_settings_client.network.server_admin_chat) {
 
						NetworkAdminChat(action, desttype, from_id, msg, data, from_admin);
 
					}
 
				}
 
			} else {
 
				/* Else find the client to send the message to */
 
				FOR_ALL_CLIENT_SOCKETS(cs) {
 
				for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
					if (cs->client_id == (ClientID)dest) {
 
						cs->SendChat(action, from_id, false, msg, data);
 
						break;
 
					}
 
				}
 
			}
 
@@ -1282,13 +1270,13 @@ void NetworkServerSendChat(NetworkAction
 
					ci = NetworkClientInfo::GetByClientID(from_id);
 
					ci_to = NetworkClientInfo::GetByClientID((ClientID)dest);
 
					if (ci != nullptr && ci_to != nullptr) {
 
						NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), true, ci_to->client_name, msg, data);
 
					}
 
				} else {
 
					FOR_ALL_CLIENT_SOCKETS(cs) {
 
					for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
						if (cs->client_id == from_id) {
 
							cs->SendChat(action, (ClientID)dest, true, msg, data);
 
							break;
 
						}
 
					}
 
				}
 
@@ -1296,13 +1284,13 @@ void NetworkServerSendChat(NetworkAction
 
			break;
 
		case DESTTYPE_TEAM: {
 
			/* If this is false, the message is already displayed on the client who sent it. */
 
			bool show_local = true;
 
			/* Find all clients that belong to this company */
 
			ci_to = nullptr;
 
			FOR_ALL_CLIENT_SOCKETS(cs) {
 
			for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
				ci = cs->GetInfo();
 
				if (ci != nullptr && ci->client_playas == (CompanyID)dest) {
 
					cs->SendChat(action, from_id, false, msg, data);
 
					if (cs->client_id == from_id) show_local = false;
 
					ci_to = ci; // Remember a client that is in the company for company-name
 
				}
 
@@ -1330,13 +1318,13 @@ void NetworkServerSendChat(NetworkAction
 
					char name[NETWORK_NAME_LENGTH];
 
					StringID str = Company::IsValidID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
 
					SetDParam(0, ci_to->client_playas);
 
					GetString(name, str, lastof(name));
 
					NetworkTextMessage(action, GetDrawStringCompanyColour(ci_own->client_playas), true, name, msg, data);
 
				} else {
 
					FOR_ALL_CLIENT_SOCKETS(cs) {
 
					for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
						if (cs->client_id == from_id) {
 
							cs->SendChat(action, ci_to->client_id, true, msg, data);
 
						}
 
					}
 
				}
 
			}
 
@@ -1344,13 +1332,13 @@ void NetworkServerSendChat(NetworkAction
 
		}
 
		default:
 
			DEBUG(net, 0, "[server] received unknown chat destination type %d. Doing broadcast instead", desttype);
 
			FALLTHROUGH;
 

	
 
		case DESTTYPE_BROADCAST:
 
			FOR_ALL_CLIENT_SOCKETS(cs) {
 
			for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
				cs->SendChat(action, from_id, false, msg, data);
 
			}
 

	
 
			NetworkAdminChat(action, desttype, from_id, msg, data, from_admin);
 

	
 
			ci = NetworkClientInfo::GetByClientID(from_id);
 
@@ -1581,20 +1569,19 @@ void NetworkPopulateCompanyStats(Network
 
/**
 
 * Send updated client info of a particular client.
 
 * @param client_id The client to send it for.
 
 */
 
void NetworkUpdateClientInfo(ClientID client_id)
 
{
 
	NetworkClientSocket *cs;
 
	NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
 

	
 
	if (ci == nullptr) return;
 

	
 
	DEBUG(desync, 1, "client: %08x; %02x; %02x; %04x", _date, _date_fract, (int)ci->client_playas, client_id);
 

	
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		cs->SendClientInfo(ci);
 
	}
 

	
 
	NetworkAdminClientUpdate(ci);
 
}
 

	
 
@@ -1613,27 +1600,26 @@ static void NetworkCheckRestartMap()
 
 *     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()
 
{
 
	const NetworkClientInfo *ci;
 
	bool clients_in_company[MAX_COMPANIES];
 
	int vehicles_in_company[MAX_COMPANIES];
 

	
 
	if (!_settings_client.network.autoclean_companies) return;
 

	
 
	memset(clients_in_company, 0, sizeof(clients_in_company));
 

	
 
	/* Detect the active companies */
 
	FOR_ALL_CLIENT_INFOS(ci) {
 
	for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
		if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
 
	}
 

	
 
	if (!_network_dedicated) {
 
		ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
 
		const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
 
		if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
 
	}
 

	
 
	if (_settings_client.network.autoclean_novehicles != 0) {
 
		memset(vehicles_in_company, 0, sizeof(vehicles_in_company));
 

	
 
@@ -1692,24 +1678,22 @@ bool NetworkFindName(char *new_name, con
 
	uint number = 0;
 
	char original_name[NETWORK_CLIENT_NAME_LENGTH];
 

	
 
	strecpy(original_name, new_name, lastof(original_name));
 

	
 
	while (!found_name) {
 
		const NetworkClientInfo *ci;
 

	
 
		found_name = true;
 
		FOR_ALL_CLIENT_INFOS(ci) {
 
		for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
			if (strcmp(ci->client_name, new_name) == 0) {
 
				/* Name already in use */
 
				found_name = false;
 
				break;
 
			}
 
		}
 
		/* Check if it is the same as the server-name */
 
		ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
 
		const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
 
		if (ci != nullptr) {
 
			if (strcmp(ci->client_name, new_name) == 0) found_name = false; // name already in use
 
		}
 

	
 
		if (!found_name) {
 
			/* Try a new name (<name> #1, <name> #2, and so on) */
 
@@ -1728,19 +1712,18 @@ bool NetworkFindName(char *new_name, con
 
 * @param client_id the client to change the name of
 
 * @param new_name the new name for the client
 
 * @return true iff the name was changed
 
 */
 
bool NetworkServerChangeClientName(ClientID client_id, const char *new_name)
 
{
 
	NetworkClientInfo *ci;
 
	/* Check if the name's already in use */
 
	FOR_ALL_CLIENT_INFOS(ci) {
 
	for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
		if (strcmp(ci->client_name, new_name) == 0) return false;
 
	}
 

	
 
	ci = NetworkClientInfo::GetByClientID(client_id);
 
	NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
 
	if (ci == nullptr) return false;
 

	
 
	NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, true, ci->client_name, new_name);
 

	
 
	strecpy(ci->client_name, new_name, lastof(ci->client_name));
 

	
 
@@ -1782,13 +1765,12 @@ static void NetworkHandleCommandQueue(Ne
 
/**
 
 * This is called every tick if this is a _network_server
 
 * @param send_frame Whether to send the frame to the clients.
 
 */
 
void NetworkServer_Tick(bool send_frame)
 
{
 
	NetworkClientSocket *cs;
 
#ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
 
	bool send_sync = false;
 
#endif
 

	
 
#ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
 
	if (_frame_counter >= _last_sync_frame + _settings_client.network.sync_freq) {
 
@@ -1796,13 +1778,13 @@ void NetworkServer_Tick(bool send_frame)
 
		send_sync = true;
 
	}
 
#endif
 

	
 
	/* Now we are done with the frame, inform the clients that they can
 
	 *  do their frame! */
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		/* We allow a number of bytes per frame, but only to the burst amount
 
		 * to be available for packet receiving at any particular time. */
 
		cs->receive_limit = min(cs->receive_limit + _settings_client.network.bytes_per_frame,
 
				_settings_client.network.bytes_per_frame_burst);
 

	
 
		/* Check if the speed of the client is what we can expect from a client */
 
@@ -1953,14 +1935,13 @@ void NetworkServerShowStatusToConsole()
 
		"map done",
 
		"ready",
 
		"active"
 
	};
 
	assert_compile(lengthof(stat_str) == NetworkClientSocket::STATUS_END);
 

	
 
	NetworkClientSocket *cs;
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		NetworkClientInfo *ci = cs->GetInfo();
 
		if (ci == nullptr) continue;
 
		uint lag = NetworkCalculateLag(cs);
 
		const char *status;
 

	
 
		status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
 
@@ -1973,15 +1954,13 @@ void NetworkServerShowStatusToConsole()
 

	
 
/**
 
 * Send Config Update
 
 */
 
void NetworkServerSendConfigUpdate()
 
{
 
	NetworkClientSocket *cs;
 

	
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) cs->SendConfigUpdate();
 
	}
 
}
 

	
 
/**
 
 * Tell that a particular company is (not) passworded.
 
@@ -1992,14 +1971,13 @@ void NetworkServerUpdateCompanyPassworde
 
{
 
	if (NetworkCompanyIsPassworded(company_id) == passworded) return;
 

	
 
	SB(_network_company_passworded, company_id, 1, !!passworded);
 
	SetWindowClassesDirty(WC_COMPANY);
 

	
 
	NetworkClientSocket *cs;
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) cs->SendCompanyUpdate();
 
	}
 

	
 
	NetworkAdminCompanyUpdate(Company::GetIfValid(company_id));
 
}
 

	
 
@@ -2087,14 +2065,13 @@ uint NetworkServerKickOrBanIP(const char
 
		if (!contains) _network_ban_list.emplace_back(ip);
 
	}
 

	
 
	uint n = 0;
 

	
 
	/* There can be multiple clients with the same IP, kick them all */
 
	NetworkClientSocket *cs;
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		if (cs->client_id == CLIENT_ID_SERVER) continue;
 
		if (cs->client_address.IsInNetmask(ip)) {
 
			NetworkServerKickClient(cs->client_id);
 
			n++;
 
		}
 
	}
 
@@ -2106,14 +2083,13 @@ uint NetworkServerKickOrBanIP(const char
 
 * Check whether a particular company has clients.
 
 * @param company The company to check.
 
 * @return True if at least one client is joined to the company.
 
 */
 
bool NetworkCompanyHasClients(CompanyID company)
 
{
 
	const NetworkClientInfo *ci;
 
	FOR_ALL_CLIENT_INFOS(ci) {
 
	for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
		if (ci->client_playas == company) return true;
 
	}
 
	return false;
 
}
 

	
 

	
 
@@ -2135,14 +2111,13 @@ void ServerNetworkGameSocketHandler::Get
 

	
 
/**
 
 * Print all the clients to the console
 
 */
 
void NetworkPrintClients()
 
{
 
	NetworkClientInfo *ci;
 
	FOR_ALL_CLIENT_INFOS(ci) {
 
	for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
		if (_network_server) {
 
			IConsolePrintF(CC_INFO, "Client #%1d  name: '%s'  company: %1d  IP: %s",
 
					ci->client_id,
 
					ci->client_name,
 
					ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
 
					ci->client_id == CLIENT_ID_SERVER ? "server" : NetworkClientSocket::GetByClientID(ci->client_id)->GetClientIP());
src/network/network_server.h
Show inline comments
 
@@ -117,20 +117,7 @@ public:
 
};
 

	
 
void NetworkServer_Tick(bool send_frame);
 
void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true);
 
void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded);
 

	
 
/**
 
 * Iterate over all the sockets from a given starting point.
 
 * @param var The variable to iterate with.
 
 * @param start The start of the iteration.
 
 */
 
#define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
 

	
 
/**
 
 * Iterate over all the sockets.
 
 * @param var The variable to iterate with.
 
 */
 
#define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)
 

	
 
#endif /* NETWORK_SERVER_H */
src/script/api/script_clientlist.cpp
Show inline comments
 
@@ -15,14 +15,13 @@
 

	
 
#include "../../safeguards.h"
 

	
 
ScriptClientList::ScriptClientList()
 
{
 
	if (!_networking) return;
 
	NetworkClientInfo *ci;
 
	FOR_ALL_CLIENT_INFOS(ci) {
 
	for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
		this->AddItem(ci->client_id);
 
	}
 
}
 

	
 
ScriptClientList_Company::ScriptClientList_Company(ScriptCompany::CompanyID company)
 
{
 
@@ -33,11 +32,10 @@ ScriptClientList_Company::ScriptClientLi
 
	} else {
 
		company = ScriptCompany::ResolveCompanyID(company);
 
		if (company == ScriptCompany::COMPANY_INVALID) return;
 
		c = (CompanyID)company;
 
	}
 

	
 
	NetworkClientInfo *ci;
 
	FOR_ALL_CLIENT_INFOS(ci) {
 
	for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
 
		if (ci->client_playas == c) this->AddItem(ci->client_id);
 
	}
 
}
0 comments (0 inline, 0 general)