Add TrafficSentByPacket and TrafficReceivedByPacket

This commit is contained in:
Sofia 2020-08-10 20:57:35 +03:00
parent 6598abf504
commit 069de32c29
4 changed files with 38 additions and 2 deletions

View File

@ -215,18 +215,22 @@ namespace NeonTea.Quakeball.TeaNet.Packets {
/// <summary>Write an entire packet using the protocol to the buffer.</summary>
public void WritePacket(Protocol protocol, Packet p) {
int old = Bytes.Count;
Write(protocol.GetPacketTypeID(p));
p.WriteMeta(this);
p.Write(this);
p.Size = Bytes.Count - old;
}
/// <summary>Read an entire packet using the given protocol from the buffer.</summary>
public Packet ReadPacket(Protocol protocol) {
int old = pos;
int packetType = ReadInt();
Type t = protocol.GetPacketType(packetType);
Packet p = (Packet)Activator.CreateInstance(t);
p.ReadMeta(this);
p.Read(this);
p.Size = pos - old;
return p;
}
}

View File

@ -5,6 +5,8 @@ namespace NeonTea.Quakeball.TeaNet.Packets {
public bool PacketIsReliable = true;
/// <summary>Packet meta-information: Id of this packet. Set just before sending.</summary>
public int PacketId;
/// <summary>Size of this packet in bytes. Only available after the packet has been Read (when received) or Written (when sent).</summary>
public int Size;
/// <summary>Write any relevant information about this packet into the buffer.</summary>
public abstract void Write(ByteBuffer buffer);

View File

@ -23,6 +23,8 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
/// <summary>The amount of bytes sent since the last clear interval from Peer</summary>
public int BytesSent;
public Dictionary<Type, int> SentByPacket = new Dictionary<Type, int>();
public Dictionary<Type, int> ReceivedByPacket = new Dictionary<Type, int>();
public ConnectionManager(Peer peer) {
Peer = peer;
@ -93,6 +95,11 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
buffer.Write(list.Length);
foreach (Packet p in list) {
buffer.WritePacket(protocol, p);
// Do the analytics dance!
int OldSentByPacket;
SentByPacket.TryGetValue(p.GetType(), out OldSentByPacket);
SentByPacket[p.GetType()] = OldSentByPacket + p.Size;
}
Send(conn, buffer);
}
@ -113,6 +120,11 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
buffer.Write(1);
buffer.WritePacket(protocol, p);
Send(conn, buffer);
// Do the analytics dance!
int OldSentByPacket;
SentByPacket.TryGetValue(p.GetType(), out OldSentByPacket);
SentByPacket[p.GetType()] = OldSentByPacket + p.Size;
}
}
@ -172,6 +184,8 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
}
public void Handle(IPEndPoint endpoint, ByteBuffer buffer) {
Connection conn = Find(endpoint);
conn.Internal.LastMessage = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
ConnectionStatus oldStatus = conn.Status;
@ -240,6 +254,11 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
conn.Internal.LatestInwardUnreliable = p.PacketId;
ProtocolActionQueues[protocol.Identifier].Enqueue(new ReceiveAction(conn, p));
}
// Do some analytics!
int OldReceivedByPacket;
ReceivedByPacket.TryGetValue(p.GetType(), out OldReceivedByPacket);
ReceivedByPacket[p.GetType()] = OldReceivedByPacket + p.Size;
}
break;
}

View File

@ -31,11 +31,15 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
}
}
/// <summary>The interval of traffic analyzed before updating</summary>
public long TrafficDataInterval;
public long TrafficDataInterval = 1000;
/// <summary>The amount of bytes received in the last TrafficDataIntervel</summary>
public int TrafficReceived { get; private set; }
/// <summary>The amount of bytes sent in the last TrafficDataIntervel</summary>
public int TrafficSent { get; private set; }
/// <summary>The amount of bytes sent in the last TrafficDataIntervel by Packet</summary>
public Dictionary<Type, int> TrafficSentByPacket => ConnectionManager.SentByPacket;
/// <summary>The amount of bytes received in the last TrafficDataIntervel by Packet</summary>
public Dictionary<Type, int> TrafficReceivedByPacket => ConnectionManager.ReceivedByPacket;
/// <summary>Whether the Peer is currently doing anything or not.null</sumary>
public bool Running { get; private set; }
@ -161,7 +165,7 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (now - LastTrafficData > TrafficDataInterval) {
LastTrafficData = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
LastTrafficData = now;
if (ListenerThread != null) {
TrafficReceived = ListenerThread.ReceivedBytes;
ListenerThread.ReceivedBytes = 0;
@ -169,6 +173,13 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
if (ConnectionManager != null) {
TrafficSent = ConnectionManager.BytesSent;
ConnectionManager.BytesSent = 0;
foreach (Type t in ConnectionManager.ReceivedByPacket.Keys) {
ConnectionManager.ReceivedByPacket[t] = 0;
}
foreach (Type t in ConnectionManager.SentByPacket.Keys) {
ConnectionManager.ReceivedByPacket[t] = 0;
}
}
}
}