bool HandleReadComplete(TransportAsyncCallbackArgs args)
{
bool completed = true;
Exception exception = null;
if (args.Exception != null)
{
exception = args.Exception;
}
else if (args.BytesTransfered == 0)
{
exception = new ObjectDisposedException(this.transport.ToString());
}
else if (args.BytesTransfered < args.Count)
{
args.SetBuffer(args.Buffer, args.Offset + args.BytesTransfered, args.Count - args.BytesTransfered);
completed = false;
}
if (completed)
{
if (exception != null || object.ReferenceEquals(args.CompletedCallback, onFrameComplete))
{
ByteBuffer buffer = null;
if (exception == null)
{
buffer = new ByteBuffer(args.Buffer, 0, args.Buffer.Length);
this.parent.OnReceiveBuffer(buffer);
}
else
{
this.parent.OnIoFault(exception);
}
}
else
{
// read size completed ok
uint size = AmqpBitConverter.ReadUInt(this.sizeBuffer, 0, this.sizeBuffer.Length);
if (size > this.maxFrameSize)
{
completed = true;
exception = new AmqpException(AmqpErrorCode.FramingError, CommonResources.GetString(CommonResources.InvalidFrameSize, size, this.maxFrameSize));
this.parent.OnIoFault(exception);
}
else
{
byte[] frameBuffer = new byte[size];
Buffer.BlockCopy(this.sizeBuffer, 0, frameBuffer, 0, this.sizeBuffer.Length);
args.SetBuffer(frameBuffer, this.sizeBuffer.Length, (int)size - this.sizeBuffer.Length);
args.CompletedCallback = onFrameComplete;
completed = false;
}
}
}
return completed;
}
}