private int StartFrameBody(int readBytes, byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
{
if (readBytes == 0)
{
//EOF
asyncRequest?.CompleteUser(0);
return 0;
}
if (!(readBytes == _ReadHeader.Length))
{
NetEventSource.Fail(this, $"Frame size must be 4 but received {readBytes} bytes.");
}
// Replace readBytes with the body size recovered from the header content.
readBytes = _ReadHeader[3];
readBytes = (readBytes << 8) | _ReadHeader[2];
readBytes = (readBytes << 8) | _ReadHeader[1];
readBytes = (readBytes << 8) | _ReadHeader[0];
//
// The body carries 4 bytes for trailer size slot plus trailer, hence <=4 frame size is always an error.
// Additionally we'd like to restrict the read frame size to 64k.
//
if (readBytes <= 4 || readBytes > NegoState.MaxReadFrameSize)
{
throw new IOException(SR.net_frame_read_size);
}
//
// Always pass InternalBuffer for SSPI "in place" decryption.
// A user buffer can be shared by many threads in that case decryption/integrity check may fail cause of data corruption.
//
EnsureInternalBufferSize(readBytes);
if (asyncRequest != null)
{
asyncRequest.SetNextRequest(InternalBuffer, 0, readBytes, s_readCallback);
_FrameReader.AsyncReadPacket(asyncRequest);
if (!asyncRequest.MustCompleteSynchronously)
{
return 0;
}
readBytes = asyncRequest.Result;
}
else //Sync
{
readBytes = _FrameReader.ReadPacket(InternalBuffer, 0, readBytes);
}
return ProcessFrameBody(readBytes, buffer, offset, count, asyncRequest);
}