Network: Use unique_ptr for packets, use deque for packet queues

This commit is contained in:
Jonathan G Rennison
2020-05-06 21:13:31 +01:00
parent caa27cfa39
commit a1d85b812b
9 changed files with 62 additions and 84 deletions

View File

@@ -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();