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
169 lines (140 loc) · 4.77 KB
/
AsyncUtils.cs
File metadata and controls
169 lines (140 loc) · 4.77 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
// 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 SL5 || PCL
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)
{
return PclExportClient.Instance.CreateTimer(state.TimedOut, timeOut, state);
}
internal static void EndReadStream(this Stream stream)
{
#if NETFX_CORE || WP
stream.Dispose();
#else
stream.Close();
#endif
}
internal static void EndWriteStream(this Stream stream)
{
#if NETFX_CORE || WP
stream.Flush();
stream.Dispose();
#else
stream.Close();
#endif
}
internal static HttpWebRequest CreateHttpWebRequest(this AsyncServiceClient client, string requestUri)
{
var webRequest = PclExport.Instance.CreateWebRequest(requestUri,
emulateHttpViaPost:client.EmulateHttpViaPost);
PclExport.Instance.Config(webRequest);
client.CancelAsyncFn = webRequest.Abort;
if (client.StoreCookies)
{
PclExportClient.Instance.SetCookieContainer(webRequest, client);
}
if (!client.DisableAutoCompression)
{
PclExport.Instance.AddCompression(webRequest);
}
return webRequest;
}
public static bool IsWebException(this WebException webEx)
{
return webEx != null && webEx.Response != null
#if !(SL5 || PCL)
&& webEx.Status == WebExceptionStatus.ProtocolError
#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 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);
}
#if !PCL
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, int offset, int count, CancellationToken token)
{
return Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, buffer, offset, count, null);
}
#endif
}
#endif
}