Ejemplo n.º 1
0
        private Task ProcessMessages(ITransportConnection connection, Func <Task> initialize)
        {
            var disposer = new Disposer();

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

            var cancelContext = new ForeverTransportContext(this, disposer);

            // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
            IDisposable registration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext);

            var messageContext = new MessageContext(this, _transportLifetime, registration);

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

            try
            {
                // Ensure we enqueue the response initialization before any messages are received
                EnqueueOperation(state => InitializeResponse((ITransportConnection)state), connection)
                .Catch((ex, state) => OnError(ex, state), messageContext);

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                IDisposable subscription = connection.Receive(LastMessageId,
                                                              (response, state) => OnMessageReceived(response, state),
                                                              MaxMessages,
                                                              messageContext);


                disposer.Set(subscription);

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

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                initialize().Then(tcs => tcs.TrySetResult(null), InitializeTcs)
                .Catch((ex, state) => OnError(ex, state), messageContext);
            }
            catch (OperationCanceledException ex)
            {
                InitializeTcs.TrySetCanceled();

                _transportLifetime.Complete(ex);
            }
            catch (Exception ex)
            {
                InitializeTcs.TrySetCanceled();

                _transportLifetime.Complete(ex);
            }

            return(_requestLifeTime.Task);
        }
Ejemplo n.º 2
0
 protected override IDisposable CreateReceiver(int connectionIndex)
 {
     return(_transportConnection.Receive(messageId: null,
                                         callback: (_, __) => TaskAsyncHelper.True,
                                         maxMessages: 10,
                                         state: null));
 }
Ejemplo n.º 3
0
        private Task ProcessMessages(ITransportConnection connection, Func <Task> initialize)
        {
            var disposer = new Disposer();

            var cancelContext = new LongPollingTransportContext(this, disposer);

            // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
            IDisposable registration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext);

            var lifeTime       = new RequestLifetime(this, _requestLifeTime, registration);
            var messageContext = new MessageContext(this, lifeTime);

            try
            {
                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                IDisposable subscription = connection.Receive(MessageId,
                                                              (response, state) => OnMessageReceived(response, state),
                                                              MaxMessages,
                                                              messageContext);

                // Set the disposable
                disposer.Set(subscription);

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                initialize().Catch((ex, state) => OnError(ex, state), messageContext);
            }
            catch (Exception ex)
            {
                lifeTime.Complete(ex);
            }

            return(_requestLifeTime.Task);
        }
