private bool CheckCompletionBeforeNextRead(int bytes) { if (bytes == 0) { // 0 bytes was requested or EOF in the beginning of a frame, the caller should decide whether it's OK. if (_totalRead == 0) { _request.CompleteRequest(0); return(true); } // EOF in the middle of a frame. throw new IOException(SR.net_io_eof); } if (_totalRead + bytes > _request.Count) { if (GlobalLog.IsEnabled) { GlobalLog.AssertFormat("FixedSizeReader::CheckCompletion()|State got out of range. Total:{0} Count:{1}", _totalRead + bytes, _request.Count); } Debug.Fail("FixedSizeReader::CheckCompletion()|State got out of range. Total:" + (_totalRead + bytes) + " Count:" + _request.Count); } if ((_totalRead += bytes) == _request.Count) { _request.CompleteRequest(_request.Count); return(true); } return(false); }
/// <summary> /// Completes "request" with 0 if 0 bytes was requested or legitimate EOF received. /// Otherwise, reads as directed or completes "request" with an Exception. /// </summary> public static async void ReadPacketAsync(Stream transport, AsyncProtocolRequest request) // "async Task" might result in additional, unnecessary allocation { try { int remainingCount = request.Count, offset = request.Offset; do { int bytes = await transport.ReadAsync(new Memory <byte>(request.Buffer, offset, remainingCount), CancellationToken.None).ConfigureAwait(false); if (bytes == 0) { if (remainingCount != request.Count) { throw new IOException(SR.net_io_eof); } request.CompleteRequest(0); return; } offset += bytes; remainingCount -= bytes; } while (remainingCount > 0); Debug.Assert(remainingCount == 0); request.CompleteRequest(request.Count); } catch (Exception e) { request.CompleteUserWithError(e); } }
private bool CheckCompletionBeforeNextRead(int bytes) { if (bytes == 0) { // 0 bytes was requested or EOF in the beginning of a frame, the caller should decide whether it's OK. if (_totalRead == 0) { _request.CompleteRequest(0); return(true); } // EOF in the middle of a frame. throw new IOException(SR.net_io_eof); } if (_totalRead + bytes > _request.Count) { NetEventSource.Fail(this, $"State got out of range. Total:{_totalRead + bytes} Count:{_request.Count}"); } if ((_totalRead += bytes) == _request.Count) { _request.CompleteRequest(_request.Count); return(true); } return(false); }
// // // private bool CheckCompletionBeforeNextRead(int bytes) { if (bytes == 0) { // 0 bytes was requested or EOF in the beginning of a frame, the caller should decide whether it's OK if (_TotalRead == 0) { _Request.CompleteRequest(0); return(true); } // EOF in the middle of a frame, bummer! throw new IOException(SR.GetString(SR.net_io_eof)); } GlobalLog.Assert(_TotalRead + bytes <= _Request.Count, "FixedSizeReader::CheckCompletion()|State got out of range. Total:{0} Count:{1}", _TotalRead + bytes, _Request.Count); if ((_TotalRead += bytes) == _Request.Count) { _Request.CompleteRequest(_Request.Count); return(true); } return(false); }