diff --git a/src/network/core/core.h b/src/network/core/core.h index a9d12b1f11..3b63aadc5f 100644 --- a/src/network/core/core.h +++ b/src/network/core/core.h @@ -42,16 +42,16 @@ struct Packet; */ class NetworkSocketHandler { 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: friend struct Packet; - std::unique_ptr receive_encryption_handler; ///< The handler for decrypting received packets. - std::unique_ptr send_encryption_handler; ///< The handler for encrypting sent packets. + std::unique_ptr receive_encryption_handler = nullptr; ///< The handler for decrypting received packets. + std::unique_ptr send_encryption_handler = nullptr; ///< The handler for encrypting sent packets. public: /** Create a new unbound socket */ - NetworkSocketHandler() { this->has_quit = false; } + NetworkSocketHandler() = default; /** Close the socket when destructing the socket handler */ virtual ~NetworkSocketHandler() = default; diff --git a/src/network/core/host.cpp b/src/network/core/host.cpp index cf999c24f9..0d674b416f 100644 --- a/src/network/core/host.cpp +++ b/src/network/core/host.cpp @@ -9,7 +9,6 @@ #include "../../stdafx.h" #include "../../debug.h" -#include "../../core/alloc_func.hpp" #include "address.h" #include "../../safeguards.h" diff --git a/src/network/core/tcp.cpp b/src/network/core/tcp.cpp index 8bd7b44f2f..a03eed0b74 100644 --- a/src/network/core/tcp.cpp +++ b/src/network/core/tcp.cpp @@ -16,16 +16,6 @@ #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() { this->CloseSocket(); diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h index 419a278587..b63bfdea6d 100644 --- a/src/network/core/tcp.h +++ b/src/network/core/tcp.h @@ -31,12 +31,12 @@ enum SendPacketsState { class NetworkTCPSocketHandler : public NetworkSocketHandler { private: std::deque> packet_queue; ///< Packets that are awaiting delivery. Cannot be std::queue as that does not have a clear() function. - std::unique_ptr packet_recv; ///< Partially received packet + std::unique_ptr packet_recv = nullptr; ///< Partially received packet void EmptyPacketQueue(); public: - SOCKET sock; ///< The socket currently connected to - bool writable; ///< Can we write to this socket? + SOCKET sock = INVALID_SOCKET; ///< The socket currently connected to + bool writable = false; ///< Can we write to this socket? /** * Whether this socket is currently bound to a socket. @@ -60,7 +60,11 @@ public: */ 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(); }; diff --git a/src/network/core/tcp_admin.cpp b/src/network/core/tcp_admin.cpp index 044950940c..12d2d02e47 100644 --- a/src/network/core/tcp_admin.cpp +++ b/src/network/core/tcp_admin.cpp @@ -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_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) { delete this; diff --git a/src/network/core/tcp_admin.h b/src/network/core/tcp_admin.h index f51f942ca3..41a653165e 100644 --- a/src/network/core/tcp_admin.h +++ b/src/network/core/tcp_admin.h @@ -116,9 +116,9 @@ enum AdminCompanyRemoveReason { /** Main socket handler for admin related connections. */ class NetworkAdminSocketHandler : public NetworkTCPSocketHandler { 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. - AdminStatus status; ///< Status of this admin. + AdminStatus status = ADMIN_STATUS_INACTIVE; ///< Status of this admin. NetworkRecvStatus ReceiveInvalidPacket(PacketAdminType type); @@ -542,7 +542,11 @@ protected: public: 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(); diff --git a/src/network/core/tcp_game.cpp b/src/network/core/tcp_game.cpp index c466ceedee..2555967058 100644 --- a/src/network/core/tcp_game.cpp +++ b/src/network/core/tcp_game.cpp @@ -26,12 +26,8 @@ static std::vector> _deferred_deletion * Create a new socket for the game connection. * @param s The socket to connect with. */ -NetworkGameSocketHandler::NetworkGameSocketHandler(SOCKET s) : info(nullptr), client_id(INVALID_CLIENT_ID), - last_frame(_frame_counter), last_frame_server(_frame_counter) -{ - this->sock = s; - this->last_packet = std::chrono::steady_clock::now(); -} +NetworkGameSocketHandler::NetworkGameSocketHandler(SOCKET s) : NetworkTCPSocketHandler(s), + last_frame(_frame_counter), last_frame_server(_frame_counter), last_packet(std::chrono::steady_clock::now()) {} /** * Functions to help ReceivePacket/SendPacket a bit diff --git a/src/network/core/tcp_game.h b/src/network/core/tcp_game.h index 5795221a2e..14237fdac1 100644 --- a/src/network/core/tcp_game.h +++ b/src/network/core/tcp_game.h @@ -141,7 +141,7 @@ using CommandQueue = std::vector; class NetworkGameSocketHandler : public NetworkTCPSocketHandler { /* TODO: rewrite into a proper class */ 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 protected: @@ -483,11 +483,11 @@ protected: NetworkGameSocketHandler(SOCKET s); public: - ClientID client_id; ///< Client identifier - uint32_t last_frame; ///< Last frame we have executed - uint32_t last_frame_server; ///< Last frame the server has executed + ClientID client_id = INVALID_CLIENT_ID; ///< Client identifier + uint32_t last_frame = 0; ///< Last frame we have executed + uint32_t last_frame_server = 0; ///< Last frame the server has executed 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; diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index 8686ddfa00..21d7d7cda8 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -109,7 +109,7 @@ void ClientNetworkEmergencySave() * Create a new socket for the client side of the game connection. * @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); ClientNetworkGameSocketHandler::my_client = this; diff --git a/src/network/network_client.h b/src/network/network_client.h index 75a1ce4dd3..b697e8add2 100644 --- a/src/network/network_client.h +++ b/src/network/network_client.h @@ -13,12 +13,12 @@ #include "network_internal.h" /** Class for handling the client side of the game connection. */ -class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler { +class ClientNetworkGameSocketHandler : public NetworkGameSocketHandler { private: - std::unique_ptr authentication_handler; ///< The handler for the authentication. + std::unique_ptr authentication_handler = nullptr; ///< The handler for the authentication. std::string connection_string; ///< Address we are connected to. - std::shared_ptr savegame; ///< 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. + std::shared_ptr savegame = nullptr; ///< Packet reader for reading the savegame. + 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. */ enum ServerStatus { @@ -34,7 +34,7 @@ private: 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: friend void NetworkExecuteLocalCommandQueue(); diff --git a/src/network/network_query.h b/src/network/network_query.h index 0f7348838f..59db813f65 100644 --- a/src/network/network_query.h +++ b/src/network/network_query.h @@ -13,7 +13,7 @@ #include "network_internal.h" /** Class for handling the client side of quering a game server. */ -class QueryNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler { +class QueryNetworkGameSocketHandler : public NetworkGameSocketHandler { private: static std::vector> queries; ///< Pending queries. std::string connection_string; ///< Address we are connected to. diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index bcbfa0fe0b..61bc46bf49 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -190,7 +190,6 @@ struct PacketWriter : SaveFilter { */ ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s) { - this->status = STATUS_INACTIVE; this->client_id = _network_client_id++; this->receive_limit = _settings_client.network.bytes_per_frame_burst; diff --git a/src/network/network_server.h b/src/network/network_server.h index 31aa2739bc..26dcf3d795 100644 --- a/src/network/network_server.h +++ b/src/network/network_server.h @@ -23,7 +23,7 @@ extern NetworkClientSocketPool _networkclientsocket_pool; /** Class for handling the server side of the game connection. */ class ServerNetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkGameSocketHandler, public TCPListenHandler { protected: - std::unique_ptr authentication_handler; ///< The handler for the authentication. + std::unique_ptr authentication_handler = nullptr; ///< The handler for the authentication. std::string peer_public_key; ///< The public key of our client. NetworkRecvStatus Receive_CLIENT_JOIN(Packet &p) override; @@ -64,15 +64,15 @@ public: 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 last_token; ///< 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 - ClientStatus status; ///< Status of this client + uint8_t lag_test = 0; ///< Byte used for lag-testing the client + uint8_t last_token = 0; ///< The last random token we did send to verify the client is listening + uint32_t last_token_frame = 0; ///< The last frame we received the right token + 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. - 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 savegame; ///< Writer used to write the savegame. - NetworkAddress client_address; ///< IP-address of the client (so they can be banned) + std::shared_ptr savegame = nullptr; ///< Writer used to write the savegame. + NetworkAddress client_address{}; ///< IP-address of the client (so they can be banned) ServerNetworkGameSocketHandler(SOCKET s); ~ServerNetworkGameSocketHandler(); diff --git a/src/network/network_udp.cpp b/src/network/network_udp.cpp index 88aaea538e..dc94da00e3 100644 --- a/src/network/network_udp.cpp +++ b/src/network/network_udp.cpp @@ -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. */ struct UDPSocket { 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() {