Beispiel #1
0
        private PersistentResponse GetResponse(MessageResult result)
        {
            // Do a single sweep through the results to process commands and extract values
            ProcessResults(result);

            var response = new PersistentResponse
            {
                MessageId = result.LastMessageId,
                Messages = result.Messages,
                Disconnect = _disconnected,
                Aborted = _aborted,
                TotalCount = result.TotalCount
            };

            PopulateResponseState(response);

            _counters.ConnectionMessagesReceivedTotal.IncrementBy(result.TotalCount);
            _counters.ConnectionMessagesReceivedPerSec.IncrementBy(result.TotalCount);

            return response;
        }
Beispiel #2
0
        private void ProcessResults(MessageResult result)
        {
            result.Messages.Enumerate(message => message.IsCommand || message.IsAck,
                                      message =>
                                      {
                                          if (message.IsAck)
                                          {
                                              _ackHandler.TriggerAck(message.CommandId);
                                          }
                                          else
                                          {
                                              var command = _serializer.Parse<Command>(message.Value);
                                              ProcessCommand(command);

                                              // Only send the ack if this command is waiting for it
                                              if (message.WaitForAck)
                                              {
                                                  // If we're on the same box and there's a pending ack for this command then
                                                  // just trip it
                                                  if (!_ackHandler.TriggerAck(message.CommandId))
                                                  {
                                                      _bus.Ack(_connectionId, message.Key, message.CommandId).Catch();
                                                  }
                                              }
                                          }
                                      });
        }
Beispiel #3
0
        private PersistentResponse GetResponse(MessageResult result)
        {
            // Do a single sweep through the results to process commands and extract values
            var messageValues = ProcessResults(result.Messages);

            var response = new PersistentResponse
            {
                MessageId = result.LastMessageId,
                Messages = messageValues,
                Disconnect = _disconnected,
                TimedOut = result.TimedOut
            };

            PopulateResponseState(response);

            _trace.Source.TraceInformation("Connection: Connection {0} received {1} messages, last id {2}", _connectionId, result.Messages.Count, result.LastMessageId);

            Debug.WriteLine("Connection: Connection {0} received {1} messages, last id {2}. Payload {3}", _connectionId, result.Messages.Count, result.LastMessageId, _serializer.Stringify(result.Messages));

            return response;
        }
Beispiel #4
0
 private void ProcessResults(MessageResult result)
 {
     for (int i = 0; i < result.Messages.Count; i++)
     {
         ArraySegment<Message> segment = result.Messages[i];
         for (int j = segment.Offset; j < segment.Offset + segment.Count; j++)
         {
             Message message = segment.Array[j];
             if (message.IsCommand)
             {
                 var command = _serializer.Parse<Command>(message.Value);
                 ProcessCommand(command);
             }
         }
     }
 }
Beispiel #5
0
            private void WorkImpl(ConcurrentDictionary<string, Topic> topics, TaskCompletionSource<object> taskCompletionSource)
            {
                Process:
                int totalCount = 0;
                string nextCursor = null;
                List<ArraySegment<Message>> items = null;
                var cursors = new List<Cursor>();

                lock (_lockObj)
                {
                    items = new List<ArraySegment<Message>>(Cursors.Count);
                    for (int i = 0; i < Cursors.Count; i++)
                    {
                        Cursor cursor = Cursor.Clone(Cursors[i]);
                        cursors.Add(cursor);

                        MessageStoreResult<Message> storeResult = cursor.Topic.Store.GetMessages(cursor.Id, _maxMessages);
                        ulong next = storeResult.FirstMessageId + (ulong)storeResult.Messages.Count;

                        cursor.Id = next;

                        if (storeResult.Messages.Count > 0)
                        {
                            items.Add(storeResult.Messages);
                            totalCount += storeResult.Messages.Count;
                        }
                    }

                    nextCursor = Cursor.MakeCursor(cursors);
                }

                if (Alive && items.Count > 0)
                {
                    lock (_lockObj)
                    {
                        _cursors = cursors;
                        cursors = null;
                    }

                    var messageResult = new MessageResult(items, nextCursor, totalCount);
                    Task<bool> callbackTask = Invoke(messageResult);

                    if (callbackTask.IsCompleted)
                    {
                        try
                        {
                            // Make sure exceptions propagate
                            callbackTask.Wait();

                            if (callbackTask.Result)
                            {
                                // Sync path
                                goto Process;
                            }
                            else
                            {
                                // If the callback said it's done then stop
                                taskCompletionSource.TrySetResult(null);
                            }
                        }
                        catch (Exception ex)
                        {
                            taskCompletionSource.TrySetException(ex);
                        }
                    }
                    else
                    {
                        WorkImplAsync(callbackTask, topics, taskCompletionSource);
                    }
                }
                else
                {
                    taskCompletionSource.TrySetResult(null);
                }
            }
