From 15d02f51ed61dd7ef26ea0fa84ab9c47424ecc2c Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 28 Jan 2024 13:28:07 +0100 Subject: [PATCH] Codechange: use span to send bytes to Packet and add span recv function --- src/network/core/packet.cpp | 31 +++++++++++++++++++++++-------- src/network/core/packet.h | 3 ++- src/network/network_server.cpp | 9 ++++----- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp index f314dc803c..5339c43677 100644 --- a/src/network/core/packet.cpp +++ b/src/network/core/packet.cpp @@ -176,16 +176,15 @@ void Packet::Send_buffer(const std::vector &data) /** * Send as many of the bytes as possible in the packet. This can mean * that it is possible that not all bytes are sent. To cope with this - * the function returns the amount of bytes that were actually sent. - * @param begin The begin of the buffer to send. - * @param end The end of the buffer to send. - * @return The number of bytes that were added to this packet. + * the function returns the span of bytes that were not sent. + * @param span The span describing the range of bytes to send. + * @return The span of bytes that were not written. */ -size_t Packet::Send_bytes(const byte *begin, const byte *end) +std::span Packet::Send_bytes(const std::span span) { - size_t amount = std::min(end - begin, this->limit - this->Size()); - this->buffer.insert(this->buffer.end(), begin, begin + amount); - return amount; + size_t amount = std::min(span.size(), this->limit - this->Size()); + this->buffer.insert(this->buffer.end(), span.data(), span.data() + amount); + return span.subspan(amount); } /* @@ -370,6 +369,22 @@ std::vector Packet::Recv_buffer() return data; } +/** + * Extract at most the length of the span bytes from the packet into the span. + * @param span The span to write the bytes to. + * @return The number of bytes that were actually read. + */ +size_t Packet::Recv_bytes(std::span span) +{ + auto tranfer_to_span = [](std::span destination, const char *source, size_t amount) { + size_t to_copy = std::min(amount, destination.size()); + std::copy(source, source + to_copy, destination.data()); + return to_copy; + }; + + return this->TransferOut(tranfer_to_span, span); +} + /** * Reads characters (bytes) from the packet until it finds a '\0', or reaches a * maximum of \c length characters. diff --git a/src/network/core/packet.h b/src/network/core/packet.h index f403f37ae7..ccb56738ff 100644 --- a/src/network/core/packet.h +++ b/src/network/core/packet.h @@ -66,7 +66,7 @@ public: void Send_uint64(uint64_t data); void Send_string(const std::string_view data); void Send_buffer(const std::vector &data); - size_t Send_bytes (const byte *begin, const byte *end); + std::span Send_bytes(const std::span span); /* Reading/receiving of packets */ bool HasPacketSizeData() const; @@ -82,6 +82,7 @@ public: uint32_t Recv_uint32(); uint64_t Recv_uint64(); std::vector Recv_buffer(); + size_t Recv_bytes(std::span span); std::string Recv_string(size_t length, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK); size_t RemainingBytesToTransfer() const; diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 65cb5388ae..2e6c51920e 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -145,14 +145,13 @@ struct PacketWriter : SaveFilter { if (this->current == nullptr) this->current = std::make_unique(this->cs, PACKET_SERVER_MAP_DATA, TCP_MTU); - byte *bufe = buf + size; - while (buf != bufe) { - size_t written = this->current->Send_bytes(buf, bufe); - buf += written; + std::span to_write(buf, size); + while (!to_write.empty()) { + to_write = this->current->Send_bytes(to_write); if (!this->current->CanWriteToPacket(1)) { this->packets.push_back(std::move(this->current)); - if (buf != bufe) this->current = std::make_unique(this->cs, PACKET_SERVER_MAP_DATA, TCP_MTU); + if (!to_write.empty()) this->current = std::make_unique(this->cs, PACKET_SERVER_MAP_DATA, TCP_MTU); } }