using System.Net.WebSockets; using Common; namespace LocalServer; public class LocalSyncServer { #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 public StateHelpBase StateHelper; public Config? SyncConfig; public Config NotNullSyncConfig { get { if (SyncConfig == null) { throw new ArgumentNullException("SyncConfig"); } return SyncConfig; } } public string Name; /// /// 发布源连接 /// public readonly AbsPipeLine LocalPipe; public readonly AbsPipeLine RemotePipe = new WebSocPipeLine( new ClientWebSocket(),false ); /// /// 父工程,用于释放资源 /// public readonly LocalSyncServerFactory Factory; public LocalSyncServer(AbsPipeLine pipe, LocalSyncServerFactory factory,string name) { LocalPipe = pipe; Factory = factory; StateHelper = new ConnectAuthorityHelper(this); Name = name; Task.Run(async () => { var rs = LocalPipe.Work( (byte[] b) => { return StateHelper.ReceiveLocalMsg(b); } ); try { await foreach (var r in rs) { } } catch (Exception e) { Close(e.Message); } }); } public void Close(string? CloseReason) { try { LocalPipe.Close(CloseReason); RemotePipe.Close(CloseReason); } catch (Exception e) { //TODO 日志 Console.WriteLine(e.Message); } finally { Factory.RemoveLocalSyncServer(this); } } }