Beispiel #6
0
        private PersistentResponse GetResponse(MessageResult result)
        {
            // Do a single sweep through the results to process commands and extract values
            var messageValues = ProcessResults(result.Messages);

            var response = new PersistentResponse
            {
                MessageId = result.LastMessageId,
                Messages = messageValues,
                Disconnect = _disconnected,
                Aborted = _aborted
            };

            PopulateResponseState(response);

            Trace.TraceInformation("Connection '{0}' received {1} messages, last id {2}", _connectionId, result.Messages.Count, result.LastMessageId);

            return response;
        }
Beispiel #7
0
        private List<string> ProcessResults(MessageResult result)
        {
            var messageValues = new List<string>(result.TotalCount);

            for (int i = 0; i < result.Messages.Count; i++)
            {
                for (int j = result.Messages[i].Offset; j < result.Messages[i].Offset + result.Messages[i].Count; j++)
                {
                    Message message = result.Messages[i].Array[j];
                    if (SignalCommand.IsCommand(message))
                    {
                        var command = _serializer.Parse<SignalCommand>(message.Value);
                        ProcessCommand(command);
                    }
                    else
                    {
                        messageValues.Add(message.Value);
                    }
                }
            }

            return messageValues;
        }
Beispiel #8
0
 public Task<bool> Invoke(MessageResult result)
 {
     return _callback.Invoke(result);
 }
        private void WorkImpl(TaskCompletionSource<object> taskCompletionSource)
        {
            Process:
            if (!Alive)
            {
                // If this subscription is dead then return immediately
                taskCompletionSource.TrySetResult(null);
                return;
            }

            int totalCount = 0;
            string nextCursor = null;
            var items = new List<ArraySegment<Message>>();
            object state = null;

            PerformWork(ref items, out nextCursor, ref totalCount, out state);

            if (Alive && items.Count > 0)
            {
                BeforeInvoke(state);

                var messageResult = new MessageResult(items, nextCursor, totalCount);
                Task<bool> callbackTask = Invoke(messageResult);

                if (callbackTask.IsCompleted)
                {
                    try
                    {
                        // Make sure exceptions propagate
                        callbackTask.Wait();

                        if (callbackTask.Result)
                        {
                            // Sync path
                            goto Process;
                        }
                        else
                        {
                            // If we're done pumping messages through to this subscription
                            // then dispose
                            Dispose();

                            // If the callback said it's done then stop
                            taskCompletionSource.TrySetResult(null);
                        }
                    }
                    catch (Exception ex)
                    {
                        taskCompletionSource.TrySetException(ex);
                    }
                }
                else
                {
                    WorkImplAsync(callbackTask, taskCompletionSource);
                }
            }
            else
            {
                taskCompletionSource.TrySetResult(null);
            }
        }
Beispiel #10
0
        private PersistentResponse GetResponse(MessageResult result)
        {
            // Do a single sweep through the results to process commands and extract values
            var messageValues = ProcessResults(result);

            var response = new PersistentResponse
            {
                MessageId = result.LastMessageId,
                Messages = messageValues,
                Disconnect = _disconnected,
                Aborted = _aborted
            };

            PopulateResponseState(response);

            return response;
        }
Beispiel #11
0
        private void ProcessResults(MessageResult result)
        {
            result.Messages.Enumerate(message => message.IsCommand || message.IsAck,
                                      message =>
                                      {
                                          if (message.IsAck)
                                          {
                                              TaskCompletionSource<object> tcs;
                                              if (_acks.TryRemove(message.CommandId, out tcs))
                                              {
                                                  tcs.TrySetResult(null);
                                              }
                                          }
                                          else
                                          {
                                              var command = _serializer.Parse<Command>(message.Value);
                                              ProcessCommand(command);

                                              // If we're on the same box and there's a pending ack for this command then
                                              // just trip it
                                              TaskCompletionSource<object> tcs;
                                              if (_acks.TryRemove(message.CommandId, out tcs))
                                              {
                                                  tcs.TrySetResult(null);
                                              }
                                              else
                                              {
                                                  // Send a message through the bus confirming that we got the message
                                                  // REVIEW: Do we retry if this fails?
                                                  _bus.Ack(_connectionId, message.Key, message.CommandId).Catch();
                                              }
                                          }
                                      });
        }
