网络计数

This commit is contained in:
sin365 2024-04-08 16:20:37 +08:00
parent d65619db43
commit a86cd5b1f6
5 changed files with 56 additions and 17 deletions

View File

@ -18,9 +18,10 @@ namespace NoSugarNet.ClientCore
public static UserDataManager user;
public static System.Timers.Timer _SpeedCheckTimeTimer;//速度检测计时器
public static int TimerInterval = 1000;//计时器间隔
static NetStatus netStatus;
#region
public delegate void OnUpdateStatusHandler(long resultReciveAllLenght, long resultSendAllLenght);
public delegate void OnUpdateStatusHandler(NetStatus Status);
public static event OnUpdateStatusHandler OnUpdateStatus;
#endregion
@ -32,6 +33,7 @@ namespace NoSugarNet.ClientCore
chat = new AppChat();
local = new AppLocalClient();
user = new UserDataManager();
netStatus = new NetStatus();
networkHelper.Init(IP, port);
_SpeedCheckTimeTimer = new System.Timers.Timer();
@ -44,7 +46,24 @@ namespace NoSugarNet.ClientCore
static void Checktimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
local.GetCurrLenght(out long resultReciveAllLenght, out long resultSendAllLenght);
OnUpdateStatus?.Invoke(resultReciveAllLenght, resultSendAllLenght);
local.GetClientCount(out int ClientUserCount, out int TunnelCount);
NetStatus resutnetStatus = new NetStatus()
{
TunnelCount = TunnelCount,
ClientUserCount = ClientUserCount,
srcSendAllLenght = resultSendAllLenght,
srcReciveAllLenght = resultReciveAllLenght,
srcReciveSecSpeed = (resultReciveAllLenght - netStatus.srcReciveAllLenght) / (TimerInterval / 1000),
srcSendSecSpeed = (resultSendAllLenght - netStatus.srcSendAllLenght) / (TimerInterval / 1000),
tSendAllLenght = local.tSendAllLenght,
tReciveAllLenght = local.tReciveAllLenght,
tSendSecSpeed = (local.tSendAllLenght - netStatus.tSendAllLenght) / (TimerInterval / 1000),
tReciveSecSpeed = (local.tReciveAllLenght - netStatus.tReciveAllLenght) / (TimerInterval / 1000),
};
netStatus = resutnetStatus;
OnUpdateStatus?.Invoke(resutnetStatus);
}
}
}

View File

