using Quartz;
|
using SuperSocket;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.Server
|
{
|
[DisallowConcurrentExecution]//此特性标识 必须等待这次任务执行完成后,才能执行下次任务
|
internal class ServiceJob : IJob
|
{
|
private static List<IServer> _appServers = new List<IServer>();//所有的服务
|
public Task Execute(IJobExecutionContext context)
|
{
|
|
return Task.Run(() =>
|
{
|
try
|
{
|
//客户标识列表
|
var corpIds = CorpHelper.GetCorpIds();
|
if (corpIds == null || corpIds.Count < 1)
|
{
|
LogHelper.Info($"Socket数据对接任务管理器中,未检索到需要对接的客户标识信息!");
|
CancelServers();
|
return;
|
}
|
|
//数据对接配置列表
|
var service_configure = new Service.DataDockingConfigure();
|
var configure_list = service_configure.GetByCorpIds(corpIds);
|
if (configure_list == null || configure_list.Count < 1)
|
{
|
LogHelper.Info($"Socket数据对接任务管理器中,未检索到数据对接配置!");
|
CancelServers();
|
return;
|
}
|
configure_list = configure_list.Where(x => x.DockingMode == Model.DataDockingConfigure.eDockingMode.Socket).ToList();
|
if (configure_list == null || configure_list.Count < 1)
|
{
|
LogHelper.Info($"Socket数据对接任务管理器中,未检索到Socket数据对接配置!");
|
CancelServers();
|
return;
|
}
|
configure_list = configure_list.Where(x => x.UseStatus == Model.eUseStatus.Enable).ToList();
|
if (configure_list == null || configure_list.Count < 1)
|
{
|
LogHelper.Info($"Socket数据对接任务管理器中,未检索到有效的Socket数据对接配置!");
|
CancelServers();
|
return;
|
}
|
var configure_socket_list = configure_list.Select(x => new Model.DataDockingConfigureExSocket(x)).ToList(); ;
|
PackageHandleHelper.ConfigureList = configure_socket_list;
|
|
//需要开启的端口
|
var port_list = configure_socket_list.Select(x => x.ConfigureParas.ServerPort).Distinct().ToList();
|
|
//循环开启服务
|
port_list.ForEach(async x =>
|
{
|
var server = _appServers.Find(t => t.Options.Listeners.First().Port == x);
|
if (server == null)
|
{
|
var host = SuperSocketHostBuilder.Create<PackageInfo>();
|
host.UsePipelineFilterFactory<MyPipeLineFilterFactory>();
|
host.UseSession<MySession>();
|
host.UseHostedService<MyServer>();
|
host.ConfigureSuperSocket(options =>
|
{
|
options.MaxPackageLength = 1000000;
|
options.ReceiveBufferSize = 1000000;
|
options.SendBufferSize = 1000000;
|
options.Listeners = new List<ListenOptions>()
|
{
|
new ListenOptions(){ Ip="Any",Port=x}
|
};
|
});
|
host.UsePackageHandler(new PackageHandleHelper().Handle);
|
server = host.BuildAsServer();
|
await server.StartAsync();
|
_appServers.Add(server);
|
LogHelper.Info(string.Format("Socket服务,端口:{0} 开启!", x));
|
}
|
});
|
|
//循环取消服务
|
_appServers.ToList().ForEach(async x =>
|
{
|
if (!port_list.Contains(x.Options.Listeners.First().Port))
|
{
|
await x.StopAsync();
|
_appServers.Remove(x);
|
LogHelper.Info(string.Format("Socket服务,端口:{0} 关闭!", x));
|
}
|
});
|
LogHelper.Info($"Socket数据对接服务管理器中,服务开启数量为{_appServers.Count}!");
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error("Socket数据对接服务管理器中,任务执行出错!", ex);
|
var e = new JobExecutionException(ex);
|
throw e;
|
}
|
});
|
}
|
|
|
|
/// <summary>
|
/// 取消所有服务
|
/// </summary>
|
public static void CancelServers()
|
{
|
if (_appServers == null || _appServers.Count < 1)
|
return;
|
_appServers.ForEach(async x =>await x.StopAsync());
|
_appServers.Clear();
|
}
|
|
}
|
}
|