基本完成重构 Controller

This commit is contained in:
ChsBuffer
2020-07-11 19:30:35 +08:00
parent d2f886ef4b
commit 9f27c4bcf9
27 changed files with 806 additions and 774 deletions

View File

@@ -6,8 +6,18 @@ using Netch.Utils;
namespace Netch.Controllers
{
public class Controller
public abstract class Controller
{
/// <summary>
/// 控制器名
/// <param />
/// 未赋值会在 <see cref="InitCheck" /> 赋值为 <see cref="MainFile" />
/// </summary>
public string AkaName;
/// <summary>
/// 其他需要文件
/// </summary>
protected string[] ExtFiles;
/// <summary>
@@ -16,21 +26,26 @@ namespace Netch.Controllers
public Process Instance;
/// <summary>
/// 程序名
/// 程序名(不含扩展名)
/// </summary>
public string MainName;
public string MainFile;
public bool ready;
/// <summary>
/// 运行检查, 由 <see cref="InitCheck()" /> 赋值
/// </summary>
public bool Ready;
/// <summary>
/// 当前状态
/// </summary>
protected State State = State.Waiting;
public abstract void Stop();
/// <summary>
/// 停止
/// </summary>
public void Stop()
protected void StopInstance()
{
try
{
@@ -40,7 +55,7 @@ namespace Netch.Controllers
}
catch (Exception e)
{
Logging.Info(e.ToString());
Logging.Error($"停止 {MainFile}.exe 错误:\n" + e);
}
}
@@ -49,53 +64,87 @@ namespace Netch.Controllers
/// 杀残留进程,清除日志,检查文件
/// </summary>
/// <returns></returns>
protected bool BeforeStartProgress()
protected void InitCheck()
{
if (string.IsNullOrEmpty(AkaName)) AkaName = MainFile;
var result = false;
// 杀残留
MainController.KillProcessByName(MainName);
MainController.KillProcessByName(MainFile);
// 清日志
try
{
if (File.Exists($"logging\\{MainName}.log")) File.Delete($"logging\\{MainName}.log");
if (File.Exists($"logging\\{AkaName}.log")) File.Delete($"logging\\{AkaName}.log");
}
catch (Exception)
{
// ignore
// ignored
}
// 检查文件
if (!File.Exists($"bin\\{MainName}.exe")) Logging.Info($"bin\\{MainName}.exe 文件不存在");
var mainResult = true;
var extResult = true;
if (!string.IsNullOrEmpty(MainFile) && !File.Exists($"bin\\{MainFile}.exe"))
{
mainResult = false;
Logging.Error($"主程序 bin\\{MainFile}.exe 不存在");
}
if (ExtFiles == null)
result = true;
extResult = true;
else
foreach (var f in ExtFiles)
if (!File.Exists($"bin\\{f}"))
Logging.Info($"bin\\{f}.exe 文件不存在");
{
extResult = false;
Logging.Error($"附加文件 bin\\{f} 不存在");
}
if (!ready) Logging.Info(MainName + "未能就绪");
return result;
result = extResult && mainResult;
if (!result)
Logging.Error(AkaName + " 未就绪");
Ready = result;
}
/// <summary>
/// 写日志
/// </summary>
/// <param name="e"></param>
/// <returns>e是否为空</returns>
public bool WriteLog(DataReceivedEventArgs e)
/// <param name="std"></param>
/// <returns><see cref="std" />是否为空</returns>
protected bool WriteLog(DataReceivedEventArgs std)
{
if (string.IsNullOrWhiteSpace(e.Data)) return false;
if (string.IsNullOrWhiteSpace(std.Data)) return false;
try
{
File.AppendAllText($"logging\\{MainName}.log", $@"{e.Data}{Global.EOF}");
File.AppendAllText($"logging\\{AkaName}.log", $@"{std.Data}{Global.EOF}");
}
catch (Exception exception)
catch (Exception e)
{
Logging.Info($"写入{MainName}日志失败" + exception);
Logging.Error($"写入{AkaName}日志错误:\n" + e);
}
return true;
}
public static Process GetProcess(string path = null, bool redirectStd = true)
{
var p = new Process
{
StartInfo =
{
Arguments = "",
WorkingDirectory = $"{Global.NetchDir}\\bin",
CreateNoWindow = true,
RedirectStandardError = redirectStd,
RedirectStandardInput = redirectStd,
RedirectStandardOutput = redirectStd,
UseShellExecute = false
},
EnableRaisingEvents = true
};
if (path != null) p.StartInfo.FileName = Path.GetFullPath(path);
return p;
}
}
}