@@ -147,12 +147,12 @@ internal static class Heartbeat
147
147
Constants . FrameEnd
148
148
} ;
149
149
150
- public static Memory < byte > GetHeartbeatFrame ( )
150
+ public static ReadOnlyMemory < byte > GetHeartbeatFrame ( ArrayPool < byte > pool )
151
151
{
152
152
// Is returned by SocketFrameHandler.WriteLoop
153
- var buffer = ArrayPool < byte > . Shared . Rent ( FrameSize ) ;
153
+ var buffer = pool . Rent ( FrameSize ) ;
154
154
Payload . CopyTo ( buffer ) ;
155
- return new Memory < byte > ( buffer , 0 , FrameSize ) ;
155
+ return new ReadOnlyMemory < byte > ( buffer , 0 , FrameSize ) ;
156
156
}
157
157
}
158
158
}
@@ -163,13 +163,15 @@ public static Memory<byte> GetHeartbeatFrame()
163
163
public readonly int Channel ;
164
164
public readonly ReadOnlyMemory < byte > Payload ;
165
165
private readonly byte [ ] _rentedArray ;
166
+ private readonly ArrayPool < byte > _rentedArrayOwner ;
166
167
167
- private InboundFrame ( FrameType type , int channel , ReadOnlyMemory < byte > payload , byte [ ] rentedArray )
168
+ private InboundFrame ( FrameType type , int channel , ReadOnlyMemory < byte > payload , byte [ ] rentedArray , ArrayPool < byte > rentedArrayOwner )
168
169
{
169
170
Type = type ;
170
171
Channel = channel ;
171
172
Payload = payload ;
172
173
_rentedArray = rentedArray ;
174
+ _rentedArrayOwner = rentedArrayOwner ;
173
175
}
174
176
175
177
private static void ProcessProtocolHeader ( Stream reader )
@@ -203,7 +205,7 @@ private static void ProcessProtocolHeader(Stream reader)
203
205
}
204
206
}
205
207
206
- internal static InboundFrame ReadFrom ( Stream reader , byte [ ] frameHeaderBuffer )
208
+ internal static InboundFrame ReadFrom ( Stream reader , byte [ ] frameHeaderBuffer , ArrayPool < byte > pool )
207
209
{
208
210
int type = default ;
209
211
try
@@ -242,7 +244,7 @@ internal static InboundFrame ReadFrom(Stream reader, byte[] frameHeaderBuffer)
242
244
const int EndMarkerLength = 1 ;
243
245
// Is returned by InboundFrame.Dispose in Connection.MainLoopIteration
244
246
var readSize = payloadSize + EndMarkerLength ;
245
- byte [ ] payloadBytes = ArrayPool < byte > . Shared . Rent ( readSize ) ;
247
+ byte [ ] payloadBytes = pool . Rent ( readSize ) ;
246
248
int bytesRead = 0 ;
247
249
try
248
250
{
@@ -254,22 +256,22 @@ internal static InboundFrame ReadFrom(Stream reader, byte[] frameHeaderBuffer)
254
256
catch ( Exception )
255
257
{
256
258
// Early EOF.
257
- ArrayPool < byte > . Shared . Return ( payloadBytes ) ;
259
+ pool . Return ( payloadBytes ) ;
258
260
throw new MalformedFrameException ( $ "Short frame - expected to read { readSize } bytes, only got { bytesRead } bytes") ;
259
261
}
260
262
261
263
if ( payloadBytes [ payloadSize ] != Constants . FrameEnd )
262
264
{
263
- ArrayPool < byte > . Shared . Return ( payloadBytes ) ;
265
+ pool . Return ( payloadBytes ) ;
264
266
throw new MalformedFrameException ( $ "Bad frame end marker: { payloadBytes [ payloadSize ] } ") ;
265
267
}
266
268
267
- return new InboundFrame ( ( FrameType ) type , channel , new Memory < byte > ( payloadBytes , 0 , payloadSize ) , payloadBytes ) ;
269
+ return new InboundFrame ( ( FrameType ) type , channel , new Memory < byte > ( payloadBytes , 0 , payloadSize ) , payloadBytes , pool ) ;
268
270
}
269
271
270
272
public void Dispose ( )
271
273
{
272
- ArrayPool < byte > . Shared . Return ( _rentedArray ) ;
274
+ _rentedArrayOwner . Return ( _rentedArray ) ;
273
275
}
274
276
275
277
public override string ToString ( )
0 commit comments