#region S# License
/******************************************************************************************
NOTICE!!! This program and source code is owned and licensed by
StockSharp, LLC, www.stocksharp.com
Viewing or use of this code requires your acceptance of the license
agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
Removal of this comment is a violation of the license agreement.
Project: StockSharp.Messages.Messages
File: InMemoryMessageChannel.cs
Created: 2015, 11, 11, 2:32 PM
Copyright 2010 by StockSharp, LLC
*******************************************************************************************/
#endregion S# License
namespace StockSharp.Messages
{
using System;
using System.Globalization;
using Ecng.Common;
using StockSharp.Localization;
using StockSharp.Logging;
///
/// Message channel, based on the queue and operate within a single process.
///
public class InMemoryMessageChannel : Cloneable, IMessageChannel
{
private static readonly MemoryStatisticsValue _msgStat = new MemoryStatisticsValue(LocalizedStrings.Messages);
static InMemoryMessageChannel()
{
MemoryStatistics.Instance.Values.Add(_msgStat);
}
private readonly Action _errorHandler;
private readonly MessagePriorityQueue _messageQueue = new MessagePriorityQueue();
///
/// Initializes a new instance of the .
///
/// Channel name.
/// Error handler.
public InMemoryMessageChannel(string name, Action errorHandler)
{
if (name.IsEmpty())
throw new ArgumentNullException(nameof(name));
if (errorHandler == null)
throw new ArgumentNullException(nameof(errorHandler));
Name = name;
_errorHandler = errorHandler;
_messageQueue.Close();
}
///
/// Handler name.
///
public string Name { get; }
///
/// Message queue count.
///
public int MessageCount => _messageQueue.Count;
///
/// Max message queue count.
///
///
/// The default value is -1, which corresponds to the size without limitations.
///
public int MaxMessageCount
{
get => _messageQueue.MaxSize;
set => _messageQueue.MaxSize = value;
}
///
/// Channel closing event.
///
public event Action Closed;
///
/// Is channel opened.
///
public bool IsOpened => !_messageQueue.IsClosed;
///
/// Open channel.
///
public void Open()
{
_messageQueue.Open();
ThreadingHelper
.Thread(() => CultureInfo.InvariantCulture.DoInCulture(() =>
{
while (!_messageQueue.IsClosed)
{
try
{
if (!_messageQueue.TryDequeue(out var message))
{
break;
}
//if (!(message is TimeMessage) && message.GetType().Name != "BasketMessage")
// Console.WriteLine("<< ({0}) {1}", System.Threading.Thread.CurrentThread.Name, message);
_msgStat.Remove(message);
NewOutMessage?.Invoke(message);
}
catch (Exception ex)
{
_errorHandler(ex);
}
}
Closed?.Invoke();
}))
.Name($"{Name} channel thread.")
//.Culture(CultureInfo.InvariantCulture)
.Launch();
}
///
/// Close channel.
///
public void Close()
{
_messageQueue.Close();
}
///
/// Send message.
///
/// Message.
public void SendInMessage(Message message)
{
if (!IsOpened)
throw new InvalidOperationException();
//if (!(message is TimeMessage) && message.GetType().Name != "BasketMessage")
// Console.WriteLine(">> ({0}) {1}", System.Threading.Thread.CurrentThread.Name, message);
_msgStat.Add(message);
_messageQueue.Enqueue(message);
}
///
/// New message event.
///
public event Action NewOutMessage;
void IDisposable.Dispose()
{
Close();
}
///
/// Create a copy of .
///
/// Copy.
public override IMessageChannel Clone()
{
return new InMemoryMessageChannel(Name, _errorHandler) { MaxMessageCount = MaxMessageCount };
}
}
}