Changeset - r28692:fcd1cb308544
[Not reviewed]
master
0 8 0
Rubidium - 4 months ago 2024-02-04 16:20:25
rubidium@openttd.org
Codechange: use reference instead of pointer for CommandPacket
8 files changed with 46 insertions and 46 deletions:
0 comments (0 inline, 0 general)
src/network/core/tcp_game.h
Show inline comments
 
@@ -527,14 +527,14 @@ public:
 
	{
 
		return this->info;
 
	}
 

	
 
	NetworkRecvStatus ReceivePackets();
 

	
 
	const char *ReceiveCommand(Packet &p, CommandPacket *cp);
 
	void SendCommand(Packet &p, const CommandPacket *cp);
 
	const char *ReceiveCommand(Packet &p, CommandPacket &cp);
 
	void SendCommand(Packet &p, const CommandPacket &cp);
 

	
 
	bool IsPendingDeletion() const { return this->is_pending_deletion; }
 

	
 
	void DeferDeletion();
 
	static void ProcessDeferredDeletions();
 
};
src/network/network_admin.cpp
Show inline comments
 
@@ -605,21 +605,21 @@ NetworkRecvStatus ServerNetworkAdminSock
 

	
 
/**
 
 * Send a command for logging purposes.
 
 * @param client_id The client executing the command.
 
 * @param cp The command that would be executed.
 
 */
 
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCmdLogging(ClientID client_id, const CommandPacket *cp)
 
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCmdLogging(ClientID client_id, const CommandPacket &cp)
 
{
 
	auto p = std::make_unique<Packet>(ADMIN_PACKET_SERVER_CMD_LOGGING);
 

	
 
	p->Send_uint32(client_id);
 
	p->Send_uint8 (cp->company);
 
	p->Send_uint16(cp->cmd);
 
	p->Send_buffer(cp->data);
 
	p->Send_uint32(cp->frame);
 
	p->Send_uint8 (cp.company);
 
	p->Send_uint16(cp.cmd);
 
	p->Send_buffer(cp.data);
 
	p->Send_uint32(cp.frame);
 

	
 
	this->SendPacket(std::move(p));
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
@@ -956,13 +956,13 @@ void NetworkAdminGameScript(const std::s
 

	
 
/**
 
 * Distribute CommandPacket details over the admin network for logging purposes.
 
 * @param owner The owner of the CommandPacket (who sent us the CommandPacket).
 
 * @param cp    The CommandPacket to be distributed.
 
 */
 
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp)
 
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket &cp)
 
{
 
	ClientID client_id = owner == nullptr ? _network_own_client_id : owner->client_id;
 

	
 
	for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
 
		if (as->update_frequency[ADMIN_UPDATE_CMD_LOGGING] & ADMIN_FREQUENCY_AUTOMATIC) {
 
			as->SendCmdLogging(client_id, cp);
src/network/network_admin.h
Show inline comments
 
@@ -64,13 +64,13 @@ public:
 

	
 
	NetworkRecvStatus SendChat(NetworkAction action, DestType desttype, ClientID client_id, const std::string &msg, int64_t data);
 
	NetworkRecvStatus SendRcon(uint16_t colour, const std::string_view command);
 
	NetworkRecvStatus SendConsole(const std::string_view origin, const std::string_view command);
 
	NetworkRecvStatus SendGameScript(const std::string_view json);
 
	NetworkRecvStatus SendCmdNames();
 
	NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket *cp);
 
	NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket &cp);
 
	NetworkRecvStatus SendRconEnd(const std::string_view command);
 

	
 
	static void Send();
 
	static void AcceptConnection(SOCKET s, const NetworkAddress &address);
 
	static bool AllowConnection();
 
	static void WelcomeAll();
 
@@ -109,9 +109,9 @@ void NetworkAdminCompanyRemove(CompanyID
 

	
 
void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_id, const std::string &msg, int64_t data = 0, bool from_admin = false);
 
void NetworkAdminUpdate(AdminUpdateFrequency freq);
 
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const std::string_view string);
 
void NetworkAdminConsole(const std::string_view origin, const std::string_view string);
 
void NetworkAdminGameScript(const std::string_view json);
 
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp);
 
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket &cp);
 

	
 
#endif /* NETWORK_ADMIN_H */
src/network/network_client.cpp
Show inline comments
 
@@ -432,15 +432,15 @@ NetworkRecvStatus ClientNetworkGameSocke
 
}
 

	
 
