diff --git a/src/Luban.ClientServer/Program.cs b/src/Luban.ClientServer/Program.cs index 467548c..6926137 100644 --- a/src/Luban.ClientServer/Program.cs +++ b/src/Luban.ClientServer/Program.cs @@ -162,7 +162,7 @@ Options: } StringTemplateUtil.AddTemplateSearchPath(FileUtil.GetPathRelateApplicationDirectory("Templates")); - GenServer.Ins.Start(options.Port, ProtocolStub.Factories); + GenServer.Ins.Start(true, options.Port, ProtocolStub.Factories); GenServer.Ins.RegisterJob("cfg", new Luban.Job.Cfg.JobController()); GenServer.Ins.RegisterJob("proto", new Luban.Job.Proto.JobController()); diff --git a/src/Luban.Server.Common/Source/IAgent.cs b/src/Luban.Server.Common/Source/IAgent.cs new file mode 100644 index 0000000..2d68c34 --- /dev/null +++ b/src/Luban.Server.Common/Source/IAgent.cs @@ -0,0 +1,27 @@ +using Luban.Common.Protos; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace Luban.Server.Common +{ + public interface IAgent + { + Task GetFromCacheOrReadAllBytesAsync(string file, string md5); + + Task ReadAllBytesAsync(string file); + + Task GetFileOrDirectoryAsync(string file, params string[] searchPatterns); + + Task QueryFileExistsAsync(QueryFilesExistsArg arg); + + Task OpenXmlAsync(string xmlFile); + + void Error(string fmt, params object[] objs); + + void Info(string fmt, params object[] objs); + } +} diff --git a/src/Luban.Server.Common/Source/LocalAgent.cs b/src/Luban.Server.Common/Source/LocalAgent.cs new file mode 100644 index 0000000..a29e296 --- /dev/null +++ b/src/Luban.Server.Common/Source/LocalAgent.cs @@ -0,0 +1,24 @@ +using Bright.Net.ServiceModes.Managers; +using Luban.Common.Protos; +using Luban.Common.Utils; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace Luban.Server.Common +{ + public class LocalAgent : RemoteAgent + { + public LocalAgent(SessionBase session) : base(session) + { + } + + public override Task ReadAllBytesAsync(string file) + { + return FileUtil.ReadAllBytesAsync(file); + } + } +} diff --git a/src/Luban.Server.Common/Source/RemoteAgent.cs b/src/Luban.Server.Common/Source/RemoteAgent.cs index 8694245..0a2477c 100644 --- a/src/Luban.Server.Common/Source/RemoteAgent.cs +++ b/src/Luban.Server.Common/Source/RemoteAgent.cs @@ -31,22 +31,19 @@ namespace Luban.Server.Common } } - public class RemoteAgent + public class RemoteAgent : IAgent { private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger(); public SessionBase Session { get; } - private readonly bool _trace; - private readonly ConcurrentDictionary> _remoteReadAllBytesTasks = new(); private readonly ConcurrentDictionary> _getImportFileOrDirTasks = new(); - public RemoteAgent(SessionBase session, bool trace) + public RemoteAgent(SessionBase session) { Session = session; - _trace = trace; } private const int GET_INPUT_FILE_TIMEOUT = 10; @@ -63,7 +60,7 @@ namespace Luban.Server.Common return content; } - public Task ReadAllBytesAsync(string file) + public virtual Task ReadAllBytesAsync(string file) { return _remoteReadAllBytesTasks.GetOrAdd(file, f => { @@ -129,7 +126,6 @@ namespace Luban.Server.Common } } - #region log public void Error(string fmt, params object[] objs) @@ -137,43 +133,15 @@ namespace Luban.Server.Common Log("error", string.Format(fmt, objs)); } - public void Error(Exception e, string s) - { - LogException(e, s); - } - - public void Error(Exception e, string fmt, params object[] objs) - { - LogException(e, string.Format(fmt, objs)); - } - public void Info(string fmt, params object[] objs) { Log("info", string.Format(fmt, objs)); } - public void Info(string s) - { - Log("info", s); - } - - public void Trace(string fmt, params object[] objs) - { - if (_trace) - { - Log("trace", string.Format(fmt, objs)); - } - } - private void Log(string level, string content) { Session.Send(new PushLog() { Level = level, LogContent = content }); } - - private void LogException(Exception e, string content) - { - Session.Send(new PushException() { LogContent = content, Message = e.Message, StackTrace = e.StackTrace }); - } #endregion } } diff --git a/src/Luban.Server/Source/GenServer.cs b/src/Luban.Server/Source/GenServer.cs index 00ffa2a..59ba72f 100644 --- a/src/Luban.Server/Source/GenServer.cs +++ b/src/Luban.Server/Source/GenServer.cs @@ -33,15 +33,18 @@ namespace Luban.Server private readonly Dictionary _jobs = new Dictionary(); - public void Start(int port, Dictionary factories) + private bool _localServer; + + public void Start(bool localhost, int port, Dictionary factories) { + _localServer = localhost; _handlers.Add(GetOutputFile.ID, (s, p) => OnGetOutputFile(s, (GetOutputFile)p)); _handlers.Add(GenJob.ID, (s, p) => OnGenJob(s, (GenJob)p)); var worker = new EventLoopGroup(4, 16); var server = new TcpServerBootstrap { - LocalAddress = new IPEndPoint(IPAddress.Any, port), + LocalAddress = new IPEndPoint(localhost ? IPAddress.Loopback : IPAddress.Any, port), ChildrenEventLoopGroup = worker, EventLoop = worker.ChooseEventLoop(), @@ -102,7 +105,7 @@ namespace Luban.Server if (_jobs.TryGetValue(rpc.Arg.JobType, out var jobController)) { - _ = jobController.GenAsync(new RemoteAgent(session, rpc.Arg.Verbose), rpc); + _ = jobController.GenAsync(_localServer ? new LocalAgent(session) : new RemoteAgent(session), rpc); } else { diff --git a/src/Luban.Server/Source/Program.cs b/src/Luban.Server/Source/Program.cs index 1690f4c..da764b5 100644 --- a/src/Luban.Server/Source/Program.cs +++ b/src/Luban.Server/Source/Program.cs @@ -59,7 +59,7 @@ namespace Luban.Server System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); - GenServer.Ins.Start(options.Port, ProtocolStub.Factories); + GenServer.Ins.Start(false, options.Port, ProtocolStub.Factories); GenServer.Ins.RegisterJob("cfg", new Luban.Job.Cfg.JobController()); GenServer.Ins.RegisterJob("proto", new Luban.Job.Proto.JobController());