Skip to content

Commit 22e6e41

Browse files
committed
add: 通道基类、utf8缓存机制
1 parent 7c9e9a6 commit 22e6e41

File tree

7 files changed

+414
-203
lines changed

7 files changed

+414
-203
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace LLCOM.Models.Channels;
8+
9+
public class BaseChannel(int bufferSize = 1024 * 10)
10+
{
11+
private readonly byte[] _buffer = new byte[bufferSize];
12+
private int _bufferLength = 0;
13+
14+
/// <summary>
15+
/// 当数据被添加之后,被触发
16+
/// </summary>
17+
public EventHandler<int>? DataAddedEvent;
18+
19+
/// <summary>
20+
/// 向buffer中添加数据
21+
/// </summary>
22+
/// <param name="data">数据</param>
23+
/// <returns>成功添加进buffer的长度</returns>
24+
public int AddData(Span<byte> data)
25+
{
26+
if (data.Length == 0)
27+
return 0;
28+
29+
var availableSpace = _buffer.Length - _bufferLength;
30+
if (availableSpace <= 0)
31+
return 0; // Buffer is full
32+
33+
var lengthToCopy = Math.Min(data.Length, availableSpace);
34+
data[..lengthToCopy].CopyTo(_buffer.AsSpan(_bufferLength));
35+
_bufferLength += lengthToCopy;
36+
37+
DataAddedEvent?.Invoke(this, lengthToCopy); // Trigger the event after data is added
38+
return lengthToCopy;
39+
}
40+
41+
/// <summary>
42+
/// 获取当前buffer中的数据
43+
/// </summary>
44+
/// <param name="maxLength">最大读取长度</param>
45+
/// <param name="keepUtf8Character">是否检查utf8截断</param>
46+
/// <returns>获取到的数据</returns>
47+
public byte[] GetData(int? maxLength = null, bool keepUtf8Character = false)
48+
{
49+
var length = maxLength ?? _bufferLength;
50+
if (length <= 0 || length > _bufferLength)
51+
length = _bufferLength;
52+
if (length == 0)
53+
return [];
54+
55+
if (keepUtf8Character)//如果需要保留utf8字符,则需要判断末尾是否有不完整的utf8字符
56+
length = Services.StringHelper.GetCompleteUtf8Length(_buffer.AsSpan(0, length));
57+
58+
if (length == 0)
59+
return [];
60+
61+
var result = new byte[length];
62+
Array.Copy(_buffer, result, length);
63+
_bufferLength -= length;
64+
if (_bufferLength > 0)
65+
Array.Copy(_buffer, length, _buffer, 0, _bufferLength);
66+
return result;
67+
}
68+
}

llcomNext/LLCOM/Models/PacketData.cs

Lines changed: 0 additions & 173 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Avalonia.Media;
6+
using LLCOM.Services;
7+
8+
namespace LLCOM.Models;
9+
10+
public class StringHelper
11+
{
12+
public StringHelper(byte[] data, MessageWay way, string channel, string? extra = null,
13+
Encoding? encoding = null, bool readable = true, string? s = null, IBrush? brush = null)
14+
{
15+
Data = data;
16+
Way = way;
17+
Channel = channel;
18+
Extra = extra ?? Time.ToString("yyyy/MM/dd HH:mm:ss.fff");
19+
encoding ??= Encoding.UTF8;
20+
String = s ?? Services.StringHelper.GenerateString(Data, encoding, readable);
21+
HexString = way == MessageWay.Unknown ? String : Services.StringHelper.GenerateHexString(Data);
22+
}
23+
24+
/// <summary>
25+
/// 此包收到的时间
26+
/// </summary>
27+
public DateTime Time { get; set; } = DateTime.Now;
28+
29+
/// <summary>
30+
/// 包内的原始数据
31+
/// </summary>
32+
public byte[] Data { get; set; }
33+
34+
public string HexString { get; }
35+
36+
/// <summary>
37+
/// 包的额外信息,一般是日期时间的字符串展示
38+
/// </summary>
39+
public string Extra { get; set; }
40+
41+
/// <summary>
42+
/// 该包的字符串表示
43+
/// </summary>
44+
public string String { get; set; }
45+
46+
/// <summary>
47+
/// 数据包的方向
48+
/// </summary>
49+
public MessageWay Way { get; set; }
50+
51+
/// <summary>
52+
/// 消息通道类型
53+
/// </summary>
54+
public string Channel { get; set; }
55+
56+
public bool IsWayUnknown => Way == MessageWay.Unknown;
57+
public bool IsWaySend => Way == MessageWay.Send;
58+
public bool IsWayReceive => Way == MessageWay.Receive;
59+
}
60+
61+
public enum MessageWay
62+
{
63+
Unknown,
64+
/// <summary>
65+
/// 从该软件发出的数据包
66+
/// </summary>
67+
Send,
68+
/// <summary>
69+
/// 从外部发到该软件的数据包
70+
/// </summary>
71+
Receive
72+
}

0 commit comments

Comments
 (0)