NoSugarNet/NoSugarNet.ClientCore/AppNoSugarNet.cs

84 lines
3.3 KiB
C#
Raw Normal View History

2024-01-22 11:56:02 +08:00
using NoSugarNet.ClientCore.Manager;
using NoSugarNet.ClientCore.Network;
using ServerCore.Manager;
2024-04-15 11:30:28 +08:00
using static NoSugarNet.ClientCore.Manager.LogManager;
2024-01-22 11:56:02 +08:00
namespace NoSugarNet.ClientCore
{
public class AppNoSugarNet
{
public static string TokenStr;
public static long RID = -1;
public static string IP;
public static int Port;
public static LogManager log;
public static NetworkHelper networkHelper;
public static AppLogin login;
public static AppChat chat;
public static AppLocalClient local;
public static UserDataManager user;
public static System.Timers.Timer _SpeedCheckTimeTimer;//速度检测计时器
public static int TimerInterval = 1000;//计时器间隔
2024-04-08 16:20:37 +08:00
static NetStatus netStatus;
#region
2024-04-08 16:20:37 +08:00
public delegate void OnUpdateStatusHandler(NetStatus Status);
public static event OnUpdateStatusHandler OnUpdateStatus;
#endregion
2024-01-22 11:56:02 +08:00
2024-04-15 15:16:10 +08:00
public static void Init(OnLogHandler onLog = null)
2024-01-22 11:56:02 +08:00
{
log = new LogManager();
2024-04-15 11:30:28 +08:00
if(onLog != null)
LogManager.OnLog += onLog;
2024-01-22 11:56:02 +08:00
networkHelper = new NetworkHelper();
login = new AppLogin();
chat = new AppChat();
local = new AppLocalClient();
user = new UserDataManager();
2024-04-08 16:20:37 +08:00
netStatus = new NetStatus();
2024-04-15 15:16:10 +08:00
_SpeedCheckTimeTimer = new System.Timers.Timer();
_SpeedCheckTimeTimer.Interval = TimerInterval;
_SpeedCheckTimeTimer.Elapsed += Checktimer_Elapsed;
_SpeedCheckTimeTimer.AutoReset = true;
}
public static void Connect(string IP, int port)
{
2024-04-15 11:30:28 +08:00
if (networkHelper.Init(IP, port))
2024-04-15 15:16:10 +08:00
_SpeedCheckTimeTimer.Enabled = true;
else
_SpeedCheckTimeTimer.Enabled = false;
}
public static void Close()
{
local.StopAll();
networkHelper.CloseConntect();
AppNoSugarNet.log.Info("停止");
_SpeedCheckTimeTimer.Enabled = false;
}
static void Checktimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
local.GetCurrLenght(out long resultReciveAllLenght, out long resultSendAllLenght);
2024-04-08 16:20:37 +08:00
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);
2024-01-22 11:56:02 +08:00
}
}
}