mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use span to send bytes to Packet and add span recv function
parent
b6c75dec3a
commit
15d02f51ed
|
@ -176,16 +176,15 @@ void Packet::Send_buffer(const std::vector<byte> &data)
|
||||||
/**
|
/**
|
||||||
* Send as many of the bytes as possible in the packet. This can mean
|
* 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
|
* 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.
|
* the function returns the span of bytes that were not sent.
|
||||||
* @param begin The begin of the buffer to send.
|
* @param span The span describing the range of bytes to send.
|
||||||
* @param end The end of the buffer to send.
|
* @return The span of bytes that were not written.
|
||||||
* @return The number of bytes that were added to this packet.
|
|
||||||
*/
|
*/
|
||||||
size_t Packet::Send_bytes(const byte *begin, const byte *end)
|
std::span<const byte> Packet::Send_bytes(const std::span<const byte> span)
|
||||||
{
|
{
|
||||||
size_t amount = std::min<size_t>(end - begin, this->limit - this->Size());
|
size_t amount = std::min<size_t>(span.size(), this->limit - this->Size());
|
||||||
this->buffer.insert(this->buffer.end(), begin, begin + amount);
|
this->buffer.insert(this->buffer.end(), span.data(), span.data() + amount);
|
||||||
return amount;
|
return span.subspan(amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -370,6 +369,22 @@ std::vector<byte> Packet::Recv_buffer()
|
||||||
return data;
|
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<byte> span)
|
||||||
|
{
|
||||||
|
auto tranfer_to_span = [](std::span<byte> 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
|
* Reads characters (bytes) from the packet until it finds a '\0', or reaches a
|
||||||
* maximum of \c length characters.
|
* maximum of \c length characters.
|
||||||
|
|
|
@ -66,7 +66,7 @@ public:
|
||||||
void Send_uint64(uint64_t data);
|
void Send_uint64(uint64_t data);
|
||||||
void Send_string(const std::string_view data);
|
void Send_string(const std::string_view data);
|
||||||
void Send_buffer(const std::vector<byte> &data);
|
void Send_buffer(const std::vector<byte> &data);
|
||||||
size_t Send_bytes (const byte *begin, const byte *end);
|
std::span<const byte> Send_bytes(const std::span<const byte> span);
|
||||||
|
|
||||||
/* Reading/receiving of packets */
|
/* Reading/receiving of packets */
|
||||||
bool HasPacketSizeData() const;
|
bool HasPacketSizeData() const;
|
||||||
|
@ -82,6 +82,7 @@ public:
|
||||||
uint32_t Recv_uint32();
|
uint32_t Recv_uint32();
|
||||||
uint64_t Recv_uint64();
|
uint64_t Recv_uint64();
|
||||||
std::vector<byte> Recv_buffer();
|
std::vector<byte> Recv_buffer();
|
||||||
|
size_t Recv_bytes(std::span<byte> span);
|
||||||
std::string Recv_string(size_t length, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
|
std::string Recv_string(size_t length, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
|
||||||
|
|
||||||
size_t RemainingBytesToTransfer() const;
|
size_t RemainingBytesToTransfer() const;
|
||||||
|
|
|
@ -145,14 +145,13 @@ struct PacketWriter : SaveFilter {
|
||||||
|
|
||||||
if (this->current == nullptr) this->current = std::make_unique<Packet>(this->cs, PACKET_SERVER_MAP_DATA, TCP_MTU);
|
if (this->current == nullptr) this->current = std::make_unique<Packet>(this->cs, PACKET_SERVER_MAP_DATA, TCP_MTU);
|
||||||
|
|
||||||
byte *bufe = buf + size;
|
std::span<const byte> to_write(buf, size);
|
||||||
while (buf != bufe) {
|
while (!to_write.empty()) {
|
||||||
size_t written = this->current->Send_bytes(buf, bufe);
|
to_write = this->current->Send_bytes(to_write);
|
||||||
buf += written;
|
|
||||||
|
|
||||||
if (!this->current->CanWriteToPacket(1)) {
|
if (!this->current->CanWriteToPacket(1)) {
|
||||||
this->packets.push_back(std::move(this->current));
|
this->packets.push_back(std::move(this->current));
|
||||||
if (buf != bufe) this->current = std::make_unique<Packet>(this->cs, PACKET_SERVER_MAP_DATA, TCP_MTU);
|
if (!to_write.empty()) this->current = std::make_unique<Packet>(this->cs, PACKET_SERVER_MAP_DATA, TCP_MTU);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue