Add TrafficSentByPacket and TrafficReceivedByPacket
This commit is contained in:
parent
6598abf504
commit
069de32c29
@ -215,18 +215,22 @@ namespace NeonTea.Quakeball.TeaNet.Packets {
|
|||||||
|
|
||||||
/// <summary>Write an entire packet using the protocol to the buffer.</summary>
|
/// <summary>Write an entire packet using the protocol to the buffer.</summary>
|
||||||
public void WritePacket(Protocol protocol, Packet p) {
|
public void WritePacket(Protocol protocol, Packet p) {
|
||||||
|
int old = Bytes.Count;
|
||||||
Write(protocol.GetPacketTypeID(p));
|
Write(protocol.GetPacketTypeID(p));
|
||||||
p.WriteMeta(this);
|
p.WriteMeta(this);
|
||||||
p.Write(this);
|
p.Write(this);
|
||||||
|
p.Size = Bytes.Count - old;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Read an entire packet using the given protocol from the buffer.</summary>
|
/// <summary>Read an entire packet using the given protocol from the buffer.</summary>
|
||||||
public Packet ReadPacket(Protocol protocol) {
|
public Packet ReadPacket(Protocol protocol) {
|
||||||
|
int old = pos;
|
||||||
int packetType = ReadInt();
|
int packetType = ReadInt();
|
||||||
Type t = protocol.GetPacketType(packetType);
|
Type t = protocol.GetPacketType(packetType);
|
||||||
Packet p = (Packet)Activator.CreateInstance(t);
|
Packet p = (Packet)Activator.CreateInstance(t);
|
||||||
p.ReadMeta(this);
|
p.ReadMeta(this);
|
||||||
p.Read(this);
|
p.Read(this);
|
||||||
|
p.Size = pos - old;
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@ namespace NeonTea.Quakeball.TeaNet.Packets {
|
|||||||
public bool PacketIsReliable = true;
|
public bool PacketIsReliable = true;
|
||||||
/// <summary>Packet meta-information: Id of this packet. Set just before sending.</summary>
|
/// <summary>Packet meta-information: Id of this packet. Set just before sending.</summary>
|
||||||
public int PacketId;
|
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>
|
/// <summary>Write any relevant information about this packet into the buffer.</summary>
|
||||||
public abstract void Write(ByteBuffer buffer);
|
public abstract void Write(ByteBuffer buffer);
|
||||||
|
@ -23,6 +23,8 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
|||||||
|
|
||||||
/// <summary>The amount of bytes sent since the last clear interval from Peer</summary>
|
/// <summary>The amount of bytes sent since the last clear interval from Peer</summary>
|
||||||
public int BytesSent;
|
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) {
|
public ConnectionManager(Peer peer) {
|
||||||
Peer = peer;
|
Peer = peer;
|
||||||
@ -93,6 +95,11 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
|||||||
buffer.Write(list.Length);
|
buffer.Write(list.Length);
|
||||||
foreach (Packet p in list) {
|
foreach (Packet p in list) {
|
||||||
buffer.WritePacket(protocol, p);
|
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);
|
Send(conn, buffer);
|
||||||
}
|
}
|
||||||
@ -113,6 +120,11 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
|||||||
buffer.Write(1);
|
buffer.Write(1);
|
||||||
buffer.WritePacket(protocol, p);
|
buffer.WritePacket(protocol, p);
|
||||||
Send(conn, buffer);
|
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) {
|
public void Handle(IPEndPoint endpoint, ByteBuffer buffer) {
|
||||||
|
|
||||||
|
|
||||||
Connection conn = Find(endpoint);
|
Connection conn = Find(endpoint);
|
||||||
conn.Internal.LastMessage = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
conn.Internal.LastMessage = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||||
ConnectionStatus oldStatus = conn.Status;
|
ConnectionStatus oldStatus = conn.Status;
|
||||||
@ -240,6 +254,11 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
|||||||
conn.Internal.LatestInwardUnreliable = p.PacketId;
|
conn.Internal.LatestInwardUnreliable = p.PacketId;
|
||||||
ProtocolActionQueues[protocol.Identifier].Enqueue(new ReceiveAction(conn, p));
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -31,11 +31,15 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>The interval of traffic analyzed before updating</summary>
|
/// <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>
|
/// <summary>The amount of bytes received in the last TrafficDataIntervel</summary>
|
||||||
public int TrafficReceived { get; private set; }
|
public int TrafficReceived { get; private set; }
|
||||||
/// <summary>The amount of bytes sent in the last TrafficDataIntervel</summary>
|
/// <summary>The amount of bytes sent in the last TrafficDataIntervel</summary>
|
||||||
public int TrafficSent { get; private set; }
|
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>
|
/// <summary>Whether the Peer is currently doing anything or not.null</sumary>
|
||||||
public bool Running { get; private set; }
|
public bool Running { get; private set; }
|
||||||
|
|
||||||
@ -161,7 +165,7 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
|||||||
|
|
||||||
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||||
if (now - LastTrafficData > TrafficDataInterval) {
|
if (now - LastTrafficData > TrafficDataInterval) {
|
||||||
LastTrafficData = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
LastTrafficData = now;
|
||||||
if (ListenerThread != null) {
|
if (ListenerThread != null) {
|
||||||
TrafficReceived = ListenerThread.ReceivedBytes;
|
TrafficReceived = ListenerThread.ReceivedBytes;
|
||||||
ListenerThread.ReceivedBytes = 0;
|
ListenerThread.ReceivedBytes = 0;
|
||||||
@ -169,6 +173,13 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
|||||||
if (ConnectionManager != null) {
|
if (ConnectionManager != null) {
|
||||||
TrafficSent = ConnectionManager.BytesSent;
|
TrafficSent = ConnectionManager.BytesSent;
|
||||||
ConnectionManager.BytesSent = 0;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user