68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using KLHZ.Trader.Core.Common.Messaging.Contracts;
|
|
using KLHZ.Trader.Core.Common.Messaging.Contracts.Messages;
|
|
using System.Collections.Concurrent;
|
|
using System.Threading.Channels;
|
|
|
|
namespace KLHZ.Trader.Core.Common.Messaging.Services
|
|
{
|
|
public class DataBus : IDataBus
|
|
{
|
|
private readonly ConcurrentDictionary<string, Channel<INewCandle>> _candlesChannels = new();
|
|
private readonly ConcurrentDictionary<string, Channel<INewPriceMessage>> _priceChannels = new();
|
|
private readonly ConcurrentDictionary<string, Channel<TradeCommand>> _commandChannels = new();
|
|
private readonly ConcurrentDictionary<string, Channel<MessageForAdmin>> _chatMessages = new();
|
|
|
|
public bool AddChannel(Channel<MessageForAdmin> channel)
|
|
{
|
|
return _chatMessages.TryAdd(Guid.NewGuid().ToString(), channel);
|
|
}
|
|
|
|
public bool AddChannel(string key, Channel<INewPriceMessage> 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(INewPriceMessage newPriceMessage)
|
|
{
|
|
foreach (var channel in _priceChannels.Values)
|
|
{
|
|
await channel.Writer.WriteAsync(newPriceMessage);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
public async Task BroadcastCommand(MessageForAdmin message)
|
|
{
|
|
foreach (var channel in _chatMessages.Values)
|
|
{
|
|
await channel.Writer.WriteAsync(message);
|
|
}
|
|
}
|
|
}
|
|
}
|