Ejemplo n.º 4
0
        private void ProcessMessages(ITransportConnection connection, Func <Task> postReceive, Action endRequest)
        {
            IDisposable subscription = null;
            var         wh           = new ManualResetEventSlim(initialState: false);

            // End the request if the connection end token is triggered
            CancellationTokenRegistration registration = ConnectionEndToken.Register(() =>
            {
                wh.Wait();
                subscription.Dispose();
            });

            subscription = connection.Receive(LastMessageId, response =>
            {
                // We need to wait until post receive has been called
                wh.Wait();

                response.TimedOut = IsTimedOut;

                if (response.Disconnect ||
                    response.TimedOut ||
                    response.Aborted ||
                    ConnectionEndToken.IsCancellationRequested)
                {
                    if (response.Aborted)
                    {
                        // If this was a clean disconnect raise the event.
                        OnDisconnect();
                    }

                    endRequest();
                    registration.Dispose();
                    subscription.Dispose();

                    return(TaskAsyncHelper.False);
                }
                else
                {
                    return(Send(response).Then(() => TaskAsyncHelper.True));
                }
            },
                                              MaxMessages);

            if (postReceive != null)
            {
                postReceive().Catch(_allErrorsTotalCounter, _allErrorsPerSecCounter)
                .ContinueWith(task => wh.Set());
            }
            else
            {
                wh.Set();
            }
        }
        private Task ProcessMessages(ITransportConnection connection, Func <Task> initialize)
        {
            var disposer = new Disposer();

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

            var cancelContext = new ForeverTransportContext(this, disposer);

            // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
            _busRegistration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext);

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

            try
            {
                // Ensure we enqueue the response initialization before any messages are received
                EnqueueOperation(state => InitializeResponse((ITransportConnection)state), connection)
                .Catch((ex, state) => ((ForeverTransport)state).OnError(ex), this, Logger);

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                IDisposable subscription = connection.Receive(LastMessageId,
                                                              (response, state) => ((ForeverTransport)state).OnMessageReceived(response),
                                                              MaxMessages,
                                                              this);

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

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                initialize().Catch((ex, state) => ((ForeverTransport)state).OnError(ex), this, Logger)
                .Finally(state => ((SubscriptionDisposerContext)state).Set(),
                         new SubscriptionDisposerContext(disposer, subscription));
            }
            catch (Exception ex)
            {
                _transportLifetime.Complete(ex);
            }

            return(_requestLifeTime.Task);
        }
        private void ProcessMessages(ITransportConnection connection, Func<Task> postReceive, Action endRequest)
        {
            IDisposable subscription = null;
            var wh = new ManualResetEventSlim(initialState: false);

            // End the request if the connection end token is triggered
            CancellationTokenRegistration registration = ConnectionEndToken.Register(() =>
            {
                wh.Wait();
                subscription.Dispose();
            });

            subscription = connection.Receive(LastMessageId, response =>
            {
                // We need to wait until post receive has been called
                wh.Wait();

                response.TimedOut = IsTimedOut;

                // If we're telling the client to disconnect then clean up the instantiated connection.
                if (response.Disconnect)
                {
                    // Send the response before removing any connection data
                    return Send(response).Then(() =>
                    {
                        // Remove connection without triggering disconnect
                        HeartBeat.RemoveConnection(this);

                        endRequest();

                        // Dispose everything
                        registration.Dispose();
                        subscription.Dispose();

                        return TaskAsyncHelper.False;
                    });
                }
                else if (response.TimedOut ||
                         response.Aborted ||
                         ConnectionEndToken.IsCancellationRequested)
                {
                    if (response.Aborted)
                    {
                        // If this was a clean disconnect raise the event.
                        OnDisconnect();
                    }

                    endRequest();

                    // Dispose everything
                    registration.Dispose();
                    subscription.Dispose();

                    return TaskAsyncHelper.False;
                }
                else
                {
                    return Send(response).Then(() => TaskAsyncHelper.True);
                }
            },
            MaxMessages);

            if (postReceive != null)
            {
                postReceive().Catch(_counters.ErrorsAllTotal, _counters.ErrorsAllPerSec)
                             .ContinueWith(task => wh.Set());
            }
            else
            {
                wh.Set();
            }
        }
Ejemplo n.º 7
0
        private void ProcessMessages(ITransportConnection connection, Func<Task> postReceive, Action endRequest)
        {
            IDisposable subscription = null;
            var wh = new ManualResetEventSlim(initialState: false);

            // End the request if the connection end token is triggered
            CancellationTokenRegistration registration = ConnectionEndToken.Register(() =>
            {
                wh.Wait();
                subscription.Dispose();
            });

            subscription = connection.Receive(LastMessageId, response =>
            {
                // We need to wait until post receive has been called
                wh.Wait();

                response.TimedOut = IsTimedOut;

                if (response.Disconnect ||
                    response.TimedOut ||
                    response.Aborted ||
                    ConnectionEndToken.IsCancellationRequested)
                {
                    if (response.Aborted)
                    {
                        // If this was a clean disconnect raise the event.
                        OnDisconnect();
                    }

                    endRequest();
                    registration.Dispose();

                    return TaskAsyncHelper.False;
                }
                else
                {
                    return Send(response).Then(() => TaskAsyncHelper.True);
                }
            },
            MaxMessages);

            if (postReceive != null)
            {
                postReceive().Catch()
                             .ContinueWith(task => wh.Set());
            }
            else
            {
                wh.Set();
            }
        }
