@@ -147,12 +147,12 @@ internal static class Heartbeat
147147 Constants . FrameEnd
148148 } ;
149149
150- public static Memory < byte > GetHeartbeatFrame ( )
150+ public static ReadOnlyMemory < byte > GetHeartbeatFrame ( ArrayPool < byte > pool )
151151 {
152152 // Is returned by SocketFrameHandler.WriteLoop
153- var buffer = ArrayPool < byte > . Shared . Rent ( FrameSize ) ;
153+ var buffer = pool . Rent ( FrameSize ) ;
154154 Payload . CopyTo ( buffer ) ;
155- return new Memory < byte > ( buffer , 0 , FrameSize ) ;
155+ return new ReadOnlyMemory < byte > ( buffer , 0 , FrameSize ) ;
156156 }
157157 }
158158 }
@@ -163,13 +163,15 @@ public static Memory<byte> GetHeartbeatFrame()
163163 public readonly int Channel ;
164164 public readonly ReadOnlyMemory < byte > Payload ;
165165 private readonly byte [ ] _rentedArray ;
166+ private readonly ArrayPool < byte > _rentedArrayOwner ;
166167
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 )
168169 {
169170 Type = type ;
170171 Channel = channel ;
171172 Payload = payload ;
172173 _rentedArray = rentedArray ;
174+ _rentedArrayOwner = rentedArrayOwner ;
173175 }
174176
175177 private static void ProcessProtocolHeader ( Stream reader )
@@ -203,7 +205,7 @@ private static void ProcessProtocolHeader(Stream reader)
203205 }
204206 }
205207
206- internal static InboundFrame ReadFrom ( Stream reader , byte [ ] frameHeaderBuffer )
208+ internal static InboundFrame ReadFrom ( Stream reader , byte [ ] frameHeaderBuffer , ArrayPool < byte > pool )
207209 {
208210 int type = default ;
209211 try
@@ -242,7 +244,7 @@ internal static InboundFrame ReadFrom(Stream reader, byte[] frameHeaderBuffer)
242244 const int EndMarkerLength = 1 ;
243245 // Is returned by InboundFrame.Dispose in Connection.MainLoopIteration
244246 var readSize = payloadSize + EndMarkerLength ;
245- byte [ ] payloadBytes = ArrayPool < byte > . Shared . Rent ( readSize ) ;
247+ byte [ ] payloadBytes = pool . Rent ( readSize ) ;
246248 int bytesRead = 0 ;
247249 try
248250 {
@@ -254,22 +256,22 @@ internal static InboundFrame ReadFrom(Stream reader, byte[] frameHeaderBuffer)
254256 catch ( Exception )
255257 {
256258 // Early EOF.
257- ArrayPool < byte > . Shared . Return ( payloadBytes ) ;
259+ pool . Return ( payloadBytes ) ;
258260 throw new MalformedFrameException ( $ "Short frame - expected to read { readSize } bytes, only got { bytesRead } bytes") ;
259261 }
260262
261263 if ( payloadBytes [ payloadSize ] != Constants . FrameEnd )
262264 {
263- ArrayPool < byte > . Shared . Return ( payloadBytes ) ;
265+ pool . Return ( payloadBytes ) ;
264266 throw new MalformedFrameException ( $ "Bad frame end marker: { payloadBytes [ payloadSize ] } ") ;
265267 }
266268
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 ) ;
268270 }
269271
270272 public void Dispose ( )
271273 {
272- ArrayPool < byte > . Shared . Return ( _rentedArray ) ;
274+ _rentedArrayOwner . Return ( _rentedArray ) ;
273275 }
274276
275277 public override string ToString ( )
0 commit comments