Пример #1
0
        private Task ProcessReceiveRequest(ITransportConnection connection)
        {
            Func <Task> initialize = null;

            bool newConnection = Heartbeat.AddConnection(this);

            if (IsConnectRequest)
            {
                if (newConnection)
                {
                    initialize = Connected;

                    _counters.ConnectionsConnected.Increment();
                }
            }
            else if (IsReconnectRequest)
            {
                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);

            return(ProcessMessages(connection, fullInit));
        }
Пример #2
0
        protected Task ProcessRequestCore(ITransportConnection connection)
        {
            Connection = connection;

            if (Context.Request.Url.LocalPath.EndsWith("/send", StringComparison.OrdinalIgnoreCase))
            {
                return(ProcessSendRequest());
            }
            else if (IsAbortRequest)
            {
                return(Connection.Abort(ConnectionId));
            }
            else
            {
                InitializePersistentState();

                if (IsConnectRequest)
                {
                    if (Connected != null)
                    {
                        // Return a task that completes when the connected event task & the receive loop task are both finished
                        bool newConnection = Heartbeat.AddConnection(this);

                        // The connected callback
                        Func <Task> connected = () =>
                        {
                            if (newConnection)
                            {
                                return(Connected().Then(() => _counters.ConnectionsConnected.Increment()));
                            }
                            return(TaskAsyncHelper.Empty);
                        };

                        return(TaskAsyncHelper.Interleave(ProcessReceiveRequestWithoutTracking, connected, connection, Completed));
                    }

                    return(ProcessReceiveRequest(connection));
                }

                if (Reconnected != null)
                {
                    // Return a task that completes when the reconnected event task & the receive loop task are both finished
                    Func <Task> reconnected = () => Reconnected().Then(() => _counters.ConnectionsReconnected.Increment());
                    return(TaskAsyncHelper.Interleave(ProcessReceiveRequest, reconnected, connection, Completed));
                }

                return(ProcessReceiveRequest(connection));
            }
        }
Пример #3
0
        private Task ProcessConnectRequest(ITransportConnection connection)
        {
            if (Connected != null)
            {
                bool newConnection = Heartbeat.AddConnection(this);

                // Return a task that completes when the connected event task & the receive loop task are both finished
                return(TaskAsyncHelper.Interleave(ProcessReceiveRequestWithoutTracking, () =>
                {
                    if (newConnection)
                    {
                        return Connected().Then(() => _counters.ConnectionsConnected.Increment());
                    }
                    return TaskAsyncHelper.Empty;
                },
                                                  connection, Completed));
            }

            return(ProcessReceiveRequest(connection));
        }
Пример #4
0
 private Task ProcessReceiveRequest(ITransportConnection connection, Func <Task> postReceive = null)
 {
     Heartbeat.AddConnection(this);
     return(ProcessReceiveRequestWithoutTracking(connection, postReceive));
 }