Network: Use unique_ptr for packets, use deque for packet queues
This commit is contained in:
@@ -26,7 +26,6 @@ Packet::Packet(NetworkSocketHandler *cs)
|
||||
assert(cs != nullptr);
|
||||
|
||||
this->cs = cs;
|
||||
this->next = nullptr;
|
||||
this->pos = 0; // We start reading from here
|
||||
this->size = 0;
|
||||
this->buffer = MallocT<byte>(SHRT_MAX);
|
||||
@@ -53,7 +52,6 @@ Packet::~Packet()
|
||||
void Packet::ResetState(PacketType type)
|
||||
{
|
||||
this->cs = nullptr;
|
||||
this->next = nullptr;
|
||||
|
||||
/* Skip the size so we can write that in before sending the packet */
|
||||
this->pos = 0;
|
||||
@@ -66,7 +64,7 @@ void Packet::ResetState(PacketType type)
|
||||
*/
|
||||
void Packet::PrepareToSend()
|
||||
{
|
||||
assert(this->cs == nullptr && this->next == nullptr);
|
||||
assert(this->cs == nullptr);
|
||||
|
||||
this->buffer[0] = GB(this->size, 0, 8);
|
||||
this->buffer[1] = GB(this->size, 8, 8);
|
||||
@@ -204,7 +202,7 @@ bool Packet::CanReadFromPacket(uint bytes_to_read, bool non_fatal)
|
||||
*/
|
||||
void Packet::ReadRawPacketSize()
|
||||
{
|
||||
assert(this->cs != nullptr && this->next == nullptr);
|
||||
assert(this->cs != nullptr);
|
||||
this->size = (PacketSize)this->buffer[0];
|
||||
this->size += (PacketSize)this->buffer[1] << 8;
|
||||
}
|
||||
|
||||
@@ -39,8 +39,6 @@ typedef uint8 PacketType; ///< Identifier for the packet
|
||||
* (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))
|
||||
*/
|
||||
struct Packet {
|
||||
/** The next packet. Used for queueing packets before sending. */
|
||||
Packet *next;
|
||||
/**
|
||||
* The size of the whole packet for received packets. For packets
|
||||
* that will be sent, the value is filled in just before the
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
*/
|
||||
NetworkTCPSocketHandler::NetworkTCPSocketHandler(SOCKET s) :
|
||||
NetworkSocketHandler(),
|
||||
packet_queue(nullptr), packet_recv(nullptr),
|
||||
sock(s), writable(false)
|
||||
{
|
||||
}
|
||||
@@ -41,13 +40,8 @@ NetworkRecvStatus NetworkTCPSocketHandler::CloseConnection(bool error)
|
||||
NetworkSocketHandler::CloseConnection(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 this->packet_recv;
|
||||
this->packet_recv = nullptr;
|
||||
this->packet_queue.clear();
|
||||
this->packet_recv.reset();
|
||||
|
||||
return NETWORK_RECV_STATUS_OKAY;
|
||||
}
|
||||
@@ -58,9 +52,8 @@ NetworkRecvStatus NetworkTCPSocketHandler::CloseConnection(bool error)
|
||||
* if the OS-network-buffer is full)
|
||||
* @param packet the packet to send
|
||||
*/
|
||||
void NetworkTCPSocketHandler::SendPacket(Packet *packet)
|
||||
void NetworkTCPSocketHandler::SendPacket(std::unique_ptr<Packet> packet)
|
||||
{
|
||||
Packet *p;
|
||||
assert(packet != nullptr);
|
||||
|
||||
packet->PrepareToSend();
|
||||
@@ -70,16 +63,7 @@ void NetworkTCPSocketHandler::SendPacket(Packet *packet)
|
||||
* to do a denial of service attack! */
|
||||
if (packet->size < ((SHRT_MAX * 2) / 3)) packet->buffer = ReallocT(packet->buffer, packet->size);
|
||||
|
||||
/* 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;
|
||||
}
|
||||
this->packet_queue.push_back(std::move(packet));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,14 +79,13 @@ void NetworkTCPSocketHandler::SendPacket(Packet *packet)
|
||||
SendPacketsState NetworkTCPSocketHandler::SendPackets(bool closing_down)
|
||||
{
|
||||
ssize_t res;
|
||||
Packet *p;
|
||||
|
||||
/* We can not write to this socket!! */
|
||||
if (!this->writable) return SPS_NONE_SENT;
|
||||
if (!this->IsConnected()) return SPS_CLOSED;
|
||||
|
||||
p = this->packet_queue;
|
||||
while (p != nullptr) {
|
||||
while (!this->packet_queue.empty()) {
|
||||
Packet *p = this->packet_queue.front().get();
|
||||
res = send(this->sock, (const char*)p->buffer + p->pos, p->size - p->pos, 0);
|
||||
if (res == -1) {
|
||||
int err = GET_LAST_ERROR();
|
||||
@@ -127,9 +110,7 @@ SendPacketsState NetworkTCPSocketHandler::SendPackets(bool closing_down)
|
||||
/* Is this packet sent? */
|
||||
if (p->pos == p->size) {
|
||||
/* Go to the next packet */
|
||||
this->packet_queue = p->next;
|
||||
delete p;
|
||||
p = this->packet_queue;
|
||||
this->packet_queue.pop_front();
|
||||
} else {
|
||||
return SPS_PARTLY_SENT;
|
||||
}
|
||||
@@ -142,17 +123,17 @@ SendPacketsState NetworkTCPSocketHandler::SendPackets(bool closing_down)
|
||||
* Receives a packet for the given client
|
||||
* @return The received packet (or nullptr when it didn't receive one)
|
||||
*/
|
||||
Packet *NetworkTCPSocketHandler::ReceivePacket()
|
||||
std::unique_ptr<Packet> NetworkTCPSocketHandler::ReceivePacket()
|
||||
{
|
||||
ssize_t res;
|
||||
|
||||
if (!this->IsConnected()) return nullptr;
|
||||
|
||||
if (this->packet_recv == nullptr) {
|
||||
this->packet_recv = new Packet(this);
|
||||
this->packet_recv.reset(new Packet(this));
|
||||
}
|
||||
|
||||
Packet *p = this->packet_recv;
|
||||
Packet *p = this->packet_recv.get();
|
||||
|
||||
/* Read packet size */
|
||||
if (p->pos < sizeof(PacketSize)) {
|
||||
@@ -205,11 +186,11 @@ Packet *NetworkTCPSocketHandler::ReceivePacket()
|
||||
p->pos += res;
|
||||
}
|
||||
|
||||
/* Prepare for receiving a new packet */
|
||||
this->packet_recv = nullptr;
|
||||
|
||||
p->PrepareToRead();
|
||||
return p;
|
||||
|
||||
/* Prepare for receiving a new packet */
|
||||
return std::move(this->packet_recv);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
#include "address.h"
|
||||
#include "packet.h"
|
||||
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
|
||||
/** The states of sending the packets. */
|
||||
enum SendPacketsState {
|
||||
SPS_CLOSED, ///< The connection got closed.
|
||||
@@ -26,8 +29,8 @@ enum SendPacketsState {
|
||||
/** Base socket handler for all TCP sockets */
|
||||
class NetworkTCPSocketHandler : public NetworkSocketHandler {
|
||||
private:
|
||||
Packet *packet_queue; ///< Packets that are awaiting delivery
|
||||
Packet *packet_recv; ///< Partially received packet
|
||||
std::deque<std::unique_ptr<Packet>> packet_queue; ///< Packets that are awaiting delivery
|
||||
std::unique_ptr<Packet> packet_recv; ///< Partially received packet
|
||||
public:
|
||||
SOCKET sock; ///< The socket currently connected to
|
||||
bool writable; ///< Can we write to this socket?
|
||||
@@ -39,10 +42,16 @@ public:
|
||||
bool IsConnected() const { return this->sock != INVALID_SOCKET; }
|
||||
|
||||
NetworkRecvStatus CloseConnection(bool error = true) override;
|
||||
virtual void SendPacket(Packet *packet);
|
||||
virtual void SendPacket(std::unique_ptr<Packet> packet);
|
||||
|
||||
void SendPacket(Packet *packet)
|
||||
{
|
||||
this->SendPacket(std::unique_ptr<Packet>(packet));
|
||||
}
|
||||
|
||||
SendPacketsState SendPackets(bool closing_down = false);
|
||||
|
||||
virtual Packet *ReceivePacket();
|
||||
virtual std::unique_ptr<Packet> ReceivePacket();
|
||||
|
||||
bool CanSendReceive();
|
||||
|
||||
@@ -50,7 +59,7 @@ public:
|
||||
* Whether there is something pending in the send queue.
|
||||
* @return true when something is pending in the send queue.
|
||||
*/
|
||||
bool HasSendQueue() { return this->packet_queue != nullptr; }
|
||||
bool HasSendQueue() { return !this->packet_queue.empty(); }
|
||||
|
||||
NetworkTCPSocketHandler(SOCKET s = INVALID_SOCKET);
|
||||
~NetworkTCPSocketHandler();
|
||||
|
||||
@@ -112,9 +112,9 @@ NetworkRecvStatus NetworkAdminSocketHandler::HandlePacket(Packet *p)
|
||||
*/
|
||||
NetworkRecvStatus NetworkAdminSocketHandler::ReceivePackets()
|
||||
{
|
||||
Packet *p;
|
||||
std::unique_ptr<Packet> p;
|
||||
while ((p = this->ReceivePacket()) != nullptr) {
|
||||
NetworkRecvStatus res = this->HandlePacket(p);
|
||||
NetworkRecvStatus res = this->HandlePacket(p.get());
|
||||
if (res != NETWORK_RECV_STATUS_OKAY) return res;
|
||||
}
|
||||
|
||||
|
||||
@@ -204,12 +204,11 @@ bool NetworkContentSocketHandler::ReceivePackets()
|
||||
*
|
||||
* What arbitrary number to choose is the ultimate question though.
|
||||
*/
|
||||
Packet *p;
|
||||
std::unique_ptr<Packet> p;
|
||||
static const int MAX_PACKETS_TO_RECEIVE = 42;
|
||||
int i = MAX_PACKETS_TO_RECEIVE;
|
||||
while (--i != 0 && (p = this->ReceivePacket()) != nullptr) {
|
||||
bool cont = this->HandlePacket(p);
|
||||
delete p;
|
||||
bool cont = this->HandlePacket(p.get());
|
||||
if (!cont) return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -137,10 +137,9 @@ NetworkRecvStatus NetworkGameSocketHandler::HandlePacket(Packet *p)
|
||||
*/
|
||||
NetworkRecvStatus NetworkGameSocketHandler::ReceivePackets()
|
||||
{
|
||||
Packet *p;
|
||||
std::unique_ptr<Packet> p;
|
||||
while ((p = this->ReceivePacket()) != nullptr) {
|
||||
NetworkRecvStatus res = HandlePacket(p);
|
||||
delete p;
|
||||
NetworkRecvStatus res = HandlePacket(p.get());
|
||||
if (res != NETWORK_RECV_STATUS_OKAY) return res;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user