初步重构 Contollers

This commit is contained in:
ChsBuffer
2020-07-09 05:00:56 +08:00
parent f605bfa437
commit 3c0a0d5f80
20 changed files with 592 additions and 1101 deletions

View File

@@ -0,0 +1,101 @@
using System;
using System.Diagnostics;
using System.IO;
using Netch.Models;
using Netch.Utils;
namespace Netch.Controllers
{
public class Controller
{
protected string[] ExtFiles;
/// <summary>
/// 进程实例
/// </summary>
public Process Instance;
/// <summary>
/// 程序名
/// </summary>
public string MainName;
public bool ready;
/// <summary>
/// 当前状态
/// </summary>
protected State State = State.Waiting;
/// <summary>
/// 停止
/// </summary>
public void Stop()
{
try
{
if (Instance == null || Instance.HasExited) return;
Instance.Kill();
Instance.WaitForExit();
}
catch (Exception e)
{
Logging.Info(e.ToString());
}
}
/// <summary>
/// 杀残留进程,清除日志,检查文件
/// </summary>
/// <returns></returns>
protected bool BeforeStartProgress()
{
var result = false;
// 杀残留
MainController.KillProcessByName(MainName);
// 清日志
try
{
if (File.Exists($"logging\\{MainName}.log")) File.Delete($"logging\\{MainName}.log");
}
catch (Exception)
{
// ignore
}
// 检查文件
if (!File.Exists($"bin\\{MainName}.exe")) Logging.Info($"bin\\{MainName}.exe 文件不存在");
if (ExtFiles == null)
result = true;
else
foreach (var f in ExtFiles)
if (!File.Exists($"bin\\{f}"))
Logging.Info($"bin\\{f}.exe 文件不存在");
if (!ready) Logging.Info(MainName + "未能就绪");
return result;
}
/// <summary>
/// 写日志
/// </summary>
/// <param name="e"></param>
/// <returns>e是否为空</returns>
public bool WriteLog(DataReceivedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Data)) return false;
try
{
File.AppendAllText($"logging\\{MainName}.log", $@"{e.Data}{Global.EOF}");
}
catch (Exception exception)
{
Logging.Info($"写入{MainName}日志失败" + exception);
}
return true;
}
}
}

View File

@@ -0,0 +1,32 @@
using System.Diagnostics;
using Netch.Models;
namespace Netch.Controllers
{
public abstract class ServerClient : Controller
{
/// <summary>
/// 启动
/// </summary>
/// <param name="server">服务器</param>
/// <param name="mode">模式</param>
/// <returns>是否启动成功</returns>
public abstract bool Start(Server server, Mode mode);
/// <summary>
/// ServerClient 停止
/// <param />
/// 注意 对象类型 以调用子类 Stop() 方法
/// </summary>
public new void Stop()
{
base.Stop();
// SSController Stop()
// 不能自动转换对象类型的兼容代码 :(
if (Global.Settings.BootShadowsocksFromDLL) NativeMethods.Shadowsocks.Stop();
}
public abstract void OnOutputDataReceived(object sender, DataReceivedEventArgs e);
}
}