@@ -215,8 +215,14 @@ public async override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byt
215
215
await ReadNextFrameAsync ( cancellationToken ) ;
216
216
}
217
217
218
- // Handle fragmentation, remember the first frame type
219
218
int opCode = _frameInProgress . OpCode ;
219
+
220
+ if ( opCode == Constants . OpCodes . CloseFrame )
221
+ {
222
+ return await ProcessCloseFrameAsync ( cancellationToken ) ;
223
+ }
224
+
225
+ // Handle fragmentation, remember the first frame type
220
226
if ( opCode == Constants . OpCodes . ContinuationFrame )
221
227
{
222
228
if ( ! _firstDataOpCode . HasValue )
@@ -230,11 +236,6 @@ public async override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byt
230
236
_firstDataOpCode = opCode ;
231
237
}
232
238
233
- if ( opCode == Constants . OpCodes . CloseFrame )
234
- {
235
- return await ProcessCloseFrameAsync ( cancellationToken ) ;
236
- }
237
-
238
239
// Make sure there's at least some data in the buffer
239
240
int bytesToBuffer = ( int ) Math . Min ( ( long ) _receiveBuffer . Length , _frameBytesRemaining ) ;
240
241
await EnsureDataAvailableOrReadAsync ( bytesToBuffer , cancellationToken ) ;
@@ -329,6 +330,11 @@ private async Task ReadNextFrameAsync(CancellationToken cancellationToken)
329
330
_frameInProgress = null ;
330
331
}
331
332
}
333
+ else if ( _firstDataOpCode . HasValue && _frameInProgress . OpCode != Constants . OpCodes . ContinuationFrame )
334
+ {
335
+ // A data frame is already in progress, but this new frame is not a continuation frame.
336
+ await SendErrorAbortAndThrow ( WebSocketCloseStatus . ProtocolError , "Expected a continuation frame: " + _frameInProgress . OpCode , cancellationToken ) ;
337
+ }
332
338
}
333
339
334
340
private async Task EnsureDataAvailableOrReadAsync ( int bytesNeeded , CancellationToken cancellationToken )
0 commit comments