/**
 
 * Send a command to the server.
 
 * @param cp The command to send.
 
 */
 
NetworkRecvStatus ClientNetworkGameSocketHandler::SendCommand(const CommandPacket *cp)
 
NetworkRecvStatus ClientNetworkGameSocketHandler::SendCommand(const CommandPacket &cp)
 
{
 
	Debug(net, 9, "Client::SendCommand(): cmd={}", cp->cmd);
 
	Debug(net, 9, "Client::SendCommand(): cmd={}", cp.cmd);
 

	
 
	auto p = std::make_unique<Packet>(PACKET_CLIENT_COMMAND);
 
	my_client->NetworkGameSocketHandler::SendCommand(*p, cp);
 

	
 
	my_client->SendPacket(std::move(p));
 
	return NETWORK_RECV_STATUS_OKAY;
 
@@ -958,13 +958,13 @@ NetworkRecvStatus ClientNetworkGameSocke
 

	
 
NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_COMMAND(Packet &p)
 
{
 
	if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
 

	
 
	CommandPacket cp;
 
	const char *err = this->ReceiveCommand(p, &cp);
 
	const char *err = this->ReceiveCommand(p, cp);
 
	cp.frame    = p.Recv_uint32();
 
	cp.my_cmd   = p.Recv_bool();
 

	
 
	Debug(net, 9, "Client::Receive_SERVER_COMMAND(): cmd={}, frame={}", cp.cmd, cp.frame);
 

	
 
	if (err != nullptr) {
src/network/network_client.h
Show inline comments
 
@@ -77,13 +77,13 @@ public:
 
	~ClientNetworkGameSocketHandler();
 

	
 
	NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override;
 
	void ClientError(NetworkRecvStatus res);
 

	
 
	static NetworkRecvStatus SendJoin();
 
	static NetworkRecvStatus SendCommand(const CommandPacket *cp);
 
	static NetworkRecvStatus SendCommand(const CommandPacket &cp);
 
	static NetworkRecvStatus SendError(NetworkErrorCode errorno);
 
	static NetworkRecvStatus SendQuit();
 
	static NetworkRecvStatus SendAck();
 

	
 
	static NetworkRecvStatus SendGamePassword(const std::string &password);
 
	static NetworkRecvStatus SendCompanyPassword(const std::string &password);
src/network/network_command.cpp
Show inline comments
 
@@ -119,15 +119,15 @@ struct CallbackArgsHelper<void(*const)(C
 
};
 

	
 

	
 
/* Helpers to generate the command dispatch table from the command traits. */
 

	
 
template <Commands Tcmd> static CommandDataBuffer SanitizeCmdStrings(const CommandDataBuffer &data);
 
template <Commands Tcmd, size_t cb> static void UnpackNetworkCommand(const CommandPacket *cp);
 
template <Commands Tcmd, size_t cb> static void UnpackNetworkCommand(const CommandPacket &cp);
 
template <Commands Tcmd> static void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id);
 
using UnpackNetworkCommandProc = void (*)(const CommandPacket *);
 
using UnpackNetworkCommandProc = void (*)(const CommandPacket &);
 
using UnpackDispatchT = std::array<UnpackNetworkCommandProc, _callback_tuple_size>;
 
struct CommandDispatch {
 
	CommandDataBuffer(*Sanitize)(const CommandDataBuffer &);
 
	void (*ReplaceClientId)(CommandPacket &, ClientID);
 
	UnpackDispatchT Unpack;
 
};
 
@@ -216,13 +216,13 @@ void NetworkSendCommand(Commands cmd, St
 
		return;
 
	}
 

	
 
	c.frame = 0; // The client can't tell which frame, so just make it 0
 

	
 
	/* Clients send their command to the server and forget all about the packet */
 
	MyClient::SendCommand(&c);
 
	MyClient::SendCommand(c);
 
}
 

	
 
/**
 
 * Sync our local command queue to the command queue of the given
 
 * socket. This is needed for the case where we receive a command
 
 * before saving the game for a joining client, but without the
 
@@ -262,13 +262,13 @@ void NetworkExecuteLocalCommandQueue()
 

	
 
		/* We can execute this command */
 
		_current_company = cp->company;
 
		size_t cb_index = FindCallbackIndex(cp->callback);
 
		assert(cb_index < _callback_tuple_size);
 
		assert(_cmd_dispatch[cp->cmd].Unpack[cb_index] != nullptr);
 
		_cmd_dispatch[cp->cmd].Unpack[cb_index](&*cp);
 
		_cmd_dispatch[cp->cmd].Unpack[cb_index](*cp);
 
	}
 
	queue.erase(queue.begin(), cp);
 

	
 
	/* Local company may have changed, so we should not restore the old value */
 
	_current_company = _local_company;
 
}
 
