75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
using KLHZ.Trader.Core.Contracts.Messaging.Dtos;
|
|
using KLHZ.Trader.Core.Contracts.Messaging.Dtos.Intarfaces;
|
|
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<string, Channel<IMessage>> _messagesChannels = new();
|
|
private readonly ConcurrentDictionary<string, Channel<INewCandle>> _candlesChannels = new();
|
|
private readonly ConcurrentDictionary<string, Channel<INewPrice>> _priceChannels = new();
|
|
private readonly ConcurrentDictionary<string, Channel<IProcessedPrice>> _processedPricesChannels = new();
|
|
private readonly ConcurrentDictionary<string, Channel<TradeCommand>> _commandChannels = new();
|
|
|
|
public bool AddChannel(string key, Channel<IProcessedPrice> channel)
|
|
{
|
|
return _processedPricesChannels.TryAdd(key, channel);
|
|
}
|
|
|
|
public bool AddChannel(string key, Channel<IMessage> channel)
|
|
{
|
|
return _messagesChannels.TryAdd(key, channel);
|
|
}
|
|
|
|
public bool AddChannel(string key, Channel<INewPrice> channel)
|
|
{
|
|
return _priceChannels.TryAdd(key, channel);
|
|
}
|
|
|
|
public bool AddChannel(string key, Channel<INewCandle> channel)
|
|
{
|
|
return _candlesChannels.TryAdd(key, channel);
|
|
}
|
|
|
|
public bool AddChannel(string key, Channel<TradeCommand> 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);
|
|
}
|
|
}
|
|
}
|
|
}
|