Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }