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

XFEExtension

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

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

XFEstudio/XFEExtension

初步完成进程间通讯的客户端和认证服务器部分

6180b86
XFE工作室室长 <xfe18827675401@126.com>
提交于

代码差异

7 个文件 +305 -6
Modified XFEExtension.NetCore.Analyzer.Test/Program.cs +67 -6
@@ -1,14 +1,75 @@
1 namespace XFEExtension.NetCore.Analyzer.Test;
1 using System.IO.Pipes;
2
3 namespace XFEExtension.NetCore.Analyzer.Test;
2 4
3 5 internal class Program
4 6 {
5 public static void Main(string[] args)
7 [SMTest]
8 public static async Task TestNamedPipeAsync()
6 9 {
7 var testClass = new TestClass("测试名称", "测试描述哈哈", 59)
10 try
11 {
12 if (Console.ReadLine() == "s")
13 {
14 using var pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut);
15 Console.WriteLine("Named Pipe Server waiting for connection...");
16 while (true)
17 {
18 await pipeServer.WaitForConnectionAsync();
19 _ = Task.Run(async () =>
20 {
21 Console.WriteLine("Client Connected!");
22 using var reader = new StreamReader(pipeServer);
23 using var writer = new StreamWriter(pipeServer);
24 var message = await reader.ReadLineAsync();
25 Console.WriteLine("Received from client: " + message);
26 string response = "Hello from server!";
27 await writer.WriteLineAsync(response);
28 writer.Flush();
29 await Task.Delay(1000);
30 await writer.WriteLineAsync(response);
31 writer.Flush();
32 await Task.Delay(1000);
33 await writer.WriteLineAsync(response);
34 writer.Flush();
35 await Task.Delay(1000);
36 await writer.WriteLineAsync(response);
37 writer.Flush();
38 await Task.Delay(1000);
39 await writer.WriteLineAsync(response);
40 writer.Flush();
41 await Task.Delay(1000);
42 await writer.WriteLineAsync(response);
43 writer.Flush();
44 await Task.Delay(1000);
45 await writer.WriteLineAsync(response);
46 writer.Flush();
47 });
48 }
49 }
50 else
51 {
52 using var pipeClient = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut);
53 Console.WriteLine("Named Pipe Client connecting to server...");
54 await pipeClient.ConnectAsync();
55 using var reader = new StreamReader(pipeClient);
56 using var writer = new StreamWriter(pipeClient);
57 string message = "Hello from client!";
58 await writer.WriteLineAsync(message);
59 writer.Flush();
60 while (true)
61 {
62 var response = await reader.ReadLineAsync();
63 Console.WriteLine("Received from server: " + response);
64 }
65 }
66 }
67 catch (Exception ex)
8 68 {
9 Tags = [["123", "321"], ["1234567", "7654321"]],
10 Enum = MyEnum.Test1
11 };
69 Console.WriteLine(ex);
70 }
71 Console.WriteLine("Program End");
72 Console.ReadLine();
12 73 }
13 74 }
14 75 enum MyEnum
Modified XFEExtension.NetCore.Analyzer.Test/XFEExtension.NetCore.Analyzer.Test.csproj +4 -0
@@ -11,6 +11,10 @@
11 11 <AutoProfile>false</AutoProfile>
12 12 </PropertyGroup>
13 13
14 <ItemGroup>
15 <PackageReference Include="XFEExtension.NetCore.XUnit" Version="2.0.3" />
16 </ItemGroup>
17
14 18 <ItemGroup>
15 19 <ProjectReference Include="..\XFEExtension.NetCore.Analyzer\XFEExtension.NetCore.Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
16 20 <ProjectReference Include="..\XFEExtension.NetCore\XFEExtension.NetCore.csproj" />
Added XFEExtension.NetCore/ProcessCommunication/Client/ProcessCommunicationClient.cs +119 -0
@@ -0,0 +1,119 @@
1 using System.IO.Pipes;
2 using XFEExtension.NetCore.FormatExtension;
3 using XFEExtension.NetCore.ProcessCommunication.Server;
4
5 namespace XFEExtension.NetCore.ProcessCommunication.Client;
6
7 /// <summary>
8 /// 进程通讯客户端
9 /// </summary>
10 public class ProcessCommunicationClient
11 {
12 private StreamReader? streamReader;
13 private StreamWriter? streamWriter;
14 /// <summary>
15 /// 明文管道通讯客户端
16 /// </summary>
17 public NamedPipeClientStream? TextPipeClient { get; set; }
18 /// <summary>
19 /// 二进制管道通讯客户端
20 /// </summary>
21 public NamedPipeClientStream? BinaryPipeClient { get; set; }
22 /// <summary>
23 /// 客户端名称
24 /// </summary>
25 public required string ClientName { get; set; }
26 /// <summary>
27 /// 客户端唯一标识
28 /// </summary>
29 public required string ClientID { get; set; }
30 /// <summary>
31 /// 是否已经连接到服务器
32 /// </summary>
33 public bool Connected { get; set; }
34 /// <summary>
35 /// 服务器信息
36 /// </summary>
37 public ProcessCommunicationServerInfo? ServerInfo { get; set; }
38 /// <summary>
39 /// 连接
40 /// </summary>
41 /// <param name="serverName">服务器名称</param>
42 /// <param name="password">密码</param>
43 /// <param name="computerName">目标计算机名称</param>
44 /// <param name="timeOut">超时(ms)</param>
45 /// <returns>是否成功</returns>
46 public async Task<bool> Connect(string serverName, string password = "", string computerName = ".", int timeOut = 500)
47 {
48 using var authenticationClient = new NamedPipeClientStream(computerName, $"{serverName}-Authentication");
49 await authenticationClient.ConnectAsync(timeOut);
50 using var reader = new StreamReader(authenticationClient);
51 using var writer = new StreamWriter(authenticationClient);
52 await writer.WriteLineAsync(new XFEDictionary("ServerName", serverName, "ClientName", ClientName, "ClientID", ClientID, "Password", password));
53 await writer.FlushAsync();
54 var result = await reader.ReadLineAsync();
55 await authenticationClient.DisposeAsync();
56 if (result is not null)
57 {
58 var resultDictionary = new XFEDictionary(result);
59 if (resultDictionary.Contains("ServerName") && resultDictionary.Contains("TextPipeID") && resultDictionary.Contains("BinaryPipeID"))
60 {
61 ServerInfo = new ProcessCommunicationServerInfo(resultDictionary["ServerName"]!, resultDictionary["TextPipeID"]!, resultDictionary["BinaryPipeID"]!);
62 TextPipeClient = new NamedPipeClientStream(computerName, ServerInfo.CurrentTextServerPipeName);
63 BinaryPipeClient = new NamedPipeClientStream(computerName, ServerInfo.CurrentBinaryServerPipeName);
64 await TextPipeClient.ConnectAsync();
65 await BinaryPipeClient.ConnectAsync();
66 streamReader = new StreamReader(TextPipeClient);
67 streamWriter = new StreamWriter(TextPipeClient);
68 return true;
69 }
70 }
71 return false;
72 }
73 /// <summary>
74 /// 发送明文信息
75 /// </summary>
76 /// <param name="message">明文信息</param>
77 /// <param name="timeOut">超时(ms)</param>
78 /// <returns>是否发送成功</returns>
79 public async Task<bool> SendTextMessage(string message, int timeOut = 50)
80 {
81 if (!CheckPipeClientExist())
82 return false;
83 var tokenSource = new CancellationTokenSource(timeOut);
84 await streamWriter!.WriteLineAsync(message);
85 await streamWriter!.FlushAsync(tokenSource.Token);
86 var result = await streamReader!.ReadLineAsync(tokenSource.Token);
87 if (result is not null)
88 {
89 var resultDictionary = new XFEDictionary(result);
90 if (resultDictionary.Contains("Successful"))
91 {
92 return resultDictionary["Successful"] == "true";
93 }
94 }
95 return false;
96 }
97 /// <summary>
98 /// 发送二进制消息
99 /// </summary>
100 /// <param name="bytes">二进制消息</param>
101 /// <param name="timeOut">超时(ms)</param>
102 /// <returns></returns>
103 public async Task<bool> SendBinaryMessage(byte[] bytes, int timeOut = 50)
104 {
105 if (!CheckPipeClientExist())
106 return false;
107 var tokenSource = new CancellationTokenSource(timeOut);
108 await BinaryPipeClient!.WriteAsync(bytes, tokenSource.Token);
109 await BinaryPipeClient!.FlushAsync(tokenSource.Token);
110 var result = new byte[1];
111 var resultCount = await BinaryPipeClient!.ReadAsync(result, tokenSource.Token);
112 if (resultCount > 0)
113 {
114 return result[0] == 1;
115 }
116 return false;
117 }
118 private bool CheckPipeClientExist() => TextPipeClient is not null && BinaryPipeClient is not null && streamReader is not null && streamWriter is not null;
119 }
Added XFEExtension.NetCore/ProcessCommunication/Client/ProcessCommunicationClientInfo.cs +23 -0
@@ -0,0 +1,23 @@
1 namespace XFEExtension.NetCore.ProcessCommunication.Client;
2
3 /// <summary>
4 /// 进程通讯客户端信息
5 /// </summary>
6 /// <param name="clientName">客户端名称</param>
7 /// <param name="clientId">客户端唯一标识</param>
8 /// <param name="password">客户端连接时密码</param>
9 public class ProcessCommunicationClientInfo(string clientName, string clientId, string password = "")
10 {
11 /// <summary>
12 /// 客户端名称
13 /// </summary>
14 public string ClientName { get; set; } = clientName;
15 /// <summary>
16 /// 客户端唯一标识
17 /// </summary>
18 public string ClientID { get; set; } = clientId;
19 /// <summary>
20 /// 客户端连接时密码
21 /// </summary>
22 public string Password { get; set; } = password;
23 }
Added XFEExtension.NetCore/ProcessCommunication/Server/ProcessCommunicationAuthenticationServer.cs +41 -0
@@ -0,0 +1,41 @@
1 using System.IO.Pipes;
2 using XFEExtension.NetCore.DelegateExtension;
3 using XFEExtension.NetCore.ProcessCommunication.Client;
4
5 namespace XFEExtension.NetCore.ProcessCommunication.Server;
6
7 /// <summary>
8 /// 进程间通讯认证服务器
9 /// </summary>
10 /// <param name="serverName">认证服务器名称</param>
11 /// <param name="password">认证服务器密码</param>
12 public class ProcessCommunicationAuthenticationServer(string serverName, string password = "")
13 {
14 /// <summary>
15 /// 客户端连接事件
16 /// </summary>
17 public event XFEEventHandler<ProcessCommunicationAuthenticationServer, ProcessCommunicationClientInfo>? ClientConnected;
18 /// <summary>
19 /// 认证服务器名称
20 /// </summary>
21 public string ServerName { get; set; } = serverName;
22 /// <summary>
23 /// 认证密码
24 /// </summary>
25 public string Password { get; set; } = password;
26 /// <summary>
27 /// 认证管道服务器
28 /// </summary>
29 public NamedPipeServerStream AuthenticationPipeServer { get; set; } = new NamedPipeServerStream(serverName);
30 public void Start(bool autoAuthenticationWithClient, int perClientTimeOut = 500)
31 {
32 Task.Run(async () =>
33 {
34 while (true)
35 {
36 await AuthenticationPipeServer.WaitForConnectionAsync();
37
38 }
39 });
40 }
41 }
Added XFEExtension.NetCore/ProcessCommunication/Server/ProcessCommunicationServer.cs +31 -0
@@ -0,0 +1,31 @@
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 public class ProcessCommunicationServer
10 {
11 /// <summary>
12 /// 管道通讯服务器
13 /// </summary>
14 public NamedPipeServerStream PipeServer { get; set; }
15 /// <summary>
16 /// 是否有客户端连接
17 /// </summary>
18 public bool HasClient { get => ClientInfo is not null; }
19 /// <summary>
20 /// 客户端名称
21 /// </summary>
22 public string? ClientName { get; set; }
23 /// <summary>
24 /// 客户端信息
25 /// </summary>
26 public ProcessCommunicationClientInfo? ClientInfo { get; set; }
27 public ProcessCommunicationServer(NamedPipeServerStream pipeServer)
28 {
29 PipeServer = pipeServer;
30 }
31 }
Added XFEExtension.NetCore/ProcessCommunication/Server/ProcessCommunicationServerInfo.cs +20 -0
@@ -0,0 +1,20 @@
1 namespace XFEExtension.NetCore.ProcessCommunication.Server;
2
3 /// <summary>
4 /// 进程通讯服务器信息
5 /// </summary>
6 public class ProcessCommunicationServerInfo(string serverName, string currentTextServerPipeName, string currentBinaryServerPipeName)
7 {
8 /// <summary>
9 /// 服务器名称
10 /// </summary>
11 public string ServerName { get; set; } = serverName;
12 /// <summary>
13 /// 当前明文服务器管道名称
14 /// </summary>
15 public string CurrentTextServerPipeName { get; set; } = currentTextServerPipeName;
16 /// <summary>
17 /// 当前二进制服务器管道名称
18 /// </summary>
19 public string CurrentBinaryServerPipeName { get; set; } = currentBinaryServerPipeName;
20 }