XFEExtension
【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等
关注
0
Fork
0
Star
0
返回提交历史
Deleted
XFEExtension.NetCore/ProcessCommunication/Client/ConnectFailReason.cs
+0
-32
Deleted
XFEExtension.NetCore/ProcessCommunication/Client/ConnectResult.cs
+0
-29
Deleted
XFEExtension.NetCore/ProcessCommunication/Client/ProcessCommunicationClient.cs
+0
-150
Deleted
XFEExtension.NetCore/ProcessCommunication/Client/ProcessCommunicationClientInfo.cs
+0
-23
Deleted
XFEExtension.NetCore/ProcessCommunication/Server/PCClient.cs
+0
-26
Deleted
XFEExtension.NetCore/ProcessCommunication/Server/ProcessCommunicationServer.cs
+0
-75
Deleted
XFEExtension.NetCore/ProcessCommunication/Server/ProcessCommunicationServerInfo.cs
+0
-20
XFEstudio/XFEExtension
移除开发中的进程通讯
cc19249
代码差异
7 个文件
+0
-355
@@ -1,32 +0,0 @@
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
}
@@ -1,29 +0,0 @@
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
}
@@ -1,150 +0,0 @@
1
using System.Diagnostics;
2
using System.IO.Pipes;
3
using XFEExtension.NetCore.FormatExtension;
4
using XFEExtension.NetCore.ProcessCommunication.Server;
5
6
namespace XFEExtension.NetCore.ProcessCommunication.Client;
7
8
/// <summary>
9
/// 进程通讯客户端
10
/// </summary>
11
public class ProcessCommunicationClient
12
{
13
private StreamReader? streamReader;
14
private StreamWriter? streamWriter;
15
/// <summary>
16
/// 明文管道通讯客户端
17
/// </summary>
18
public NamedPipeClientStream? TextPipeClient { get; set; }
19
/// <summary>
20
/// 二进制管道通讯客户端
21
/// </summary>
22
public NamedPipeClientStream? BinaryPipeClient { get; set; }
23
/// <summary>
24
/// 客户端名称
25
/// </summary>
26
public required string ClientName { get; set; }
27
/// <summary>
28
/// 客户端唯一标识
29
/// </summary>
30
public required string ClientID { get; set; }
31
/// <summary>
32
/// 是否已经连接到服务器
33
/// </summary>
34
public bool Connected { get; set; }
35
/// <summary>
36
/// 服务器信息
37
/// </summary>
38
public ProcessCommunicationServerInfo? ServerInfo { get; set; }
39
/// <summary>
40
/// 连接
41
/// </summary>
42
/// <param name="serverName">服务器名称</param>
43
/// <param name="password">密码</param>
44
/// <param name="computerName">目标计算机名称</param>
45
/// <param name="timeOut">超时(ms)</param>
46
/// <returns>连接结果</returns>
47
public async Task<ConnectResult> Connect(string serverName, string password = "", string computerName = ".", int timeOut = 500)
48
{
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}-{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}-{retryTime}");
69
}
70
else
71
{
72
return new ConnectResult()
73
{
74
IsSuccessful = false,
75
FailReason = ConnectFailReason.ConnectToServerTimeOut,
76
Message = "连接至认证服务器超时"
77
};
78
}
79
}
80
}
81
using var reader = new StreamReader(authenticationClient);
82
using var writer = new StreamWriter(authenticationClient);
83
await writer.WriteLineAsync(new XFEDictionary("ServerName", serverName, "ClientName", ClientName, "ClientID", ClientID, "Password", password));
84
await writer.FlushAsync();
85
var result = await reader.ReadLineAsync(tokenSource.Token);
86
await authenticationClient.DisposeAsync();
87
if (result is not null)
88
{
89
var resultDictionary = new XFEDictionary(result);
90
if (resultDictionary.Contains("ServerName") && resultDictionary.Contains("TextPipeID") && resultDictionary.Contains("BinaryPipeID"))
91
{
92
ServerInfo = new ProcessCommunicationServerInfo(resultDictionary["ServerName"]!, resultDictionary["TextPipeID"]!, resultDictionary["BinaryPipeID"]!);
93
TextPipeClient = new NamedPipeClientStream(computerName, ServerInfo.CurrentTextServerPipeName);
94
BinaryPipeClient = new NamedPipeClientStream(computerName, ServerInfo.CurrentBinaryServerPipeName);
95
await TextPipeClient.ConnectAsync(tokenSource.Token);
96
await BinaryPipeClient.ConnectAsync(tokenSource.Token);
97
streamReader = new StreamReader(TextPipeClient);
98
streamWriter = new StreamWriter(TextPipeClient);
99
return ConnectResult.Successful;
100
}
101
}
102
return ConnectResult.Successful;
103
}
104
/// <summary>
105
/// 发送明文信息
106
/// </summary>
107
/// <param name="message">明文信息</param>
108
/// <param name="timeOut">超时(ms)</param>
109
/// <returns>是否发送成功</returns>
110
public async Task<bool> SendTextMessage(string message, int timeOut = 50)
111
{
112
if (!CheckPipeClientExist())
113
return false;
114
var tokenSource = new CancellationTokenSource(timeOut);
115
await streamWriter!.WriteLineAsync(message);
116
await streamWriter!.FlushAsync(tokenSource.Token);
117
var result = await streamReader!.ReadLineAsync(tokenSource.Token);
118
if (result is not null)
119
{
120
var resultDictionary = new XFEDictionary(result);
121
if (resultDictionary.Contains("Successful"))
122
{
123
return resultDictionary["Successful"] == "true";
124
}
125
}
126
return false;
127
}
128
/// <summary>
129
/// 发送二进制消息
130
/// </summary>
131
/// <param name="bytes">二进制消息</param>
132
/// <param name="timeOut">超时(ms)</param>
133
/// <returns></returns>
134
public async Task<bool> SendBinaryMessage(byte[] bytes, int timeOut = 50)
135
{
136
if (!CheckPipeClientExist())
137
return false;
138
var tokenSource = new CancellationTokenSource(timeOut);
139
await BinaryPipeClient!.WriteAsync(bytes, tokenSource.Token);
140
await BinaryPipeClient!.FlushAsync(tokenSource.Token);
141
var result = new byte[1];
142
var resultCount = await BinaryPipeClient!.ReadAsync(result, tokenSource.Token);
143
if (resultCount > 0)
144
{
145
return result[0] == 1;
146
}
147
return false;
148
}
149
private bool CheckPipeClientExist() => TextPipeClient is not null && BinaryPipeClient is not null && streamReader is not null && streamWriter is not null;
150
}
@@ -1,23 +0,0 @@
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
}
@@ -1,26 +0,0 @@
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
}
@@ -1,75 +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 ProcessCommunicationServer(string serverName, string password = "")
14
{
15
/// <summary>
16
/// 客户端连接事件
17
/// </summary>
18
public event XFEEventHandler<ProcessCommunicationServer, 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> 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)
37
{
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
});
74
}
75
}
@@ -1,20 +0,0 @@
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
}