namespace StockSharp.Messages; /// /// Message channel, based on the queue and operate within a single process. /// public class InMemoryMessageChannel : IMessageChannel { private readonly IMessageQueue _queue; private readonly Action _errorHandler; private readonly SyncObject _suspendLock = new(); private int _version; /// /// Initializes a new instance of the . /// /// Message queue. /// Channel name. /// Error handler. public InMemoryMessageChannel(IMessageQueue queue, string name, Action errorHandler) { if (name.IsEmpty()) throw new ArgumentNullException(nameof(name)); Name = name; _queue = queue ?? throw new ArgumentNullException(nameof(queue)); _errorHandler = errorHandler ?? throw new ArgumentNullException(nameof(errorHandler)); _queue.Close(); } /// /// Handler name. /// public string Name { get; } /// /// Message queue count. /// public int MessageCount => _queue.Count; /// /// Max message queue count. /// /// /// The default value is -1, which corresponds to the size without limitations. /// public int MaxMessageCount { get => _queue.MaxSize; set => _queue.MaxSize = value; } /// /// The channel cannot be opened. /// public bool Disabled { get; set; } private ChannelStates _state = ChannelStates.Stopped; /// public ChannelStates State { get => _state; private set { if (_state == value) return; _state = value; StateChanged?.Invoke(); } } /// public event Action StateChanged; /// public void Open() { if (Disabled) return; State = ChannelStates.Started; _queue.Open(); var version = Interlocked.Increment(ref _version); ThreadingHelper .Thread(() => Do.Invariant(() => { while (this.IsOpened()) { try { if (!_queue.TryDequeue(out var message)) break; if (State == ChannelStates.Suspended) { _suspendLock.Wait(); if (!this.IsOpened()) break; } if (_version != version) break; NewOutMessage?.Invoke(message); } catch (Exception ex) { _errorHandler(ex); } } State = ChannelStates.Stopped; })) .Name($"{Name} channel thread.") //.Culture(CultureInfo.InvariantCulture) .Launch(); } /// public void Close() { State = ChannelStates.Stopping; _queue.Close(); _queue.Clear(); _suspendLock.Pulse(); } void IMessageChannel.Suspend() { State = ChannelStates.Suspended; } void IMessageChannel.Resume() { State = ChannelStates.Started; _suspendLock.PulseAll(); } void IMessageChannel.Clear() { _queue.Clear(); } /// public bool SendInMessage(Message message) { if (!this.IsOpened()) { //throw new InvalidOperationException(); return false; } if (State == ChannelStates.Suspended) { _suspendLock.Wait(); if (!this.IsOpened()) return false; } _queue.Enqueue(message); return true; } /// public event Action NewOutMessage; /// /// Create a copy of . /// /// Copy. public virtual IMessageChannel Clone() { return new InMemoryMessageChannel(_queue, Name, _errorHandler) { MaxMessageCount = MaxMessageCount, }; } object ICloneable.Clone() { return Clone(); } void IDisposable.Dispose() { Close(); GC.SuppressFinalize(this); } }