Beispiel #12
0
 private void ProcessResults(MessageResult result)
 {
     for (int i = 0; i < result.Messages.Count; i++)
     {
         for (int j = result.Messages[i].Offset; j < result.Messages[i].Offset + result.Messages[i].Count; j++)
         {
             Message message = result.Messages[i].Array[j];
             if (SignalCommand.IsCommand(message))
             {
                 var command = _serializer.Parse<SignalCommand>(message.Value);
                 ProcessCommand(command);
             }
         }
     }
 }
Beispiel #13
0
 public virtual Task <bool> Invoke(MessageResult result)
 {
     return(_callback.Invoke(result));
 }
Beispiel #14
0
        private void WorkImpl(TaskCompletionSource <object> taskCompletionSource)
        {
Process:
            if (!Alive)
            {
                // If this subscription is dead then return immediately
                taskCompletionSource.TrySetResult(null);
                return;
            }

            int    totalCount = 0;
            string nextCursor = null;
            var    items      = new List <ArraySegment <Message> >();
            object state      = null;

            PerformWork(ref items, out nextCursor, ref totalCount, out state);

            if (Alive && items.Count > 0)
            {
                BeforeInvoke(state);

                var         messageResult = new MessageResult(items, nextCursor, totalCount);
                Task <bool> callbackTask  = Invoke(messageResult);

                if (callbackTask.IsCompleted)
                {
                    try
                    {
                        // Make sure exceptions propagate
                        callbackTask.Wait();

                        if (callbackTask.Result)
                        {
                            // Sync path
                            goto Process;
                        }
                        else
                        {
                            // If we're done pumping messages through to this subscription
                            // then dispose
                            Dispose();

                            // If the callback said it's done then stop
                            taskCompletionSource.TrySetResult(null);
                        }
                    }
                    catch (Exception ex)
                    {
                        taskCompletionSource.TrySetException(ex);
                    }
                }
                else
                {
                    WorkImplAsync(callbackTask, taskCompletionSource);
                }
            }
            else
            {
                taskCompletionSource.TrySetResult(null);
            }
        }
Beispiel #15
0
            private void WorkImpl(ConcurrentDictionary <string, Topic> topics, TaskCompletionSource <object> taskCompletionSource)
            {
Process:
                int totalCount = 0;
                string nextCursor = null;
                List <ArraySegment <Message> > items = null;

                lock (_lockObj)
                {
                    items = new List <ArraySegment <Message> >(Cursors.Count);
                    for (int i = 0; i < Cursors.Count; i++)
                    {
                        Cursor cursor = Cursors[i];

                        MessageStoreResult <Message> storeResult = cursor.Topic.Store.GetMessages(cursor.Id, _maxMessages);
                        ulong next = storeResult.FirstMessageId + (ulong)storeResult.Messages.Count;
                        cursor.Id = next;

                        if (storeResult.Messages.Count > 0)
                        {
                            items.Add(storeResult.Messages);
                            totalCount += storeResult.Messages.Count;
                        }
                    }

                    nextCursor = Cursor.MakeCursor(Cursors);
                }

                if (Alive && items.Count > 0)
                {
                    var         messageResult = new MessageResult(items, nextCursor, totalCount);
                    Task <bool> callbackTask  = Invoke(messageResult);

                    if (callbackTask.IsCompleted)
                    {
                        try
                        {
                            // Make sure exceptions propagate
                            callbackTask.Wait();

                            if (callbackTask.Result)
                            {
                                // Sync path
                                goto Process;
                            }
                            else
                            {
                                // If the callback said it's done then stop
                                taskCompletionSource.TrySetResult(null);
                            }
                        }
                        catch (Exception ex)
                        {
                            taskCompletionSource.TrySetException(ex);
                        }
                    }
                    else
                    {
                        WorkImplAsync(callbackTask, topics, taskCompletionSource);
                    }
                }
                else
                {
                    taskCompletionSource.TrySetResult(null);
                }
            }