private Task ProcessReceiveRequestWithoutTracking(ITransportConnection connection, Action postReceive = null)
        {
            if (TransportConnected != null)
            {
                TransportConnected().Catch();
            }

            // ReceiveAsync() will async wait until a message arrives then return
            var receiveTask = IsConnectRequest ?
                              connection.ReceiveAsync(null, ConnectionEndToken, MaxMessages) :
                              connection.ReceiveAsync(MessageId, ConnectionEndToken, MaxMessages);

            if (postReceive != null)
            {
                postReceive();
            }

            return(receiveTask.Then(response =>
            {
                response.TimedOut = IsTimedOut;

                if (response.Aborted)
                {
                    // If this was a clean disconnect then raise the event
                    OnDisconnect();
                }

                return Send(response);
            }));
        }
        private Task ProcessReceiveRequest(ITransportConnection connection, Action postReceive = null)
        {
            HeartBeat.UpdateConnection(this);
            HeartBeat.MarkConnection(this);

            if (TransportConnected != null)
            {
                TransportConnected().Catch();
            }

            // ReceiveAsync() will async wait until a message arrives then return
            var receiveTask = IsConnectRequest ?
                              connection.ReceiveAsync(TimeoutToken) :
                              connection.ReceiveAsync(MessageId, TimeoutToken);

            if (postReceive != null)
            {
                postReceive();
            }

            return(receiveTask.Then(response =>
            {
                if (response.Aborted)
                {
                    // If this was a clean disconnect then raise the event
                    OnDisconnect();
                }

                return Send(response);
            }));
        }
Exemple #3
0
        private void ProcessMessagesImpl(TaskCompletionSource <object> taskCompletetionSource, ITransportConnection connection, Action postReceive = null)
        {
            if (!IsTimedOut && !IsDisconnected && IsAlive && !ConnectionEndToken.IsCancellationRequested)
            {
                // ResponseTask will either subscribe and wait for a signal then return new messages,
                // or return immediately with messages that were pending
                var receiveAsyncTask = LastMessageId == null
                    ? connection.ReceiveAsync(ConnectionEndToken)
                    : connection.ReceiveAsync(LastMessageId, ConnectionEndToken);

                if (postReceive != null)
                {
                    postReceive();
                }

                receiveAsyncTask.Then(response =>
                {
                    LastMessageId = response.MessageId;

                    response.TimedOut = IsTimedOut;

                    // If the response has the Disconnect flag, just send the response and exit the loop,
                    // the server thinks connection is gone. Otherwse, send the response then re-enter the loop
                    Task sendTask = Send(response);
                    if (response.Disconnect || response.TimedOut || response.Aborted)
                    {
                        if (response.Aborted)
                        {
                            // If this was a clean disconnect raise the event.
                            OnDisconnect();
                        }

                        // Signal the tcs when the task is done
                        return(sendTask.Then(tcs => tcs.SetResult(null), taskCompletetionSource));
                    }

                    // Continue the receive loop
                    return(sendTask.Then((conn) => ProcessMessagesImpl(taskCompletetionSource, conn), connection));
                })
                .ContinueWith(t =>
                {
                    if (t.IsCanceled)
                    {
                        taskCompletetionSource.SetCanceled();
                    }
                    else if (t.IsFaulted)
                    {
                        taskCompletetionSource.SetException(t.Exception);
                    }
                },
                              TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.NotOnRanToCompletion);

                // Stop execution here
                return;
            }

            taskCompletetionSource.SetResult(null);
            CompleteRequest();
            return;
        }
Exemple #4
0
        private static void ReceiveLoop(ITransportConnection connection, string messageId)
        {
            connection.ReceiveAsync(messageId, CancellationToken.None, maxMessages: 5000).Then(r =>
            {
                Interlocked.Add(ref _received, r.TotalCount);
                Interlocked.Add(ref _avgLastReceivedCount, r.TotalCount);

                ReceiveLoop(connection, r.MessageId);
            });
        }
        private static void ReceiveLoop(CountdownEvent connectionCountDown, ITransportConnection connection, string messageId, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                connectionCountDown.Signal();
                return;
            }

            connection.ReceiveAsync(messageId, cancellationToken, maxMessages: 5000).Then(r =>
            {
                ReceiveLoop(connectionCountDown, connection, r.MessageId, cancellationToken);
            });
        }
