diff --git a/Assets/Scripts/TeaNet/Packets/ByteBuffer.cs b/Assets/Scripts/TeaNet/Packets/ByteBuffer.cs index c8886e2..2c8ce71 100644 --- a/Assets/Scripts/TeaNet/Packets/ByteBuffer.cs +++ b/Assets/Scripts/TeaNet/Packets/ByteBuffer.cs @@ -215,18 +215,22 @@ namespace NeonTea.Quakeball.TeaNet.Packets { /// Write an entire packet using the protocol to the buffer. 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; } /// Read an entire packet using the given protocol from the buffer. 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; } } diff --git a/Assets/Scripts/TeaNet/Packets/Packet.cs b/Assets/Scripts/TeaNet/Packets/Packet.cs index 3146845..5d9646e 100644 --- a/Assets/Scripts/TeaNet/Packets/Packet.cs +++ b/Assets/Scripts/TeaNet/Packets/Packet.cs @@ -5,6 +5,8 @@ namespace NeonTea.Quakeball.TeaNet.Packets { public bool PacketIsReliable = true; /// Packet meta-information: Id of this packet. Set just before sending. public int PacketId; + /// Size of this packet in bytes. Only available after the packet has been Read (when received) or Written (when sent). + public int Size; /// Write any relevant information about this packet into the buffer. public abstract void Write(ByteBuffer buffer); diff --git a/Assets/Scripts/TeaNet/Peers/ConnectionManager.cs b/Assets/Scripts/TeaNet/Peers/ConnectionManager.cs index a99a6dd..7a0b8f2 100644 --- a/Assets/Scripts/TeaNet/Peers/ConnectionManager.cs +++ b/Assets/Scripts/TeaNet/Peers/ConnectionManager.cs @@ -23,6 +23,8 @@ namespace NeonTea.Quakeball.TeaNet.Peers { /// The amount of bytes sent since the last clear interval from Peer public int BytesSent; + public Dictionary SentByPacket = new Dictionary(); + public Dictionary ReceivedByPacket = new Dictionary(); 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; } diff --git a/Assets/Scripts/TeaNet/Peers/Peer.cs b/Assets/Scripts/TeaNet/Peers/Peer.cs index 7f1ef86..c6fc7a0 100644 --- a/Assets/Scripts/TeaNet/Peers/Peer.cs +++ b/Assets/Scripts/TeaNet/Peers/Peer.cs @@ -31,11 +31,15 @@ namespace NeonTea.Quakeball.TeaNet.Peers { } } /// The interval of traffic analyzed before updating - public long TrafficDataInterval; + public long TrafficDataInterval = 1000; /// The amount of bytes received in the last TrafficDataIntervel public int TrafficReceived { get; private set; } /// The amount of bytes sent in the last TrafficDataIntervel public int TrafficSent { get; private set; } + /// The amount of bytes sent in the last TrafficDataIntervel by Packet + public Dictionary TrafficSentByPacket => ConnectionManager.SentByPacket; + /// The amount of bytes received in the last TrafficDataIntervel by Packet + public Dictionary TrafficReceivedByPacket => ConnectionManager.ReceivedByPacket; /// Whether the Peer is currently doing anything or not.null 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; + } } } }