private BulletHttpResponse Read(Socket socket) { BulletHttpResponse result = null; try { var sw = Stopwatch.StartNew(); int length = 0; //int readBytes = socket.Receive(_buffer.Span, SocketFlags.None); int readBytes = _stram.Read(_buffer.Span); if (readBytes == 0) { return(result); } sw.Stop(); length += readBytes; var responseSpan = _buffer.Slice(0, readBytes); var statusCode = HttpStreamHelper.GetStatusCode(responseSpan.Span); var responseType = HttpStreamHelper.GetResponseType(responseSpan.Span); if (responseType == ResponseType.Chunked) { while (!HttpStreamHelper.IsEndOfChunkedStream(_buffer.Span.Slice(0, readBytes))) { //readBytes = socket.Receive(_buffer.Span, SocketFlags.None); readBytes = _stram.Read(_buffer.Span); length += readBytes; } } if (responseType == ResponseType.Chunked) { while (!HttpStreamHelper.IsEndOfChunkedStream(_buffer.Span.Slice(0, readBytes))) { //readBytes = socket.Receive(_buffer.Span, SocketFlags.None); readBytes = _stram.Read(_buffer.Span); length += readBytes; } } result = new BulletHttpResponse(statusCode, length, sw.Elapsed.TotalMilliseconds, _localWatch.Elapsed.TotalMilliseconds); } catch (Exception ex) { Debug.WriteLine($"Error: {ex}"); } finally { } return(result); }
private async ValueTask <BulletHttpResponse> ReadAsync(Socket socket, CancellationToken ctk = default) { BulletHttpResponse result = null; try { var sw = Stopwatch.StartNew(); int length = 0; int readBytes = await socket.ReceiveAsync(_buffer, SocketFlags.None, ctk) .ConfigureAwait(false); if (readBytes == 0) { return(result); } sw.Stop(); length += readBytes; var responseSpan = _buffer.Slice(0, readBytes); var statusCode = HttpStreamHelper.GetStatusCode(responseSpan.Span); var responseType = HttpStreamHelper.GetResponseType(responseSpan.Span); if (responseType == ResponseType.Chunked) { while (!HttpStreamHelper.IsEndOfChunkedStream(_buffer.Span.Slice(0, readBytes))) { readBytes = await socket.ReceiveAsync(_buffer, SocketFlags.None, ctk).ConfigureAwait(false); length += readBytes; } } if (responseType == ResponseType.Chunked) { while (!HttpStreamHelper.IsEndOfChunkedStream(_buffer.Span.Slice(0, readBytes))) { readBytes = await socket.ReceiveAsync(_buffer, SocketFlags.None, ctk); length += readBytes; } } //result = new BulletHttpResponse(statusCode, length, sw.Elapsed.TotalMilliseconds, _localWatch.Elapsed.TotalMilliseconds); } catch { } finally { } return(result); }
private async ValueTask ReadAsync(Socket socket) { try { var sw = Stopwatch.StartNew(); int lenght = 0; int readBytes = await socket.ReceiveAsync(_buffer, SocketFlags.None); if (readBytes == 0) { return; } lenght += readBytes; var responseSpan = _buffer.Slice(0, readBytes); var statusCode = BulletHttpHeader.GetStatusCode(responseSpan.Span); var responseType = BulletHttpHeader.GetResponseType(responseSpan.Span); if (responseType == ResponseType.Chunked) { while (!HttpStreamHelper.IsEndOfChunkedStream(_buffer.Span.Slice(0, readBytes))) { readBytes = await socket.ReceiveAsync(_buffer, SocketFlags.None); lenght += readBytes; } } sw.Stop(); _totalReads++; ResponseHeaders.Add(new BulletHttpHeader(statusCode, lenght, sw.Elapsed.TotalMilliseconds)); } catch { } finally { } }