internal static Task Ack(this IMessageBus bus, string acker, string commandId)
 {
     // Prepare the ack
     var message = new Message(acker, AckSubscriber.Signal, null);
     message.CommandId = commandId;
     message.IsAck = true;
     return bus.Publish(message);
 }
        public override Task Publish(Message message)
        {
            Counters.MessageBusMessagesPublishedTotal.Increment();
            Counters.MessageBusMessagesPublishedPerSec.Increment();

            // TODO: Implement message batching here
            return Send(new[] { message });
        }
        public static Message ReadFrom(Stream stream)
        {
            var message = new Message();
            var binaryReader = new BinaryReader(stream);
            message.Source = binaryReader.ReadString();
            message.Key = binaryReader.ReadString();
            int bytes = binaryReader.ReadInt32();
            message.Value = new ArraySegment<byte>(binaryReader.ReadBytes(bytes));
            message.CommandId = binaryReader.ReadString();
            message.WaitForAck = binaryReader.ReadBoolean();
            message.IsAck = binaryReader.ReadBoolean();
            message.Filter = binaryReader.ReadString();

            return message;
        }
        protected ulong Save(Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            // GetTopic will return a topic for the given key. If topic exists and is Dying, 
            // it will revive it and mark it as NoSubscriptions
            Topic topic = GetTopic(message.Key);
            // Mark the topic as used so it doesn't immediately expire (if it was in that state before).
            topic.MarkUsed();

            return topic.Store.Add(message);
        }
        /// <summary>
        /// Publishes a new message to the specified event on the bus.
        /// </summary>
        /// <param name="message">The message to publish.</param>
        public virtual Task Publish(Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            Topic topic;
            if (Topics.TryGetValue(message.Key, out topic))
            {
                topic.Store.Add(message);
                ScheduleTopic(topic);
            }

            Counters.MessageBusMessagesPublishedTotal.Increment();
            Counters.MessageBusMessagesPublishedPerSec.Increment();


            return TaskAsyncHelper.Empty;
        }