#include <iomanip>
#include <cerrno>
#include <algorithm>
+#include <cmath>
#include "connection.h"
#include "serialization.h"
#include "log.h"
#define PING_TIMEOUT 5.0
-BufferedPacket makePacket(Address &address, SharedBuffer<u8> data,
- u32 protocol_id, u16 sender_peer_id, u8 channel)
+BufferedPacket makePacket(Address &address, const SharedBuffer<u8> &data,
+ u32 protocol_id, session_t sender_peer_id, u8 channel)
{
u32 packet_size = data.getSize() + BASE_HEADER_SIZE;
BufferedPacket p(packet_size);
}
}
-void makeAutoSplitPacket(SharedBuffer<u8> data, u32 chunksize_max,
+void makeAutoSplitPacket(const SharedBuffer<u8> &data, u32 chunksize_max,
u16 &split_seqnum, std::list<SharedBuffer<u8>> *list)
{
u32 original_header_size = 1;
list->push_back(makeOriginalPacket(data));
}
-SharedBuffer<u8> makeReliablePacket(SharedBuffer<u8> data, u16 seqnum)
+SharedBuffer<u8> makeReliablePacket(const SharedBuffer<u8> &data, u16 seqnum)
{
u32 header_size = 3;
u32 packet_size = data.getSize() + header_size;
ConnectionCommand
*/
-void ConnectionCommand::send(u16 peer_id_, u8 channelnum_, NetworkPacket *pkt,
+void ConnectionCommand::send(session_t peer_id_, u8 channelnum_, NetworkPacket *pkt,
bool reliable_)
{
type = CONNCMD_SEND;
{
MutexAutoLock internal(m_internal_mutex);
current_bytes_transfered += bytes;
- current_packet_successfull += packets;
+ current_packet_successful += packets;
}
void Channel::UpdateBytesReceived(unsigned int bytes) {
current_packet_too_late++;
}
-void Channel::UpdateTimers(float dtime,bool legacy_peer)
+void Channel::UpdateTimers(float dtime)
{
bpm_counter += dtime;
packet_loss_counter += dtime;
- if (packet_loss_counter > 1.0)
- {
- packet_loss_counter -= 1.0;
+ if (packet_loss_counter > 1.0f) {
+ packet_loss_counter -= 1.0f;
unsigned int packet_loss = 11; /* use a neutral value for initialization */
- unsigned int packets_successfull = 0;
+ unsigned int packets_successful = 0;
//unsigned int packet_too_late = 0;
bool reasonable_amount_of_data_transmitted = false;
MutexAutoLock internal(m_internal_mutex);
packet_loss = current_packet_loss;
//packet_too_late = current_packet_too_late;
- packets_successfull = current_packet_successfull;
+ packets_successful = current_packet_successful;
- if (current_bytes_transfered > (unsigned int) (window_size*512/2))
- {
+ if (current_bytes_transfered > (unsigned int) (window_size*512/2)) {
reasonable_amount_of_data_transmitted = true;
}
current_packet_loss = 0;
current_packet_too_late = 0;
- current_packet_successfull = 0;
+ current_packet_successful = 0;
}
- /* dynamic window size is only available for non legacy peers */
- if (!legacy_peer) {
- float successfull_to_lost_ratio = 0.0;
- bool done = false;
+ /* dynamic window size */
+ float successful_to_lost_ratio = 0.0f;
+ bool done = false;
+
+ if (packets_successful > 0) {
+ successful_to_lost_ratio = packet_loss/packets_successful;
+ } else if (packet_loss > 0) {
+ window_size = std::max(
+ (window_size - 10),
+ MIN_RELIABLE_WINDOW_SIZE);
+ done = true;
+ }
- if (packets_successfull > 0) {
- successfull_to_lost_ratio = packet_loss/packets_successfull;
- }
- else if (packet_loss > 0)
- {
- window_size = MYMAX(
- (window_size - 10),
+ if (!done) {
+ if ((successful_to_lost_ratio < 0.01f) &&
+ (window_size < MAX_RELIABLE_WINDOW_SIZE)) {
+ /* don't even think about increasing if we didn't even
+ * use major parts of our window */
+ if (reasonable_amount_of_data_transmitted)
+ window_size = std::min(
+ (window_size + 100),
+ MAX_RELIABLE_WINDOW_SIZE);
+ } else if ((successful_to_lost_ratio < 0.05f) &&
+ (window_size < MAX_RELIABLE_WINDOW_SIZE)) {
+ /* don't even think about increasing if we didn't even
+ * use major parts of our window */
+ if (reasonable_amount_of_data_transmitted)
+ window_size = std::min(
+ (window_size + 50),
+ MAX_RELIABLE_WINDOW_SIZE);
+ } else if (successful_to_lost_ratio > 0.15f) {
+ window_size = std::max(
+ (window_size - 100),
+ MIN_RELIABLE_WINDOW_SIZE);
+ } else if (successful_to_lost_ratio > 0.1f) {
+ window_size = std::max(
+ (window_size - 50),
MIN_RELIABLE_WINDOW_SIZE);
- done = true;
- }
-
- if (!done)
- {
- if ((successfull_to_lost_ratio < 0.01) &&
- (window_size < MAX_RELIABLE_WINDOW_SIZE))
- {
- /* don't even think about increasing if we didn't even
- * use major parts of our window */
- if (reasonable_amount_of_data_transmitted)
- window_size = MYMIN(
- (window_size + 100),
- MAX_RELIABLE_WINDOW_SIZE);
- }
- else if ((successfull_to_lost_ratio < 0.05) &&
- (window_size < MAX_RELIABLE_WINDOW_SIZE))
- {
- /* don't even think about increasing if we didn't even
- * use major parts of our window */
- if (reasonable_amount_of_data_transmitted)
- window_size = MYMIN(
- (window_size + 50),
- MAX_RELIABLE_WINDOW_SIZE);
- }
- else if (successfull_to_lost_ratio > 0.15)
- {
- window_size = MYMAX(
- (window_size - 100),
- MIN_RELIABLE_WINDOW_SIZE);
- }
- else if (successfull_to_lost_ratio > 0.1)
- {
- window_size = MYMAX(
- (window_size - 50),
- MIN_RELIABLE_WINDOW_SIZE);
- }
}
}
}
- if (bpm_counter > 10.0)
- {
+ if (bpm_counter > 10.0f) {
{
MutexAutoLock internal(m_internal_mutex);
cur_kbps =
- (((float) current_bytes_transfered)/bpm_counter)/1024.0;
+ (((float) current_bytes_transfered)/bpm_counter)/1024.0f;
current_bytes_transfered = 0;
cur_kbps_lost =
- (((float) current_bytes_lost)/bpm_counter)/1024.0;
+ (((float) current_bytes_lost)/bpm_counter)/1024.0f;
current_bytes_lost = 0;
cur_incoming_kbps =
- (((float) current_bytes_received)/bpm_counter)/1024.0;
+ (((float) current_bytes_received)/bpm_counter)/1024.0f;
current_bytes_received = 0;
- bpm_counter = 0;
+ bpm_counter = 0.0f;
}
- if (cur_kbps > max_kbps)
- {
+ if (cur_kbps > max_kbps) {
max_kbps = cur_kbps;
}
- if (cur_kbps_lost > max_kbps_lost)
- {
+ if (cur_kbps_lost > max_kbps_lost) {
max_kbps_lost = cur_kbps_lost;
}
jitter * (1/num_samples);
if (!profiler_id.empty()) {
- g_profiler->graphAdd(profiler_id + "_rtt", rtt);
- g_profiler->graphAdd(profiler_id + "_jitter", jitter);
+ g_profiler->graphAdd(profiler_id + " RTT [ms]", rtt * 1000.f);
+ g_profiler->graphAdd(profiler_id + " jitter [ms]", jitter * 1000.f);
}
}
/* save values required for next loop */
UDPPeer::UDPPeer(u16 a_id, Address a_address, Connection* connection) :
Peer(a_address,a_id,connection)
{
+ for (Channel &channel : channels)
+ channel.setWindowSize(g_settings->getU16("max_packets_per_iteration"));
}
bool UDPPeer::getAddress(MTProtocols type,Address& toset)
return false;
}
-void UDPPeer::setNonLegacyPeer()
-{
- m_legacy_peer = false;
- for(unsigned int i=0; i< CHANNEL_COUNT; i++)
- {
- channels->setWindowSize(g_settings->getU16("max_packets_per_iteration"));
- }
-}
-
void UDPPeer::reportRTT(float rtt)
{
if (rtt < 0.0) {
m_sendThread->Trigger();
}
-PeerHelper Connection::getPeerNoEx(u16 peer_id)
+PeerHelper Connection::getPeerNoEx(session_t peer_id)
{
MutexAutoLock peerlock(m_peers_mutex);
- std::map<u16, Peer*>::iterator node = m_peers.find(peer_id);
+ std::map<session_t, Peer *>::iterator node = m_peers.find(peer_id);
if (node == m_peers.end()) {
return PeerHelper(NULL);
return PEER_ID_INEXISTENT;
}
-std::list<Peer*> Connection::getPeers()
-{
- std::list<Peer*> list;
- for (auto &p : m_peers) {
- Peer *peer = p.second;
- list.push_back(peer);
- }
- return list;
-}
-
-bool Connection::deletePeer(u16 peer_id, bool timeout)
+bool Connection::deletePeer(session_t peer_id, bool timeout)
{
Peer *peer = 0;
if (m_peers.size() != 1)
return false;
- std::map<u16, Peer*>::iterator node = m_peers.find(PEER_ID_SERVER);
+ std::map<session_t, Peer *>::iterator node = m_peers.find(PEER_ID_SERVER);
if (node == m_peers.end())
return false;
throw NoIncomingDataException("No incoming data");
}
-void Connection::Send(u16 peer_id, u8 channelnum,
- NetworkPacket* pkt, bool reliable)
+void Connection::Send(session_t peer_id, u8 channelnum,
+ NetworkPacket *pkt, bool reliable)
{
assert(channelnum < CHANNEL_COUNT); // Pre-condition
putCommand(c);
}
-Address Connection::GetPeerAddress(u16 peer_id)
+Address Connection::GetPeerAddress(session_t peer_id)
{
PeerHelper peer = getPeerNoEx(peer_id);
return peer_address;
}
-float Connection::getPeerStat(u16 peer_id, rtt_stat_type type)
+float Connection::getPeerStat(session_t peer_id, rtt_stat_type type)
{
PeerHelper peer = getPeerNoEx(peer_id);
if (!peer) return -1;
// Somebody wants to make a new connection
// Get a unique peer id (2 or higher)
- u16 peer_id_new = m_next_remote_peer_id;
+ session_t peer_id_new = m_next_remote_peer_id;
u16 overflow = MAX_UDP_PEERS;
/*
itos(m_udpSocket.GetHandle())+"/"+itos(m_peer_id)+")";
}
-void Connection::DisconnectPeer(u16 peer_id)
+void Connection::DisconnectPeer(session_t peer_id)
{
ConnectionCommand discon;
discon.disconnect_peer(peer_id);
putCommand(discon);
}
-void Connection::sendAck(u16 peer_id, u8 channelnum, u16 seqnum)
+void Connection::sendAck(session_t peer_id, u8 channelnum, u16 seqnum)
{
assert(channelnum < CHANNEL_COUNT); // Pre-condition