Ejemplo n.º 8
0
        private void ProcessMessages(ITransportConnection connection, Func <Task> postReceive, Action <Exception> endRequest)
        {
            IDisposable subscription = null;
            var         wh           = new ManualResetEventSlim(initialState: false);
            var         registration = default(CancellationTokenRegistration);
            bool        disposeSubscriptionImmediately = false;

            try
            {
                // End the request if the connection end token is triggered
                registration = ConnectionEndToken.Register(() =>
                {
                    wh.Wait();

                    // This is only null if we failed to create the subscription
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                });
            }
            catch (ObjectDisposedException)
            {
                // If we've ended the connection before we got a chance to register the connection
                // then dispose the subscription immediately
                disposeSubscriptionImmediately = true;
            }

            try
            {
                subscription = connection.Receive(LastMessageId, response =>
                {
                    // We need to wait until post receive has been called
                    wh.Wait();

                    response.TimedOut = IsTimedOut;

                    // If we're telling the client to disconnect then clean up the instantiated connection.
                    if (response.Disconnect)
                    {
                        // Send the response before removing any connection data
                        return(Send(response).Then(() =>
                        {
                            // Remove connection without triggering disconnect
                            HeartBeat.RemoveConnection(this);

                            endRequest(null);

                            // Dispose everything
                            registration.Dispose();
                            subscription.Dispose();

                            return TaskAsyncHelper.False;
                        }));
                    }
                    else if (response.TimedOut ||
                             response.Aborted ||
                             ConnectionEndToken.IsCancellationRequested)
                    {
                        if (response.Aborted)
                        {
                            // If this was a clean disconnect raise the event.
                            OnDisconnect();
                        }

                        endRequest(null);

                        // Dispose everything
                        registration.Dispose();
                        subscription.Dispose();

                        return(TaskAsyncHelper.False);
                    }
                    else
                    {
                        return(Send(response).Then(() => TaskAsyncHelper.True));
                    }
                },
                                                  MaxMessages);
            }
            catch (Exception ex)
            {
                endRequest(ex);

                wh.Set();

                registration.Dispose();

                return;
            }

            if (postReceive != null)
            {
                postReceive().Catch(_counters.ErrorsAllTotal, _counters.ErrorsAllPerSec)
                .Catch(ex => endRequest(ex))
                .ContinueWith(task => wh.Set());
            }
            else
            {
                wh.Set();
            }

            if (disposeSubscriptionImmediately)
            {
                subscription.Dispose();
            }
        }
Ejemplo n.º 9
0
        private Task ProcessReceiveRequestWithoutTracking(ITransportConnection connection, Func<Task> postReceive = null)
        {
            if (TransportConnected != null)
            {
                TransportConnected().Catch();
            }

            // Receive() will async wait until a message arrives then return
            var receiveTask = IsConnectRequest ?
                              connection.Receive(null, ConnectionEndToken, MaxMessages) :
                              connection.Receive(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);
                });
            });
        }
Ejemplo n.º 10
0
        private Task ProcessMessages(ITransportConnection connection, Func<Task> initialize)
        {
            var disposer = new Disposer();

            var cancelContext = new LongPollingTransportContext(this, disposer);

            // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
            IDisposable registration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext);

            var lifeTime = new RequestLifetime(this, _requestLifeTime, registration);
            var messageContext = new MessageContext(this, lifeTime);

            try
            {
                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                IDisposable subscription = connection.Receive(MessageId,
                                                              (response, state) => OnMessageReceived(response, state),
                                                              MaxMessages,
                                                              messageContext);

                // Set the disposable
                disposer.Set(subscription);

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                initialize().Catch((ex, state) => OnError(ex, state), messageContext);
            }
            catch (Exception ex)
            {
                lifeTime.Complete(ex);
            }

            return _requestLifeTime.Task;
        }
Ejemplo n.º 11
0
        private Task ProcessMessages(ITransportConnection connection, Func<Task> initialize)
        {
            var disposer = new Disposer();

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

            var cancelContext = new ForeverTransportContext(this, disposer);

            // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
            _busRegistration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext);

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

            try
            {
                // Ensure we enqueue the response initialization before any messages are received
                EnqueueOperation(state => InitializeResponse((ITransportConnection)state), connection)
                    .Catch((ex, state) => ((ForeverTransport)state).OnError(ex), this, Logger);

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                IDisposable subscription = connection.Receive(LastMessageId,
                                                              (response, state) => ((ForeverTransport)state).OnMessageReceived(response),
                                                               MaxMessages,
                                                               this);

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

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                initialize().Catch((ex, state) => ((ForeverTransport)state).OnError(ex), this, Logger)
                            .Finally(state => ((SubscriptionDisposerContext)state).Set(),
                                new SubscriptionDisposerContext(disposer, subscription));
            }
            catch (Exception ex)
            {
                _transportLifetime.Complete(ex);
            }

            return _requestLifeTime.Task;
        }
