C++11 cleanup on constructors dir network (#6021)

* C++11 cleanup on constructors dir network
This commit is contained in:
Vincent Glize 2017-06-21 08:28:57 +02:00 committed by Loïc Blot
parent af3badf7a9
commit 8daf5b5338
4 changed files with 98 additions and 177 deletions

@ -60,14 +60,6 @@ static inline float CALC_DTIME(u64 lasttime, u64 curtime)
return MYMAX(MYMIN(value,0.1),0.0); 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 MAX_UDP_PEERS 65535
#define PING_TIMEOUT 5.0 #define PING_TIMEOUT 5.0
@ -209,8 +201,6 @@ SharedBuffer<u8> makeReliablePacket(
ReliablePacketBuffer ReliablePacketBuffer
*/ */
ReliablePacketBuffer::ReliablePacketBuffer(): m_list_size(0) {}
void ReliablePacketBuffer::print() void ReliablePacketBuffer::print()
{ {
MutexAutoLock listlock(m_list_mutex); MutexAutoLock listlock(m_list_mutex);
@ -577,36 +567,6 @@ void IncomingSplitBuffer::removeUnreliableTimedOuts(float dtime, float timeout)
Channel 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() u16 Channel::readNextIncomingSeqNum()
{ {
MutexAutoLock internal(m_internal_mutex); MutexAutoLock internal(m_internal_mutex);
@ -849,40 +809,26 @@ void Channel::UpdateTimers(float dtime,bool legacy_peer)
Peer Peer
*/ */
PeerHelper::PeerHelper() :
m_peer(0)
{}
PeerHelper::PeerHelper(Peer* peer) : PeerHelper::PeerHelper(Peer* peer) :
m_peer(peer) m_peer(peer)
{ {
if (peer != NULL) if (peer && !peer->IncUseCount())
{ m_peer = nullptr;
if (!peer->IncUseCount())
{
m_peer = 0;
}
}
} }
PeerHelper::~PeerHelper() PeerHelper::~PeerHelper()
{ {
if (m_peer != 0) if (m_peer)
m_peer->DecUseCount(); m_peer->DecUseCount();
m_peer = 0; m_peer = nullptr;
} }
PeerHelper& PeerHelper::operator=(Peer* peer) PeerHelper& PeerHelper::operator=(Peer* peer)
{ {
m_peer = peer; m_peer = peer;
if (peer != NULL) if (peer && !peer->IncUseCount())
{ m_peer = nullptr;
if (!peer->IncUseCount())
{
m_peer = 0;
}
}
return *this; return *this;
} }
@ -909,8 +855,7 @@ bool Peer::IncUseCount()
{ {
MutexAutoLock lock(m_exclusive_access_mutex); MutexAutoLock lock(m_exclusive_access_mutex);
if (!m_pending_deletion) if (!m_pending_deletion) {
{
this->m_usage++; this->m_usage++;
return true; return true;
} }
@ -1014,10 +959,7 @@ void Peer::Drop()
} }
UDPPeer::UDPPeer(u16 a_id, Address a_address, Connection* connection) : UDPPeer::UDPPeer(u16 a_id, Address a_address, Connection* connection) :
Peer(a_address,a_id,connection), Peer(a_address,a_id,connection)
m_pending_disconnect(false),
resend_timeout(0.5),
m_legacy_peer(true)
{ {
} }
@ -1261,12 +1203,9 @@ SharedBuffer<u8> UDPPeer::addSpiltPacket(u8 channel,
ConnectionSendThread::ConnectionSendThread(unsigned int max_packet_size, ConnectionSendThread::ConnectionSendThread(unsigned int max_packet_size,
float timeout) : float timeout) :
Thread("ConnectionSend"), Thread("ConnectionSend"),
m_connection(NULL),
m_max_packet_size(max_packet_size), m_max_packet_size(max_packet_size),
m_timeout(timeout), m_timeout(timeout),
m_max_commands_per_iteration(1), m_max_data_packets_per_iteration(g_settings->getU16("max_packets_per_iteration"))
m_max_data_packets_per_iteration(g_settings->getU16("max_packets_per_iteration")),
m_max_packets_requeued(256)
{ {
} }
@ -2031,8 +1970,7 @@ void ConnectionSendThread::sendAsPacket(u16 peer_id, u8 channelnum,
} }
ConnectionReceiveThread::ConnectionReceiveThread(unsigned int max_packet_size) : ConnectionReceiveThread::ConnectionReceiveThread(unsigned int max_packet_size) :
Thread("ConnectionReceive"), Thread("ConnectionReceive")
m_connection(NULL)
{ {
} }
@ -2676,17 +2614,10 @@ SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout, Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
bool ipv6, PeerHandler *peerhandler) : bool ipv6, PeerHandler *peerhandler) :
m_udpSocket(ipv6), m_udpSocket(ipv6),
m_command_queue(),
m_event_queue(),
m_peer_id(0),
m_protocol_id(protocol_id), m_protocol_id(protocol_id),
m_sendThread(max_packet_size, timeout), m_sendThread(max_packet_size, timeout),
m_receiveThread(max_packet_size), m_receiveThread(max_packet_size),
m_info_mutex(), m_bc_peerhandler(peerhandler)
m_bc_peerhandler(peerhandler),
m_bc_receive_timeout(0),
m_shutting_down(false),
m_next_remote_peer_id(2)
{ {
m_udpSocket.setTimeoutMs(5); m_udpSocket.setTimeoutMs(5);

@ -165,19 +165,18 @@ inline bool seqnum_in_window(u16 seqnum, u16 next,u16 window_size)
struct BufferedPacket struct BufferedPacket
{ {
BufferedPacket(u8 *a_data, u32 a_size): BufferedPacket(u8 *a_data, u32 a_size):
data(a_data, a_size), time(0.0), totaltime(0.0), absolute_send_time(-1), data(a_data, a_size)
resend_count(0)
{} {}
BufferedPacket(u32 a_size): BufferedPacket(u32 a_size):
data(a_size), time(0.0), totaltime(0.0), absolute_send_time(-1), data(a_size), time(0.0), totaltime(0.0), absolute_send_time(-1),
resend_count(0) resend_count(0)
{} {}
Buffer<u8> data; // Data of the packet, including headers Buffer<u8> data; // Data of the packet, including headers
float time; // Seconds from buffering the packet or re-sending float time = 0.0f; // Seconds from buffering the packet or re-sending
float totaltime; // Seconds from buffering the packet float totaltime = 0.0f; // Seconds from buffering the packet
u64 absolute_send_time; u64 absolute_send_time = -1;
Address address; // Sender or destination 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 // This adds the base headers to the data and makes a packet out of it
@ -210,16 +209,12 @@ SharedBuffer<u8> makeReliablePacket(
struct IncomingSplitPacket struct IncomingSplitPacket
{ {
IncomingSplitPacket() IncomingSplitPacket() {}
{
time = 0.0;
reliable = false;
}
// Key is chunk number, value is data without headers // Key is chunk number, value is data without headers
std::map<u16, SharedBuffer<u8> > chunks; std::map<u16, SharedBuffer<u8> > chunks;
u32 chunk_count; u32 chunk_count;
float time; // Seconds from adding float time = 0.0f; // Seconds from adding
bool reliable; // If true, isn't deleted on timeout bool reliable = false; // If true, isn't deleted on timeout
bool allReceived() bool allReceived()
{ {
@ -322,7 +317,7 @@ typedef std::list<BufferedPacket>::iterator RPBSearchResult;
class ReliablePacketBuffer class ReliablePacketBuffer
{ {
public: public:
ReliablePacketBuffer(); ReliablePacketBuffer() {};
bool getFirstSeqnum(u16& result); bool getFirstSeqnum(u16& result);
@ -345,7 +340,7 @@ private:
RPBSearchResult findPacket(u16 seqnum); RPBSearchResult findPacket(u16 seqnum);
std::list<BufferedPacket> m_list; std::list<BufferedPacket> m_list;
u32 m_list_size; u32 m_list_size = 0;
u16 m_oldest_non_answered_ack; u16 m_oldest_non_answered_ack;
@ -409,15 +404,15 @@ enum ConnectionCommandType{
struct ConnectionCommand struct ConnectionCommand
{ {
enum ConnectionCommandType type; enum ConnectionCommandType type = CONNCMD_NONE;
Address address; Address address;
u16 peer_id; u16 peer_id = PEER_ID_INEXISTENT;
u8 channelnum; u8 channelnum;
Buffer<u8> data; Buffer<u8> data;
bool reliable; bool reliable = false;
bool raw; bool raw = false;
ConnectionCommand(): type(CONNCMD_NONE), peer_id(PEER_ID_INEXISTENT), reliable(false), raw(false) {} ConnectionCommand() {}
void serve(Address address_) 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 class Channel
{ {
@ -507,8 +510,8 @@ public:
IncomingSplitBuffer incoming_splits; IncomingSplitBuffer incoming_splits;
Channel(); Channel() {};
~Channel(); ~Channel() {};
void UpdatePacketLossCounter(unsigned int count); void UpdatePacketLossCounter(unsigned int count);
void UpdatePacketTooLateCounter(); void UpdatePacketTooLateCounter();
@ -545,33 +548,33 @@ public:
void setWindowSize(unsigned int size) { window_size = size; }; void setWindowSize(unsigned int size) { window_size = size; };
private: private:
std::mutex m_internal_mutex; 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_seqnum = SEQNUM_INITIAL;
u16 next_outgoing_split_seqnum; u16 next_outgoing_split_seqnum = SEQNUM_INITIAL;
unsigned int current_packet_loss; unsigned int current_packet_loss = 0;
unsigned int current_packet_too_late; unsigned int current_packet_too_late = 0;
unsigned int current_packet_successfull; unsigned int current_packet_successfull = 0;
float packet_loss_counter; float packet_loss_counter = 0.0f;
unsigned int current_bytes_transfered; unsigned int current_bytes_transfered = 0;
unsigned int current_bytes_received; unsigned int current_bytes_received = 0;
unsigned int current_bytes_lost; unsigned int current_bytes_lost = 0;
float max_kbps; float max_kbps = 0.0f;
float cur_kbps; float cur_kbps = 0.0f;
float avg_kbps; float avg_kbps = 0.0f;
float max_incoming_kbps; float max_incoming_kbps = 0.0f;
float cur_incoming_kbps; float cur_incoming_kbps = 0.0f;
float avg_incoming_kbps; float avg_incoming_kbps = 0.0f;
float max_kbps_lost; float max_kbps_lost = 0.0f;
float cur_kbps_lost; float cur_kbps_lost = 0.0f;
float avg_kbps_lost; float avg_kbps_lost = 0.0f;
float bpm_counter; float bpm_counter = 0.0f;
unsigned int rate_samples; unsigned int rate_samples = 0;
}; };
class Peer; class Peer;
@ -614,7 +617,7 @@ public:
class PeerHelper class PeerHelper
{ {
public: public:
PeerHelper(); PeerHelper() {};
PeerHelper(Peer* peer); PeerHelper(Peer* peer);
~PeerHelper(); ~PeerHelper();
@ -625,7 +628,7 @@ public:
bool operator!=(void* ptr); bool operator!=(void* ptr);
private: private:
Peer* m_peer; Peer *m_peer = nullptr;
}; };
class Connection; class Connection;
@ -654,23 +657,10 @@ class Peer {
Peer(Address address_,u16 id_,Connection* connection) : Peer(Address address_,u16 id_,Connection* connection) :
id(id_), id(id_),
m_increment_packets_remaining(9),
m_increment_bytes_remaining(0),
m_pending_deletion(false),
m_connection(connection), m_connection(connection),
address(address_), 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_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() { virtual ~Peer() {
@ -692,12 +682,12 @@ class Peer {
{ MutexAutoLock lock(m_exclusive_access_mutex); return m_pending_deletion; }; { MutexAutoLock lock(m_exclusive_access_mutex); return m_pending_deletion; };
void ResetTimeout() 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); bool isTimedOut(float timeout);
unsigned int m_increment_packets_remaining; unsigned int m_increment_packets_remaining = 9;
unsigned int m_increment_bytes_remaining; unsigned int m_increment_bytes_remaining = 0;
virtual u16 getNextSplitSequenceNumber(u8 channel) { return 0; }; virtual u16 getNextSplitSequenceNumber(u8 channel) { return 0; };
virtual void setNextSplitSequenceNumber(u8 channel, u16 seqnum) {}; virtual void setNextSplitSequenceNumber(u8 channel, u16 seqnum) {};
@ -740,7 +730,7 @@ class Peer {
std::mutex m_exclusive_access_mutex; std::mutex m_exclusive_access_mutex;
bool m_pending_deletion; bool m_pending_deletion = false;
Connection* m_connection; Connection* m_connection;
@ -748,26 +738,28 @@ class Peer {
Address address; Address address;
// Ping timer // Ping timer
float m_ping_timer; float m_ping_timer = 0.0f;
private: private:
struct rttstats { struct rttstats {
float jitter_min; float jitter_min = FLT_MAX;
float jitter_max; float jitter_max = 0.0f;
float jitter_avg; float jitter_avg = -1.0f;
float min_rtt; float min_rtt = FLT_MAX;
float max_rtt; float max_rtt = 0.0f;
float avg_rtt; float avg_rtt = -1.0f;
rttstats() {};
}; };
rttstats m_rtt; rttstats m_rtt;
float m_last_rtt; float m_last_rtt = -1.0f;
// current usage count // current usage count
unsigned int m_usage; unsigned int m_usage = 0;
// Seconds from last receive // Seconds from last receive
float m_timeout_counter; float m_timeout_counter = 0.0f;
u64 m_last_timeout_check; u64 m_last_timeout_check;
}; };
@ -822,16 +814,16 @@ protected:
bool Ping(float dtime,SharedBuffer<u8>& data); bool Ping(float dtime,SharedBuffer<u8>& data);
Channel channels[CHANNEL_COUNT]; Channel channels[CHANNEL_COUNT];
bool m_pending_disconnect; bool m_pending_disconnect = false;
private: private:
// This is changed dynamically // This is changed dynamically
float resend_timeout; float resend_timeout = 0.5;
bool processReliableSendCommand( bool processReliableSendCommand(
ConnectionCommand &c, ConnectionCommand &c,
unsigned int max_packet_size); unsigned int max_packet_size);
bool m_legacy_peer; bool m_legacy_peer = true;
}; };
/* /*
@ -848,14 +840,13 @@ enum ConnectionEventType{
struct ConnectionEvent struct ConnectionEvent
{ {
enum ConnectionEventType type; enum ConnectionEventType type = CONNEVENT_NONE;
u16 peer_id; u16 peer_id = 0;
Buffer<u8> data; Buffer<u8> data;
bool timeout; bool timeout = false;
Address address; Address address;
ConnectionEvent(): type(CONNEVENT_NONE), peer_id(0), ConnectionEvent() {}
timeout(false) {}
std::string describe() std::string describe()
{ {
@ -946,16 +937,16 @@ private:
bool packetsQueued(); bool packetsQueued();
Connection* m_connection; Connection *m_connection = nullptr;
unsigned int m_max_packet_size; unsigned int m_max_packet_size;
float m_timeout; float m_timeout;
std::queue<OutgoingPacket> m_outgoing_queue; std::queue<OutgoingPacket> m_outgoing_queue;
Semaphore m_send_sleep_semaphore; Semaphore m_send_sleep_semaphore;
unsigned int m_iteration_packets_avaialble; 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_data_packets_per_iteration;
unsigned int m_max_packets_requeued; unsigned int m_max_packets_requeued = 256;
}; };
class ConnectionReceiveThread : public Thread { class ConnectionReceiveThread : public Thread {
@ -993,7 +984,7 @@ private:
u8 channelnum, bool reliable); u8 channelnum, bool reliable);
Connection* m_connection; Connection *m_connection = nullptr;
}; };
class Connection class Connection
@ -1059,7 +1050,7 @@ private:
MutexedQueue<ConnectionEvent> m_event_queue; MutexedQueue<ConnectionEvent> m_event_queue;
u16 m_peer_id; u16 m_peer_id = 0;
u32 m_protocol_id; u32 m_protocol_id;
std::map<u16, Peer*> m_peers; std::map<u16, Peer*> m_peers;
@ -1073,11 +1064,11 @@ private:
// Backwards compatibility // Backwards compatibility
PeerHandler *m_bc_peerhandler; 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 } // namespace

@ -23,13 +23,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/serialize.h" #include "util/serialize.h"
NetworkPacket::NetworkPacket(u16 command, u32 datasize, u16 peer_id): 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); m_data.resize(m_datasize);
} }
NetworkPacket::NetworkPacket(u16 command, u32 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); m_data.resize(m_datasize);
} }

@ -30,8 +30,7 @@ class NetworkPacket
public: public:
NetworkPacket(u16 command, u32 datasize, u16 peer_id); NetworkPacket(u16 command, u32 datasize, u16 peer_id);
NetworkPacket(u16 command, u32 datasize); NetworkPacket(u16 command, u32 datasize);
NetworkPacket(): m_datasize(0), m_read_offset(0), m_command(0), NetworkPacket() {}
m_peer_id(0) {}
~NetworkPacket(); ~NetworkPacket();
void putRawPacket(u8 *data, u32 datasize, u16 peer_id); void putRawPacket(u8 *data, u32 datasize, u16 peer_id);
@ -126,10 +125,10 @@ private:
} }
std::vector<u8> m_data; std::vector<u8> m_data;
u32 m_datasize; u32 m_datasize = 0;
u32 m_read_offset; u32 m_read_offset = 0;
u16 m_command; u16 m_command = 0;
u16 m_peer_id; u16 m_peer_id = 0;
}; };
#endif #endif