public void ChunkedRequest()
{
if (requests.Messages.Count != 0)
rig.Manager.PieceManager.Picker.CancelRequests(id);
requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List<PeerId>(), 256);
byte[] sendBuffer = requests.Encode();
int offset = 0;
amountSent = Math.Min(sendBuffer.Length - offset, 2048);
IAsyncResult sendResult = connection.BeginSend(sendBuffer, offset, amountSent, null, null);
while (sendResult.AsyncWaitHandle.WaitOne(10, true))
{
Assert.AreEqual(amountSent, connection.EndSend(sendResult), "#1." + amountSent);
offset += amountSent;
amountSent = Math.Min(sendBuffer.Length - offset, 2048);
if (amountSent == 0)
Assert.Fail("This should never happen");
sendResult = connection.BeginSend(sendBuffer, offset, amountSent, null, null);
}
byte[] buffer = new byte[1024 * 1024 * 3];
IAsyncResult receiveResult = connection.BeginReceive(buffer, 0, 4, null, null);
CompleteSendOrReceiveFirst(buffer, receiveResult, sendResult);
}