From ba409e8c4502cc435d0d3f4bec7ba5f5fe28b658 Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Tue, 27 Apr 2021 21:25:52 +0200 Subject: Add: [Network] Writing std::string to a packet --- src/network/core/packet.cpp | 9 ++++----- src/network/core/packet.h | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'src/network/core') 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 */ -- cgit v1.2.3-70-g09d2