-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDataPackageHandlerBase.cs
More file actions
68 lines (60 loc) · 3.21 KB
/
DataPackageHandlerBase.cs
File metadata and controls
68 lines (60 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
namespace BootstrapBlazor.Socket.DataHandlers;
/// <summary>
/// Provides a base implementation for handling data packages in a communication system.
/// </summary>
/// <remarks>This abstract class defines the core contract for receiving and sending data packages. Derived
/// classes should override and extend its functionality to implement specific data handling logic. The default
/// implementation simply returns the provided data.</remarks>
public abstract class DataPackageHandlerBase : IDataPackageHandler
{
private Memory<byte> _lastReceiveBuffer = Memory<byte>.Empty;
/// <summary>
/// <inheritdoc/>
/// </summary>
public Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack { get; set; }
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="data"></param>
/// <param name="token"></param>
/// <returns></returns>
public abstract ValueTask HandlerAsync(ReadOnlyMemory<byte> data, CancellationToken token = default);
/// <summary>
/// Handles the processing of a sticky package by adjusting the provided buffer and length.
/// </summary>
/// <remarks>This method processes the portion of the buffer beyond the specified length and updates the
/// internal state accordingly. The caller must ensure that the <paramref name="buffer"/> contains sufficient data
/// for the specified <paramref name="length"/>.</remarks>
/// <param name="buffer">The memory buffer containing the data to process.</param>
/// <param name="length">The length of the valid data within the buffer.</param>
protected void SlicePackage(ReadOnlyMemory<byte> buffer, int length)
{
_lastReceiveBuffer = buffer[length..].ToArray().AsMemory();
}
/// <summary>
/// Concatenates the provided buffer with any previously stored data and returns the combined result.
/// </summary>
/// <remarks>This method combines the provided buffer with any data stored in the internal buffer. After
/// concatenation, the internal buffer is cleared. The returned memory block is allocated from a shared memory pool
/// and should be used promptly to avoid holding onto pooled resources.</remarks>
/// <param name="buffer">The buffer to concatenate with the previously stored data. Must not be empty.</param>
/// <returns>A <see cref="Memory{T}"/> instance containing the concatenated data. If no previously stored data exists, the
/// method returns the input <paramref name="buffer"/>.</returns>
protected ReadOnlyMemory<byte> ConcatBuffer(ReadOnlyMemory<byte> buffer)
{
if (_lastReceiveBuffer.IsEmpty)
{
return buffer;
}
// 计算缓存区长度
Memory<byte> merged = new byte[_lastReceiveBuffer.Length + buffer.Length];
_lastReceiveBuffer.CopyTo(merged);
buffer.CopyTo(merged[_lastReceiveBuffer.Length..]);
// Clear the sticky buffer
_lastReceiveBuffer = Memory<byte>.Empty;
return merged;
}
}