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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! Error and status types for protocol-level message assembly.
//!
//! These enums keep the message assembly logic decoupled from specific
//! protocols while surfacing precise diagnostics for behavioural tests.
use std::num::NonZeroUsize;
use thiserror::Error;
use super::{FrameSequence, MessageKey};
/// Result of feeding a frame into a [`MessageSeries`](super::MessageSeries).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MessageSeriesStatus {
/// The message still expects more frames.
Incomplete,
/// The frame completed the message.
Complete,
}
/// Errors produced by [`MessageSeries`](super::MessageSeries).
#[derive(Clone, Copy, Debug, Error, PartialEq, Eq)]
pub enum MessageSeriesError {
/// The frame belongs to a different message.
#[error("message key mismatch: expected {expected}, found {found}")]
KeyMismatch {
/// Message key currently being assembled.
expected: MessageKey,
/// Key carried by the incoming frame.
found: MessageKey,
},
/// A frame arrived out of order (sequence number mismatch).
#[error("frame sequence mismatch: expected {expected}, found {found}")]
SequenceMismatch {
/// Sequence the series expected next.
expected: FrameSequence,
/// Sequence carried by the frame that was received.
found: FrameSequence,
},
/// The series already consumed a final frame.
#[error("message series already complete")]
SeriesComplete,
/// The frame sequence overflowed `u32::MAX`.
#[error("frame sequence overflow after {last}")]
SequenceOverflow {
/// Last valid sequence observed before overflow occurred.
last: FrameSequence,
},
/// A continuation frame arrived before any first frame for this key.
#[error("continuation frame received without first frame for key {key}")]
MissingFirstFrame {
/// Message key that has no in-progress assembly.
key: MessageKey,
},
/// Duplicate frame detected (same sequence number already processed).
#[error("duplicate frame: sequence {sequence} already received for key {key}")]
DuplicateFrame {
/// Message key the duplicate belongs to.
key: MessageKey,
/// Sequence number that was duplicated.
sequence: FrameSequence,
},
/// A continuation frame arrived without a sequence number after sequence
/// tracking was activated.
#[error("continuation frame missing sequence number for key {key}")]
MissingSequence {
/// Message key that expected a sequence number.
key: MessageKey,
},
/// Continuation frame body length in header does not match actual payload.
#[error(
"continuation body length mismatch for key {key}: header declares {header_len} bytes, got \
{actual_len}"
)]
ContinuationBodyLengthMismatch {
/// Message key the frame belongs to.
key: MessageKey,
/// Length declared in header.
header_len: usize,
/// Actual length of body slice.
actual_len: usize,
},
}
/// Errors produced during message assembly.
#[derive(Clone, Debug, Error, PartialEq, Eq)]
pub enum MessageAssemblyError {
/// A continuity or ordering error from the series tracker.
#[error("series validation failed: {0}")]
Series(#[from] MessageSeriesError),
/// A first frame arrived for a key that already has an in-progress
/// assembly.
#[error("duplicate first frame for key {key}")]
DuplicateFirstFrame {
/// Message key that already has an active assembly.
key: MessageKey,
},
/// The assembled message would exceed the configured size limit.
#[error("message {key} exceeds size limit: {attempted} bytes > {limit} bytes")]
MessageTooLarge {
/// Message key that exceeded the limit.
key: MessageKey,
/// Total size that triggered the guard, counting retained metadata
/// plus body bytes. An unrepresentable sum is reported as
/// `usize::MAX`.
attempted: usize,
/// Configured size cap.
limit: NonZeroUsize,
},
/// Accepting this frame would exceed the per-connection buffering budget.
#[error("connection budget exceeded for key {key}: {attempted} bytes > {limit} bytes")]
ConnectionBudgetExceeded {
/// Message key whose frame triggered the guard.
key: MessageKey,
/// Total bytes that would result from accepting the frame.
attempted: usize,
/// Configured per-connection byte cap.
limit: NonZeroUsize,
},
/// Accepting this frame would exceed the in-flight assembly byte budget.
#[error("in-flight budget exceeded for key {key}: {attempted} bytes > {limit} bytes")]
InFlightBudgetExceeded {
/// Message key whose frame triggered the guard.
key: MessageKey,
/// Total bytes that would result from accepting the frame.
attempted: usize,
/// Configured in-flight byte cap.
limit: NonZeroUsize,
},
/// The first frame declared fewer total body bytes than it carried.
///
/// A declaration smaller than the first frame's own body can never be
/// satisfied, and accepting it would let a peer understate the size of the
/// message that is actually dispatched.
#[error(
"declared total body length too small for key {key}: declared {declared} bytes, first \
frame carries {body_len}"
)]
DeclaredTotalTooSmall {
/// Message key whose declaration is unsatisfiable.
key: MessageKey,
/// Total body length declared by the first frame.
declared: usize,
/// Body bytes actually carried by the first frame.
body_len: usize,
},
/// Appending this frame would carry the body past its declared total.
#[error(
"declared total body length exceeded for key {key}: declared {declared} bytes, \
accumulated {attempted}"
)]
DeclaredTotalExceeded {
/// Message key whose declaration was exceeded.
key: MessageKey,
/// Total body length declared by the first frame.
declared: usize,
/// Body bytes that would result from accepting the frame.
attempted: usize,
},
/// The completed message's body length differed from its declared total.
#[error(
"declared total body length mismatch for key {key}: declared {declared} bytes, assembled \
{actual}"
)]
DeclaredTotalMismatch {
/// Message key whose declaration was not honoured.
key: MessageKey,
/// Total body length declared by the first frame.
declared: usize,
/// Body bytes actually assembled.
actual: usize,
},
}