Browse Source

incoming/outgoing packet counter, last heard time logic added (#2380)

* incoming/outgoing packet counter, last heard time logic added

* typo fix, Connection::GetLastHeardTime is now non-const

* Lua and Angelscript bindings updated

* AngelScript typo fixed

* GetLastHeardTime updates
Arnis Lielturks 7 years ago
parent
commit
dd8b41feac

+ 3 - 3
Source/Urho3D/AngelScript/NetworkAPI.cpp

@@ -74,11 +74,11 @@ static void RegisterConnection(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Connection", "String get_address() const", asMETHOD(Connection, GetAddress), asCALL_THISCALL);
     engine->RegisterObjectMethod("Connection", "uint16 get_port() const", asMETHOD(Connection, GetPort), asCALL_THISCALL);
     engine->RegisterObjectMethod("Connection", "float get_roundTripTime() const", asMETHOD(Connection, GetRoundTripTime), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Connection", "float get_lastHeardTime() const", asMETHOD(Connection, GetLastHeardTime), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Connection", "uint get_lastHeardTime() const", asMETHOD(Connection, GetLastHeardTime), asCALL_THISCALL);
     engine->RegisterObjectMethod("Connection", "float get_bytesInPerSec() const", asMETHOD(Connection, GetBytesInPerSec), asCALL_THISCALL);
     engine->RegisterObjectMethod("Connection", "float get_bytesOutPerSec() const", asMETHOD(Connection, GetBytesOutPerSec), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Connection", "float get_packetsInPerSec() const", asMETHOD(Connection, GetPacketsInPerSec), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Connection", "float get_packetsOutPerSec() const", asMETHOD(Connection, GetPacketsOutPerSec), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Connection", "int get_packetsInPerSec() const", asMETHOD(Connection, GetPacketsInPerSec), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Connection", "int get_packetsOutPerSec() const", asMETHOD(Connection, GetPacketsOutPerSec), asCALL_THISCALL);
     engine->RegisterObjectMethod("Connection", "uint get_numDownloads() const", asMETHOD(Connection, GetNumDownloads), asCALL_THISCALL);
     engine->RegisterObjectMethod("Connection", "const String& get_downloadName() const", asMETHOD(Connection, GetDownloadName), asCALL_THISCALL);
     engine->RegisterObjectMethod("Connection", "float get_downloadProgress() const", asMETHOD(Connection, GetDownloadProgress), asCALL_THISCALL);

+ 3 - 3
Source/Urho3D/LuaScript/pkgs/Network/Connection.pkg

@@ -43,11 +43,11 @@ class Connection : public Object
     String GetAddress() const;
     unsigned short GetPort() const;
     float GetRoundTripTime() const;
-    float GetLastHeardTime() const;
+    unsigned GetLastHeardTime() const;
     float GetBytesInPerSec() const;
     float GetBytesOutPerSec() const;
-    float GetPacketsInPerSec() const;
-    float GetPacketsOutPerSec() const;
+    int GetPacketsInPerSec() const;
+    int GetPacketsOutPerSec() const;
     String ToString() const;
     unsigned GetNumDownloads() const;
     const String GetDownloadName() const;

+ 26 - 16
Source/Urho3D/Network/Connection.cpp

@@ -118,8 +118,10 @@ void Connection::SendMessage(int msgID, bool reliable, bool inOrder, const unsig
     buffer.WriteUByte((unsigned char)msgID);
     buffer.Write(data, numBytes);
     PacketReliability reliability = reliable ? (inOrder ? RELIABLE_ORDERED : RELIABLE) : (inOrder ? UNRELIABLE_SEQUENCED : UNRELIABLE);
-    if (peer_)
-        peer_->Send((const char*)buffer.GetData(), (int)buffer.GetSize(), HIGH_PRIORITY, reliability, (char)0, *address_, false);
+    if (peer_) {
+        peer_->Send((const char *) buffer.GetData(), (int) buffer.GetSize(), HIGH_PRIORITY, reliability, (char) 0, *address_, false);
+        tempPacketCounter_.y_++;
+    }
 }
 
 void Connection::SendRemoteEvent(StringHash eventType, bool inOrder, const VariantMap& eventData)
@@ -288,15 +290,23 @@ void Connection::SendRemoteEvents()
     {
         statsTimer_.Reset();
         char statsBuffer[256];
-        sprintf(statsBuffer, "RTT %.3f ms Pkt in %d Pkt out %d Data in %.3f KB/s Data out %.3f KB/s", GetRoundTripTime(),
-            (int)0, //TODO: Packets in per second
-            (int)0, //TODO: Packets out per second
-            0.0f, //TODO: Bytes in per second
-            0.0f); // TODO: Bytes out per second
+        sprintf(statsBuffer, "RTT %.3f ms Pkt in %i Pkt out %i Data in %.3f KB/s Data out %.3f KB/s, Last heard %u", GetRoundTripTime(),
+            GetPacketsInPerSec(),
+            GetPacketsOutPerSec(),
+            GetBytesInPerSec(),
+            GetBytesOutPerSec(),
+            GetLastHeardTime());
         URHO3D_LOGINFO(statsBuffer);
     }
 #endif
 
+    if (packetCounterTimer_.GetMSec(false) > 1000)
+    {
+        packetCounterTimer_.Reset();
+        packetCounter_ = tempPacketCounter_;
+        tempPacketCounter_ = IntVector2::ZERO;
+    }
+
     if (remoteEvents_.Empty())
         return;
 
@@ -389,6 +399,9 @@ void Connection::ProcessPendingLatestData()
 
 bool Connection::ProcessMessage(int msgID, MemoryBuffer& msg)
 {
+    // New incomming message, reset last heard timer
+    lastHeardTimer_.Reset();
+    tempPacketCounter_.x_++;
     bool processed = true;
 
     switch (msgID)
@@ -1010,10 +1023,9 @@ float Connection::GetRoundTripTime() const
     return 0.0f;
 }
 
-float Connection::GetLastHeardTime() const
+unsigned Connection::GetLastHeardTime() const
 {
-    //TODO
-    return 0.0f;
+    return const_cast<Timer&>(lastHeardTimer_).GetMSec(false);
 }
 
 float Connection::GetBytesInPerSec() const
@@ -1038,16 +1050,14 @@ float Connection::GetBytesOutPerSec() const
     return 0.0f;
 }
 
-float Connection::GetPacketsInPerSec() const
+int Connection::GetPacketsInPerSec() const
 {
-    //TODO
-    return 0.0f;
+    return packetCounter_.x_;
 }
 
-float Connection::GetPacketsOutPerSec() const
+int Connection::GetPacketsOutPerSec() const
 {
-    //TODO
-    return 0.0f;
+    return packetCounter_.y_;
 }
 
 String Connection::ToString() const

+ 11 - 3
Source/Urho3D/Network/Connection.h

@@ -201,7 +201,7 @@ public:
     float GetRoundTripTime() const;
 
     /// Return the time since last received data from the remote host in milliseconds.
-    float GetLastHeardTime() const;
+    unsigned GetLastHeardTime() const;
 
     /// Return bytes received per second.
     float GetBytesInPerSec() const;
@@ -210,10 +210,10 @@ public:
     float GetBytesOutPerSec() const;
 
     /// Return packets received per second.
-    float GetPacketsInPerSec() const;
+    int GetPacketsInPerSec() const;
 
     /// Return packets sent per second.
-    float GetPacketsOutPerSec() const;
+    int GetPacketsOutPerSec() const;
 
     /// Return an address:port string.
     String ToString() const;
@@ -318,6 +318,14 @@ private:
     SLNet::AddressOrGUID* address_;
     /// Raknet peer object.
     SLNet::RakPeerInterface* peer_;
+    /// Temporary variable to hold packet count in the next second, x - packets in, y - packets out
+    IntVector2 tempPacketCounter_;
+    /// Packet count in the last second, x - packets in, y - packets out
+    IntVector2 packetCounter_;
+    /// Packet count timer which resets every 1s
+    Timer packetCounterTimer_;
+    /// Last heard timer, resets when new packet is incoming
+    Timer lastHeardTimer_;
 };
 
 }