diff options
author | rubidium42 <rubidium@openttd.org> | 2021-05-29 16:16:51 +0200 |
---|---|---|
committer | rubidium42 <rubidium42@users.noreply.github.com> | 2021-05-29 19:02:18 +0200 |
commit | ef991b1772f5f8b4874f76ea715a9a95811ad979 (patch) | |
tree | 1eec8f86826d2865bceb31612843dba05f237d86 /src | |
parent | 2e136285e1dfb4435c11769bf7cabb1ec2057e08 (diff) | |
download | openttd-ef991b1772f5f8b4874f76ea715a9a95811ad979.tar.xz |
Codechange: [Network] Use std::string in CommandPacket
Diffstat (limited to 'src')
-rw-r--r-- | src/command_type.h | 2 | ||||
-rw-r--r-- | src/network/network.cpp | 11 | ||||
-rw-r--r-- | src/network/network_client.cpp | 2 | ||||
-rw-r--r-- | src/network/network_command.cpp | 13 | ||||
-rw-r--r-- | src/network/network_gui.cpp | 2 | ||||
-rw-r--r-- | src/network/network_server.cpp | 2 | ||||
-rw-r--r-- | src/settings.cpp | 2 |
7 files changed, 17 insertions, 17 deletions
diff --git a/src/command_type.h b/src/command_type.h index a8e16e6d7..61154ea04 100644 --- a/src/command_type.h +++ b/src/command_type.h @@ -482,7 +482,7 @@ struct CommandContainer { uint32 p2; ///< parameter p2. uint32 cmd; ///< command being executed. 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 */ diff --git a/src/network/network.cpp b/src/network/network.cpp index 94a5539f0..2bc358944 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -1071,7 +1071,7 @@ void NetworkGameLoop() if (_date == next_date && _date_fract == next_date_fract) { if (cp != nullptr) { 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); cp = nullptr; } @@ -1107,10 +1107,11 @@ void NetworkGameLoop() ) { p += 5; if (*p == ' ') p++; - cp = CallocT<CommandPacket>(1); + cp = new CommandPacket(); int company; - static_assert(sizeof(cp->text) == 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); + 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, buffer); + cp->text = buffer; /* 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 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); assert(ret == 2); 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->cmd = CMD_PAUSE; cp->p1 = PM_PAUSED_NORMAL; diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index 1a729e34f..44359941a 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -944,7 +944,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, CMD_COMPANY_CTRL, nullptr, nullptr, _local_company); + NetworkSendCommand(0, CCA_NEW, 0, CMD_COMPANY_CTRL, nullptr, {}, _local_company); } } else { /* take control over an existing company */ diff --git a/src/network/network_command.cpp b/src/network/network_command.cpp index d41c89272..d43e971ff 100644 --- a/src/network/network_command.cpp +++ b/src/network/network_command.cpp @@ -56,7 +56,7 @@ static CommandCallback * const _callback_table[] = { */ void CommandQueue::Append(CommandPacket *p) { - CommandPacket *add = MallocT<CommandPacket>(1); + CommandPacket *add = new CommandPacket(); *add = *p; add->next = nullptr; if (this->first == nullptr) { @@ -113,7 +113,7 @@ void CommandQueue::Free() { CommandPacket *cp; while ((cp = this->Pop()) != nullptr) { - free(cp); + delete cp; } assert(this->count == 0); } @@ -144,8 +144,7 @@ void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, Comman c.p2 = p2; c.cmd = cmd; c.callback = callback; - - strecpy(c.text, text.c_str(), lastof(c.text)); + c.text = text; if (_network_server) { /* If we are the server, we queue the command in our 'special' queue. @@ -212,7 +211,7 @@ void NetworkExecuteLocalCommandQueue() DoCommandP(cp, cp->my_cmd); queue.Pop(); - free(cp); + delete cp; } /* 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) { DistributeCommandPacket(*cp, owner); 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->p2 = 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(); if (callback >= lengthof(_callback_table)) return "invalid callback"; diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index d06ff27be..322e128cf 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -1825,7 +1825,7 @@ private: if (_network_server) { DoCommandP(0, CCA_NEW, _network_own_client_id, CMD_COMPANY_CTRL); } else { - NetworkSendCommand(0, CCA_NEW, 0, CMD_COMPANY_CTRL, nullptr, nullptr, _local_company); + NetworkSendCommand(0, CCA_NEW, 0, CMD_COMPANY_CTRL, nullptr, {}, _local_company); } } diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 3a9128427..fe585c03c 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -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->client_playas = c->index; 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. */ diff --git a/src/settings.cpp b/src/settings.cpp index 158c15fc3..11dff7143 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1864,7 +1864,7 @@ void SyncCompanySettings() for (auto &sd : _company_settings) { uint32 old_value = (uint32)sd->AsIntSetting()->Read(new_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++; } } |