forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncUtils.cs
More file actions
265 lines (221 loc) · 7.74 KB
/
Copy pathAsyncUtils.cs
File metadata and controls
265 lines (221 loc) · 7.74 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
259
260
261
262
263
264
265
// Copyright (c) Service Stack LLC. All Rights Reserved.
// License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
/*
* Keep as much platform specific stuff here
*/
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
#if NETFX_CORE
using Windows.System.Threading;
#endif
namespace ServiceStack
{
public interface ITimer : IDisposable
{
void Cancel();
}
public delegate void ProgressDelegate(long done, long total);
internal static class AsyncUtils
{
public static Exception CreateTimeoutException(this Exception ex, string errorMsg)
{
#if SILVERLIGHT
return new WebException("The request timed out", ex, WebExceptionStatus.RequestCanceled, null);
#else
return new WebException("The request timed out", ex, WebExceptionStatus.Timeout, null);
#endif
}
internal static ITimer CreateTimer<TResponse>(this AsyncState<TResponse> state, TimeSpan timeOut)
{
#if NETFX_CORE
return new NetFxAsyncTimer(ThreadPoolTimer.CreateTimer(request.TimedOut, timeOut));
#else
return new AsyncTimer(new Timer(state.TimedOut, state, (int)timeOut.TotalMilliseconds, Timeout.Infinite));
#endif
}
internal static void EndReadStream(this Stream stream)
{
#if NETFX_CORE || WINDOWS_PHONE
stream.Dispose();
#else
stream.Close();
#endif
}
internal static void EndWriteStream(this Stream stream)
{
#if NETFX_CORE || WINDOWS_PHONE
stream.Flush();
stream.Dispose();
#else
stream.Close();
#endif
}
internal static HttpWebRequest CreateHttpWebRequest(this AsyncServiceClient client, string requestUri)
{
#if SILVERLIGHT && !WINDOWS_PHONE && !NETFX_CORE
var creator = client.EmulateHttpViaPost
? System.Net.Browser.WebRequestCreator.BrowserHttp
: System.Net.Browser.WebRequestCreator.ClientHttp;
var webRequest = (HttpWebRequest) creator.Create(new Uri(requestUri));
if (client.StoreCookies && !client.EmulateHttpViaPost)
{
if (client.ShareCookiesWithBrowser)
{
if (client.CookieContainer == null)
client.CookieContainer = new CookieContainer();
client.CookieContainer.SetCookies(new Uri(requestUri), System.Windows.Browser.HtmlPage.Document.Cookies);
}
webRequest.CookieContainer = client.CookieContainer;
}
#else
var webRequest = (HttpWebRequest)WebRequest.Create(requestUri);
webRequest.MaximumResponseHeadersLength = int.MaxValue; //throws "The message length limit was exceeded" exception
client.CancelAsyncFn = webRequest.Abort;
if (client.StoreCookies)
{
webRequest.CookieContainer = client.CookieContainer;
}
#endif
#if !SILVERLIGHT
if (!client.DisableAutoCompression)
{
webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
}
#endif
return webRequest;
}
public static void SynchronizeCookies(this AsyncServiceClient client)
{
#if SILVERLIGHT && !WINDOWS_PHONE && !NETFX_CORE
if (client.StoreCookies && client.ShareCookiesWithBrowser && !client.EmulateHttpViaPost)
{
// browser cookies must be set on the ui thread
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => {
var cookieHeader = client.CookieContainer.GetCookieHeader(new Uri(client.BaseUri));
System.Windows.Browser.HtmlPage.Document.Cookies = cookieHeader;
});
}
#endif
}
public static bool IsWebException(this WebException webEx)
{
return webEx != null
#if !SILVERLIGHT
&& webEx.Status == WebExceptionStatus.ProtocolError
#endif
;
}
public static void ResetStream(this Stream stream)
{
#if !IOS
// MonoTouch throws NotSupportedException when setting System.Net.WebConnectionStream.Position
// Not sure if the stream is used later though, so may have to copy to MemoryStream and
// pass that around instead after this point?
stream.Position = 0;
#endif
}
}
#if NETFX_CORE
public class NetFxAsyncTimer : ITimer
{
public ThreadPoolTimer Timer;
public NetFxAsyncTimer(ThreadPoolTimer timer)
{
Timer = timer;
}
public void Cancel()
{
this.Timer.Cancel();
}
public void Dispose()
{
this.Timer.Dispose();
this.Timer = null;
}
}
#else
public class AsyncTimer : ITimer
{
public Timer Timer;
public AsyncTimer(Timer timer)
{
Timer = timer;
}
public void Cancel()
{
this.Timer.Change(Timeout.Infinite, Timeout.Infinite);
this.Dispose();
}
public void Dispose()
{
this.Timer.Dispose();
this.Timer = null;
}
}
#endif
#if !NET45
internal class TaskConstants<T>
{
internal static readonly Task<T> Canceled;
static TaskConstants()
{
var tcs = new TaskCompletionSource<T>();
tcs.SetCanceled();
Canceled = tcs.Task;
}
}
internal class TaskConstants
{
public static readonly Task Finished;
public static readonly Task Canceled;
static TaskConstants()
{
var tcs = new TaskCompletionSource<object>();
tcs.SetResult(null);
Finished = tcs.Task;
tcs = new TaskCompletionSource<object>();
tcs.SetCanceled();
Canceled = tcs.Task;
}
}
internal static class AsyncNet45StreamExtensions
{
public static Task FlushAsync(this Stream stream)
{
return stream.FlushAsync(CancellationToken.None);
}
public static Task FlushAsync(this Stream stream, CancellationToken token)
{
return token.IsCancellationRequested
? TaskConstants.Canceled
: Task.Factory.StartNew(l => ((Stream)l).Flush(), stream, token);
}
public static Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count)
{
return stream.ReadAsync(buffer, offset, count, CancellationToken.None);
}
public static Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count, CancellationToken token)
{
return token.IsCancellationRequested
? TaskConstants<int>.Canceled
: Task<int>.Factory.FromAsync(stream.BeginRead, stream.EndRead, buffer, offset, count, null);
}
public static Task WriteAsync(this Stream stream, byte[] buffer)
{
return stream.WriteAsync(buffer, 0, buffer.Length, CancellationToken.None);
}
public static Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count)
{
return stream.WriteAsync(buffer, offset, count, CancellationToken.None);
}
public static Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count, CancellationToken token)
{
return Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, buffer, offset, count, null);
}
}
#endif
}