2024-07-30 07:36:42 +00:00
|
|
|
using System.Net.WebSockets;
|
2024-07-31 09:14:07 +00:00
|
|
|
using Common;
|
2024-07-30 07:36:42 +00:00
|
|
|
|
|
|
|
namespace LocalServer;
|
|
|
|
|
|
|
|
public class LocalSyncServer
|
|
|
|
{
|
2024-09-05 01:59:57 +00:00
|
|
|
#pragma warning disable CA2211 // Non-constant fields should not be visible
|
|
|
|
public static string TempRootFile = "C:/TempPack";
|
|
|
|
#pragma warning restore CA2211 // Non-constant fields should not be visible
|
2024-07-31 09:14:07 +00:00
|
|
|
public StateHelpBase StateHelper;
|
|
|
|
|
|
|
|
public Config? SyncConfig;
|
|
|
|
|
2024-09-05 09:48:08 +00:00
|
|
|
public Config NotNullSyncConfig
|
|
|
|
{
|
|
|
|
get
|
2024-09-05 01:59:57 +00:00
|
|
|
{
|
2024-09-05 09:48:08 +00:00
|
|
|
if (SyncConfig == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("SyncConfig");
|
|
|
|
}
|
|
|
|
return SyncConfig;
|
2024-09-05 01:59:57 +00:00
|
|
|
}
|
2024-09-05 09:48:08 +00:00
|
|
|
}
|
2024-09-05 01:59:57 +00:00
|
|
|
|
2024-09-07 06:46:08 +00:00
|
|
|
public string Name;
|
|
|
|
|
2024-07-31 09:14:07 +00:00
|
|
|
/// <summary>
|
|
|
|
/// 发布源连接
|
|
|
|
/// </summary>
|
2024-09-05 09:48:08 +00:00
|
|
|
public readonly AbsPipeLine LocalPipe;
|
2024-07-31 09:14:07 +00:00
|
|
|
|
2024-09-05 09:48:08 +00:00
|
|
|
public readonly AbsPipeLine RemotePipe = new WebSocPipeLine<ClientWebSocket>(
|
2024-09-07 06:46:08 +00:00
|
|
|
new ClientWebSocket(),false
|
2024-09-05 09:48:08 +00:00
|
|
|
);
|
2024-07-31 09:14:07 +00:00
|
|
|
/// <summary>
|
|
|
|
/// 父工程,用于释放资源
|
|
|
|
/// </summary>
|
2024-07-30 07:36:42 +00:00
|
|
|
public readonly LocalSyncServerFactory Factory;
|
|
|
|
|
2024-09-07 06:46:08 +00:00
|
|
|
public LocalSyncServer(AbsPipeLine pipe, LocalSyncServerFactory factory,string name)
|
2024-07-30 07:36:42 +00:00
|
|
|
{
|
2024-09-05 09:48:08 +00:00
|
|
|
LocalPipe = pipe;
|
2024-07-30 07:36:42 +00:00
|
|
|
Factory = factory;
|
2024-09-05 01:59:57 +00:00
|
|
|
StateHelper = new ConnectAuthorityHelper(this);
|
2024-09-07 06:46:08 +00:00
|
|
|
Name = name;
|
2024-07-31 09:14:07 +00:00
|
|
|
|
2024-09-05 09:48:08 +00:00
|
|
|
Task.Run(async () =>
|
2024-09-05 01:59:57 +00:00
|
|
|
{
|
2024-09-05 09:48:08 +00:00
|
|
|
var rs = LocalPipe.Work(
|
|
|
|
(byte[] b) =>
|
2024-09-05 01:59:57 +00:00
|
|
|
{
|
2024-09-05 09:48:08 +00:00
|
|
|
return StateHelper.ReceiveLocalMsg(b);
|
2024-09-05 01:59:57 +00:00
|
|
|
}
|
2024-09-05 09:48:08 +00:00
|
|
|
);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await foreach (var r in rs) { }
|
2024-09-05 01:59:57 +00:00
|
|
|
}
|
2024-09-05 09:48:08 +00:00
|
|
|
catch (Exception e)
|
2024-09-05 01:59:57 +00:00
|
|
|
{
|
2024-09-05 09:48:08 +00:00
|
|
|
Close(e.Message);
|
2024-09-05 01:59:57 +00:00
|
|
|
}
|
2024-09-05 09:48:08 +00:00
|
|
|
});
|
2024-07-30 07:36:42 +00:00
|
|
|
}
|
2024-07-31 09:14:07 +00:00
|
|
|
|
|
|
|
|
2024-09-05 01:59:57 +00:00
|
|
|
public void Close(string? CloseReason)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-09-05 09:48:08 +00:00
|
|
|
LocalPipe.Close(CloseReason);
|
|
|
|
RemotePipe.Close(CloseReason);
|
2024-09-05 01:59:57 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
//TODO 日志
|
|
|
|
Console.WriteLine(e.Message);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Factory.RemoveLocalSyncServer(this);
|
|
|
|
}
|
|
|
|
}
|
2024-07-30 07:36:42 +00:00
|
|
|
}
|