This repository has been archived by the owner on May 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueuedStream.cs
245 lines (210 loc) · 5.52 KB
/
QueuedStream.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace Fleck
{
/// <summary>
/// Wraps a stream and queues multiple write operations.
/// Useful for wrapping SslStream as it does not support multiple simultaneous write operations.
/// </summary>
public class QueuedStream : Stream
{
readonly Stream _stream;
readonly Queue<WriteData> _queue = new Queue<WriteData>();
int _pendingWrite;
bool _disposed;
public QueuedStream(Stream stream)
{
_stream = stream;
}
public override bool CanRead
{
get { return _stream.CanRead; }
}
public override bool CanSeek
{
get { return _stream.CanSeek; }
}
public override bool CanWrite
{
get { return _stream.CanWrite; }
}
public override long Length
{
get { return _stream.Length; }
}
public override long Position
{
get { return _stream.Position; }
set { _stream.Position = value; }
}
public override int Read(byte[] buffer, int offset, int count)
{
return _stream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return _stream.Seek(offset, origin);
}
public override void SetLength(long value)
{
_stream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException("QueuedStream does not support synchronous write operations yet.");
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
return _stream.BeginRead(buffer, offset, count, callback, state);
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
lock (_queue)
{
var data = new WriteData(buffer, offset, count, callback, state);
if (_pendingWrite > 0)
{
_queue.Enqueue(data);
return data.AsyncResult;
}
return BeginWriteInternal(buffer, offset, count, callback, state, data);
}
}
public override int EndRead(IAsyncResult asyncResult)
{
return _stream.EndRead(asyncResult);
}
public override void EndWrite(IAsyncResult asyncResult)
{
if (asyncResult is QueuedWriteResult)
{
var queuedResult = asyncResult as QueuedWriteResult;
if (queuedResult.Exception != null) throw queuedResult.Exception;
var ar = queuedResult.ActualResult;
if (ar == null)
{
throw new NotSupportedException(
"QueuedStream does not support synchronous write operations. Please wait for callback to be invoked before calling EndWrite.");
}
// EndWrite on actual stream should already be invoked.
}
else
{
throw new ArgumentException();
}
}
public override void Flush()
{
_stream.Flush();
}
public override void Close()
{
_stream.Close();
}
protected override void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_stream.Dispose();
}
_disposed = true;
}
base.Dispose(disposing);
}
IAsyncResult BeginWriteInternal(byte[] buffer, int offset, int count, AsyncCallback callback, object state, WriteData queued)
{
_pendingWrite++;
var result = _stream.BeginWrite(buffer, offset, count, ar =>
{
// callback can be executed even before return value of BeginWriteInternal is set to this property
queued.AsyncResult.ActualResult = ar;
try
{
// so that we can call BeginWrite again
_stream.EndWrite(ar);
}
catch (Exception exc)
{
queued.AsyncResult.Exception = exc;
}
// one down, another is good to go
lock (_queue)
{
_pendingWrite--;
while (_queue.Count > 0)
{
var data = _queue.Dequeue();
try
{
data.AsyncResult.ActualResult = BeginWriteInternal(data.Buffer, data.Offset, data.Count, data.Callback, data.State, data);
break;
}
catch (Exception exc)
{
_pendingWrite--;
data.AsyncResult.Exception = exc;
data.Callback(data.AsyncResult);
}
}
callback(queued.AsyncResult);
}
}, state);
// always return the wrapped async result.
// this is especially important if the underlying stream completed the operation synchronously (hence "result.CompletedSynchronously" is true!)
queued.AsyncResult.ActualResult = result;
return queued.AsyncResult;
}
#region Nested type: WriteData
class WriteData
{
public readonly byte[] Buffer;
public readonly int Offset;
public readonly int Count;
public readonly AsyncCallback Callback;
public readonly object State;
public readonly QueuedWriteResult AsyncResult;
public WriteData(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
Buffer = buffer;
Offset = offset;
Count = count;
Callback = callback;
State = state;
AsyncResult = new QueuedWriteResult(state);
}
}
#endregion
#region Nested type: QueuedWriteResult
class QueuedWriteResult : IAsyncResult
{
readonly object _state;
public QueuedWriteResult(object state)
{
_state = state;
}
public Exception Exception { get; set; }
public IAsyncResult ActualResult { get; set; }
public object AsyncState
{
get { return _state; }
}
public WaitHandle AsyncWaitHandle
{
get { throw new NotSupportedException("Queued write operations do not support wait handle."); }
}
public bool CompletedSynchronously
{
get { return false; }
}
public bool IsCompleted
{
get { return ActualResult != null && ActualResult.IsCompleted; }
}
}
#endregion
}
}