summaryrefslogtreecommitdiff
path: root/src/network/core
diff options
context:
space:
mode:
authorRubidium <rubidium@openttd.org>2021-04-20 18:50:46 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-04-24 20:42:01 +0200
commit3abefdf56190ef55d8680acb1aeab9f1b2fc8108 (patch)
tree779a93162e2a0875ff2c12a8783f30b07c538df1 /src/network/core
parentf71fb0f54af443cee37d3c41a0d4f39a24617741 (diff)
downloadopenttd-3abefdf56190ef55d8680acb1aeab9f1b2fc8108.tar.xz
Codechange: remove public access to the next pointer in Packet
Diffstat (limited to 'src/network/core')
-rw-r--r--src/network/core/packet.cpp26
-rw-r--r--src/network/core/packet.h3
-rw-r--r--src/network/core/tcp.cpp24
3 files changed, 33 insertions, 20 deletions
diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp
index e32b7fad8..9e9ce6901 100644
--- a/src/network/core/packet.cpp
+++ b/src/network/core/packet.cpp
@@ -63,6 +63,32 @@ Packet::~Packet()
}
/**
+ * Add the given Packet to the end of the queue of packets.
+ * @param queue The pointer to the begin of the queue.
+ * @param packet The packet to append to the queue.
+ */
+/* static */ void Packet::AddToQueue(Packet **queue, Packet *packet)
+{
+ while (*queue != nullptr) queue = &(*queue)->next;
+ *queue = packet;
+}
+
+/**
+ * Pop the packet from the begin of the queue and set the
+ * begin of the queue to the second element in the queue.
+ * @param queue The pointer to the begin of the queue.
+ * @return The Packet that used to be a the begin of the queue.
+ */
+/* static */ Packet *Packet::PopFromQueue(Packet **queue)
+{
+ Packet *p = *queue;
+ *queue = p->next;
+ p->next = nullptr;
+ return p;
+}
+
+
+/**
* Writes the packet size from the raw packet from packet->size
*/
void Packet::PrepareToSend()
diff --git a/src/network/core/packet.h b/src/network/core/packet.h
index b6a7ff5d3..d7ab7fee6 100644
--- a/src/network/core/packet.h
+++ b/src/network/core/packet.h
@@ -62,6 +62,9 @@ public:
Packet(PacketType type);
~Packet();
+ static void AddToQueue(Packet **queue, Packet *packet);
+ static Packet *PopFromQueue(Packet **queue);
+
/* Sending/writing of packets */
void PrepareToSend();
diff --git a/src/network/core/tcp.cpp b/src/network/core/tcp.cpp
index c779beb96..a749b6195 100644
--- a/src/network/core/tcp.cpp
+++ b/src/network/core/tcp.cpp
@@ -42,9 +42,7 @@ NetworkRecvStatus NetworkTCPSocketHandler::CloseConnection(bool error)
/* Free all pending and partially received packets */
while (this->packet_queue != nullptr) {
- Packet *p = this->packet_queue->next;
- delete this->packet_queue;
- this->packet_queue = p;
+ delete Packet::PopFromQueue(&this->packet_queue);
}
delete this->packet_recv;
this->packet_recv = nullptr;
@@ -60,21 +58,10 @@ NetworkRecvStatus NetworkTCPSocketHandler::CloseConnection(bool error)
*/
void NetworkTCPSocketHandler::SendPacket(Packet *packet)
{
- Packet *p;
assert(packet != nullptr);
packet->PrepareToSend();
-
- /* Locate last packet buffered for the client */
- p = this->packet_queue;
- if (p == nullptr) {
- /* No packets yet */
- this->packet_queue = packet;
- } else {
- /* Skip to the last packet */
- while (p->next != nullptr) p = p->next;
- p->next = packet;
- }
+ Packet::AddToQueue(&this->packet_queue, packet);
}
/**
@@ -96,8 +83,7 @@ SendPacketsState NetworkTCPSocketHandler::SendPackets(bool closing_down)
if (!this->writable) return SPS_NONE_SENT;
if (!this->IsConnected()) return SPS_CLOSED;
- p = this->packet_queue;
- while (p != nullptr) {
+ while ((p = this->packet_queue) != nullptr) {
res = p->TransferOut<int>(send, this->sock, 0);
if (res == -1) {
int err = GET_LAST_ERROR();
@@ -120,9 +106,7 @@ SendPacketsState NetworkTCPSocketHandler::SendPackets(bool closing_down)
/* Is this packet sent? */
if (p->RemainingBytesToTransfer() == 0) {
/* Go to the next packet */
- this->packet_queue = p->next;
- delete p;
- p = this->packet_queue;
+ delete Packet::PopFromQueue(&this->packet_queue);
} else {
return SPS_PARTLY_SENT;
}