Add a traffic monitoring system to Peer
This commit is contained in:
parent
707fd3b7b1
commit
6598abf504
@ -5,6 +5,8 @@ using System.Text;
|
||||
namespace NeonTea.Quakeball.TeaNet.Packets {
|
||||
/// <summary>Contains a stream of bytes for sending or receiving over the internet.</summary>
|
||||
public class ByteBuffer {
|
||||
public int Size => Bytes.Count;
|
||||
|
||||
private List<byte> Bytes;
|
||||
private int pos = 0;
|
||||
|
||||
|
@ -1,6 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net;
|
||||
using System;
|
||||
|
||||
using NeonTea.Quakeball.TeaNet.Packets;
|
||||
|
@ -1,4 +1,3 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
@ -22,6 +21,9 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
||||
public long Timeout = 8000;
|
||||
public long Interval = 100;
|
||||
|
||||
/// <summary>The amount of bytes sent since the last clear interval from Peer</summary>
|
||||
public int BytesSent;
|
||||
|
||||
public ConnectionManager(Peer peer) {
|
||||
Peer = peer;
|
||||
UpdateThread = new Thread(new ThreadStart(UpdateThreadMethod));
|
||||
@ -163,6 +165,7 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
||||
if (conn.Status == ConnectionStatus.Lost) {
|
||||
return;
|
||||
}
|
||||
BytesSent += buffer.Size;
|
||||
byte[] bytes = buffer.Pack();
|
||||
Peer.ListenerThread.LastSentConnection = conn;
|
||||
Peer.UdpClient.Send(bytes, bytes.Length, conn.Endpoint);
|
||||
|
@ -17,6 +17,9 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
||||
|
||||
private static int[] CONN_LOST_CODES = new int[] { 10054, 10051 };
|
||||
|
||||
/// <summary>The amount of bytes received since the last clear interval from Peer</summary>
|
||||
public int ReceivedBytes;
|
||||
|
||||
public ListenerThread(Peer peer, IPEndPoint endpoint) {
|
||||
EndPoint = endpoint;
|
||||
Peer = peer;
|
||||
@ -61,6 +64,7 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
||||
}
|
||||
if (Buffer.ReadFingerprint(Peer.Fingerprint)) {
|
||||
Peer.ConnectionManager.Handle(Listened, Buffer);
|
||||
ReceivedBytes += Buffer.Size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
@ -31,6 +30,12 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
||||
ConnectionManager.Interval = value;
|
||||
}
|
||||
}
|
||||
/// <summary>The interval of traffic analyzed before updating</summary>
|
||||
public long TrafficDataInterval;
|
||||
/// <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>Whether the Peer is currently doing anything or not.null</sumary>
|
||||
public bool Running { get; private set; }
|
||||
|
||||
@ -41,11 +46,14 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
||||
/// <summary>Listener for messages and errors from within the Peer.</summary>
|
||||
public PeerMessageListener MessageListener;
|
||||
|
||||
private long LastTrafficData;
|
||||
|
||||
/// <summary>Creates a new Peer with the given fingerprint. The fingerprint can be anything, it just must be same on both peers.</summary>
|
||||
public Peer(byte[] fingerprint) {
|
||||
Fingerprint = fingerprint;
|
||||
ConnectionManager = new ConnectionManager(this);
|
||||
MessageListener = this;
|
||||
LastTrafficData = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
}
|
||||
|
||||
/// <summary>Starts the UdpClient, but does no networking as is.</summary>
|
||||
@ -150,6 +158,19 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
||||
/// <summary>Shorthand for Peer.ConnectionManager.Update(): Handles network stuff that was received since last update.</summary>
|
||||
public void Update() {
|
||||
ConnectionManager.Update();
|
||||
|
||||
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
if (now - LastTrafficData > TrafficDataInterval) {
|
||||
LastTrafficData = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
if (ListenerThread != null) {
|
||||
TrafficReceived = ListenerThread.ReceivedBytes;
|
||||
ListenerThread.ReceivedBytes = 0;
|
||||
}
|
||||
if (ConnectionManager != null) {
|
||||
TrafficSent = ConnectionManager.BytesSent;
|
||||
ConnectionManager.BytesSent = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Message(string msg) { }
|
||||
|
Loading…
Reference in New Issue
Block a user