From 8daf5b5338647373baffe045d728ea1734c63789 Mon Sep 17 00:00:00 2001 From: Vincent Glize Date: Wed, 21 Jun 2017 08:28:57 +0200 Subject: [PATCH] C++11 cleanup on constructors dir network (#6021) * C++11 cleanup on constructors dir network --- src/network/connection.cpp | 91 +++--------------- src/network/connection.h | 169 ++++++++++++++++------------------ src/network/networkpacket.cpp | 4 +- src/network/networkpacket.h | 11 +-- 4 files changed, 98 insertions(+), 177 deletions(-) diff --git a/src/network/connection.cpp b/src/network/connection.cpp index 61b98b6d7..d1ab948db 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -60,14 +60,6 @@ static inline float CALC_DTIME(u64 lasttime, u64 curtime) return MYMAX(MYMIN(value,0.1),0.0); } -/* maximum window size to use, 0xFFFF is theoretical maximum don't think about - * touching it, the less you're away from it the more likely data corruption - * will occur - */ -#define MAX_RELIABLE_WINDOW_SIZE 0x8000 - /* starting value for window size */ -#define MIN_RELIABLE_WINDOW_SIZE 0x40 - #define MAX_UDP_PEERS 65535 #define PING_TIMEOUT 5.0 @@ -209,8 +201,6 @@ SharedBuffer makeReliablePacket( ReliablePacketBuffer */ -ReliablePacketBuffer::ReliablePacketBuffer(): m_list_size(0) {} - void ReliablePacketBuffer::print() { MutexAutoLock listlock(m_list_mutex); @@ -577,36 +567,6 @@ void IncomingSplitBuffer::removeUnreliableTimedOuts(float dtime, float timeout) Channel */ -Channel::Channel() : - window_size(MIN_RELIABLE_WINDOW_SIZE), - next_incoming_seqnum(SEQNUM_INITIAL), - next_outgoing_seqnum(SEQNUM_INITIAL), - next_outgoing_split_seqnum(SEQNUM_INITIAL), - current_packet_loss(0), - current_packet_too_late(0), - current_packet_successfull(0), - packet_loss_counter(0), - current_bytes_transfered(0), - current_bytes_received(0), - current_bytes_lost(0), - max_kbps(0.0), - cur_kbps(0.0), - avg_kbps(0.0), - max_incoming_kbps(0.0), - cur_incoming_kbps(0.0), - avg_incoming_kbps(0.0), - max_kbps_lost(0.0), - cur_kbps_lost(0.0), - avg_kbps_lost(0.0), - bpm_counter(0.0), - rate_samples(0) -{ -} - -Channel::~Channel() -{ -} - u16 Channel::readNextIncomingSeqNum() { MutexAutoLock internal(m_internal_mutex); @@ -849,40 +809,26 @@ void Channel::UpdateTimers(float dtime,bool legacy_peer) Peer */ -PeerHelper::PeerHelper() : - m_peer(0) -{} - PeerHelper::PeerHelper(Peer* peer) : m_peer(peer) { - if (peer != NULL) - { - if (!peer->IncUseCount()) - { - m_peer = 0; - } - } + if (peer && !peer->IncUseCount()) + m_peer = nullptr; } PeerHelper::~PeerHelper() { - if (m_peer != 0) + if (m_peer) m_peer->DecUseCount(); - m_peer = 0; + m_peer = nullptr; } PeerHelper& PeerHelper::operator=(Peer* peer) { m_peer = peer; - if (peer != NULL) - { - if (!peer->IncUseCount()) - { - m_peer = 0; - } - } + if (peer && !peer->IncUseCount()) + m_peer = nullptr; return *this; } @@ -909,8 +855,7 @@ bool Peer::IncUseCount() { MutexAutoLock lock(m_exclusive_access_mutex); - if (!m_pending_deletion) - { + if (!m_pending_deletion) { this->m_usage++; return true; } @@ -1014,10 +959,7 @@ void Peer::Drop() } UDPPeer::UDPPeer(u16 a_id, Address a_address, Connection* connection) : - Peer(a_address,a_id,connection), - m_pending_disconnect(false), - resend_timeout(0.5), - m_legacy_peer(true) + Peer(a_address,a_id,connection) { } @@ -1261,12 +1203,9 @@ SharedBuffer UDPPeer::addSpiltPacket(u8 channel, ConnectionSendThread::ConnectionSendThread(unsigned int max_packet_size, float timeout) : Thread("ConnectionSend"), - m_connection(NULL), m_max_packet_size(max_packet_size), m_timeout(timeout), - m_max_commands_per_iteration(1), - m_max_data_packets_per_iteration(g_settings->getU16("max_packets_per_iteration")), - m_max_packets_requeued(256) + m_max_data_packets_per_iteration(g_settings->getU16("max_packets_per_iteration")) { } @@ -2031,8 +1970,7 @@ void ConnectionSendThread::sendAsPacket(u16 peer_id, u8 channelnum, } ConnectionReceiveThread::ConnectionReceiveThread(unsigned int max_packet_size) : - Thread("ConnectionReceive"), - m_connection(NULL) + Thread("ConnectionReceive") { } @@ -2676,17 +2614,10 @@ SharedBuffer ConnectionReceiveThread::processPacket(Channel *channel, Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout, bool ipv6, PeerHandler *peerhandler) : m_udpSocket(ipv6), - m_command_queue(), - m_event_queue(), - m_peer_id(0), m_protocol_id(protocol_id), m_sendThread(max_packet_size, timeout), m_receiveThread(max_packet_size), - m_info_mutex(), - m_bc_peerhandler(peerhandler), - m_bc_receive_timeout(0), - m_shutting_down(false), - m_next_remote_peer_id(2) + m_bc_peerhandler(peerhandler) { m_udpSocket.setTimeoutMs(5); diff --git a/src/network/connection.h b/src/network/connection.h index 51cf6aec6..1ee4f6762 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -165,19 +165,18 @@ inline bool seqnum_in_window(u16 seqnum, u16 next,u16 window_size) struct BufferedPacket { BufferedPacket(u8 *a_data, u32 a_size): - data(a_data, a_size), time(0.0), totaltime(0.0), absolute_send_time(-1), - resend_count(0) + data(a_data, a_size) {} BufferedPacket(u32 a_size): data(a_size), time(0.0), totaltime(0.0), absolute_send_time(-1), resend_count(0) {} Buffer data; // Data of the packet, including headers - float time; // Seconds from buffering the packet or re-sending - float totaltime; // Seconds from buffering the packet - u64 absolute_send_time; + float time = 0.0f; // Seconds from buffering the packet or re-sending + float totaltime = 0.0f; // Seconds from buffering the packet + u64 absolute_send_time = -1; Address address; // Sender or destination - unsigned int resend_count; + unsigned int resend_count = 0; }; // This adds the base headers to the data and makes a packet out of it @@ -210,16 +209,12 @@ SharedBuffer makeReliablePacket( struct IncomingSplitPacket { - IncomingSplitPacket() - { - time = 0.0; - reliable = false; - } + IncomingSplitPacket() {} // Key is chunk number, value is data without headers std::map > chunks; u32 chunk_count; - float time; // Seconds from adding - bool reliable; // If true, isn't deleted on timeout + float time = 0.0f; // Seconds from adding + bool reliable = false; // If true, isn't deleted on timeout bool allReceived() { @@ -322,7 +317,7 @@ typedef std::list::iterator RPBSearchResult; class ReliablePacketBuffer { public: - ReliablePacketBuffer(); + ReliablePacketBuffer() {}; bool getFirstSeqnum(u16& result); @@ -345,7 +340,7 @@ private: RPBSearchResult findPacket(u16 seqnum); std::list m_list; - u32 m_list_size; + u32 m_list_size = 0; u16 m_oldest_non_answered_ack; @@ -409,15 +404,15 @@ enum ConnectionCommandType{ struct ConnectionCommand { - enum ConnectionCommandType type; + enum ConnectionCommandType type = CONNCMD_NONE; Address address; - u16 peer_id; + u16 peer_id = PEER_ID_INEXISTENT; u8 channelnum; Buffer data; - bool reliable; - bool raw; + bool reliable = false; + bool raw = false; - ConnectionCommand(): type(CONNCMD_NONE), peer_id(PEER_ID_INEXISTENT), reliable(false), raw(false) {} + ConnectionCommand() {} void serve(Address address_) { @@ -478,6 +473,14 @@ struct ConnectionCommand } }; +/* maximum window size to use, 0xFFFF is theoretical maximum don't think about + * touching it, the less you're away from it the more likely data corruption + * will occur + */ +#define MAX_RELIABLE_WINDOW_SIZE 0x8000 + /* starting value for window size */ +#define MIN_RELIABLE_WINDOW_SIZE 0x40 + class Channel { @@ -507,8 +510,8 @@ public: IncomingSplitBuffer incoming_splits; - Channel(); - ~Channel(); + Channel() {}; + ~Channel() {}; void UpdatePacketLossCounter(unsigned int count); void UpdatePacketTooLateCounter(); @@ -545,33 +548,33 @@ public: void setWindowSize(unsigned int size) { window_size = size; }; private: std::mutex m_internal_mutex; - int window_size; + int window_size = MIN_RELIABLE_WINDOW_SIZE; - u16 next_incoming_seqnum; + u16 next_incoming_seqnum = SEQNUM_INITIAL; - u16 next_outgoing_seqnum; - u16 next_outgoing_split_seqnum; + u16 next_outgoing_seqnum = SEQNUM_INITIAL; + u16 next_outgoing_split_seqnum = SEQNUM_INITIAL; - unsigned int current_packet_loss; - unsigned int current_packet_too_late; - unsigned int current_packet_successfull; - float packet_loss_counter; + unsigned int current_packet_loss = 0; + unsigned int current_packet_too_late = 0; + unsigned int current_packet_successfull = 0; + float packet_loss_counter = 0.0f; - unsigned int current_bytes_transfered; - unsigned int current_bytes_received; - unsigned int current_bytes_lost; - float max_kbps; - float cur_kbps; - float avg_kbps; - float max_incoming_kbps; - float cur_incoming_kbps; - float avg_incoming_kbps; - float max_kbps_lost; - float cur_kbps_lost; - float avg_kbps_lost; - float bpm_counter; + unsigned int current_bytes_transfered = 0; + unsigned int current_bytes_received = 0; + unsigned int current_bytes_lost = 0; + float max_kbps = 0.0f; + float cur_kbps = 0.0f; + float avg_kbps = 0.0f; + float max_incoming_kbps = 0.0f; + float cur_incoming_kbps = 0.0f; + float avg_incoming_kbps = 0.0f; + float max_kbps_lost = 0.0f; + float cur_kbps_lost = 0.0f; + float avg_kbps_lost = 0.0f; + float bpm_counter = 0.0f; - unsigned int rate_samples; + unsigned int rate_samples = 0; }; class Peer; @@ -614,7 +617,7 @@ public: class PeerHelper { public: - PeerHelper(); + PeerHelper() {}; PeerHelper(Peer* peer); ~PeerHelper(); @@ -625,7 +628,7 @@ public: bool operator!=(void* ptr); private: - Peer* m_peer; + Peer *m_peer = nullptr; }; class Connection; @@ -654,23 +657,10 @@ class Peer { Peer(Address address_,u16 id_,Connection* connection) : id(id_), - m_increment_packets_remaining(9), - m_increment_bytes_remaining(0), - m_pending_deletion(false), m_connection(connection), address(address_), - m_ping_timer(0.0), - m_last_rtt(-1.0), - m_usage(0), - m_timeout_counter(0.0), m_last_timeout_check(porting::getTimeMs()) { - m_rtt.avg_rtt = -1.0; - m_rtt.jitter_avg = -1.0; - m_rtt.jitter_max = 0.0; - m_rtt.max_rtt = 0.0; - m_rtt.jitter_min = FLT_MAX; - m_rtt.min_rtt = FLT_MAX; }; virtual ~Peer() { @@ -692,12 +682,12 @@ class Peer { { MutexAutoLock lock(m_exclusive_access_mutex); return m_pending_deletion; }; void ResetTimeout() - {MutexAutoLock lock(m_exclusive_access_mutex); m_timeout_counter=0.0; }; + {MutexAutoLock lock(m_exclusive_access_mutex); m_timeout_counter = 0.0; }; bool isTimedOut(float timeout); - unsigned int m_increment_packets_remaining; - unsigned int m_increment_bytes_remaining; + unsigned int m_increment_packets_remaining = 9; + unsigned int m_increment_bytes_remaining = 0; virtual u16 getNextSplitSequenceNumber(u8 channel) { return 0; }; virtual void setNextSplitSequenceNumber(u8 channel, u16 seqnum) {}; @@ -740,7 +730,7 @@ class Peer { std::mutex m_exclusive_access_mutex; - bool m_pending_deletion; + bool m_pending_deletion = false; Connection* m_connection; @@ -748,26 +738,28 @@ class Peer { Address address; // Ping timer - float m_ping_timer; + float m_ping_timer = 0.0f; private: struct rttstats { - float jitter_min; - float jitter_max; - float jitter_avg; - float min_rtt; - float max_rtt; - float avg_rtt; + float jitter_min = FLT_MAX; + float jitter_max = 0.0f; + float jitter_avg = -1.0f; + float min_rtt = FLT_MAX; + float max_rtt = 0.0f; + float avg_rtt = -1.0f; + + rttstats() {}; }; rttstats m_rtt; - float m_last_rtt; + float m_last_rtt = -1.0f; // current usage count - unsigned int m_usage; + unsigned int m_usage = 0; // Seconds from last receive - float m_timeout_counter; + float m_timeout_counter = 0.0f; u64 m_last_timeout_check; }; @@ -822,16 +814,16 @@ protected: bool Ping(float dtime,SharedBuffer& data); Channel channels[CHANNEL_COUNT]; - bool m_pending_disconnect; + bool m_pending_disconnect = false; private: // This is changed dynamically - float resend_timeout; + float resend_timeout = 0.5; bool processReliableSendCommand( ConnectionCommand &c, unsigned int max_packet_size); - bool m_legacy_peer; + bool m_legacy_peer = true; }; /* @@ -848,14 +840,13 @@ enum ConnectionEventType{ struct ConnectionEvent { - enum ConnectionEventType type; - u16 peer_id; + enum ConnectionEventType type = CONNEVENT_NONE; + u16 peer_id = 0; Buffer data; - bool timeout; + bool timeout = false; Address address; - ConnectionEvent(): type(CONNEVENT_NONE), peer_id(0), - timeout(false) {} + ConnectionEvent() {} std::string describe() { @@ -946,16 +937,16 @@ private: bool packetsQueued(); - Connection* m_connection; + Connection *m_connection = nullptr; unsigned int m_max_packet_size; float m_timeout; std::queue m_outgoing_queue; Semaphore m_send_sleep_semaphore; unsigned int m_iteration_packets_avaialble; - unsigned int m_max_commands_per_iteration; + unsigned int m_max_commands_per_iteration = 1; unsigned int m_max_data_packets_per_iteration; - unsigned int m_max_packets_requeued; + unsigned int m_max_packets_requeued = 256; }; class ConnectionReceiveThread : public Thread { @@ -993,7 +984,7 @@ private: u8 channelnum, bool reliable); - Connection* m_connection; + Connection *m_connection = nullptr; }; class Connection @@ -1059,7 +1050,7 @@ private: MutexedQueue m_event_queue; - u16 m_peer_id; + u16 m_peer_id = 0; u32 m_protocol_id; std::map m_peers; @@ -1073,11 +1064,11 @@ private: // Backwards compatibility PeerHandler *m_bc_peerhandler; - int m_bc_receive_timeout; + int m_bc_receive_timeout = 0; - bool m_shutting_down; + bool m_shutting_down = false; - u16 m_next_remote_peer_id; + u16 m_next_remote_peer_id = 2; }; } // namespace diff --git a/src/network/networkpacket.cpp b/src/network/networkpacket.cpp index f7a6499dd..48cf3a374 100644 --- a/src/network/networkpacket.cpp +++ b/src/network/networkpacket.cpp @@ -23,13 +23,13 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/serialize.h" NetworkPacket::NetworkPacket(u16 command, u32 datasize, u16 peer_id): -m_datasize(datasize), m_read_offset(0), m_command(command), m_peer_id(peer_id) +m_datasize(datasize), m_command(command), m_peer_id(peer_id) { m_data.resize(m_datasize); } NetworkPacket::NetworkPacket(u16 command, u32 datasize): -m_datasize(datasize), m_read_offset(0), m_command(command), m_peer_id(0) +m_datasize(datasize), m_command(command) { m_data.resize(m_datasize); } diff --git a/src/network/networkpacket.h b/src/network/networkpacket.h index 83dc33f6f..3058a22ca 100644 --- a/src/network/networkpacket.h +++ b/src/network/networkpacket.h @@ -30,8 +30,7 @@ class NetworkPacket public: NetworkPacket(u16 command, u32 datasize, u16 peer_id); NetworkPacket(u16 command, u32 datasize); - NetworkPacket(): m_datasize(0), m_read_offset(0), m_command(0), - m_peer_id(0) {} + NetworkPacket() {} ~NetworkPacket(); void putRawPacket(u8 *data, u32 datasize, u16 peer_id); @@ -126,10 +125,10 @@ private: } std::vector m_data; - u32 m_datasize; - u32 m_read_offset; - u16 m_command; - u16 m_peer_id; + u32 m_datasize = 0; + u32 m_read_offset = 0; + u16 m_command = 0; + u16 m_peer_id = 0; }; #endif