@@ -44,34 +44,19 @@ internal sealed class CommandAssembler
44
44
{
45
45
private const int MaxArrayOfBytesSize = 2_147_483_591 ;
46
46
47
- private ProtocolCommandId _commandId ;
48
- private RentedMemory _methodMemory ;
49
- private RentedMemory _headerMemory ;
50
- private RentedMemory _bodyMemory ;
47
+ private readonly IncomingCommand _currentCommand ;
48
+ private readonly uint _maxBodyLength ;
49
+
51
50
private int _remainingBodyByteCount ;
52
- private int _offset ;
53
51
private AssemblyState _state ;
54
52
55
- private readonly uint _maxBodyLength ;
56
-
57
53
public CommandAssembler ( uint maxBodyLength )
58
54
{
55
+ _currentCommand = new IncomingCommand ( ) ;
59
56
_maxBodyLength = maxBodyLength ;
60
- Reset ( ) ;
61
- }
62
-
63
- private void Reset ( )
64
- {
65
- _commandId = default ;
66
- _methodMemory = default ;
67
- _headerMemory = default ;
68
- _bodyMemory = default ;
69
- _remainingBodyByteCount = 0 ;
70
- _offset = 0 ;
71
- _state = AssemblyState . ExpectingMethod ;
72
57
}
73
58
74
- public void HandleFrame ( InboundFrame frame , out IncomingCommand command )
59
+ public IncomingCommand ? HandleFrame ( InboundFrame frame )
75
60
{
76
61
switch ( _state )
77
62
{
@@ -88,13 +73,14 @@ public void HandleFrame(InboundFrame frame, out IncomingCommand command)
88
73
89
74
if ( _state != AssemblyState . Complete )
90
75
{
91
- command = IncomingCommand . Empty ;
92
- return ;
76
+ return default ;
93
77
}
94
78
95
79
RabbitMqClientEventSource . Log . CommandReceived ( ) ;
96
- command = new IncomingCommand ( _commandId , _methodMemory , _headerMemory , _bodyMemory ) ;
97
- Reset ( ) ;
80
+ _remainingBodyByteCount = 0 ;
81
+ _state = AssemblyState . ExpectingMethod ;
82
+
83
+ return _currentCommand ;
98
84
}
99
85
100
86
private void ParseMethodFrame ( InboundFrame frame )
@@ -104,10 +90,10 @@ private void ParseMethodFrame(InboundFrame frame)
104
90
throw new UnexpectedFrameException ( frame . Type ) ;
105
91
}
106
92
107
- _commandId = ( ProtocolCommandId ) NetworkOrderDeserializer . ReadUInt32 ( frame . Payload . Span ) ;
108
- _methodMemory = frame . TakeoverPayload ( Framing . Method . ArgumentsOffset ) ;
93
+ _currentCommand . CommandId = ( ProtocolCommandId ) NetworkOrderDeserializer . ReadUInt32 ( frame . Payload . Span ) ;
94
+ _currentCommand . Method = frame . TakeoverPayload ( Framing . Method . ArgumentsOffset ) ;
109
95
110
- switch ( _commandId )
96
+ switch ( _currentCommand . CommandId )
111
97
{
112
98
// Commands with payload
113
99
case ProtocolCommandId . BasicGetOk :
@@ -154,7 +140,7 @@ private void ParseHeaderFrame(InboundFrame frame)
154
140
}
155
141
else
156
142
{
157
- _headerMemory = frame . TakeoverPayload ( Framing . Header . HeaderArgumentOffset ) ;
143
+ _currentCommand . Header = frame . TakeoverPayload ( Framing . Header . HeaderArgumentOffset ) ;
158
144
}
159
145
160
146
_remainingBodyByteCount = ( int ) totalBodyBytes ;
@@ -174,25 +160,25 @@ private void ParseBodyFrame(InboundFrame frame)
174
160
throw new MalformedFrameException ( $ "Overlong content body received - { _remainingBodyByteCount } bytes remaining, { payloadLength } bytes received") ;
175
161
}
176
162
177
- if ( _bodyMemory . RentedArray is null )
163
+ if ( _currentCommand . Body . RentedArray is null )
178
164
{
179
165
// check for single frame payload for an early exit
180
166
if ( payloadLength == _remainingBodyByteCount )
181
167
{
182
- _bodyMemory = frame . TakeoverPayload ( 0 ) ;
168
+ _currentCommand . Body = frame . TakeoverPayload ( 0 ) ;
183
169
_state = AssemblyState . Complete ;
184
170
return ;
185
171
}
186
172
187
173
// Is returned by IncomingCommand.ReturnPayload in Session.HandleFrame
188
174
var rentedBodyArray = ArrayPool < byte > . Shared . Rent ( _remainingBodyByteCount ) ;
189
- _bodyMemory = new RentedMemory ( new ReadOnlyMemory < byte > ( rentedBodyArray , 0 , _remainingBodyByteCount ) , rentedBodyArray ) ;
175
+ _currentCommand . Body . RentedArray = rentedBodyArray ;
176
+ _currentCommand . Body . Memory = new ReadOnlyMemory < byte > ( rentedBodyArray , 0 , _remainingBodyByteCount ) ;
190
177
}
191
178
192
- frame . Payload . Span . CopyTo ( _bodyMemory . RentedArray . AsSpan ( _offset ) ) ;
179
+ frame . Payload . Span . CopyTo ( _currentCommand . Body . RentedArray . AsSpan ( _currentCommand . Body . Memory . Length - _remainingBodyByteCount ) ) ;
193
180
frame . TryReturnPayload ( ) ;
194
181
_remainingBodyByteCount -= payloadLength ;
195
- _offset += payloadLength ;
196
182
UpdateContentBodyState ( ) ;
197
183
}
198
184
0 commit comments