Codechange: [Network] Use std::string in CommandPacket

This commit is contained in:
rubidium42 2021-05-29 16:16:51 +02:00 committed by rubidium42
parent 2e136285e1
commit ef991b1772
7 changed files with 17 additions and 17 deletions

View File

@ -482,7 +482,7 @@ struct CommandContainer {
uint32 p2; ///< parameter p2. uint32 p2; ///< parameter p2.
uint32 cmd; ///< command being executed. uint32 cmd; ///< command being executed.
CommandCallback *callback; ///< any callback function executed upon successful completion of the command. CommandCallback *callback; ///< any callback function executed upon successful completion of the command.
char text[32 * MAX_CHAR_LENGTH]; ///< possible text sent for name changes etc, in bytes including '\0'. std::string text; ///< possible text sent for name changes etc.
}; };
#endif /* COMMAND_TYPE_H */ #endif /* COMMAND_TYPE_H */

View File

@ -1071,7 +1071,7 @@ void NetworkGameLoop()
if (_date == next_date && _date_fract == next_date_fract) { if (_date == next_date && _date_fract == next_date_fract) {
if (cp != nullptr) { if (cp != nullptr) {
NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->cmd & ~CMD_FLAGS_MASK, nullptr, cp->text, cp->company); NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->cmd & ~CMD_FLAGS_MASK, nullptr, cp->text, cp->company);
DEBUG(desync, 0, "Injecting: %08x; %02x; %02x; %06x; %08x; %08x; %08x; \"%s\" (%s)", _date, _date_fract, (int)_current_company, cp->tile, cp->p1, cp->p2, cp->cmd, cp->text, GetCommandName(cp->cmd)); DEBUG(desync, 0, "Injecting: %08x; %02x; %02x; %06x; %08x; %08x; %08x; \"%s\" (%s)", _date, _date_fract, (int)_current_company, cp->tile, cp->p1, cp->p2, cp->cmd, cp->text.c_str(), GetCommandName(cp->cmd));
free(cp); free(cp);
cp = nullptr; cp = nullptr;
} }
@ -1107,10 +1107,11 @@ void NetworkGameLoop()
) { ) {
p += 5; p += 5;
if (*p == ' ') p++; if (*p == ' ') p++;
cp = CallocT<CommandPacket>(1); cp = new CommandPacket();
int company; int company;
static_assert(sizeof(cp->text) == 128); char buffer[128];
int ret = sscanf(p, "%x; %x; %x; %x; %x; %x; %x; \"%127[^\"]\"", &next_date, &next_date_fract, &company, &cp->tile, &cp->p1, &cp->p2, &cp->cmd, cp->text); int ret = sscanf(p, "%x; %x; %x; %x; %x; %x; %x; \"%127[^\"]\"", &next_date, &next_date_fract, &company, &cp->tile, &cp->p1, &cp->p2, &cp->cmd, buffer);
cp->text = buffer;
/* There are 8 pieces of data to read, however the last is a /* There are 8 pieces of data to read, however the last is a
* string that might or might not exist. Ignore it if that * string that might or might not exist. Ignore it if that
* string misses because in 99% of the time it's not used. */ * string misses because in 99% of the time it's not used. */
@ -1121,7 +1122,7 @@ void NetworkGameLoop()
int ret = sscanf(p + 6, "%x; %x", &next_date, &next_date_fract); int ret = sscanf(p + 6, "%x; %x", &next_date, &next_date_fract);
assert(ret == 2); assert(ret == 2);
DEBUG(desync, 0, "Injecting pause for join at %08x:%02x; please join when paused", next_date, next_date_fract); DEBUG(desync, 0, "Injecting pause for join at %08x:%02x; please join when paused", next_date, next_date_fract);
cp = CallocT<CommandPacket>(1); cp = new CommandPacket();
cp->company = COMPANY_SPECTATOR; cp->company = COMPANY_SPECTATOR;
cp->cmd = CMD_PAUSE; cp->cmd = CMD_PAUSE;
cp->p1 = PM_PAUSED_NORMAL; cp->p1 = PM_PAUSED_NORMAL;

View File

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

View File

@ -56,7 +56,7 @@ static CommandCallback * const _callback_table[] = {
*/ */
void CommandQueue::Append(CommandPacket *p) void CommandQueue::Append(CommandPacket *p)
{ {
CommandPacket *add = MallocT<CommandPacket>(1); CommandPacket *add = new CommandPacket();
*add = *p; *add = *p;
add->next = nullptr; add->next = nullptr;
if (this->first == nullptr) { if (this->first == nullptr) {
@ -113,7 +113,7 @@ void CommandQueue::Free()
{ {
CommandPacket *cp; CommandPacket *cp;
while ((cp = this->Pop()) != nullptr) { while ((cp = this->Pop()) != nullptr) {
free(cp); delete cp;
} }
assert(this->count == 0); assert(this->count == 0);
} }
@ -144,8 +144,7 @@ void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, Comman
c.p2 = p2; c.p2 = p2;
c.cmd = cmd; c.cmd = cmd;
c.callback = callback; c.callback = callback;
c.text = text;
strecpy(c.text, text.c_str(), lastof(c.text));
if (_network_server) { if (_network_server) {
/* If we are the server, we queue the command in our 'special' queue. /* If we are the server, we queue the command in our 'special' queue.
@ -212,7 +211,7 @@ void NetworkExecuteLocalCommandQueue()
DoCommandP(cp, cp->my_cmd); DoCommandP(cp, cp->my_cmd);
queue.Pop(); queue.Pop();
free(cp); delete cp;
} }
/* Local company may have changed, so we should not restore the old value */ /* Local company may have changed, so we should not restore the old value */
@ -271,7 +270,7 @@ static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owne
while (--to_go >= 0 && (cp = queue->Pop(true)) != nullptr) { while (--to_go >= 0 && (cp = queue->Pop(true)) != nullptr) {
DistributeCommandPacket(*cp, owner); DistributeCommandPacket(*cp, owner);
NetworkAdminCmdLogging(owner, cp); NetworkAdminCmdLogging(owner, cp);
free(cp); delete cp;
} }
} }
@ -304,7 +303,7 @@ const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *c
cp->p1 = p->Recv_uint32(); cp->p1 = p->Recv_uint32();
cp->p2 = p->Recv_uint32(); cp->p2 = p->Recv_uint32();
cp->tile = p->Recv_uint32(); cp->tile = p->Recv_uint32();
p->Recv_string(cp->text, lengthof(cp->text), (!_network_server && GetCommandFlags(cp->cmd) & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK); cp->text = p->Recv_string(NETWORK_COMPANY_NAME_LENGTH, (!_network_server && GetCommandFlags(cp->cmd) & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK);
byte callback = p->Recv_uint8(); byte callback = p->Recv_uint8();
if (callback >= lengthof(_callback_table)) return "invalid callback"; if (callback >= lengthof(_callback_table)) return "invalid callback";

View File

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

View File

@ -2182,7 +2182,7 @@ void NetworkServerNewCompany(const Company *c, NetworkClientInfo *ci)
/* ci is nullptr when replaying, or for AIs. In neither case there is a client. */ /* ci is nullptr when replaying, or for AIs. In neither case there is a client. */
ci->client_playas = c->index; ci->client_playas = c->index;
NetworkUpdateClientInfo(ci->client_id); NetworkUpdateClientInfo(ci->client_id);
NetworkSendCommand(0, 0, 0, CMD_RENAME_PRESIDENT, nullptr, ci->client_name.c_str(), c->index); NetworkSendCommand(0, 0, 0, CMD_RENAME_PRESIDENT, nullptr, ci->client_name, c->index);
} }
/* Announce new company on network. */ /* Announce new company on network. */

View File

@ -1864,7 +1864,7 @@ void SyncCompanySettings()
for (auto &sd : _company_settings) { for (auto &sd : _company_settings) {
uint32 old_value = (uint32)sd->AsIntSetting()->Read(new_object); uint32 old_value = (uint32)sd->AsIntSetting()->Read(new_object);
uint32 new_value = (uint32)sd->AsIntSetting()->Read(old_object); uint32 new_value = (uint32)sd->AsIntSetting()->Read(old_object);
if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, nullptr, nullptr, _local_company); if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, nullptr, {}, _local_company);
i++; i++;
} }
} }