bool CompleteReadBytes(IAsyncResult result) { int bytesRead = channel.EndSocketReceive(result); bytesReadTotal += bytesRead; if (bytesRead == 0) { if (size == 0 || !throwOnEmpty) { channel.bufferManager.ReturnBuffer(this.buffer); this.buffer = null; return(true); } else { throw new CommunicationException("Premature EOF reached"); } } while (bytesReadTotal < size) { IAsyncResult socketReceiveResult = channel.BeginSocketReceive(buffer, bytesReadTotal, size - bytesReadTotal, OnReadBytes, this); if (!socketReceiveResult.CompletedSynchronously) { return(false); } } return(true); }
public SocketReceiveAsyncResult(int size, bool throwOnEmpty, SizedTcpBaseChannel channel, AsyncCallback callback, object state) : base(callback, state) { this.size = size; this.channel = channel; this.throwOnEmpty = throwOnEmpty; this.bytesReadTotal = 0; this.buffer = channel.bufferManager.TakeBuffer(size); bool success = false; try { IAsyncResult socketReceiveResult = channel.BeginSocketReceive(this.buffer, bytesReadTotal, size, OnReadBytes, this); if (socketReceiveResult.CompletedSynchronously) { if (CompleteReadBytes(socketReceiveResult)) { base.Complete(true); } } success = true; } finally { if (!success) { this.Cleanup(); } } }