@@ -334,13 +334,13 @@ static void DistributeQueue(CommandQueue
 
		if (_pause_mode != PM_UNPAUSED && !IsCommandAllowedWhilePaused(cp->cmd)) {
 
			++cp;
 
			continue;
 
		}
 

	
 
		DistributeCommandPacket(*cp, owner);
 
		NetworkAdminCmdLogging(owner, &*cp);
 
		NetworkAdminCmdLogging(owner, *cp);
 
		cp = queue.erase(cp);
 
	}
 
}
 

	
 
/** Distribute the commands of ourself and the clients. */
 
void NetworkDistributeCommands()
 
@@ -357,43 +357,43 @@ void NetworkDistributeCommands()
 
/**
 
 * Receives a command from the network.
 
 * @param p the packet to read from.
 
 * @param cp the struct to write the data to.
 
 * @return an error message. When nullptr there has been no error.
 
 */
 
const char *NetworkGameSocketHandler::ReceiveCommand(Packet &p, CommandPacket *cp)
 
const char *NetworkGameSocketHandler::ReceiveCommand(Packet &p, CommandPacket &cp)
 
{
 
	cp->company = (CompanyID)p.Recv_uint8();
 
	cp->cmd     = static_cast<Commands>(p.Recv_uint16());
 
	if (!IsValidCommand(cp->cmd))               return "invalid command";
 
	if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "single-player only command";
 
	cp->err_msg = p.Recv_uint16();
 
	cp->data    = _cmd_dispatch[cp->cmd].Sanitize(p.Recv_buffer());
 
	cp.company = (CompanyID)p.Recv_uint8();
 
	cp.cmd     = static_cast<Commands>(p.Recv_uint16());
 
	if (!IsValidCommand(cp.cmd))               return "invalid command";
 
	if (GetCommandFlags(cp.cmd) & CMD_OFFLINE) return "single-player only command";
 
	cp.err_msg = p.Recv_uint16();
 
	cp.data    = _cmd_dispatch[cp.cmd].Sanitize(p.Recv_buffer());
 

	
 
	byte callback = p.Recv_uint8();
 
	if (callback >= _callback_table.size() || _cmd_dispatch[cp->cmd].Unpack[callback] == nullptr)  return "invalid callback";
 
	if (callback >= _callback_table.size() || _cmd_dispatch[cp.cmd].Unpack[callback] == nullptr)  return "invalid callback";
 

	
 
	cp->callback = _callback_table[callback];
 
	cp.callback = _callback_table[callback];
 
	return nullptr;
 
}
 

	
 
/**
 
 * Sends a command over the network.
 
 * @param p the packet to send it in.
 
 * @param cp the packet to actually send.
 
 */
 
void NetworkGameSocketHandler::SendCommand(Packet &p, const CommandPacket *cp)
 
void NetworkGameSocketHandler::SendCommand(Packet &p, const CommandPacket &cp)
 
