Exemple #1
0
        public bool Publish(BusChannel channel, Packet packet)
        {
            try
            {
                if (BindQueue(channel))
                {
                    var body   = packet.GetByteArray();
                    var router = $"{GetChannelName(_exchangeName, channel)}.{packet.Service}";
                    _channel.BasicPublish(_exchangeName, router, false, _properties, body);
                }

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Exemple #2
0
        public bool Subscribe(BusChannel channel, PacketProcessor processor)
        {
            if (BindQueue(channel))
            {
                string channelName = GetChannelName(_exchangeName, channel);

                //queue
                _queues.TryAdd(channelName, new ConcurrentQueue <Packet>());
                var queue = _queues[channelName];

                //processor
                List <PacketProcessor> subscriber = null;
                if (_subscribers.ContainsKey(channelName))
                {
                    subscriber = _subscribers[channelName];
                }
                else
                {
                    subscriber = new List <PacketProcessor>();
                }
                AddProcessor2Subscriber(processor, subscriber);
                _subscribers.TryAdd(channelName, subscriber);

                //consumer
                var consumer = new EventingBasicConsumer(_channel);
                consumer.Received += (sender, args) =>
                {
                    var packet = args.Body.FromByteArray();
                    queue.Enqueue(packet);
                };
                _channel.BasicConsume(channelName, true, consumer);

                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #3
0
 private async Task <bool> _processor(BusChannel channel, Packet packet)
 {
     return(await Task.FromResult(true));
 }
Exemple #4
0
 public DataRecordBuilder Channel(BusChannel channel)
 {
     record.Channel = channel;
     return(this);
 }