-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerFrameCodec.cs
More file actions
119 lines (103 loc) · 3.51 KB
/
Copy pathWorkerFrameCodec.cs
File metadata and controls
119 lines (103 loc) · 3.51 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System.Buffers.Binary;
namespace DotPython.Protocol;
public sealed class WorkerFrameCodec
{
private const int HeaderSize = sizeof(int);
private readonly int _maxMessageBytes;
public WorkerFrameCodec(int maxMessageBytes = WorkerProtocolLimits.DefaultMaxMessageBytes)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maxMessageBytes);
_maxMessageBytes = maxMessageBytes;
}
public async ValueTask WriteAsync(
Stream stream,
WorkerEnvelope envelope,
CancellationToken cancellationToken = default
)
{
ArgumentNullException.ThrowIfNull(stream);
var payload = WorkerProtocolSerializer.Serialize(envelope);
if (payload.Length > _maxMessageBytes)
{
throw LimitExceeded(payload.Length);
}
var header = new byte[HeaderSize];
BinaryPrimitives.WriteInt32LittleEndian(header, payload.Length);
await stream.WriteAsync(header, cancellationToken).ConfigureAwait(false);
await stream.WriteAsync(payload, cancellationToken).ConfigureAwait(false);
await stream.FlushAsync(cancellationToken).ConfigureAwait(false);
}
public async ValueTask<WorkerEnvelope?> ReadAsync(
Stream stream,
CancellationToken cancellationToken = default
)
{
ArgumentNullException.ThrowIfNull(stream);
var header = new byte[HeaderSize];
var headerBytes = await ReadAtMostAsync(stream, header, cancellationToken)
.ConfigureAwait(false);
if (headerBytes == 0)
{
return null;
}
if (headerBytes != HeaderSize)
{
throw Malformed("The worker frame ended before its length prefix was complete.");
}
var length = BinaryPrimitives.ReadInt32LittleEndian(header);
if (length <= 0)
{
throw Malformed("The worker frame length must be positive.");
}
if (length > _maxMessageBytes)
{
throw LimitExceeded(length);
}
var payload = new byte[length];
var payloadBytes = await ReadAtMostAsync(stream, payload, cancellationToken)
.ConfigureAwait(false);
if (payloadBytes != length)
{
throw Malformed("The worker frame ended before its declared payload was complete.");
}
return WorkerProtocolSerializer.Deserialize(payload);
}
private static async ValueTask<int> ReadAtMostAsync(
Stream stream,
Memory<byte> buffer,
CancellationToken cancellationToken
)
{
var total = 0;
while (total < buffer.Length)
{
var read = await stream
.ReadAsync(buffer[total..], cancellationToken)
.ConfigureAwait(false);
if (read == 0)
{
break;
}
total += read;
}
return total;
}
private static WorkerProtocolException LimitExceeded(int length) =>
new(
new WorkerFault(
WorkerProtocolFaultCodes.LimitExceeded,
WorkerFaultPhase.Framing,
$"Worker message length {length} bytes exceeds the configured limit.",
false
)
);
private static WorkerProtocolException Malformed(string message) =>
new(
new WorkerFault(
WorkerProtocolFaultCodes.HandshakeFailed,
WorkerFaultPhase.Framing,
message,
false
)
);
}