Ejemplo n.º 1
0
        private ConnectionState GetOrCreateConnection(HttpContext context, out bool isNewConnection)
        {
            var             connectionId = context.Request.Query["id"];
            ConnectionState connectionState;

            isNewConnection = false;

            // There's no connection id so this is a branch new connection
            if (StringValues.IsNullOrEmpty(connectionId))
            {
                isNewConnection = true;
                var channel = new HttpChannel(_channelFactory);
                connectionState = _manager.AddNewConnection(channel);
            }
            else
            {
                // REVIEW: Fail if not reserved? Reused an existing connection id?

                // There's a connection id
                if (!_manager.TryGetConnection(connectionId, out connectionState))
                {
                    throw new InvalidOperationException("Unknown connection id");
                }

                // Reserved connection, we need to provide a channel
                if (connectionState.Connection.Channel == null)
                {
                    isNewConnection = true;
                    connectionState.Connection.Channel = new HttpChannel(_channelFactory);
                    connectionState.Active             = true;
                    connectionState.LastSeen           = DateTimeOffset.UtcNow;
                }
            }

            return(connectionState);
        }