common/Hardware/Common.Hardware.SIRT/SIRTChanel.cs
2026-04-23 17:50:07 +02:00

57 lines
1.4 KiB
C#

namespace Common.Hardware.SIRT
{
using System;
using System.Collections.Generic;
using System.Threading;
public class SIRTChanel
{
private readonly ManualResetEventSlim awaiter = new ManualResetEventSlim();
private readonly Queue<SIRTMessage> messages = new Queue<SIRTMessage>();
public bool IsFree { get; private set; }
public uint Address { get; private set; }
public event Action<SIRTChanel> Captured;
public event Action<SIRTChanel> Released;
public void Capture(uint address)
{
this.IsFree = false;
this.Address = address;
this.messages.Clear();
this.Captured?.Invoke(this);
}
public IEnumerable<SIRTMessage> GetEnumerable()
{
while (true)
{
while (this.messages.Count > 0)
{
yield return this.messages.Dequeue();
}
this.awaiter.Reset();
this.awaiter.Wait();
}
}
public void Release()
{
this.Released?.Invoke(this);
this.messages.Clear();
this.Address = uint.MaxValue;
this.IsFree = true;
}
internal void Receive(SIRTMessage message)
{
this.messages.Enqueue(message);
this.awaiter.Set();
}
}
}