Ejemplo n.º 12
0
        private void ProcessMessages(ITransportConnection connection, Func <Task> postReceive, Action endRequest)
        {
            IDisposable subscription = null;
            var         wh           = new ManualResetEventSlim(initialState: false);

            // End the request if the connection end token is triggered
            CancellationTokenRegistration registration = ConnectionEndToken.Register(() =>
            {
                wh.Wait();
                subscription.Dispose();
            });

            subscription = connection.Receive(LastMessageId, response =>
            {
                // We need to wait until post receive has been called
                wh.Wait();

                response.TimedOut = IsTimedOut;

                // If we're telling the client to disconnect then clean up the instantiated connection.
                if (response.Disconnect)
                {
                    // Send the response before removing any connection data
                    return(Send(response).Then(() =>
                    {
                        // Remove connection without triggering disconnect
                        HeartBeat.RemoveConnection(this);

                        endRequest();

                        // Dispose everything
                        registration.Dispose();
                        subscription.Dispose();

                        return TaskAsyncHelper.False;
                    }));
                }
                else if (response.TimedOut ||
                         response.Aborted ||
                         ConnectionEndToken.IsCancellationRequested)
                {
                    if (response.Aborted)
                    {
                        // If this was a clean disconnect raise the event.
                        OnDisconnect();
                    }

                    endRequest();

                    // Dispose everything
                    registration.Dispose();
                    subscription.Dispose();

                    return(TaskAsyncHelper.False);
                }
                else
                {
                    return(Send(response).Then(() => TaskAsyncHelper.True));
                }
            },
                                              MaxMessages);

            if (postReceive != null)
            {
                postReceive().Catch(_counters.ErrorsAllTotal, _counters.ErrorsAllPerSec)
                .ContinueWith(task => wh.Set());
            }
            else
            {
                wh.Set();
            }
        }
Ejemplo n.º 13
0
        private static void ReceiveLoop(CountdownEvent connectionCountDown, ITransportConnection connection, string messageId, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                connectionCountDown.Signal();
                return;
            }

            connection.Receive(messageId, cancellationToken, maxMessages: 5000).Then(r =>
            {
                ReceiveLoop(connectionCountDown, connection, r.MessageId, cancellationToken);
            });
        }
Ejemplo n.º 14
0
        private void ProcessMessages(ITransportConnection connection, Func <Task> postReceive, Action <Exception> endRequest)
        {
            IDisposable subscription = null;
            IDisposable registration = null;

            var wh = new ManualResetEventSlim();

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

            try
            {
                subscription = connection.Receive(LastMessageId, response =>
                {
                    // We need to wait until post receive has been called
                    wh.Wait();

                    response.TimedOut = IsTimedOut;

                    // If we're telling the client to disconnect then clean up the instantiated connection.
                    if (response.Disconnect)
                    {
                        // Send the response before removing any connection data
                        return(Send(response).Then(() =>
                        {
                            registration.Dispose();

                            // Remove connection without triggering disconnect
                            Heartbeat.RemoveConnection(this);

                            endRequest(null);

                            return TaskAsyncHelper.False;
                        }));
                    }
                    else if (response.TimedOut ||
                             response.Aborted ||
                             ConnectionEndToken.IsCancellationRequested)
                    {
                        // If this is null it's because the cancellation token tripped
                        // before we setup the registration at all.
                        if (registration != null)
                        {
                            registration.Dispose();
                        }

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

                        endRequest(null);

                        return(TaskAsyncHelper.False);
                    }
                    else
                    {
                        return(Send(response).Then(() => TaskAsyncHelper.True)
                               .Catch(IncrementErrorCounters)
                               .Catch(ex =>
                        {
                            Trace.TraceInformation("Send failed for {0} with: {1}", ConnectionId, ex.GetBaseException());
                        }));
                    }
                },
                                                  MaxMessages);
            }
            catch (Exception ex)
            {
                endRequest(ex);

                wh.Set();

                return;
            }

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

            if (postReceive != null)
            {
                postReceive().Catch(_counters.ErrorsAllTotal, _counters.ErrorsAllPerSec)
                .Catch(ex => endRequest(ex))
                .Catch(ex =>
                {
                    Trace.TraceInformation("Failed post receive for {0} with: {1}", ConnectionId, ex.GetBaseException());
                })
                .ContinueWith(task => wh.Set());
            }
            else
            {
                wh.Set();
            }

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

            // This has to be done last incase it runs synchronously.
            registration = ConnectionEndToken.SafeRegister(state =>
            {
                Trace.TraceInformation("Cancel(" + ConnectionId + ")");

                state.Dispose();
            },
                                                           subscription);
        }
