NoSugarNet/NoSugarNet.ClientCore/AppNoSugarNet.cs

91 lines
3.8 KiB
C#
Raw Normal View History

2024-06-25 18:23:08 +08:00
using NoSugarNet.Adapter.DataHelper;
using NoSugarNet.ClientCore.Common;
using NoSugarNet.ClientCore.Manager;
2024-01-22 11:56:02 +08:00
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;
2024-06-25 14:35:44 +08:00
public static AppForwardLocalClient forwardlocal;
2024-06-25 18:23:08 +08:00
public static AppReverseLocalClient reverselocal;
2024-01-22 11:56:02 +08:00
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-06-25 18:23:08 +08:00
public static void Init(Dictionary<byte, TunnelClientData> cfgs, int compressAdapterType = 0,OnLogHandler onLog = null)
2024-01-22 11:56:02 +08:00
{
2024-06-25 18:23:08 +08:00
Config.cfgs = cfgs;
Config.compressAdapterType = (E_CompressAdapter)compressAdapterType;
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();
2024-06-25 14:35:44 +08:00
forwardlocal = new AppForwardLocalClient();
2024-06-25 18:23:08 +08:00
reverselocal = new AppReverseLocalClient(Config.compressAdapterType);
2024-01-22 11:56:02 +08:00
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()
{
2024-06-25 14:35:44 +08:00
forwardlocal.StopAll();
2024-04-15 15:16:10 +08:00
networkHelper.CloseConntect();
AppNoSugarNet.log.Info("停止");
_SpeedCheckTimeTimer.Enabled = false;
}
static void Checktimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
2024-06-25 14:35:44 +08:00
forwardlocal.GetCurrLenght(out long resultReciveAllLenght, out long resultSendAllLenght);
forwardlocal.GetClientCount(out int ClientUserCount, out int TunnelCount);
2024-04-08 16:20:37 +08:00
NetStatus resutnetStatus = new NetStatus()
{
TunnelCount = TunnelCount,
ClientUserCount = ClientUserCount,
srcSendAllLenght = resultSendAllLenght,
srcReciveAllLenght = resultReciveAllLenght,
srcReciveSecSpeed = (resultReciveAllLenght - netStatus.srcReciveAllLenght) / (TimerInterval / 1000),
srcSendSecSpeed = (resultSendAllLenght - netStatus.srcSendAllLenght) / (TimerInterval / 1000),
2024-06-25 14:35:44 +08:00
tSendAllLenght = forwardlocal.tSendAllLenght,
tReciveAllLenght = forwardlocal.tReciveAllLenght,
tSendSecSpeed = (forwardlocal.tSendAllLenght - netStatus.tSendAllLenght) / (TimerInterval / 1000),
tReciveSecSpeed = (forwardlocal.tReciveAllLenght - netStatus.tReciveAllLenght) / (TimerInterval / 1000),
2024-04-08 16:20:37 +08:00
};
netStatus = resutnetStatus;
OnUpdateStatus?.Invoke(resutnetStatus);
2024-01-22 11:56:02 +08:00
}
}
}