Repository navigation
Expand file tree
/
Copy pathApmStreamWrapper.cs
More file actions
258 lines (223 loc) · 6.3 KB
/
Copy pathApmStreamWrapper.cs
File metadata and controls
258 lines (223 loc) · 6.3 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Net.Http.Client
{
public class ApmStreamWrapper : ApmStream
{
private Stream _innerStream;
public ApmStreamWrapper(Stream innerStream)
{
_innerStream = innerStream;
}
public override bool CanRead
{
get
{
return _innerStream.CanRead;
}
}
public override bool CanSeek
{
get
{
return _innerStream.CanSeek;
}
}
public override bool CanWrite
{
get
{
return _innerStream.CanWrite;
}
}
public override bool CanTimeout
{
get
{
return _innerStream.CanTimeout;
}
}
public override int ReadTimeout
{
get
{
return _innerStream.ReadTimeout;
}
set
{
_innerStream.ReadTimeout = value;
}
}
public override int WriteTimeout
{
get
{
return _innerStream.WriteTimeout;
}
set
{
_innerStream.WriteTimeout = value;
}
}
public override long Length
{
get
{
return _innerStream.Length;
}
}
public override long Position
{
get
{
return _innerStream.Position;
}
set
{
_innerStream.Position = value;
}
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
{
#if ASPNETCORE50
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>(state);
InternalReadAsync(buffer, offset, size, callback, tcs);
return tcs.Task;
#else
return _innerStream.BeginRead(buffer, offset, size, callback, state);
#endif
}
#if ASPNETCORE50
private async void InternalReadAsync(byte[] buffer, int offset, int size, AsyncCallback callback, TaskCompletionSource<int> tcs)
{
try
{
int read = await _innerStream.ReadAsync(buffer, offset, size);
tcs.TrySetResult(read);
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
try
{
callback(tcs.Task);
}
catch (Exception)
{
}
}
#endif
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
{
#if ASPNETCORE50
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(state);
InternalWriteAsync(buffer, offset, size, callback, tcs);
return tcs.Task;
#else
return _innerStream.BeginWrite(buffer, offset, size, callback, state);
#endif
}
#if ASPNETCORE50
private async void InternalWriteAsync(byte[] buffer, int offset, int size, AsyncCallback callback, TaskCompletionSource<object> tcs)
{
try
{
await _innerStream.WriteAsync(buffer, offset, size);
tcs.TrySetResult(null);
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
try
{
callback(tcs.Task);
}
catch (Exception)
{
}
}
#endif
public override int EndRead(IAsyncResult asyncResult)
{
#if ASPNETCORE50
Task<int> t = (Task<int>)asyncResult;
t.Wait();
if (t.IsFaulted)
{
throw new IOException(string.Empty, t.Exception);
}
return t.Result;
#else
return _innerStream.EndRead(asyncResult);
#endif
}
public override void EndWrite(IAsyncResult asyncResult)
{
#if ASPNETCORE50
Task t = (Task)asyncResult;
t.Wait();
if (t.IsFaulted)
{
throw new IOException(string.Empty, t.Exception);
}
#else
_innerStream.EndWrite(asyncResult);
#endif
}
public override void Flush()
{
_innerStream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return _innerStream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return _innerStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
_innerStream.Write(buffer, offset, count);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_innerStream.Dispose();
}
}
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{
return _innerStream.CopyToAsync(destination, bufferSize, cancellationToken);
}
public override Task FlushAsync(CancellationToken cancellationToken)
{
return _innerStream.FlushAsync(cancellationToken);
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return _innerStream.ReadAsync(buffer, offset, count, cancellationToken);
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return _innerStream.WriteAsync(buffer, offset, count, cancellationToken);
}
public override int ReadByte()
{
return _innerStream.ReadByte();
}
public override void WriteByte(byte value)
{
_innerStream.WriteByte(value);
}
}
}