summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-04-27 21:25:52 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-05-03 17:56:05 +0200
commitba409e8c4502cc435d0d3f4bec7ba5f5fe28b658 (patch)
tree0dcb7e927b2f9fdb8ba92a5b3af9168e73777eef
parent8228021afea409ad41268f995ce6ab629f3cbc0d (diff)
downloadopenttd-ba409e8c4502cc435d0d3f4bec7ba5f5fe28b658.tar.xz
Add: [Network] Writing std::string to a packet
-rw-r--r--src/network/core/packet.cpp9
-rw-r--r--src/network/core/packet.h2
2 files changed, 5 insertions, 6 deletions
diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp
index 644490e0f..6f56e4a56 100644
--- a/src/network/core/packet.cpp
+++ b/src/network/core/packet.cpp
@@ -178,12 +178,11 @@ void Packet::Send_uint64(uint64 data)
* the string + '\0'. No size-byte or something.
* @param data The string to send
*/
-void Packet::Send_string(const char *data)
+void Packet::Send_string(const std::string_view data)
{
- assert(data != nullptr);
- /* Length of the string + 1 for the '\0' termination. */
- assert(this->CanWriteToPacket(strlen(data) + 1));
- while (this->buffer.emplace_back(*data++) != '\0') {}
+ assert(this->CanWriteToPacket(data.size() + 1));
+ this->buffer.insert(this->buffer.end(), data.begin(), data.end());
+ this->buffer.emplace_back('\0');
}
/**
diff --git a/src/network/core/packet.h b/src/network/core/packet.h
index bc7bab6c4..0f58d3be8 100644
--- a/src/network/core/packet.h
+++ b/src/network/core/packet.h
@@ -71,7 +71,7 @@ public:
void Send_uint16(uint16 data);
void Send_uint32(uint32 data);
void Send_uint64(uint64 data);
- void Send_string(const char *data);
+ void Send_string(const std::string_view data);
size_t Send_bytes (const byte *begin, const byte *end);
/* Reading/receiving of packets */