@ -17,6 +17,9 @@ namespace ServerCore.Manager
E_CompressAdapter compressAdapterType;
public LocalMsgQueuePool _localMsgPool = new LocalMsgQueuePool(1000);
public long tReciveAllLenght { get; private set; }
public long tSendAllLenght { get; private set; }
public AppLocalClient()
{
//注册网络消息
@ -33,11 +36,19 @@ namespace ServerCore.Manager
byte[] Keys = mDictTunnelID2Listeners.Keys.ToArray();
for (int i = 0; i < Keys.Length; i++)
{
resultReciveAllLenght += mDictTunnelID2Listeners[Keys[i]].mReciveAllLenght;
resultSendAllLenght += mDictTunnelID2Listeners[Keys[i]].mSendAllLenght;
//local和转发 收发相反
resultSendAllLenght += mDictTunnelID2Listeners[Keys[i]].mReciveAllLenght;
resultReciveAllLenght += mDictTunnelID2Listeners[Keys[i]].mSendAllLenght;
}
}
public void GetClientCount(out int ClientUserCount, out int TunnelCount)
{
TunnelCount = mDictTunnelID2Listeners.Count;
ClientUserCount = mDictTunnelID2Listeners.Count;
}
/// <summary>
/// 初始化连接,先获取到配置
/// </summary>
@ -241,6 +252,8 @@ namespace ServerCore.Manager
//AppNoSugarNet.log.Debug($"OnServerLocalDataCallBack {tunnelId},{Idx},Data长度{data.Length}");
if (!GetLocalListener(tunnelId, out LocalListener _listener))
return;
//记录压缩前数据长度
tReciveAllLenght += data.Length;
//解压
data = mCompressAdapter.Decompress(data);
_listener.SendSocketByIdx(Idx,data);
@ -286,6 +299,8 @@ namespace ServerCore.Manager
{
//压缩
data = mCompressAdapter.Compress(data);
//记录压缩后数据长度
tSendAllLenght += data.Length;
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_C2S_DATA()
{

View File

@ -45,8 +45,9 @@ namespace ServerCore.Manager
long[] Keys = mDictCommKey2ServerLocalClients.Keys.ToArray();
for(int i =0; i < Keys.Length; i++)
{
resultReciveAllLenght += mDictCommKey2ServerLocalClients[Keys[i]].mReciveAllLenght;
resultSendAllLenght += mDictCommKey2ServerLocalClients[Keys[i]].mSendAllLenght;
//local和转发 收发相反
resultSendAllLenght += mDictCommKey2ServerLocalClients[Keys[i]].mReciveAllLenght;
resultReciveAllLenght += mDictCommKey2ServerLocalClients[Keys[i]].mSendAllLenght;
}
}
@ -284,7 +285,7 @@ namespace ServerCore.Manager
if (!GetServerLocalClient(uid, tunnelId, Idx, out ServerLocalClient serverLocalClient))
return;
//记录数据长度
tSendAllLenght += data.Length;
tReciveAllLenght += data.Length;
//解压
data = mCompressAdapter.Decompress(data);
//记录数据长度
@ -336,11 +337,10 @@ namespace ServerCore.Manager
if (!ServerManager.g_ClientMgr.GetClientByUID(uid, out ClientInfo client))
return;
//压缩
data = mCompressAdapter.Compress(data);
//记录压缩后数据长度
tReciveAllLenght += data.Length;
tSendAllLenght += data.Length;
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_S2C_DATA()
{

View File

@ -20,11 +20,16 @@ namespace NoSugarNet.ClientCli
Console.ReadLine();
}
}
static void OnUpdateStatus(long resultReciveAllLenght, long resultSendAllLenght)
static void OnUpdateStatus(NetStatus netState)
{
Console.Title = $"{Title} Recive:{resultReciveAllLenght} Send:{resultSendAllLenght}";
Console.WriteLine($"{Title} Recive:{resultReciveAllLenght} Send:{resultSendAllLenght}");
//string info = $"User:{netState.ClientUserCount} Tun:{netState.TunnelCount} rec:{netState.srcReciveAllLenght}|{netState.tReciveAllLenght} {ConvertBytesToKilobytes(netState.srcReciveSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tReciveSecSpeed)}K/s send:{netState.srcSendAllLenght}|{netState.tSendAllLenght} {ConvertBytesToKilobytes(netState.srcSendSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tSendSecSpeed)}K/s";
string info = $"User:{netState.ClientUserCount} Tun:{netState.TunnelCount} rec: {ConvertBytesToKilobytes(netState.srcReciveSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tReciveSecSpeed)}K/s send: {ConvertBytesToKilobytes(netState.srcSendSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tSendSecSpeed)}K/s";
Console.Title = Title + info;
Console.WriteLine(info);
}
static string ConvertBytesToKilobytes(long bytes)
{
return Math.Round((double)bytes / 1024, 2).ToString("F2");
}
}
}

View File

@ -40,14 +40,14 @@ namespace NoSugarNet.ServerCli
static void OnUpdateStatus(NetStatus netState)
{
string info = $"User:{netState.ClientUserCount} Tun:{netState.TunnelCount} rec:{netState.srcReciveAllLenght}|{netState.tReciveAllLenght} {ConvertBytesToKilobytes(netState.srcReciveSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tReciveSecSpeed)}K/s send:{netState.srcSendAllLenght}|{netState.tSendAllLenght} {ConvertBytesToKilobytes(netState.srcSendSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tSendSecSpeed)}K/s";
//string info = $"User:{netState.ClientUserCount} Tun:{netState.TunnelCount} rec:{netState.srcReciveAllLenght}|{netState.tReciveAllLenght} {ConvertBytesToKilobytes(netState.srcReciveSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tReciveSecSpeed)}K/s send:{netState.srcSendAllLenght}|{netState.tSendAllLenght} {ConvertBytesToKilobytes(netState.srcSendSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tSendSecSpeed)}K/s";
string info = $"User:{netState.ClientUserCount} Tun:{netState.TunnelCount} rec: {ConvertBytesToKilobytes(netState.srcReciveSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tReciveSecSpeed)}K/s send: {ConvertBytesToKilobytes(netState.srcSendSecSpeed)}K/s|{ConvertBytesToKilobytes(netState.tSendSecSpeed)}K/s";
Console.Title = Title + info;
Console.WriteLine(info);
}
private static double ConvertBytesToKilobytes(long bytes)
static string ConvertBytesToKilobytes(long bytes)
{
return Math.Round((double)bytes / 1024, 2);
return Math.Round((double)bytes / 1024, 2).ToString("F2");
}
}
}