Exemple #6
0
        private static void ReceiveLoop(ITransportConnection connection, string messageId)
        {
            connection.ReceiveAsync(messageId, CancellationToken.None, maxMessages: 5000).Then(r =>
            {
                Interlocked.Add(ref _received, r.TotalCount);
                Interlocked.Add(ref _avgLastReceivedCount, r.TotalCount);

                ReceiveLoop(connection, r.MessageId);
            });
        }
        private Task ProcessReceiveRequestWithoutTracking(ITransportConnection connection, Action postReceive = null)
        {
            if (TransportConnected != null)
            {
                TransportConnected().Catch();
            }

            // ReceiveAsync() will async wait until a message arrives then return
            var receiveTask = IsConnectRequest ?
                              connection.ReceiveAsync(null, ConnectionEndToken, MessageBufferSize) :
                              connection.ReceiveAsync(MessageId, ConnectionEndToken, MessageBufferSize);

            if (postReceive != null)
            {
                postReceive();
            }

            return receiveTask.Then(response =>
            {
                response.TimedOut = IsTimedOut;

                if (response.Aborted)
                {
                    // If this was a clean disconnect then raise the event
                    OnDisconnect();
                }

                return Send(response);
            });
        }
        private void ProcessMessagesImpl(TaskCompletionSource<object> taskCompletetionSource, ITransportConnection connection, Action postReceive = null)
        {
            if (!IsTimedOut && !IsDisconnected && IsAlive)
            {
                // ResponseTask will either subscribe and wait for a signal then return new messages,
                // or return immediately with messages that were pending
                var receiveAsyncTask = LastMessageId == null
                    ? connection.ReceiveAsync(TimeoutToken)
                    : connection.ReceiveAsync(LastMessageId, TimeoutToken);

                if (postReceive != null)
                {
                    postReceive();
                }

                receiveAsyncTask.Then(response =>
                {
                    LastMessageId = response.MessageId;
                    // If the response has the Disconnect flag, just send the response and exit the loop,
                    // the server thinks connection is gone. Otherwse, send the response then re-enter the loop
                    Task sendTask = Send(response);
                    if (response.Disconnect || response.TimedOut || response.Aborted)
                    {
                        if (response.Aborted)
                        {
                            // If this was a clean disconnect raise the event.
                            OnDisconnect();
                        }

                        // Signal the tcs when the task is done
                        return sendTask.Then(tcs => tcs.SetResult(null), taskCompletetionSource);
                    }

                    // Continue the receive loop
                    return sendTask.Then((conn) => ProcessMessagesImpl(taskCompletetionSource, conn), connection);
                })
                .ContinueWith(t =>
                {
                    if (t.IsCanceled)
                    {
                        taskCompletetionSource.SetCanceled();
                    }
                    else if (t.IsFaulted)
                    {
                        taskCompletetionSource.SetException(t.Exception);
                    }
                },
                TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.NotOnRanToCompletion);

                // Stop execution here
                return;
            }

            taskCompletetionSource.SetResult(null);
            return;
        }
        private Task ProcessReceiveRequestWithoutTracking(ITransportConnection connection, Func<Task> postReceive = null)
        {
            if (TransportConnected != null)
            {
                TransportConnected().Catch();
            }

            // ReceiveAsync() will async wait until a message arrives then return
            var receiveTask = IsConnectRequest ?
                              connection.ReceiveAsync(null, ConnectionEndToken, MaxMessages) :
                              connection.ReceiveAsync(MessageId, ConnectionEndToken, MaxMessages);

            return TaskAsyncHelper.Series(() =>
            {
                if (postReceive != null)
                {
                    return postReceive();
                }
                return TaskAsyncHelper.Empty;
            },
            () =>
            {
                return receiveTask.Then(response =>
                {
                    response.TimedOut = IsTimedOut;

                    if (response.Aborted)
                    {
                        // If this was a clean disconnect then raise the event
                        OnDisconnect();
                    }

                    return Send(response);
                });
            });
        }
        private Task ProcessReceiveRequest(ITransportConnection connection, Action postReceive = null)
        {
            HeartBeat.UpdateConnection(this);
            HeartBeat.MarkConnection(this);

            if (TransportConnected != null)
            {
                TransportConnected().Catch();
            }

            // ReceiveAsync() will async wait until a message arrives then return
            var receiveTask = IsConnectRequest ?
                              connection.ReceiveAsync(TimeoutToken) :
                              connection.ReceiveAsync(MessageId, TimeoutToken);

            if (postReceive != null)
            {
                postReceive();
            }

            return receiveTask.Then(response =>
            {
                if (response.Aborted)
                {
                    // If this was a clean disconnect then raise the event
                    OnDisconnect();
                }

                return Send(response);
            });
        }
        private Task ProcessReceiveRequest(ITransportConnection connection, Action postReceive = null)
        {
            HeartBeat.MarkConnection(this);

            // ReceiveAsync() will async wait until a message arrives then return
            var receiveTask = IsConnectRequest ?
                              connection.ReceiveAsync(TimeoutToken) :
                              connection.ReceiveAsync(MessageId, TimeoutToken);

            if (postReceive != null)
            {
                postReceive();
            }

            return receiveTask.Then(response => Send(response));
        }
Exemple #12
0
        private static void ReceiveLoop(CountdownEvent connectionCountDown, ITransportConnection connection, string messageId, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                connectionCountDown.Signal();
                return;
            }

            connection.ReceiveAsync(messageId, cancellationToken, maxMessages: 5000).Then(r =>
            {
                ReceiveLoop(connectionCountDown, connection, r.MessageId, cancellationToken);
            });
        }