FileSqlServerSync/Server/LocalServer/LocalSyncServerFactory.cs

36 lines
830 B
C#
Raw Normal View History

using System.Net.WebSockets;
namespace LocalServer;
public class LocalSyncServerFactory
{
2024-07-31 09:14:07 +00:00
private readonly object Lock = new();
2024-09-05 01:59:57 +00:00
public void CreateLocalSyncServer(WebSocket socket, string name)
{
if (Servers.Select(x => x.Name == name).Any())
{
2024-09-05 01:59:57 +00:00
throw new Exception("LocalServer:存在同名发布源!");
}
2024-07-31 09:14:07 +00:00
var server = new LocalSyncServer(socket, name, this);
lock (Lock)
{
Servers.Add(server);
}
2024-09-05 01:59:57 +00:00
//脱离当前函数栈
Task.Run(async ()=>{
await server.LocalSocketListen();
});
}
private readonly List<LocalSyncServer> Servers = [];
public void RemoveLocalSyncServer(LocalSyncServer server)
{
2024-07-31 09:14:07 +00:00
lock (Lock)
{
Servers.Remove(server);
}
}
}