1
0
Fork 0

Codechange: encapsulate the logic about how many bytes can be sent from a buffer in to a Packet

pull/9100/head
Rubidium 2021-04-18 10:49:12 +02:00 committed by rubidium42
parent 38d15fc9b7
commit 6f161f6559
3 changed files with 26 additions and 12 deletions

View File

@ -175,6 +175,21 @@ void Packet::Send_string(const char *data)
while ((this->buffer[this->size++] = *data++) != '\0') {} while ((this->buffer[this->size++] = *data++) != '\0') {}
} }
/**
* 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.
*/
size_t Packet::Send_bytes(const byte *begin, const byte *end)
{
size_t amount = std::min<size_t>(end - begin, SEND_MTU - this->size);
memcpy(this->buffer + this->size, begin, amount);
this->size += static_cast<PacketSize>(amount);
return amount;
}
/* /*
* Receiving commands * Receiving commands

View File

@ -65,13 +65,14 @@ public:
/* Sending/writing of packets */ /* Sending/writing of packets */
void PrepareToSend(); void PrepareToSend();
bool CanWriteToPacket(size_t bytes_to_write); bool CanWriteToPacket(size_t bytes_to_write);
void Send_bool (bool data); void Send_bool (bool data);
void Send_uint8 (uint8 data); void Send_uint8 (uint8 data);
void Send_uint16(uint16 data); void Send_uint16(uint16 data);
void Send_uint32(uint32 data); void Send_uint32(uint32 data);
void Send_uint64(uint64 data); void Send_uint64(uint64 data);
void Send_string(const char *data); void Send_string(const char *data);
size_t Send_bytes (const byte *begin, const byte *end);
/* Reading/receiving of packets */ /* Reading/receiving of packets */
bool HasPacketSizeData() const; bool HasPacketSizeData() const;

View File

@ -174,12 +174,10 @@ struct PacketWriter : SaveFilter {
byte *bufe = buf + size; byte *bufe = buf + size;
while (buf != bufe) { while (buf != bufe) {
size_t to_write = std::min<size_t>(SEND_MTU - this->current->size, bufe - buf); size_t written = this->current->Send_bytes(buf, bufe);
memcpy(this->current->buffer + this->current->size, buf, to_write); buf += written;
this->current->size += (PacketSize)to_write;
buf += to_write;
if (this->current->size == SEND_MTU) { if (!this->current->CanWriteToPacket(1)) {
this->AppendQueue(); this->AppendQueue();
if (buf != bufe) this->current = new Packet(PACKET_SERVER_MAP_DATA); if (buf != bufe) this->current = new Packet(PACKET_SERVER_MAP_DATA);
} }