Repository navigation
Expand file tree
/
Copy pathAbstractStream.cs
More file actions
148 lines (121 loc) · 3.64 KB
/
Copy pathAbstractStream.cs
File metadata and controls
148 lines (121 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Threading;
namespace NuclearVOIP
{
public class AbstractStream<T> : InStream<T>, OutStream<T>
{
private InStream<T>? consumer;
protected class Node(T data)
{
public readonly T data = data;
public Node? next;
}
protected Node? head;
protected Node? tail;
public event Action<StreamArgs<T>>? OnData;
public virtual void Write(T data)
{
Write([data]);
}
public virtual void Write(T[] data)
{
if (data.Length == 0)
return;
if (consumer != null)
{
consumer.Write(data);
return;
}
StreamArgs<T> args = new(data);
OnData?.Invoke(args);
if (!args.Handled)
_Write(data);
}
/// <summary>
/// Write to the storage without invoking the event system.
/// Designed to be called from derived classes which monitor the event system.
/// </summary>
/// <param name="data">Data to write</param>
protected void _Write(T[] data)
{
Node ourHead = new(data[0]);
Node curNode = ourHead;
for (int i = 1; i < data.Length; i++)
{
curNode.next = new(data[i]);
curNode = curNode.next;
}
Node? old = Interlocked.Exchange(ref tail, curNode);
Interlocked.CompareExchange(ref head, ourHead, null);
if (old != null)
old.next = ourHead;
}
public virtual T? Read()
{
if (head == null)
return default;
Node? ourNode = head;
while (ourNode != null && ourNode != Interlocked.CompareExchange(ref head, ourNode.next, ourNode))
ourNode = head;
if (ourNode != null)
{
Interlocked.CompareExchange(ref tail, null, ourNode);
return ourNode.data;
}
else
return default;
}
public virtual T[]? Read(int num)
{
if (num == 0)
return null;
loop:
if (head == null)
return null;
Node ourHead = head;
Node? endNode = head;
for (int i = 0; i < num; i++)
{
if (endNode == null)
break;
else
endNode = endNode.next;
}
if (Interlocked.CompareExchange(ref head, endNode, ourHead) != ourHead)
goto loop;
if (endNode == null)
Interlocked.CompareExchange(ref tail, null, null);
Node curNode = ourHead;
T[] values = new T[num];
for (int i = 0; i < num; i++)
{
values[i] = curNode.data;
curNode = curNode.next!;
}
return values;
}
public bool Empty()
{
return head == null;
}
public int Count()
{
int count = 0;
for (Node? curNode = head; curNode != null; curNode = curNode.next)
count++;
return count;
}
public void Pipe(InStream<T>? stream)
{
if (stream == null)
consumer = null;
else
{
T[]? prefix = Read(Count());
if (prefix != null)
stream.Write(prefix);
consumer = stream;
}
}
}
}