-
Notifications
You must be signed in to change notification settings - Fork 0
/
PipeStream.cs
150 lines (126 loc) · 4.2 KB
/
PipeStream.cs
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
149
150
using System;
using System.IO;
using System.Threading;
namespace AutoCaption
{
public class PipeStream : Stream
{
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => -1;
public override long Position
{
get => 0;
set {}
}
private readonly byte[] _buffer;
private int _read, _write;
private bool _open;
public PipeStream(int bufferSize)
{
if(bufferSize <= 0)
{
throw new ArgumentOutOfRangeException("Buffer size must be greater than 0.");
}
bufferSize--;
bufferSize |= bufferSize >> 1;
bufferSize |= bufferSize >> 2;
bufferSize |= bufferSize >> 4;
bufferSize |= bufferSize >> 8;
bufferSize |= bufferSize >> 16;
bufferSize++;
_buffer = new byte[(uint)bufferSize];
_open = true;
}
public override void Flush() {}
public override long Seek(long offset, SeekOrigin origin) => -1;
public override void SetLength(long value) {}
public override int Read(byte[] buffer, int offset, int count)
{
lock(_buffer)
{
var length = _buffer.Length;
var total = 0;
while(count > 0)
{
var available = _write - _read;
if(available <= 0)
{
Monitor.Wait(_buffer);
available = _write - _read;
}
if(!_open)
{
break;
}
var copy = Math.Min(available, count);
var first = _read % length;
if(first + copy <= length)
{
Buffer.BlockCopy(_buffer, first, buffer, offset, copy);
}
else
{
var a = length - first;
var b = copy - a;
Buffer.BlockCopy(_buffer, first, buffer, offset, a);
Buffer.BlockCopy(_buffer, 0, buffer, offset + a, b);
}
_read += copy;
count -= copy;
offset += copy;
total += copy;
Monitor.Pulse(_buffer);
}
return total;
}
}
public override void Write(byte[] buffer, int offset, int count)
{
lock(_buffer)
{
var length = _buffer.Length;
while(count > 0)
{
var free = length - (_write - _read);
if(free <= 0)
{
Monitor.Wait(_buffer);
free = length - (_write - _read);
}
if(!_open)
{
break;
}
var copy = Math.Min(count, free);
var first = _write % length;
if(first + copy <= length)
{
Buffer.BlockCopy(buffer, offset, _buffer, first, copy);
}
else
{
var a = length - first;
var b = copy - a;
Buffer.BlockCopy(buffer, offset, _buffer, first, a);
Buffer.BlockCopy(buffer, offset + a, _buffer, 0, b);
}
count -= copy;
offset += copy;
_write += copy;
Monitor.Pulse(_buffer);
}
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
lock(_buffer)
{
_open = false;
Monitor.PulseAll(_buffer);
}
}
}
}