Ejemplo n.º 15
0
        private void ProcessMessages(ITransportConnection connection, Action postReceive, Action endRequest)
        {
            IDisposable subscription = null;

            // End the request if the connection end token is triggered
            ConnectionEndToken.Register(() =>
            {
                if (subscription != null)
                {
                    subscription.Dispose();
                }
            });

            subscription = connection.Receive(LastMessageId, response =>
            {
                response.TimedOut = IsTimedOut;

                if (response.Disconnect ||
                    response.TimedOut ||
                    response.Aborted ||
                    ConnectionEndToken.IsCancellationRequested)
                {
                    if (response.Aborted)
                    {
                        // If this was a clean disconnect raise the event.
                        OnDisconnect();
                    }

                    endRequest();
                    return TaskAsyncHelper.False;
                }
                else
                {
                    return Send(response).Then(() => TaskAsyncHelper.True);
                }
            },
            MessageBufferSize);

            if (postReceive != null)
            {
                postReceive();
            }
        }
Ejemplo n.º 16
0
        private void ProcessMessages(ITransportConnection connection, Func<Task> postReceive, Action<Exception> endRequest)
        {
            IDisposable subscription = null;
            var wh = new ManualResetEventSlim(initialState: false);
            var registration = default(CancellationTokenRegistration);
            bool disposeSubscriptionImmediately = false;

            try
            {
                // End the request if the connection end token is triggered
                registration = ConnectionEndToken.Register(() =>
                {
                    wh.Wait();

                    // This is only null if we failed to create the subscription
                    if (subscription != null)
                    {
                        subscription.Dispose();
                    }
                });
            }
            catch (ObjectDisposedException)
            {
                // If we've ended the connection before we got a chance to register the connection
                // then dispose the subscription immediately
                disposeSubscriptionImmediately = true;
            }

            try
            {
                subscription = connection.Receive(LastMessageId, response =>
                {
                    // We need to wait until post receive has been called
                    wh.Wait();

                    response.TimedOut = IsTimedOut;

                    // If we're telling the client to disconnect then clean up the instantiated connection.
                    if (response.Disconnect)
                    {
                        // Send the response before removing any connection data
                        return Send(response).Then(() =>
                        {
                            // Remove connection without triggering disconnect
                            HeartBeat.RemoveConnection(this);

                            endRequest(null);

                            // Dispose everything
                            registration.Dispose();
                            subscription.Dispose();

                            return TaskAsyncHelper.False;
                        });
                    }
                    else if (response.TimedOut ||
                             response.Aborted ||
                             ConnectionEndToken.IsCancellationRequested)
                    {
                        if (response.Aborted)
                        {
                            // If this was a clean disconnect raise the event.
                            OnDisconnect();
                        }

                        endRequest(null);

                        // Dispose everything
                        registration.Dispose();
                        subscription.Dispose();

                        return TaskAsyncHelper.False;
                    }
                    else
                    {
                        return Send(response).Then(() => TaskAsyncHelper.True);
                    }
                },
                MaxMessages);
            }
            catch (Exception ex)
            {
                endRequest(ex);

                wh.Set();

                registration.Dispose();

                return;
            }

            if (postReceive != null)
            {
                postReceive().Catch(_counters.ErrorsAllTotal, _counters.ErrorsAllPerSec)
                             .Catch(ex => endRequest(ex))
                             .ContinueWith(task => wh.Set());
            }
            else
            {
                wh.Set();
            }

            if (disposeSubscriptionImmediately)
            {
                subscription.Dispose();
            }
        }
Ejemplo n.º 17
0
        private Task ProcessMessages(ITransportConnection connection, Func<Task> initialize)
        {
            var disposer = new Disposer();

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

            var cancelContext = new ForeverTransportContext(this, disposer);

            // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
            IDisposable registration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext);

            var messageContext = new MessageContext(this, _transportLifetime, registration);

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

            try
            {
                // Ensure we enqueue the response initialization before any messages are received
                EnqueueOperation(state => InitializeResponse((ITransportConnection)state), connection)
                    .Catch((ex, state) => OnError(ex, state), messageContext);

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                IDisposable subscription = connection.Receive(LastMessageId,
                                                              (response, state) => OnMessageReceived(response, state),
                                                               MaxMessages,
                                                               messageContext);


                disposer.Set(subscription);

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

                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                initialize().Then(tcs => tcs.TrySetResult(null), InitializeTcs)
                            .Catch((ex, state) => OnError(ex, state), messageContext);
            }
            catch (OperationCanceledException ex)
            {
                InitializeTcs.TrySetCanceled();

                _transportLifetime.Complete(ex);
            }
            catch (Exception ex)
            {
                InitializeTcs.TrySetCanceled();

                _transportLifetime.Complete(ex);
            }

            return _requestLifeTime.Task;
        }
