XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension

添加进程通讯客户端实例

b267bbe
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

5 个文件 +93 -75
Modified XFEExtension.NetCore.Analyzer.Test/Program.cs +10 -2
@@ -12,14 +12,17 @@ internal class Program
12 12 {
13 13 if (Console.ReadLine() == "s")
14 14 {
15 using var pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut);
15 using var pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 100);
16 using var pipeServer2 = new NamedPipeServerStream("testpipe2", PipeDirection.InOut, 100);
16 17 Console.WriteLine("Named Pipe Server waiting for connection...");
17 18 while (true)
18 19 {
19 20 await pipeServer.WaitForConnectionAsync();
21 Console.WriteLine("Client 1 Connected!");
22 await pipeServer2.WaitForConnectionAsync();
23 Console.WriteLine("Client 2 Connected!");
20 24 _ = Task.Run(async () =>
21 25 {
22 Console.WriteLine("Client Connected!");
23 26 using var reader = new StreamReader(pipeServer);
24 27 using var writer = new StreamWriter(pipeServer);
25 28 var message = await reader.ReadLineAsync();
@@ -51,8 +54,13 @@ internal class Program
51 54 else
52 55 {
53 56 using var pipeClient = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut);
57 using var pipeClient2 = new NamedPipeClientStream(".", "testpipe2", PipeDirection.InOut);
54 58 Console.WriteLine("Named Pipe Client connecting to server...");
55 59 await pipeClient.ConnectAsync();
60 Console.WriteLine("Server 1 Connected!");
61 await Task.Delay(3000);
62 await pipeClient2.ConnectAsync();
63 Console.WriteLine("Server 2 Connected!");
56 64 using var reader = new StreamReader(pipeClient);
57 65 using var writer = new StreamWriter(pipeClient);
58 66 string message = "Hello from client!";
Modified XFEExtension.NetCore/ProcessCommunication/Client/ProcessCommunicationClient.cs +2 -2
@@ -50,7 +50,7 @@ public class ProcessCommunicationClient
50 50 var tokenSource = new CancellationTokenSource(timeOut);
51 51 int retryTime = 0;
52 52 stopWatch.Start();
53 var authenticationClient = new NamedPipeClientStream(computerName, $"{serverName}-Authentication-{retryTime}");
53 var authenticationClient = new NamedPipeClientStream(computerName, $"{serverName}-{retryTime}");
54 54 while (true)
55 55 {
56 56 try
@@ -65,7 +65,7 @@ public class ProcessCommunicationClient
65 65 {
66 66 retryTime++;
67 67 await authenticationClient.DisposeAsync();
68 authenticationClient = new NamedPipeClientStream($"{serverName}-Authentication-{retryTime}");
68 authenticationClient = new NamedPipeClientStream($"{serverName}-{retryTime}");
69 69 }
70 70 else
71 71 {
Added XFEExtension.NetCore/ProcessCommunication/Server/PCClient.cs +26 -0
@@ -0,0 +1,26 @@
1 using System.IO.Pipes;
2 using XFEExtension.NetCore.ProcessCommunication.Client;
3
4 namespace XFEExtension.NetCore.ProcessCommunication.Server;
5
6 /// <summary>
7 /// 进程间通讯客户端实例
8 /// </summary>
9 /// <param name="clientInfo">客户端信息</param>
10 /// <param name="serverPipe">服务器管道</param>
11 /// <param name="isConnected">客户端是否连接</param>
12 public class PCClient(ProcessCommunicationClientInfo clientInfo, NamedPipeServerStream serverPipe, bool isConnected)
13 {
14 /// <summary>
15 /// 客户端信息
16 /// </summary>
17 public ProcessCommunicationClientInfo ClientInfo { get; set; } = clientInfo;
18 /// <summary>
19 /// 服务器通讯管道
20 /// </summary>
21 public NamedPipeServerStream ServerPipe { get; set; } = serverPipe;
22 /// <summary>
23 /// 客户端是否已经连接
24 /// </summary>
25 public bool IsConnected { get; set; } = isConnected;
26 }
Deleted XFEExtension.NetCore/ProcessCommunication/Server/ProcessCommunicationAuthenticationServer.cs +0 -60
@@ -1,60 +0,0 @@
1 using System.IO.Pipes;
2 using XFEExtension.NetCore.DelegateExtension;
3 using XFEExtension.NetCore.FormatExtension;
4 using XFEExtension.NetCore.ProcessCommunication.Client;
5
6 namespace XFEExtension.NetCore.ProcessCommunication.Server;
7
8 /// <summary>
9 /// 进程间通讯认证服务器
10 /// </summary>
11 /// <param name="serverName">认证服务器名称</param>
12 /// <param name="password">认证服务器密码</param>
13 public class ProcessCommunicationAuthenticationServer(string serverName, string password = "")
14 {
15 /// <summary>
16 /// 客户端连接事件
17 /// </summary>
18 public event XFEEventHandler<ProcessCommunicationAuthenticationServer, ProcessCommunicationClientInfo>? ClientConnected;
19 /// <summary>
20 /// 认证服务器名称
21 /// </summary>
22 public string ServerName { get; set; } = serverName;
23 /// <summary>
24 /// 认证密码
25 /// </summary>
26 public string Password { get; set; } = password;
27 /// <summary>
28 /// 认证管道服务器
29 /// </summary>
30 public Dictionary<int, NamedPipeServerStream> AuthenticationPipeServerDictionary { get; set; } = [];
31 public void Start(bool autoAuthenticationWithClient, int perClientTimeOut = 500)
32 {
33 Task.Run(async () =>
34 {
35 while (true)
36 {
37 var currentServerIndex = AuthenticationPipeServerDictionary.Last().Key + 1;
38 using var authenticationPipeServer = new NamedPipeServerStream($"{ServerName}-{currentServerIndex}");
39 var tokenSource = new CancellationTokenSource(perClientTimeOut);
40 AuthenticationPipeServerDictionary.Add(currentServerIndex, authenticationPipeServer);
41 await authenticationPipeServer.WaitForConnectionAsync();
42 using var streamReader = new StreamReader(authenticationPipeServer);
43 using var streamWriter = new StreamWriter(authenticationPipeServer);
44 var clientInfoString = await streamReader.ReadLineAsync(tokenSource.Token);
45 if (clientInfoString is not null)
46 {
47 var clientInfoDictionary = new XFEDictionary(clientInfoString);
48 if (clientInfoDictionary is not null)
49 {
50 if (clientInfoDictionary.Contains("ServerName") && clientInfoDictionary.Contains("ClientName") && clientInfoDictionary.Contains("ClientID") && clientInfoDictionary.Contains("Password"))
51 {
52 var clientInfo = new ProcessCommunicationClientInfo(clientInfoDictionary["ClientName"]!, clientInfoDictionary["ClientID"]!, clientInfoDictionary["Password"]!);
53 ClientConnected?.Invoke(this, clientInfo);
54 }
55 }
56 }
57 }
58 });
59 }
60 }
Modified XFEExtension.NetCore/ProcessCommunication/Server/ProcessCommunicationServer.cs +55 -11
@@ -1,4 +1,6 @@
1 1 using System.IO.Pipes;
2 using XFEExtension.NetCore.DelegateExtension;
3 using XFEExtension.NetCore.FormatExtension;
2 4 using XFEExtension.NetCore.ProcessCommunication.Client;
3 5
4 6 namespace XFEExtension.NetCore.ProcessCommunication.Server;
@@ -6,26 +8,68 @@ namespace XFEExtension.NetCore.ProcessCommunication.Server;
6 8 /// <summary>
7 9 /// 进程间通讯服务器
8 10 /// </summary>
9 public class ProcessCommunicationServer
11 /// <param name="serverName">服务器名称</param>
12 /// <param name="password">服务器密码</param>
13 public class ProcessCommunicationServer(string serverName, string password = "")
10 14 {
11 15 /// <summary>
12 /// 管道通讯服务器
16 /// 客户端连接事件
13 17 /// </summary>
14 public NamedPipeServerStream PipeServer { get; set; }
18 public event XFEEventHandler<ProcessCommunicationServer, ProcessCommunicationClientInfo>? ClientConnected;
15 19 /// <summary>
16 /// 是否有客户端连接
20 /// 服务器名称
17 21 /// </summary>
18 public bool HasClient { get => ClientInfo is not null; }
22 public string ServerName { get; set; } = serverName;
19 23 /// <summary>
20 /// 客户端名称
24 /// 认证密码
21 25 /// </summary>
22 public string? ClientName { get; set; }
26 public string Password { get; set; } = password;
23 27 /// <summary>
24 /// 客户端信息
28 /// 管道服务器
25 29 /// </summary>
26 public ProcessCommunicationClientInfo? ClientInfo { get; set; }
27 public ProcessCommunicationServer(NamedPipeServerStream pipeServer)
30 public Dictionary<int, NamedPipeServerStream> PipeServerDictionary { get; set; } = [];
31 /// <summary>
32 /// 启动服务器
33 /// </summary>
34 /// <param name="autoAuthenticationWithClient">自动对客户端进行认证</param>
35 /// <param name="perClientTimeOut">每个客户端的连接超时</param>
36 public void Start(bool autoAuthenticationWithClient, int perClientTimeOut = 500)
28 37 {
29 PipeServer = pipeServer;
38 Task.Run(async () =>
39 {
40 while (true)
41 {
42 var currentServerIndex = PipeServerDictionary.Last().Key + 1;
43 using var authenticationPipeServer = new NamedPipeServerStream($"{ServerName}-{currentServerIndex}");
44 var tokenSource = new CancellationTokenSource(perClientTimeOut);
45 PipeServerDictionary.Add(currentServerIndex, authenticationPipeServer);
46 await authenticationPipeServer.WaitForConnectionAsync();
47 _ = Task.Run(async () =>
48 {
49 using var streamReader = new StreamReader(authenticationPipeServer);
50 using var streamWriter = new StreamWriter(authenticationPipeServer);
51 var clientInfoString = await streamReader.ReadLineAsync(tokenSource.Token);
52 if (clientInfoString is not null)
53 {
54 var clientInfoDictionary = new XFEDictionary(clientInfoString);
55 if (clientInfoDictionary is not null)
56 {
57 if (clientInfoDictionary.Contains("ServerName") && clientInfoDictionary.Contains("ClientName") && clientInfoDictionary.Contains("ClientID") && clientInfoDictionary.Contains("Password"))
58 {
59 var clientInfo = new ProcessCommunicationClientInfo(clientInfoDictionary["ClientName"]!, clientInfoDictionary["ClientID"]!, clientInfoDictionary["Password"]!);
60 if (autoAuthenticationWithClient && Password == clientInfo.Password)
61
62 ClientConnected?.Invoke(this, clientInfo);
63
64 }
65 }
66 }
67 else
68 {
69
70 }
71 });
72 }
73 });
30 74 }
31 75 }