private Task ProcessReceiveRequest(ITransportConnection connection)
        {
            Func <Task> initialize = null;

            // If this transport isn't replacing an existing transport, oldConnection will be null.
            ITrackingConnection oldConnection = Heartbeat.AddOrUpdateConnection(this);
            bool newConnection = oldConnection == null;

            if (IsConnectRequest)
            {
                if (_protocolResolver.SupportsDelayedStart(Context.Request))
                {
                    // TODO: Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                    initialize = () => connection.Initialize(ConnectionId);
                }
                else
                {
                    Func <Task> connected;
                    if (newConnection)
                    {
                        connected = Connected ?? _emptyTaskFunc;
                        _counters.ConnectionsConnected.Increment();
                    }
                    else
                    {
                        // Wait until the previous call to Connected completes.
                        // We don't want to call Connected twice
                        connected = () => oldConnection.ConnectTask;
                    }

                    initialize = () =>
                    {
                        return(connected().Then((conn, id) => conn.Initialize(id), connection, ConnectionId));
                    };
                }
            }
            else if (!SuppressReconnect)
            {
                initialize = Reconnected;
                _counters.ConnectionsReconnected.Increment();
            }

            initialize = initialize ?? _emptyTaskFunc;

            Func <Task> fullInit = () => initialize().ContinueWith(_connectTcs);

            return(ProcessMessages(connection, fullInit));
        }
Example #2
0
        private Task ProcessReceiveRequest(ITransportConnection connection)
        {
            Func <Task> initialize = null;

            // If this transport isn't replacing an existing transport, oldConnection will be null.
            ITrackingConnection oldConnection = Heartbeat.AddOrUpdateConnection(this);
            bool newConnection = oldConnection == null;

            if (IsConnectRequest)
            {
                Func <Task> connected;
                if (newConnection)
                {
                    connected = Connected ?? _emptyTaskFunc;
                    _counters.ConnectionsConnected.Increment();
                }
                else
                {
                    // Wait until the previous call to Connected completes.
                    // We don't want to call Connected twice
                    connected = () => oldConnection.ConnectTask;
                }

                initialize = () =>
                {
                    return(connected().Then((conn, id) => conn.Initialize(id), connection, ConnectionId));
                };
            }
            else
            {
                initialize = Reconnected;
            }

            var series = new Func <object, Task>[]
            {
                state => ((Func <Task>)state).Invoke(),
                state => ((Func <Task>)state).Invoke()
            };

            var states = new object[] { TransportConnected ?? _emptyTaskFunc,
                                        initialize ?? _emptyTaskFunc };

            Func <Task> fullInit = () => TaskAsyncHelper.Series(series, states).ContinueWith(_connectTcs);

            return(ProcessMessages(connection, fullInit));
        }