179 changes: 85 additions & 94 deletions src/network/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> 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
Expand Down Expand Up @@ -210,16 +209,12 @@ SharedBuffer<u8> makeReliablePacket(

struct IncomingSplitPacket
{
IncomingSplitPacket()
{
time = 0.0;
reliable = false;
}
IncomingSplitPacket() {}
// Key is chunk number, value is data without headers
std::map<u16, SharedBuffer<u8> > 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()
{
Expand Down Expand Up @@ -322,7 +317,7 @@ typedef std::list<BufferedPacket>::iterator RPBSearchResult;
class ReliablePacketBuffer
{
public:
ReliablePacketBuffer();
ReliablePacketBuffer() {};

bool getFirstSeqnum(u16& result);

Expand All @@ -345,7 +340,7 @@ class ReliablePacketBuffer
RPBSearchResult findPacket(u16 seqnum);

std::list<BufferedPacket> m_list;
u32 m_list_size;
u32 m_list_size = 0;

u16 m_oldest_non_answered_ack;

Expand Down Expand Up @@ -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<u8> 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_)
{
Expand Down Expand Up @@ -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
{

Expand Down Expand Up @@ -507,8 +510,8 @@ class Channel

IncomingSplitBuffer incoming_splits;

Channel();
~Channel();
Channel() {};
~Channel() {};

void UpdatePacketLossCounter(unsigned int count);
void UpdatePacketTooLateCounter();
Expand Down Expand Up @@ -545,33 +548,33 @@ class Channel
void setWindowSize(unsigned int size) { window_size = size; };
private:
std::mutex m_internal_mutex;
int window_size;

u16 next_incoming_seqnum;

u16 next_outgoing_seqnum;
u16 next_outgoing_split_seqnum;

unsigned int current_packet_loss;
unsigned int current_packet_too_late;
unsigned int current_packet_successfull;
float packet_loss_counter;

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 rate_samples;
int window_size = MIN_RELIABLE_WINDOW_SIZE;

u16 next_incoming_seqnum = SEQNUM_INITIAL;

u16 next_outgoing_seqnum = SEQNUM_INITIAL;
u16 next_outgoing_split_seqnum = SEQNUM_INITIAL;

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 = 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 = 0;
};

class Peer;
Expand Down Expand Up @@ -614,7 +617,7 @@ class PeerHandler
class PeerHelper
{
public:
PeerHelper();
PeerHelper() {};
PeerHelper(Peer* peer);
~PeerHelper();

Expand All @@ -625,7 +628,7 @@ class PeerHelper
bool operator!=(void* ptr);

private:
Peer* m_peer;
Peer *m_peer = nullptr;
};

class Connection;
Expand Down Expand Up @@ -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() {
Expand All @@ -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) {};
Expand Down Expand Up @@ -740,34 +730,36 @@ class Peer {

std::mutex m_exclusive_access_mutex;

bool m_pending_deletion;
bool m_pending_deletion = false;

Connection* m_connection;

// Address of the 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;
};
Expand Down Expand Up @@ -822,16 +814,16 @@ class UDPPeer : public Peer
bool Ping(float dtime,SharedBuffer<u8>& 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;
};

/*
Expand All @@ -848,14 +840,13 @@ enum ConnectionEventType{

struct ConnectionEvent
{
enum ConnectionEventType type;
u16 peer_id;
enum ConnectionEventType type = CONNEVENT_NONE;
u16 peer_id = 0;
Buffer<u8> data;
bool timeout;
bool timeout = false;
Address address;

ConnectionEvent(): type(CONNEVENT_NONE), peer_id(0),
timeout(false) {}
ConnectionEvent() {}

std::string describe()
{
Expand Down Expand Up @@ -946,16 +937,16 @@ class ConnectionSendThread : public Thread {

bool packetsQueued();

Connection* m_connection;
Connection *m_connection = nullptr;
unsigned int m_max_packet_size;
float m_timeout;
std::queue<OutgoingPacket> 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 {
Expand Down Expand Up @@ -993,7 +984,7 @@ class ConnectionReceiveThread : public Thread {
u8 channelnum, bool reliable);


Connection* m_connection;
Connection *m_connection = nullptr;
};

class Connection
Expand Down Expand Up @@ -1059,7 +1050,7 @@ class Connection

MutexedQueue<ConnectionEvent> m_event_queue;

u16 m_peer_id;
u16 m_peer_id = 0;
u32 m_protocol_id;

std::map<u16, Peer*> m_peers;
Expand All @@ -1073,11 +1064,11 @@ class Connection

// 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
Expand Down
4 changes: 2 additions & 2 deletions src/network/networkpacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 5 additions & 6 deletions src/network/networkpacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -126,10 +125,10 @@ class NetworkPacket
}

std::vector<u8> 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