using KLHZ.Trader.Core.Contracts.Messaging.Dtos; using KLHZ.Trader.Core.Contracts.Messaging.Dtos.Interfaces; using KLHZ.Trader.Core.Contracts.Messaging.Interfaces; using System.Collections.Concurrent; using System.Threading.Channels; namespace KLHZ.Trader.Core.Common.Messaging.Services { public class DataBus : IDataBus { private readonly ConcurrentDictionary> _messagesChannels = new(); private readonly ConcurrentDictionary> _candlesChannels = new(); private readonly ConcurrentDictionary> _priceChannels = new(); private readonly ConcurrentDictionary> _processedPricesChannels = new(); private readonly ConcurrentDictionary> _commandChannels = new(); public bool AddChannel(string key, Channel channel) { return _processedPricesChannels.TryAdd(key, channel); } public bool AddChannel(string key, Channel channel) { return _messagesChannels.TryAdd(key, channel); } public bool AddChannel(string key, Channel channel) { return _priceChannels.TryAdd(key, channel); } public bool AddChannel(string key, Channel channel) { return _candlesChannels.TryAdd(key, channel); } public bool AddChannel(string key, Channel channel) { return _commandChannels.TryAdd(key, channel); } public async Task BroadcastNewPrice(INewPrice newPriceMessage) { foreach (var channel in _priceChannels.Values) { await channel.Writer.WriteAsync(newPriceMessage); } } public async Task BroadcastProcessedPrice(IProcessedPrice mess) { foreach (var channel in _processedPricesChannels.Values) { await channel.Writer.WriteAsync(mess); } } public async Task BroadcastNewCandle(INewCandle newPriceMessage) { foreach (var channel in _candlesChannels.Values) { await channel.Writer.WriteAsync(newPriceMessage); } } public async Task BroadcastCommand(TradeCommand command) { foreach (var channel in _commandChannels.Values) { await channel.Writer.WriteAsync(command); } } } }