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

XFEExtension

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

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

XFEstudio/XFEExtension

完善部分管道通讯机制

1ece252
XFE工作室室长 <xfe18827675401@126.com>
提交于

代码差异

5 个文件 +126 -14
Modified XFEExtension.NetCore.Analyzer.Test/Program.cs +2 -1
@@ -1,4 +1,5 @@
1 using System.IO.Pipes;
1 using System.Diagnostics;
2 using System.IO.Pipes;
2 3
3 4 namespace XFEExtension.NetCore.Analyzer.Test;
4 5
Added XFEExtension.NetCore/ProcessCommunication/Client/ConnectFailReason.cs +32 -0
@@ -0,0 +1,32 @@
1 namespace XFEExtension.NetCore.ProcessCommunication.Client;
2
3 /// <summary>
4 /// 连接失败原因代码
5 /// </summary>
6 public enum ConnectFailReason
7 {
8 /// <summary>
9 /// 无法连接至认证服务器
10 /// </summary>
11 FailedToConnectAuthenticationServer = 0,
12 /// <summary>
13 /// 认证失败
14 /// </summary>
15 AuthenticationFailed = 1,
16 /// <summary>
17 /// 服务器自身错误
18 /// </summary>
19 ServerError = 2,
20 /// <summary>
21 /// 无法连接至明文服务器
22 /// </summary>
23 FailedToConnectTextServer = 3,
24 /// <summary>
25 /// 无法连接至二进制服务器
26 /// </summary>
27 FailedToConnectBinaryServer = 4,
28 /// <summary>
29 /// 连接至服务器超时
30 /// </summary>
31 ConnectToServerTimeOut = 5
32 }
Added XFEExtension.NetCore/ProcessCommunication/Client/ConnectResult.cs +29 -0
@@ -0,0 +1,29 @@
1 namespace XFEExtension.NetCore.ProcessCommunication.Client;
2
3 /// <summary>
4 /// 连接结果
5 /// </summary>
6 public class ConnectResult
7 {
8 /// <summary>
9 /// 成功
10 /// </summary>
11 public static ConnectResult Successful { get; set; } = new ConnectResult()
12 {
13 IsSuccessful = true,
14 Message = "Successful",
15 FailReason = null
16 };
17 /// <summary>
18 /// 是否成功
19 /// </summary>
20 public bool IsSuccessful { get; set; }
21 /// <summary>
22 /// 消息
23 /// </summary>
24 public string Message { get; set; } = "";
25 /// <summary>
26 /// 失败原因
27 /// </summary>
28 public ConnectFailReason? FailReason { get; set; }
29 }
Modified XFEExtension.NetCore/ProcessCommunication/Client/ProcessCommunicationClient.cs +41 -10
@@ -1,4 +1,5 @@
1 using System.IO.Pipes;
1 using System.Diagnostics;
2 using System.IO.Pipes;
2 3 using XFEExtension.NetCore.FormatExtension;
3 4 using XFEExtension.NetCore.ProcessCommunication.Server;
4 5
@@ -42,16 +43,46 @@ public class ProcessCommunicationClient
42 43 /// <param name="password">密码</param>
43 44 /// <param name="computerName">目标计算机名称</param>
44 45 /// <param name="timeOut">超时(ms)</param>
45 /// <returns>是否成功</returns>
46 public async Task<bool> Connect(string serverName, string password = "", string computerName = ".", int timeOut = 500)
46 /// <returns>连接结果</returns>
47 public async Task<ConnectResult> Connect(string serverName, string password = "", string computerName = ".", int timeOut = 500)
47 48 {
48 using var authenticationClient = new NamedPipeClientStream(computerName, $"{serverName}-Authentication");
49 await authenticationClient.ConnectAsync(timeOut);
49 var stopWatch = new Stopwatch();
50 var tokenSource = new CancellationTokenSource(timeOut);
51 int retryTime = 0;
52 stopWatch.Start();
53 var authenticationClient = new NamedPipeClientStream(computerName, $"{serverName}-Authentication-{retryTime}");
54 while (true)
55 {
56 try
57 {
58 await authenticationClient.ConnectAsync(timeOut);
59 break;
60 }
61 catch (Exception ex)
62 {
63 Console.WriteLine(ex);
64 if (stopWatch.ElapsedMilliseconds < timeOut)
65 {
66 retryTime++;
67 await authenticationClient.DisposeAsync();
68 authenticationClient = new NamedPipeClientStream($"{serverName}-Authentication-{retryTime}");
69 }
70 else
71 {
72 return new ConnectResult()
73 {
74 IsSuccessful = false,
75 FailReason = ConnectFailReason.ConnectToServerTimeOut,
76 Message = "连接至认证服务器超时"
77 };
78 }
79 }
80 }
50 81 using var reader = new StreamReader(authenticationClient);
51 82 using var writer = new StreamWriter(authenticationClient);
52 83 await writer.WriteLineAsync(new XFEDictionary("ServerName", serverName, "ClientName", ClientName, "ClientID", ClientID, "Password", password));
53 84 await writer.FlushAsync();
54 var result = await reader.ReadLineAsync();
85 var result = await reader.ReadLineAsync(tokenSource.Token);
55 86 await authenticationClient.DisposeAsync();
56 87 if (result is not null)
57 88 {
@@ -61,14 +92,14 @@ public class ProcessCommunicationClient
61 92 ServerInfo = new ProcessCommunicationServerInfo(resultDictionary["ServerName"]!, resultDictionary["TextPipeID"]!, resultDictionary["BinaryPipeID"]!);
62 93 TextPipeClient = new NamedPipeClientStream(computerName, ServerInfo.CurrentTextServerPipeName);
63 94 BinaryPipeClient = new NamedPipeClientStream(computerName, ServerInfo.CurrentBinaryServerPipeName);
64 await TextPipeClient.ConnectAsync();
65 await BinaryPipeClient.ConnectAsync();
95 await TextPipeClient.ConnectAsync(tokenSource.Token);
96 await BinaryPipeClient.ConnectAsync(tokenSource.Token);
66 97 streamReader = new StreamReader(TextPipeClient);
67 98 streamWriter = new StreamWriter(TextPipeClient);
68 return true;
99 return ConnectResult.Successful;
69 100 }
70 101 }
71 return false;
102 return ConnectResult.Successful;
72 103 }
73 104 /// <summary>
74 105 /// 发送明文信息
Modified XFEExtension.NetCore/ProcessCommunication/Server/ProcessCommunicationAuthenticationServer.cs +22 -3
@@ -1,5 +1,6 @@
1 1 using System.IO.Pipes;
2 2 using XFEExtension.NetCore.DelegateExtension;
3 using XFEExtension.NetCore.FormatExtension;
3 4 using XFEExtension.NetCore.ProcessCommunication.Client;
4 5
5 6 namespace XFEExtension.NetCore.ProcessCommunication.Server;
@@ -26,15 +27,33 @@ public class ProcessCommunicationAuthenticationServer(string serverName, string
26 27 /// <summary>
27 28 /// 认证管道服务器
28 29 /// </summary>
29 public NamedPipeServerStream AuthenticationPipeServer { get; set; } = new NamedPipeServerStream(serverName);
30 public Dictionary<int, NamedPipeServerStream> AuthenticationPipeServerDictionary { get; set; } = [];
30 31 public void Start(bool autoAuthenticationWithClient, int perClientTimeOut = 500)
31 32 {
32 33 Task.Run(async () =>
33 34 {
34 35 while (true)
35 36 {
36 await AuthenticationPipeServer.WaitForConnectionAsync();
37
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 }
38 57 }
39 58 });
40 59 }