Ejemplo n.º 18
0
        private void ProcessMessages(ITransportConnection connection, Func<Task> postReceive, Action<Exception> endRequest)
        {
            IDisposable subscription = null;
            IDisposable registration = null;

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

            try
            {
                subscription = connection.Receive(LastMessageId, response =>
                {
                    response.TimedOut = IsTimedOut;

                    // If we're telling the client to disconnect then clean up the instantiated connection.
                    if (response.Disconnect)
                    {
                        // Send the response before removing any connection data
                        return Send(response).Then(() =>
                        {
                            registration.Dispose();

                            // Remove connection without triggering disconnect
                            Heartbeat.RemoveConnection(this);

                            endRequest(null);

                            return TaskAsyncHelper.False;
                        });
                    }
                    else if (response.TimedOut ||
                             response.Aborted ||
                             ConnectionEndToken.IsCancellationRequested)
                    {
                        // If this is null it's because the cancellation token tripped
                        // before we setup the registration at all.
                        if (registration != null)
                        {
                            registration.Dispose();
                        }

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

                        endRequest(null);

                        return TaskAsyncHelper.False;
                    }
                    else
                    {
                        return Send(response).Then(() => TaskAsyncHelper.True)
                                             .Catch(IncrementErrorCounters)
                                             .Catch(ex =>
                                             {
                                                 Trace.TraceInformation("Send failed for {0} with: {1}", ConnectionId, ex.GetBaseException());
                                             });
                    }
                },
                MaxMessages);
            }
            catch (Exception ex)
            {
                // Set the tcs so that the task queue isn't waiting forever
                InitializeTcs.TrySetResult(null);

                endRequest(ex);

                return;
            }

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

            postReceive().Catch(_counters.ErrorsAllTotal, _counters.ErrorsAllPerSec)
                         .Catch(ex => endRequest(ex))
                         .Catch(ex =>
                         {
                             Trace.TraceInformation("Failed post receive for {0} with: {1}", ConnectionId, ex.GetBaseException());
                         })
                         .ContinueWith(InitializeTcs);

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

            // This has to be done last incase it runs synchronously.
            registration = ConnectionEndToken.SafeRegister(state =>
            {
                Trace.TraceInformation("Cancel(" + ConnectionId + ")");

                state.Dispose();
            },
            subscription);
        }
Ejemplo n.º 19
0
        private void ProcessMessages(ITransportConnection connection, Func<Task> postReceive, Action<Exception> endRequest)
        {
            IDisposable subscription = null;
            IDisposable registration = null;

            var wh = new ManualResetEventSlim(initialState: false);

            try
            {
                subscription = connection.Receive(LastMessageId, response =>
                {
                    // We need to wait until post receive has been called
                    wh.Wait();

                    response.TimedOut = IsTimedOut;

                    // If we're telling the client to disconnect then clean up the instantiated connection.
                    if (response.Disconnect)
                    {
                        // Send the response before removing any connection data
                        return Send(response).Then(() =>
                        {
                            registration.Dispose();

                            // Remove connection without triggering disconnect
                            Heartbeat.RemoveConnection(this);

                            endRequest(null);

                            return TaskAsyncHelper.False;
                        });
                    }
                    else if (response.TimedOut ||
                             response.Aborted ||
                             ConnectionEndToken.IsCancellationRequested)
                    {
                        // If this is null it's because the cancellation token tripped
                        // before we setup the registration at all.
                        if (registration != null)
                        {
                            registration.Dispose();
                        }

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

                        endRequest(null);

                        return TaskAsyncHelper.False;
                    }
                    else
                    {
                        return Send(response).Then(() => TaskAsyncHelper.True)
                                             .Catch(IncrementErrorCounters)
                                             .Catch(ex =>
                                             {
                                                 Trace.TraceInformation("Send failed with: " + ex.GetBaseException());
                                             });
                    }
                },
                MaxMessages);
            }
            catch (Exception ex)
            {
                endRequest(ex);

                wh.Set();

                return;
            }

            // End the request if the connection end token is triggered
            registration = ConnectionEndToken.SafeRegister(state =>
            {
                Trace.TraceInformation("Cancel(" + ConnectionId + ")");

                state.Dispose();
            },
            subscription);

            if (postReceive != null)
            {
                postReceive().Catch(_counters.ErrorsAllTotal, _counters.ErrorsAllPerSec)
                             .Catch(ex => endRequest(ex))
                             .Catch(ex =>
                             {
                                 Trace.TraceInformation("Failed post receive with:" + ex.GetBaseException());
                             })
                             .ContinueWith(task => wh.Set());
            }
            else
            {
                wh.Set();
            }
        }