{
 
	p.Send_uint8(cp->company);
 
	p.Send_uint16(cp->cmd);
 
	p.Send_uint16(cp->err_msg);
 
	p.Send_buffer(cp->data);
 
	p.Send_uint8(cp.company);
 
	p.Send_uint16(cp.cmd);
 
	p.Send_uint16(cp.err_msg);
 
	p.Send_buffer(cp.data);
 

	
 
	size_t callback = FindCallbackIndex(cp->callback);
 
	if (callback > UINT8_MAX || _cmd_dispatch[cp->cmd].Unpack[callback] == nullptr) {
 
		Debug(net, 0, "Unknown callback for command; no callback sent (command: {})", cp->cmd);
 
	size_t callback = FindCallbackIndex(cp.callback);
 
	if (callback > UINT8_MAX || _cmd_dispatch[cp.cmd].Unpack[callback] == nullptr) {
 
		Debug(net, 0, "Unknown callback for command; no callback sent (command: {})", cp.cmd);
 
		callback = 0; // _callback_table[0] == nullptr
 
	}
 
	p.Send_uint8 ((uint8_t)callback);
 
}
 

	
 
/** Helper to process a single ClientID argument. */
 
@@ -470,11 +470,11 @@ CommandDataBuffer SanitizeCmdStrings(con
 
 * Unpack a generic command packet into its actual typed components.
 
 * @tparam Tcmd Command type to be unpacked.
 
 * @tparam Tcb Index into the callback list.
 
 * @param cp Command packet to unpack.
 
 */
 
template <Commands Tcmd, size_t Tcb>
 
void UnpackNetworkCommand(const CommandPacket *cp)
 
void UnpackNetworkCommand(const CommandPacket &cp)
 
{
 
	auto args = EndianBufferReader::ToValue<typename CommandTraits<Tcmd>::Args>(cp->data);
 
	Command<Tcmd>::PostFromNet(cp->err_msg, std::get<Tcb>(_callback_tuple), cp->my_cmd, args);
 
	auto args = EndianBufferReader::ToValue<typename CommandTraits<Tcmd>::Args>(cp.data);
 
	Command<Tcmd>::PostFromNet(cp.err_msg, std::get<Tcb>(_callback_tuple), cp.my_cmd, args);
 
}
src/network/network_server.cpp
Show inline comments
 
@@ -653,21 +653,21 @@ NetworkRecvStatus ServerNetworkGameSocke
 
}
 

	
 
/**
 
 * Send a command to the client to execute.
 
 * @param cp The command to send.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendCommand(const CommandPacket *cp)
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendCommand(const CommandPacket &cp)
 
{
 
	Debug(net, 9, "client[{}] SendCommand(): cmd={}", this->client_id, cp->cmd);
 
	Debug(net, 9, "client[{}] SendCommand(): cmd={}", this->client_id, cp.cmd);
 

	
 
	auto p = std::make_unique<Packet>(PACKET_SERVER_COMMAND);
 

	
 
	this->NetworkGameSocketHandler::SendCommand(*p, cp);
 
	p->Send_uint32(cp->frame);
 
	p->Send_bool  (cp->my_cmd);
 
	p->Send_uint32(cp.frame);
 
	p->Send_bool  (cp.my_cmd);
 

	
 
	this->SendPacket(std::move(p));
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/**
 
@@ -1064,13 +1064,13 @@ NetworkRecvStatus ServerNetworkGameSocke
 
		return this->SendError(NETWORK_ERROR_TOO_MANY_COMMANDS);
 
	}
 

	
 
	Debug(net, 9, "client[{}] Receive_CLIENT_COMMAND()", this->client_id);
 

	
 
	CommandPacket cp;
 
	const char *err = this->ReceiveCommand(p, &cp);
 
	const char *err = this->ReceiveCommand(p, cp);
 

	
 
	if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CLIENT_QUIT;
 

	
 
	NetworkClientInfo *ci = this->GetInfo();
 

	
 
	if (err != nullptr) {
 
@@ -1698,13 +1698,13 @@ void NetworkServerSetCompanyPassword(Com
 
/**
 
 * Handle the command-queue of a socket.
 
 * @param cs The socket to handle the queue for.
 
 */
 
static void NetworkHandleCommandQueue(NetworkClientSocket *cs)
 
{
 
	for (auto &cp : cs->outgoing_queue) cs->SendCommand(&cp);
 
	for (auto &cp : cs->outgoing_queue) cs->SendCommand(cp);
 
	cs->outgoing_queue.clear();
 
}
 

	
 
/**
 
 * This is called every tick if this is a _network_server
 
 * @param send_frame Whether to send the frame to the clients.
src/network/network_server.h
Show inline comments
 
@@ -94,13 +94,13 @@ public:
 
	NetworkRecvStatus SendError(NetworkErrorCode error, const std::string &reason = {});
 
	NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const std::string &msg, int64_t data);
 
	NetworkRecvStatus SendExternalChat(const std::string &source, TextColour colour, const std::string &user, const std::string &msg);
 
	NetworkRecvStatus SendJoin(ClientID client_id);
 
	NetworkRecvStatus SendFrame();
 
	NetworkRecvStatus SendSync();
 
	NetworkRecvStatus SendCommand(const CommandPacket *cp);
 
	NetworkRecvStatus SendCommand(const CommandPacket &cp);
 
	NetworkRecvStatus SendCompanyUpdate();
 
	NetworkRecvStatus SendConfigUpdate();
 

	
 
	static void Send();
 
	static void AcceptConnection(SOCKET s, const NetworkAddress &address);
 
	static bool AllowConnection();
0 comments (0 inline, 0 general)