1
0
Fork 0

Codechange: Remove ZeroedMemoryAllocator from network socket handlers. (#13377)

Prefer member default initialisation instead.
pull/13383/head
Peter Nelson 2025-01-28 19:10:00 +00:00 committed by GitHub
parent 4099acb946
commit 77f4d776c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 43 additions and 60 deletions

View File

@ -42,16 +42,16 @@ struct Packet;
*/ */
class NetworkSocketHandler { class NetworkSocketHandler {
private: private:
bool has_quit; ///< Whether the current client has quit/send a bad packet bool has_quit = false; ///< Whether the current client has quit/send a bad packet
protected: protected:
friend struct Packet; friend struct Packet;
std::unique_ptr<class NetworkEncryptionHandler> receive_encryption_handler; ///< The handler for decrypting received packets. std::unique_ptr<class NetworkEncryptionHandler> receive_encryption_handler = nullptr; ///< The handler for decrypting received packets.
std::unique_ptr<class NetworkEncryptionHandler> send_encryption_handler; ///< The handler for encrypting sent packets. std::unique_ptr<class NetworkEncryptionHandler> send_encryption_handler = nullptr; ///< The handler for encrypting sent packets.
public: public:
/** Create a new unbound socket */ /** Create a new unbound socket */
NetworkSocketHandler() { this->has_quit = false; } NetworkSocketHandler() = default;
/** Close the socket when destructing the socket handler */ /** Close the socket when destructing the socket handler */
virtual ~NetworkSocketHandler() = default; virtual ~NetworkSocketHandler() = default;

View File

@ -9,7 +9,6 @@
#include "../../stdafx.h" #include "../../stdafx.h"
#include "../../debug.h" #include "../../debug.h"
#include "../../core/alloc_func.hpp"
#include "address.h" #include "address.h"
#include "../../safeguards.h" #include "../../safeguards.h"

View File

@ -16,16 +16,6 @@
#include "../../safeguards.h" #include "../../safeguards.h"
/**
* Construct a socket handler for a TCP connection.
* @param s The just opened TCP connection.
*/
NetworkTCPSocketHandler::NetworkTCPSocketHandler(SOCKET s) :
NetworkSocketHandler(),
sock(s), writable(false)
{
}
NetworkTCPSocketHandler::~NetworkTCPSocketHandler() NetworkTCPSocketHandler::~NetworkTCPSocketHandler()
{ {
this->CloseSocket(); this->CloseSocket();

View File

@ -31,12 +31,12 @@ enum SendPacketsState {
class NetworkTCPSocketHandler : public NetworkSocketHandler { class NetworkTCPSocketHandler : public NetworkSocketHandler {
private: private:
std::deque<std::unique_ptr<Packet>> packet_queue; ///< Packets that are awaiting delivery. Cannot be std::queue as that does not have a clear() function. std::deque<std::unique_ptr<Packet>> packet_queue; ///< Packets that are awaiting delivery. Cannot be std::queue as that does not have a clear() function.
std::unique_ptr<Packet> packet_recv; ///< Partially received packet std::unique_ptr<Packet> packet_recv = nullptr; ///< Partially received packet
void EmptyPacketQueue(); void EmptyPacketQueue();
public: public:
SOCKET sock; ///< The socket currently connected to SOCKET sock = INVALID_SOCKET; ///< The socket currently connected to
bool writable; ///< Can we write to this socket? bool writable = false; ///< Can we write to this socket?
/** /**
* Whether this socket is currently bound to a socket. * Whether this socket is currently bound to a socket.
@ -60,7 +60,11 @@ public:
*/ */
bool HasSendQueue() { return !this->packet_queue.empty(); } bool HasSendQueue() { return !this->packet_queue.empty(); }
NetworkTCPSocketHandler(SOCKET s = INVALID_SOCKET); /**
* Construct a socket handler for a TCP connection.
* @param s The just opened TCP connection.
*/
NetworkTCPSocketHandler(SOCKET s = INVALID_SOCKET) : sock(s) {}
~NetworkTCPSocketHandler(); ~NetworkTCPSocketHandler();
}; };

View File

@ -23,15 +23,6 @@ static_assert((int)CRR_AUTOCLEAN == (int)ADMIN_CRR_AUTOCLEAN);
static_assert((int)CRR_BANKRUPT == (int)ADMIN_CRR_BANKRUPT); static_assert((int)CRR_BANKRUPT == (int)ADMIN_CRR_BANKRUPT);
static_assert((int)CRR_END == (int)ADMIN_CRR_END); static_assert((int)CRR_END == (int)ADMIN_CRR_END);
/**
* Create the admin handler for the given socket.
* @param s The socket to communicate over.
*/
NetworkAdminSocketHandler::NetworkAdminSocketHandler(SOCKET s) : status(ADMIN_STATUS_INACTIVE)
{
this->sock = s;
}
NetworkRecvStatus NetworkAdminSocketHandler::CloseConnection(bool) NetworkRecvStatus NetworkAdminSocketHandler::CloseConnection(bool)
{ {
delete this; delete this;

View File

@ -118,7 +118,7 @@ class NetworkAdminSocketHandler : public NetworkTCPSocketHandler {
protected: protected:
std::string admin_name; ///< Name of the admin. std::string admin_name; ///< Name of the admin.
std::string admin_version; ///< Version string of the admin. std::string admin_version; ///< Version string of the admin.
AdminStatus status; ///< Status of this admin. AdminStatus status = ADMIN_STATUS_INACTIVE; ///< Status of this admin.
NetworkRecvStatus ReceiveInvalidPacket(PacketAdminType type); NetworkRecvStatus ReceiveInvalidPacket(PacketAdminType type);
@ -542,7 +542,11 @@ protected:
public: public:
NetworkRecvStatus CloseConnection(bool error = true) override; NetworkRecvStatus CloseConnection(bool error = true) override;
NetworkAdminSocketHandler(SOCKET s); /**
* Create the admin handler for the given socket.
* @param s The socket to communicate over.
*/
NetworkAdminSocketHandler(SOCKET s) : NetworkTCPSocketHandler(s) {}
NetworkRecvStatus ReceivePackets(); NetworkRecvStatus ReceivePackets();

View File

@ -26,12 +26,8 @@ static std::vector<std::unique_ptr<NetworkGameSocketHandler>> _deferred_deletion
* Create a new socket for the game connection. * Create a new socket for the game connection.
* @param s The socket to connect with. * @param s The socket to connect with.
*/ */
NetworkGameSocketHandler::NetworkGameSocketHandler(SOCKET s) : info(nullptr), client_id(INVALID_CLIENT_ID), NetworkGameSocketHandler::NetworkGameSocketHandler(SOCKET s) : NetworkTCPSocketHandler(s),
last_frame(_frame_counter), last_frame_server(_frame_counter) last_frame(_frame_counter), last_frame_server(_frame_counter), last_packet(std::chrono::steady_clock::now()) {}
{
this->sock = s;
this->last_packet = std::chrono::steady_clock::now();
}
/** /**
* Functions to help ReceivePacket/SendPacket a bit * Functions to help ReceivePacket/SendPacket a bit

View File

@ -141,7 +141,7 @@ using CommandQueue = std::vector<CommandPacket>;
class NetworkGameSocketHandler : public NetworkTCPSocketHandler { class NetworkGameSocketHandler : public NetworkTCPSocketHandler {
/* TODO: rewrite into a proper class */ /* TODO: rewrite into a proper class */
private: private:
NetworkClientInfo *info; ///< Client info related to this socket NetworkClientInfo *info = nullptr; ///< Client info related to this socket
bool is_pending_deletion = false; ///< Whether this socket is pending deletion bool is_pending_deletion = false; ///< Whether this socket is pending deletion
protected: protected:
@ -483,11 +483,11 @@ protected:
NetworkGameSocketHandler(SOCKET s); NetworkGameSocketHandler(SOCKET s);
public: public:
ClientID client_id; ///< Client identifier ClientID client_id = INVALID_CLIENT_ID; ///< Client identifier
uint32_t last_frame; ///< Last frame we have executed uint32_t last_frame = 0; ///< Last frame we have executed
uint32_t last_frame_server; ///< Last frame the server has executed uint32_t last_frame_server = 0; ///< Last frame the server has executed
CommandQueue incoming_queue; ///< The command-queue awaiting handling CommandQueue incoming_queue; ///< The command-queue awaiting handling
std::chrono::steady_clock::time_point last_packet; ///< Time we received the last frame. std::chrono::steady_clock::time_point last_packet{}; ///< Time we received the last frame.
NetworkRecvStatus CloseConnection(bool error = true) override; NetworkRecvStatus CloseConnection(bool error = true) override;

View File

@ -109,7 +109,7 @@ void ClientNetworkEmergencySave()
* Create a new socket for the client side of the game connection. * Create a new socket for the client side of the game connection.
* @param s The socket to connect with. * @param s The socket to connect with.
*/ */
ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s, const std::string &connection_string) : NetworkGameSocketHandler(s), connection_string(connection_string), savegame(nullptr), status(STATUS_INACTIVE) ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s, const std::string &connection_string) : NetworkGameSocketHandler(s), connection_string(connection_string)
{ {
assert(ClientNetworkGameSocketHandler::my_client == nullptr); assert(ClientNetworkGameSocketHandler::my_client == nullptr);
ClientNetworkGameSocketHandler::my_client = this; ClientNetworkGameSocketHandler::my_client = this;

View File

@ -13,12 +13,12 @@
#include "network_internal.h" #include "network_internal.h"
/** Class for handling the client side of the game connection. */ /** Class for handling the client side of the game connection. */
class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler { class ClientNetworkGameSocketHandler : public NetworkGameSocketHandler {
private: private:
std::unique_ptr<class NetworkAuthenticationClientHandler> authentication_handler; ///< The handler for the authentication. std::unique_ptr<class NetworkAuthenticationClientHandler> authentication_handler = nullptr; ///< The handler for the authentication.
std::string connection_string; ///< Address we are connected to. std::string connection_string; ///< Address we are connected to.
std::shared_ptr<struct PacketReader> savegame; ///< Packet reader for reading the savegame. std::shared_ptr<struct PacketReader> savegame = nullptr; ///< Packet reader for reading the savegame.
uint8_t token; ///< The token we need to send back to the server to prove we're the right client. uint8_t token = 0; ///< The token we need to send back to the server to prove we're the right client.
/** Status of the connection with the server. */ /** Status of the connection with the server. */
enum ServerStatus { enum ServerStatus {
@ -34,7 +34,7 @@ private:
STATUS_END, ///< Must ALWAYS be on the end of this list!! (period) STATUS_END, ///< Must ALWAYS be on the end of this list!! (period)
}; };
ServerStatus status; ///< Status of the connection with the server. ServerStatus status = STATUS_INACTIVE; ///< Status of the connection with the server.
protected: protected:
friend void NetworkExecuteLocalCommandQueue(); friend void NetworkExecuteLocalCommandQueue();

View File

@ -13,7 +13,7 @@
#include "network_internal.h" #include "network_internal.h"
/** Class for handling the client side of quering a game server. */ /** Class for handling the client side of quering a game server. */
class QueryNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler { class QueryNetworkGameSocketHandler : public NetworkGameSocketHandler {
private: private:
static std::vector<std::unique_ptr<QueryNetworkGameSocketHandler>> queries; ///< Pending queries. static std::vector<std::unique_ptr<QueryNetworkGameSocketHandler>> queries; ///< Pending queries.
std::string connection_string; ///< Address we are connected to. std::string connection_string; ///< Address we are connected to.

View File

@ -190,7 +190,6 @@ struct PacketWriter : SaveFilter {
*/ */
ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s) ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s)
{ {
this->status = STATUS_INACTIVE;
this->client_id = _network_client_id++; this->client_id = _network_client_id++;
this->receive_limit = _settings_client.network.bytes_per_frame_burst; this->receive_limit = _settings_client.network.bytes_per_frame_burst;

View File

@ -23,7 +23,7 @@ extern NetworkClientSocketPool _networkclientsocket_pool;
/** Class for handling the server side of the game connection. */ /** Class for handling the server side of the game connection. */
class ServerNetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkGameSocketHandler, public TCPListenHandler<ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED> { class ServerNetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkGameSocketHandler, public TCPListenHandler<ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED> {
protected: protected:
std::unique_ptr<class NetworkAuthenticationServerHandler> authentication_handler; ///< The handler for the authentication. std::unique_ptr<class NetworkAuthenticationServerHandler> authentication_handler = nullptr; ///< The handler for the authentication.
std::string peer_public_key; ///< The public key of our client. std::string peer_public_key; ///< The public key of our client.
NetworkRecvStatus Receive_CLIENT_JOIN(Packet &p) override; NetworkRecvStatus Receive_CLIENT_JOIN(Packet &p) override;
@ -64,15 +64,15 @@ public:
STATUS_END, ///< Must ALWAYS be on the end of this list!! (period). STATUS_END, ///< Must ALWAYS be on the end of this list!! (period).
}; };
uint8_t lag_test; ///< Byte used for lag-testing the client uint8_t lag_test = 0; ///< Byte used for lag-testing the client
uint8_t last_token; ///< The last random token we did send to verify the client is listening uint8_t last_token = 0; ///< The last random token we did send to verify the client is listening
uint32_t last_token_frame; ///< The last frame we received the right token uint32_t last_token_frame = 0; ///< The last frame we received the right token
ClientStatus status; ///< Status of this client ClientStatus status = STATUS_INACTIVE; ///< Status of this client
CommandQueue outgoing_queue; ///< The command-queue awaiting delivery; conceptually more a bucket to gather commands in, after which the whole bucket is sent to the client. CommandQueue outgoing_queue; ///< The command-queue awaiting delivery; conceptually more a bucket to gather commands in, after which the whole bucket is sent to the client.
size_t receive_limit; ///< Amount of bytes that we can receive at this moment size_t receive_limit = 0; ///< Amount of bytes that we can receive at this moment
std::shared_ptr<struct PacketWriter> savegame; ///< Writer used to write the savegame. std::shared_ptr<struct PacketWriter> savegame = nullptr; ///< Writer used to write the savegame.
NetworkAddress client_address; ///< IP-address of the client (so they can be banned) NetworkAddress client_address{}; ///< IP-address of the client (so they can be banned)
ServerNetworkGameSocketHandler(SOCKET s); ServerNetworkGameSocketHandler(SOCKET s);
~ServerNetworkGameSocketHandler(); ~ServerNetworkGameSocketHandler();

View File

@ -38,9 +38,9 @@ static uint16_t _network_udp_broadcast; ///< Timeout for the UDP broadcasts.
/** Some information about a socket, which exists before the actual socket has been created to provide locking and the likes. */ /** Some information about a socket, which exists before the actual socket has been created to provide locking and the likes. */
struct UDPSocket { struct UDPSocket {
const std::string name; ///< The name of the socket. const std::string name; ///< The name of the socket.
NetworkUDPSocketHandler *socket; ///< The actual socket, which may be nullptr when not initialized yet. NetworkUDPSocketHandler *socket = nullptr; ///< The actual socket, which may be nullptr when not initialized yet.
UDPSocket(const std::string &name) : name(name), socket(nullptr) {} UDPSocket(const std::string &name) : name(name) {}
void CloseSocket() void CloseSocket()
{ {