Command: Replace binary_length field with auxiliary data

Use for CMD_ADD_PLAN_LINE
This commit is contained in:
Jonathan G Rennison
2022-12-07 20:37:23 +00:00
parent b0329ec77a
commit f32733ef22
23 changed files with 166 additions and 147 deletions

View File

@@ -85,6 +85,13 @@ bool Packet::CanWriteToPacket(size_t bytes_to_write)
return this->Size() + bytes_to_write <= this->limit;
}
void Packet::WriteAtOffset_uint16(size_t offset, uint16 data)
{
assert(offset + 1 < this->buffer.size());
this->buffer[offset] = GB(data, 0, 8);
this->buffer[offset + 1] = GB(data, 8, 8);
}
/*

View File

@@ -72,7 +72,9 @@ public:
PacketSize &GetDeserialisationPosition() { return this->pos; }
bool CanDeserialiseBytes(size_t bytes_to_read, bool raise_error) { return this->CanReadFromPacket(bytes_to_read, raise_error); }
bool CanWriteToPacket(size_t bytes_to_write);
bool CanWriteToPacket(size_t bytes_to_write);
void WriteAtOffset_uint16(size_t offset, uint16);
/* Reading/receiving of packets */
size_t ReadRawPacketSize() const;

View File

@@ -1120,7 +1120,7 @@ void NetworkGameLoop()
while (f != nullptr && !feof(f)) {
if (_date == next_date && _date_fract == next_date_fract) {
if (cp != nullptr) {
NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->p3, cp->cmd & ~CMD_FLAGS_MASK, nullptr, cp->text.c_str(), cp->company, cp->binary_length);
NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->p3, cp->cmd & ~CMD_FLAGS_MASK, nullptr, cp->text.c_str(), cp->company, cp->aux_data);
DEBUG(net, 0, "injecting: date{%08x; %02x; %02x}; %02x; %06x; %08x; %08x; " OTTD_PRINTFHEX64PAD " %08x; \"%s\" (%x) (%s)", _date, _date_fract, _tick_skip_counter, (int)_current_company, cp->tile, cp->p1, cp->p2, cp->p3, cp->cmd, cp->text.c_str(), cp->binary_length, GetCommandName(cp->cmd));
cp.reset();
}
@@ -1167,7 +1167,7 @@ void NetworkGameLoop()
* string misses because in 99% of the time it's not used. */
assert(ret == 10 || ret == 9);
cp->company = (CompanyID)company;
cp->binary_length = 0;
cp->aux_data = nullptr;
} else if (strncmp(p, "join: ", 6) == 0) {
/* Manually insert a pause when joining; this way the client can join at the exact right time. */
int ret = sscanf(p + 6, "date{%x; %x; %x}", &next_date, &next_date_fract, &next_tick_skip_counter);
@@ -1181,7 +1181,7 @@ void NetworkGameLoop()
cp->p2 = 1;
cp->p3 = 0;
cp->callback = nullptr;
cp->binary_length = 0;
cp->aux_data = nullptr;
_ddc_fastforward = false;
} else if (strncmp(p, "sync: ", 6) == 0) {
int ret = sscanf(p + 6, "date{%x; %x; %x}; %x; %x", &next_date, &next_date_fract, &next_tick_skip_counter, &sync_state[0], &sync_state[1]);

View File

@@ -1002,7 +1002,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet
* the server will give us a client-id and let us in */
_network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
ShowJoinStatusWindow();
NetworkSendCommand(0, CCA_NEW, 0, 0, CMD_COMPANY_CTRL, nullptr, nullptr, _local_company, 0);
NetworkSendCommand(0, CCA_NEW, 0, 0, CMD_COMPANY_CTRL, nullptr, nullptr, _local_company, nullptr);
}
} else {
/* take control over an existing company */

View File

@@ -12,6 +12,7 @@
#include "network_client.h"
#include "network_server.h"
#include "../command_func.h"
#include "../command_aux.h"
#include "../company_func.h"
#include "../settings_type.h"
@@ -143,9 +144,9 @@ static CommandQueue _local_execution_queue;
* @param callback A callback function to call after the command is finished
* @param text The text to pass
* @param company The company that wants to send the command
* @param binary_length The quantity of binary data in text
* @param aux_data Auxiliary command data
*/
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint64 p3, uint32 cmd, CommandCallback *callback, const char *text, CompanyID company, uint32 binary_length)
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint64 p3, uint32 cmd, CommandCallback *callback, const char *text, CompanyID company, const CommandAuxiliaryBase *aux_data)
{
assert((cmd & CMD_FLAGS_MASK) == 0);
@@ -157,16 +158,12 @@ void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint64 p3, uint32
c.p3 = p3;
c.cmd = cmd;
c.callback = callback;
if (aux_data != nullptr) c.aux_data.reset(aux_data->Clone());
c.binary_length = binary_length;
if (binary_length == 0) {
if (text != nullptr) {
c.text.assign(text);
} else {
c.text.clear();
}
if (text != nullptr) {
c.text.assign(text);
} else {
c.text.assign(text, binary_length);
c.text.clear();
}
if (_network_server) {
@@ -328,21 +325,24 @@ const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *c
cp->p2 = p->Recv_uint32();
cp->p3 = p->Recv_uint64();
cp->tile = p->Recv_uint32();
cp->binary_length = p->Recv_uint32();
if (cp->binary_length == 0) {
StringValidationSettings settings = (!_network_server && GetCommandFlags(cp->cmd) & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK;
if (GetCommandFlags(cp->cmd) & CMD_STR_SEP) settings |= SVS_ALLOW_SEPARATOR_CODE;
p->Recv_string(cp->text, settings);
} else {
if (!p->CanReadFromPacket(cp->binary_length + /* callback index */ 1)) return "invalid binary data length";
if (cp->binary_length > MAX_CMD_TEXT_LENGTH) return "over-size binary data length";
p->Recv_binary(cp->text, cp->binary_length);
}
StringValidationSettings settings = (!_network_server && GetCommandFlags(cp->cmd) & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK;
if (GetCommandFlags(cp->cmd) & CMD_STR_SEP) settings |= SVS_ALLOW_SEPARATOR_CODE;
p->Recv_string(cp->text, settings);
byte callback = p->Recv_uint8();
if (callback >= lengthof(_callback_table)) return "invalid callback";
cp->callback = _callback_table[callback];
uint16 aux_data_size = p->Recv_uint16();
if (aux_data_size > 0 && p->CanReadFromPacket(aux_data_size, true)) {
CommandAuxiliarySerialised *aux_data = new CommandAuxiliarySerialised();
cp->aux_data.reset(aux_data);
aux_data->serialised_data.resize(aux_data_size);
p->Recv_binary((char *)(aux_data->serialised_data.data()), aux_data_size);
}
return nullptr;
}
@@ -359,13 +359,7 @@ void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
p->Send_uint32(cp->p2);
p->Send_uint64(cp->p3);
p->Send_uint32(cp->tile);
p->Send_uint32(cp->binary_length);
if (cp->binary_length == 0) {
p->Send_string(cp->text.c_str());
} else {
assert(cp->text.size() >= cp->binary_length);
p->Send_binary(cp->text.c_str(), cp->binary_length);
}
p->Send_string(cp->text.c_str());
byte callback = 0;
while (callback < lengthof(_callback_table) && _callback_table[callback] != cp->callback) {
@@ -377,4 +371,12 @@ void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
callback = 0; // _callback_table[0] == nullptr
}
p->Send_uint8 (callback);
size_t aux_data_size_pos = p->Size();
p->Send_uint16(0);
if (cp->aux_data != nullptr) {
CommandSerialisationBuffer serialiser(p->GetSerialisationBuffer(), p->GetSerialisationLimit());
cp->aux_data->Serialise(serialiser);
p->WriteAtOffset_uint16(aux_data_size_pos, (uint16)(p->Size() - aux_data_size_pos - 2));
}
}

View File

@@ -1563,7 +1563,7 @@ private:
if (_network_server) {
DoCommandP(0, CCA_NEW, _network_own_client_id, CMD_COMPANY_CTRL);
} else {
NetworkSendCommand(0, CCA_NEW, 0, 0, CMD_COMPANY_CTRL, nullptr, nullptr, _local_company, 0);
NetworkSendCommand(0, CCA_NEW, 0, 0, CMD_COMPANY_CTRL, nullptr, nullptr, _local_company, nullptr);
}
}

View File

@@ -1211,7 +1211,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet *p
_settings_client.network.sync_freq = std::min<uint16>(_settings_client.network.sync_freq, 16);
// have the server and all clients run some sanity checks
NetworkSendCommand(0, 0, 0, 0, CMD_DESYNC_CHECK, nullptr, nullptr, _local_company, 0);
NetworkSendCommand(0, 0, 0, 0, CMD_DESYNC_CHECK, nullptr, nullptr, _local_company, nullptr);
SendPacketsState send_state = this->SendPackets(true);
if (send_state != SPS_CLOSED) {
@@ -2301,7 +2301,7 @@ void NetworkServerNewCompany(const Company *c, NetworkClientInfo *ci)
/* ci is nullptr when replaying, or for AIs. In neither case there is a client. */
ci->client_playas = c->index;
NetworkUpdateClientInfo(ci->client_id);
NetworkSendCommand(0, 0, 0, 0, CMD_RENAME_PRESIDENT, nullptr, ci->client_name.c_str(), c->index, 0);
NetworkSendCommand(0, 0, 0, 0, CMD_RENAME_PRESIDENT, nullptr, ci->client_name.c_str(), c->index, nullptr);
}
/* Announce new company on network. */