67 lines
2.3 KiB
C#
67 lines
2.3 KiB
C#
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<string, Channel<IOrderbook>> _orderbooksChannels = new();
|
|
private readonly ConcurrentDictionary<string, Channel<IMessage>> _messagesChannels = new();
|
|
private readonly ConcurrentDictionary<string, Channel<ITradeCommand>> _commandsChannel = new();
|
|
private readonly ConcurrentDictionary<string, Channel<ITradeDataItem>> _priceChannels = new();
|
|
|
|
public bool AddChannel(string key, Channel<IMessage> channel)
|
|
{
|
|
return _messagesChannels.TryAdd(key, channel);
|
|
}
|
|
|
|
public bool AddChannel(string key, Channel<ITradeCommand> channel)
|
|
{
|
|
return _commandsChannel.TryAdd(key, channel);
|
|
}
|
|
|
|
public bool AddChannel(string key, Channel<ITradeDataItem> channel)
|
|
{
|
|
return _priceChannels.TryAdd(key, channel);
|
|
}
|
|
|
|
public bool AddChannel(string key, Channel<IOrderbook> channel)
|
|
{
|
|
return _orderbooksChannels.TryAdd(key, channel);
|
|
}
|
|
|
|
public async Task Broadcast(ITradeDataItem newPriceMessage)
|
|
{
|
|
foreach (var channel in _priceChannels.Values)
|
|
{
|
|
await channel.Writer.WriteAsync(newPriceMessage);
|
|
}
|
|
}
|
|
|
|
public async Task Broadcast(ITradeCommand command)
|
|
{
|
|
foreach (var channel in _commandsChannel.Values)
|
|
{
|
|
await channel.Writer.WriteAsync(command);
|
|
}
|
|
}
|
|
public async Task Broadcast(IOrderbook orderbook)
|
|
{
|
|
foreach (var channel in _orderbooksChannels.Values)
|
|
{
|
|
await channel.Writer.WriteAsync(orderbook);
|
|
}
|
|
}
|
|
|
|
public async Task Broadcast(IMessage message)
|
|
{
|
|
foreach (var channel in _messagesChannels.Values)
|
|
{
|
|
await channel.Writer.WriteAsync(message);
|
|
}
|
|
}
|
|
}
|
|
}
|