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
123 lines (100 loc) · 3.66 KB
/
AsyncUtils.cs
File metadata and controls
123 lines (100 loc) · 3.66 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
// Copyright (c) ServiceStack, Inc. 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;
namespace ServiceStack
{
public interface ITimer : IDisposable
{
void Cancel();
}
public delegate void ProgressDelegate(long done, long total);
internal static class AsyncUtils
{
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;
}
}
#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, TaskCreationOptions.None, TaskScheduler.Default);
}
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 || NETSTANDARD1_1 || NETSTANDARD2_0)
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, result => stream.CanRead ? stream.EndRead(result) : 0, 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
}