55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using BCG.Utilities.General;
|
|
|
|
namespace BCG
|
|
{
|
|
/*
|
|
* The threadsafe stream wraps around an input stream, and amalgamates all the values from that stream
|
|
* locally. It only fires packets on the thread where the VSD's doWork function is called.
|
|
*
|
|
* */
|
|
public class ThreadSafeStream : ViewStream, IWorker
|
|
{
|
|
|
|
private ViewStream myParentStream;
|
|
readonly ThreadSafeQueue<IDataPacket> myQueuedUpdates = new();
|
|
private ViewStreamDatabase myVSD;
|
|
|
|
public ThreadSafeStream(ViewStream parent) : this(ViewStreamDatabase.instance, parent) { }
|
|
public ThreadSafeStream(ViewStreamDatabase vsd, ViewStream parentStream)
|
|
{
|
|
myParentStream = parentStream;
|
|
myVSD = vsd;
|
|
}
|
|
|
|
protected override void myOnFirstSubscriber()
|
|
{
|
|
myParentStream.onChange += new ChangeDelegate(myParentStreamChangeHandler);
|
|
myVSD.addWorker(this);
|
|
}
|
|
|
|
|
|
|
|
protected override void myOnLastSubscriber()
|
|
{
|
|
myParentStream.onChange -= new ChangeDelegate(myParentStreamChangeHandler);
|
|
myVSD.removeWorker(this);
|
|
}
|
|
|
|
void myParentStreamChangeHandler(object newValue)
|
|
{
|
|
myQueuedUpdates.enqueue((IDataPacket)newValue);
|
|
}
|
|
|
|
public void doWork()
|
|
{
|
|
foreach (var dp in myQueuedUpdates.dequeueAll()) {
|
|
myFireEvent(dp);
|
|
}
|
|
}
|
|
}
|
|
}
|