]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/network/connection.cpp
Better F6 profiler (#8750)
[dragonfireclient.git] / src / network / connection.cpp
index b2e44390143e01cc0d1e542987383dc66ac74a7e..913088da7b3da1c9a9e359ebc08400a01934eb25 100644 (file)
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <iomanip>
 #include <cerrno>
 #include <algorithm>
+#include <cmath>
 #include "connection.h"
 #include "serialization.h"
 #include "log.h"
@@ -55,8 +56,8 @@ std::mutex log_message_mutex;
 
 #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);
@@ -125,7 +126,7 @@ void makeSplitPacket(const SharedBuffer<u8> &data, u32 chunksize_max, u16 seqnum
        }
 }
 
-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;
@@ -139,7 +140,7 @@ void makeAutoSplitPacket(SharedBuffer<u8> data, u32 chunksize_max,
        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;
@@ -501,7 +502,7 @@ void IncomingSplitBuffer::removeUnreliableTimedOuts(float dtime, float timeout)
        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;
@@ -600,7 +601,7 @@ void Channel::UpdateBytesSent(unsigned int bytes, unsigned int packets)
 {
        MutexAutoLock internal(m_internal_mutex);
        current_bytes_transfered += bytes;
-       current_packet_successfull += packets;
+       current_packet_successful += packets;
 }
 
 void Channel::UpdateBytesReceived(unsigned int bytes) {
@@ -627,17 +628,16 @@ void Channel::UpdatePacketTooLateCounter()
        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;
@@ -646,94 +646,78 @@ void Channel::UpdateTimers(float dtime,bool legacy_peer)
                        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;
                }
 
@@ -865,8 +849,8 @@ void Peer::RTTStatistics(float rtt, const std::string &profiler_id,
                                                                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 */
@@ -910,6 +894,8 @@ void Peer::Drop()
 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)
@@ -923,15 +909,6 @@ 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) {
@@ -1198,10 +1175,10 @@ void Connection::TriggerSend()
        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);
@@ -1237,17 +1214,7 @@ u16 Connection::lookupPeer(Address& sender)
        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;
 
@@ -1316,7 +1283,7 @@ bool Connection::Connected()
        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;
 
@@ -1371,8 +1338,8 @@ void Connection::Receive(NetworkPacket* pkt)
        throw NoIncomingDataException("No incoming data");
 }
 
-void Connection::Send(u16 peer_id, u8 channelnum,
-               NetworkPacketpkt, bool reliable)
+void Connection::Send(session_t peer_id, u8 channelnum,
+               NetworkPacket *pkt, bool reliable)
 {
        assert(channelnum < CHANNEL_COUNT); // Pre-condition
 
@@ -1382,7 +1349,7 @@ void Connection::Send(u16 peer_id, u8 channelnum,
        putCommand(c);
 }
 
-Address Connection::GetPeerAddress(u16 peer_id)
+Address Connection::GetPeerAddress(session_t peer_id)
 {
        PeerHelper peer = getPeerNoEx(peer_id);
 
@@ -1393,7 +1360,7 @@ Address Connection::GetPeerAddress(u16 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;
@@ -1440,7 +1407,7 @@ u16 Connection::createPeer(Address& sender, MTProtocols protocol, int fd)
        // 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;
 
        /*
@@ -1508,14 +1475,14 @@ const std::string Connection::getDesc()
                        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