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

XFEExtension

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

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

XFEstudio/XFEExtension

增加服务器回调校验 可自定义消息ID

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

代码差异

2 个文件 +97 -68
Modified XFE各类拓展/CyberComm.cs +55 -26
@@ -11,6 +11,7 @@ using System.Threading.Tasks;
11 11 using XFE各类拓展.ArrayExtension;
12 12 using XFE各类拓展.BufferExtension;
13 13 using XFE各类拓展.FormatExtension;
14 using XFE各类拓展.TaskExtension;
14 15
15 16 namespace XFE各类拓展.CyberComm
16 17 {
@@ -1011,6 +1012,7 @@ namespace XFE各类拓展.CyberComm
1011 1012 /// </summary>
1012 1013 public abstract class XCCGroup
1013 1014 {
1015 private event EndTaskTrigger<bool> UpdateTaskTrigger;
1014 1016 private readonly XCCNetWorkBase workBase;
1015 1017 private int reconnectTimes = -1;
1016 1018 #region 公有属性
@@ -1117,6 +1119,9 @@ namespace XFE各类拓展.CyberComm
1117 1119 case "audio":
1118 1120 messageType = XCCBinaryMessageType.Audio;
1119 1121 break;
1122 case "callback":
1123 UpdateTaskTrigger.Invoke(true, messageId);
1124 continue;
1120 1125 default:
1121 1126 messageType = XCCBinaryMessageType.Binary;
1122 1127 break;
@@ -1150,17 +1155,30 @@ namespace XFE各类拓展.CyberComm
1150 1155 workBase.connectionClosed?.Invoke(this, new XCCConnectionClosedEventArgsImpl(this, ClientWebSocket, true));
1151 1156 }
1152 1157 /// <summary>
1158 /// 发送文本消息,返回消息ID
1159 /// </summary>
1160 /// <param name="message">待发送的文本</param>
1161 /// <returns>消息ID</returns>
1162 public async Task<string> SendTextMessage(string message)
1163 {
1164 var messageId = Guid.NewGuid().ToString();
1165 await SendTextMessage(message, messageId);
1166 return messageId;
1167 }
1168 /// <summary>
1153 1169 /// 发送文本消息
1154 1170 /// </summary>
1155 1171 /// <param name="message">待发送的文本</param>
1172 /// <param name="messageId">消息ID</param>
1156 1173 /// <exception cref="XFECyberCommException"></exception>
1157 /// <returns>发送进程</returns>
1158 public async Task SendTextMessage(string message)
1174 /// <returns>服务器接收校验等待</returns>
1175 public async Task SendTextMessage(string message, string messageId)
1159 1176 {
1160 1177 try
1161 1178 {
1162 byte[] sendBuffer = Encoding.UTF8.GetBytes(new string[] { Guid.NewGuid().ToString(), message }.ToXFEString());
1179 byte[] sendBuffer = Encoding.UTF8.GetBytes(new string[] { messageId, message }.ToXFEString());
1163 1180 await ClientWebSocket.SendAsync(new ArraySegment<byte>(sendBuffer), WebSocketMessageType.Text, true, CancellationToken.None);
1181 await new XFEWaitTask<bool>(ref UpdateTaskTrigger, messageId);
1164 1182 }
1165 1183 catch (Exception ex)
1166 1184 {
@@ -1173,13 +1191,13 @@ namespace XFE各类拓展.CyberComm
1173 1191 /// <param name="role">发送者角色</param>
1174 1192 /// <param name="message">待发送的文本</param>
1175 1193 /// <exception cref="XFECyberCommException"></exception>
1176 /// <returns>发送进程</returns>
1194 /// <returns>消息ID</returns>
1177 1195 [Obsolete("发送者已统一,请使用SendTextMessage或SendBinaryTextMessage")]
1178 public async Task SendStandardTextMessage(string role, string message)
1196 public async Task<string> SendStandardTextMessage(string role, string message)
1179 1197 {
1180 1198 try
1181 1199 {
1182 await SendTextMessage(message);
1200 return await SendTextMessage(message);
1183 1201 }
1184 1202 catch (Exception ex)
1185 1203 {
@@ -1187,18 +1205,32 @@ namespace XFE各类拓展.CyberComm
1187 1205 }
1188 1206 }
1189 1207 /// <summary>
1190 /// 发送签名二进制消息
1208 /// 发送签名二进制消息,返回消息ID
1191 1209 /// </summary>
1192 1210 /// <param name="message">二进制消息</param>
1193 /// <param name="signature">标识</param>
1211 /// <param name="signature">签名标识</param>
1194 1212 /// <returns></returns>
1213 public async Task<string> SendSignedBinaryMessage(byte[] message, string signature)
1214 {
1215 var messageId = Guid.NewGuid().ToString();
1216 await SendSignedBinaryMessage(message, messageId, signature);
1217 return messageId;
1218 }
1219 /// <summary>
1220 /// 发送签名二进制消息
1221 /// </summary>
1222 /// <param name="message">二进制消息</param>
1223 /// <param name="messageId">消息ID</param>
1224 /// <param name="signature">签名标识</param>
1225 /// <returns>服务器接收校验等待</returns>
1195 1226 /// <exception cref="XFECyberCommException"></exception>
1196 public async Task SendSignedBinaryMessage(byte[] message, string signature)
1227 public async Task SendSignedBinaryMessage(byte[] message, string messageId, string signature)
1197 1228 {
1198 1229 try
1199 1230 {
1200 var xFEBuffer = new XFEBuffer(Sender, message, "Type", Encoding.UTF8.GetBytes(signature), "ID", Guid.NewGuid().ToByteArray());
1231 var xFEBuffer = new XFEBuffer(Sender, message, "Type", Encoding.UTF8.GetBytes(signature), "ID", Encoding.UTF8.GetBytes(messageId));
1201 1232 await ClientWebSocket.SendAsync(new ArraySegment<byte>(xFEBuffer.ToBuffer()), WebSocketMessageType.Binary, true, CancellationToken.None);
1233 await new XFEWaitTask<bool>(ref UpdateTaskTrigger, messageId);
1202 1234 }
1203 1235 catch (Exception ex)
1204 1236 {
@@ -1209,13 +1241,13 @@ namespace XFE各类拓展.CyberComm
1209 1241 /// 发送二进制文本消息
1210 1242 /// </summary>
1211 1243 /// <param name="message">消息</param>
1212 /// <returns></returns>
1244 /// <returns>消息ID</returns>
1213 1245 /// <exception cref="XFECyberCommException"></exception>
1214 public async Task SendBinaryTextMessage(string message)
1246 public async Task<string> SendBinaryTextMessage(string message)
1215 1247 {
1216 1248 try
1217 1249 {
1218 await SendSignedBinaryMessage(Encoding.UTF8.GetBytes(message), "text");
1250 return await SendSignedBinaryMessage(Encoding.UTF8.GetBytes(message), "text");
1219 1251 }
1220 1252 catch (Exception ex)
1221 1253 {
@@ -1227,12 +1259,12 @@ namespace XFE各类拓展.CyberComm
1227 1259 /// </summary>
1228 1260 /// <param name="message">待发送的二进制数据</param>
1229 1261 /// <exception cref="XFECyberCommException"></exception>
1230 /// <returns>发送进程</returns>
1231 public async Task SendBinaryMessage(byte[] message)
1262 /// <returns>消息ID</returns>
1263 public async Task<string> SendBinaryMessage(byte[] message)
1232 1264 {
1233 1265 try
1234 1266 {
1235 await SendSignedBinaryMessage(message, "binary");
1267 return await SendSignedBinaryMessage(message, "binary");
1236 1268 }
1237 1269 catch (Exception ex)
1238 1270 {
@@ -1243,13 +1275,13 @@ namespace XFE各类拓展.CyberComm
1243 1275 /// 发送图片
1244 1276 /// </summary>
1245 1277 /// <param name="filePath">图片路径</param>
1246 /// <returns></returns>
1278 /// <returns>消息ID</returns>
1247 1279 /// <exception cref="XFECyberCommException"></exception>
1248 public async Task SendImage(string filePath)
1280 public async Task<string> SendImage(string filePath)
1249 1281 {
1250 1282 try
1251 1283 {
1252 await SendSignedBinaryMessage(File.ReadAllBytes(filePath), "image");
1284 return await SendSignedBinaryMessage(File.ReadAllBytes(filePath), "image");
1253 1285 }
1254 1286 catch (Exception ex)
1255 1287 {
@@ -1260,18 +1292,15 @@ namespace XFE各类拓展.CyberComm
1260 1292 /// 发送音频
1261 1293 /// </summary>
1262 1294 /// <param name="buffer">二进制音频流</param>
1263 /// <returns></returns>
1295 /// <returns>消息ID</returns>
1264 1296 /// <exception cref="XFECyberCommException"></exception>
1265 public async Task SendAudioBuffer(byte[] buffer)
1297 public async Task<string> SendAudioBuffer(byte[] buffer)
1266 1298 {
1267 1299 try
1268 1300 {
1269 await SendSignedBinaryMessage(buffer, "audio");
1270 }
1271 catch (Exception ex)
1272 {
1273 throw new XFECyberCommException("客户端发送音频到服务器时出现异常", ex);
1301 return await SendSignedBinaryMessage(buffer, "audio");
1274 1302 }
1303 catch (Exception ex) { throw new XFECyberCommException("客户端发送音频到服务器时出现异常", ex); }
1275 1304 }
1276 1305 /// <summary>
1277 1306 /// 获取历史记录
Modified XFE各类拓展/TaskExtension.cs +42 -42
@@ -10,13 +10,6 @@ namespace XFE各类拓展.TaskExtension
10 10 /// </summary>
11 11 public static class TaskExtension
12 12 {
13 /// <summary>
14 /// 传入值并终止等待器
15 /// </summary>
16 /// <typeparam name="T">泛型</typeparam>
17 /// <param name="taskId">任务ID</param>
18 /// <param name="result">目标等待器返回值</param>
19 public delegate void EndTaskTrigger<T>(T result, string taskId = "DEFAULTTASKID");
20 13 private static long CTimeCounter = 0;
21 14 #region 新建任务并开始
22 15 /// <summary>
@@ -89,45 +82,52 @@ namespace XFE各类拓展.TaskExtension
89 82 Console.WriteLine($"标识名:{timerName}\t执行批次:{CTimeCounter}\t执行时间: {elapsedTime.TotalMilliseconds * 1000:F3} 纳秒");
90 83 return elapsedTime;
91 84 }
85 }
86 /// <summary>
87 /// 传入值并终止等待器
88 /// </summary>
89 /// <typeparam name="T">泛型</typeparam>
90 /// <param name="taskId">任务ID</param>
91 /// <param name="result">目标等待器返回值</param>
92 public delegate void EndTaskTrigger<T>(T result, string taskId = "DEFAULTTASKID");
93 /// <summary>
94 /// XFE等待器
95 /// </summary>
96 /// <typeparam name="T">泛型</typeparam>
97 public class XFEWaitTask<T>
98 {
99 private TaskCompletionSource<T> WaitTaskSource { get; set; } = new TaskCompletionSource<T>();
100 /// <summary>
101 /// 任务ID
102 /// </summary>
103 public string TaskId { get; set; }
104 /// <summary>
105 /// 结束任务并返回
106 /// </summary>
107 /// <param name="result">返回结果</param>
108 /// <param name="taskId">任务ID</param>
109 public void EndTask(T result, string taskId)
110 {
111 if (taskId == TaskId)
112 WaitTaskSource.SetResult(result);
113 }
114 /// <summary>
115 /// 获取等待器
116 /// </summary>
117 /// <returns></returns>
118 public TaskAwaiter<T> GetAwaiter()
119 {
120 return WaitTaskSource.Task.GetAwaiter();
121 }
92 122 /// <summary>
93 123 /// XFE等待器
94 124 /// </summary>
95 /// <typeparam name="T">泛型</typeparam>
96 public class XFEWaitTask<T>
125 /// <param name="endTaskAndReturn">触发任务结束事件</param>
126 /// <param name="taskId">任务ID</param>
127 public XFEWaitTask(ref EndTaskTrigger<T> endTaskAndReturn, string taskId = "DEFAULTTASKID")
97 128 {
98 private TaskCompletionSource<T> WaitTaskSource { get; set; } = new TaskCompletionSource<T>();
99 /// <summary>
100 /// 任务ID
101 /// </summary>
102 public string TaskId { get; set; }
103 /// <summary>
104 /// 结束任务并返回
105 /// </summary>
106 /// <param name="result">返回结果</param>
107 /// <param name="taskId">任务ID</param>
108 public void EndTask(T result, string taskId)
109 {
110 if (taskId == TaskId)
111 WaitTaskSource.SetResult(result);
112 }
113 /// <summary>
114 /// 获取等待器
115 /// </summary>
116 /// <returns></returns>
117 public TaskAwaiter<T> GetAwaiter()
118 {
119 return WaitTaskSource.Task.GetAwaiter();
120 }
121 /// <summary>
122 /// XFE等待器
123 /// </summary>
124 /// <param name="endTaskAndReturn">触发任务结束事件</param>
125 /// <param name="taskId">任务ID</param>
126 public XFEWaitTask(ref EndTaskTrigger<T> endTaskAndReturn, string taskId = "DEFAULTTASKID")
127 {
128 endTaskAndReturn += EndTask;
129 TaskId = taskId;
130 }
129 endTaskAndReturn += EndTask;
130 TaskId = taskId;
131 131 }
132 132 }
133 133 }