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

XFEExtension

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

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

XFEstudio/XFEExtension

修复大量协议bug 修复Replace在Buffer过长时替换失败的bug

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

代码差异

3 个文件 +61 -23
Modified XFE各类拓展/ArrayExtension.cs +1 -0
@@ -1,4 +1,5 @@
1 1 using System;
2 using System.Collections.Generic;
2 3
3 4 namespace XFE各类拓展.ArrayExtension
4 5 {
Modified XFE各类拓展/BufferExtension.cs +56 -19
@@ -1,4 +1,5 @@
1 using System.Collections.Generic;
1 using System;
2 using System.Collections.Generic;
2 3 using System.Linq;
3 4 using System.Text;
4 5
@@ -82,27 +83,43 @@ namespace XFE各类拓展.BufferExtension
82 83 /// <returns>替换后的Buffer</returns>
83 84 public static byte[] Replace(this byte[] buffer, byte[] originBuffer, byte[] targetBuffer)
84 85 {
85 var indexes = buffer.IndexesOf(originBuffer);
86 var newBuffer = new byte[buffer.Length + (targetBuffer.Length - originBuffer.Length) * indexes.Length];
87 int index = 0;
88 for (int i = 0; i < indexes.Length; i++)
86 if (originBuffer == null || originBuffer.LongLength == 0)
89 87 {
90 for (int j = index; j < indexes[i]; j++)
88 throw new ArgumentException("Origin buffer cannot be null or empty.");
89 }
90 if (buffer == null || buffer.LongLength == 0)
91 {
92 throw new ArgumentException("Input buffer cannot be null or empty.");
93 }
94 List<byte> result = new List<byte>();
95 for (long i = 0; i <= buffer.LongLength - originBuffer.LongLength; i++)
96 {
97 bool isMatch = true;
98 for (long j = 0; j < originBuffer.LongLength; j++)
99 {
100 if (buffer[i + j] != originBuffer[j])
101 {
102 isMatch = false;
103 break;
104 }
105 }
106 if (isMatch)
91 107 {
92 newBuffer[j] = buffer[j];
108 result.AddRange(targetBuffer);
109 i += originBuffer.LongLength - 1;
93 110 }
94 for (int j = 0; j < targetBuffer.Length; j++)
111 else
95 112 {
96 newBuffer[indexes[i] + j] = targetBuffer[j];
113 result.Add(buffer[i]);
97 114 }
98 index = indexes[i] + targetBuffer.Length;
99 115 }
100 for (int i = index; i < newBuffer.Length; i++)
116 for (long i = buffer.LongLength - originBuffer.LongLength + 1; i < buffer.LongLength; i++)
101 117 {
102 newBuffer[i] = buffer[i - (targetBuffer.Length - originBuffer.Length) * indexes.Length];
118 result.Add(buffer[i]);
103 119 }
104 return newBuffer;
120 return result.ToArray();
105 121 }
122
106 123 /// <summary>
107 124 /// 分割Buffer
108 125 /// </summary>
@@ -113,19 +130,19 @@ namespace XFE各类拓展.BufferExtension
113 130 {
114 131 var indexes = buffer.IndexesOf(targetBuffer);
115 132 var buffers = new List<byte[]>();
116 int index = 0;
117 for (int i = 0; i < indexes.Length; i++)
133 long index = 0;
134 for (long i = 0; i < indexes.LongLength; i++)
118 135 {
119 136 var newBuffer = new byte[indexes[i] - index];
120 for (int j = index; j < indexes[i]; j++)
137 for (long j = index; j < indexes[i]; j++)
121 138 {
122 139 newBuffer[j - index] = buffer[j];
123 140 }
124 141 buffers.Add(newBuffer);
125 index = indexes[i] + targetBuffer.Length;
142 index = indexes[i] + targetBuffer.LongLength;
126 143 }
127 var lastBuffer = new byte[buffer.Length - index];
128 for (int i = index; i < buffer.Length; i++)
144 var lastBuffer = new byte[buffer.LongLength - index];
145 for (long i = index; i < buffer.LongLength; i++)
129 146 {
130 147 lastBuffer[i - index] = buffer[i];
131 148 }
@@ -197,6 +214,26 @@ namespace XFE各类拓展.BufferExtension
197 214 private Dictionary<string, byte[]> bufferDictionary = new Dictionary<string, byte[]>();
198 215 private List<byte[]> headerBuffers = new List<byte[]>();
199 216 /// <summary>
217 /// 所有的头
218 /// </summary>
219 public Dictionary<string, byte[]>.KeyCollection Headers
220 {
221 get
222 {
223 return bufferDictionary.Keys;
224 }
225 }
226 /// <summary>
227 /// 所有的值
228 /// </summary>
229 public Dictionary<string, byte[]>.ValueCollection Buffers
230 {
231 get
232 {
233 return bufferDictionary.Values;
234 }
235 }
236 /// <summary>
200 237 /// 长度
201 238 /// </summary>
202 239 public int Count
Modified XFE各类拓展/CyberComm.cs +4 -4
@@ -1195,9 +1195,9 @@ namespace XFE各类拓展.CyberComm
1195 1195 {
1196 1196 byte[] sendBuffer = Encoding.UTF8.GetBytes(new string[] { messageId, message }.ToXFEString());
1197 1197 await ClientWebSocket.SendAsync(new ArraySegment<byte>(sendBuffer), WebSocketMessageType.Text, true, CancellationToken.None);
1198 var endTask = Task.Run(() =>
1198 var endTask = Task.Run(async () =>
1199 1199 {
1200 Task.Delay(timeout);
1200 await Task.Delay(timeout);
1201 1201 UpdateTaskTrigger?.Invoke(false, messageId);
1202 1202 });
1203 1203 return await new XFEWaitTask<bool>(ref UpdateTaskTrigger, messageId);
@@ -1253,9 +1253,9 @@ namespace XFE各类拓展.CyberComm
1253 1253 {
1254 1254 var xFEBuffer = new XFEBuffer(Sender, message, "Type", Encoding.UTF8.GetBytes(signature), "ID", Encoding.UTF8.GetBytes(messageId));
1255 1255 await ClientWebSocket.SendAsync(new ArraySegment<byte>(xFEBuffer.ToBuffer()), WebSocketMessageType.Binary, true, CancellationToken.None);
1256 var endTask = Task.Run(() =>
1256 var endTask = Task.Run(async () =>
1257 1257 {
1258 Task.Delay(timeout);
1258 await Task.Delay(timeout);
1259 1259 UpdateTaskTrigger?.Invoke(false, messageId);
1260 1260 });
1261 1261 return await new XFEWaitTask<bool>(ref UpdateTaskTrigger, messageId);