예제 #1
0
        private async Task ProcessNegotiate(HttpContext context, HttpConnectionDispatcherOptions options, ConnectionLogScope logScope)
        {
            context.Response.ContentType = "application/json";

            // Establish the connection
            var connection = CreateConnection(options);

            // Set the Connection ID on the logging scope so that logs from now on will have the
            // Connection ID metadata set.
            logScope.ConnectionId = connection.ConnectionId;

            // Don't use thread static instance here because writer is used with async
            var writer = new MemoryBufferWriter();

            try
            {
                // Get the bytes for the connection id
                WriteNegotiatePayload(writer, connection.ConnectionId, context, options);

                Log.NegotiationRequest(_logger);

                // Write it out to the response with the right content length
                context.Response.ContentLength = writer.Length;
                await writer.CopyToAsync(context.Response.Body);
            }
            finally
            {
                writer.Reset();
            }
        }
예제 #2
0
        public void ResetResetsTheMemoryBufferWriter()
        {
            var bufferWriter = new MemoryBufferWriter();

            bufferWriter.WriteByte(1);
            Assert.Equal(1, bufferWriter.Length);
            bufferWriter.Reset();
            Assert.Equal(0, bufferWriter.Length);
        }
예제 #3
0
        public Task WriteResponse_MemoryBufferWriter()
        {
            var writer = new MemoryBufferWriter();

            try
            {
                NegotiateProtocol.WriteResponse(_negotiateResponse, writer);
                return(writer.CopyToAsync(_stream));
            }
            finally
            {
                writer.Reset();
            }
        }
예제 #4
0
        private async Task ProcessNegotiate(HttpContext context, HttpConnectionDispatcherOptions options, ConnectionLogScope logScope)
        {
            context.Response.ContentType = "application/json";
            string error = null;
            int    clientProtocolVersion = 0;

            if (context.Request.Query.TryGetValue("NegotiateVersion", out var queryStringVersion))
            {
                // Set the negotiate response to the protocol we use.
                var queryStringVersionValue = queryStringVersion.ToString();
                if (!int.TryParse(queryStringVersionValue, out clientProtocolVersion))
                {
                    error = $"The client requested an invalid protocol version '{queryStringVersionValue}'";
                    Log.InvalidNegotiateProtocolVersion(_logger, queryStringVersionValue);
                }
            }

            // Establish the connection
            HttpConnectionContext connection = null;

            if (error == null)
            {
                connection = CreateConnection(options, clientProtocolVersion);
            }

            // Set the Connection ID on the logging scope so that logs from now on will have the
            // Connection ID metadata set.
            logScope.ConnectionId = connection?.ConnectionId;

            // Don't use thread static instance here because writer is used with async
            var writer = new MemoryBufferWriter();

            try
            {
                // Get the bytes for the connection id
                WriteNegotiatePayload(writer, connection?.ConnectionId, connection?.ConnectionToken, context, options, clientProtocolVersion, error);

                Log.NegotiationRequest(_logger);

                // Write it out to the response with the right content length
                context.Response.ContentLength = writer.Length;
                await writer.CopyToAsync(context.Response.Body);
            }
            finally
            {
                writer.Reset();
            }
        }