From a86cd5b1f6ae9153a15206302fd96eaace4d0b73 Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Mon, 8 Apr 2024 16:20:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E7=BB=9C=E8=AE=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NoSugarNet.ClientCore/AppNoSugarNet.cs | 23 +++++++++++++++++-- .../Manager/AppLocalClient.cs | 19 +++++++++++++-- .../Manager/LocalClientManager.cs | 10 ++++---- Sample/NoSugarNet.ClientCli/Program.cs | 13 +++++++---- Sample/NoSugarNet.ServerCli/Program.cs | 8 +++---- 5 files changed, 56 insertions(+), 17 deletions(-) diff --git a/NoSugarNet.ClientCore/AppNoSugarNet.cs b/NoSugarNet.ClientCore/AppNoSugarNet.cs index fe86ca2..07242f0 100644 --- a/NoSugarNet.ClientCore/AppNoSugarNet.cs +++ b/NoSugarNet.ClientCore/AppNoSugarNet.cs @@ -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); } } } \ No newline at end of file diff --git a/NoSugarNet.ClientCore/Manager/AppLocalClient.cs b/NoSugarNet.ClientCore/Manager/AppLocalClient.cs index 7c328a4..919fb58 100644 --- a/NoSugarNet.ClientCore/Manager/AppLocalClient.cs +++ b/NoSugarNet.ClientCore/Manager/AppLocalClient.cs @@ -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; + } + /// /// 初始化连接,先获取到配置 /// @@ -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() { diff --git a/NoSugarNet.ServerCore/Manager/LocalClientManager.cs b/NoSugarNet.ServerCore/Manager/LocalClientManager.cs index f00b9ec..08bed42 100644 --- a/NoSugarNet.ServerCore/Manager/LocalClientManager.cs +++ b/NoSugarNet.ServerCore/Manager/LocalClientManager.cs @@ -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() { diff --git a/Sample/NoSugarNet.ClientCli/Program.cs b/Sample/NoSugarNet.ClientCli/Program.cs index 79c3bd9..4d48f11 100644 --- a/Sample/NoSugarNet.ClientCli/Program.cs +++ b/Sample/NoSugarNet.ClientCli/Program.cs @@ -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"); } } - } \ No newline at end of file diff --git a/Sample/NoSugarNet.ServerCli/Program.cs b/Sample/NoSugarNet.ServerCli/Program.cs index 3f9414d..e16ab74 100644 --- a/Sample/NoSugarNet.ServerCli/Program.cs +++ b/Sample/NoSugarNet.ServerCli/Program.cs @@ -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"); } } } \ No newline at end of file