1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-15 10:22:57 +00:00

Socket client update, added headers to rest result

This commit is contained in:
Jan Korf
2019-04-30 11:58:39 +02:00
parent c489b4e9aa
commit 9008c4ed2c
16 changed files with 446 additions and 321 deletions
+44
View File
@@ -4,6 +4,8 @@ using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
namespace CryptoExchange.Net
{
@@ -83,5 +85,47 @@ namespace CryptoExchange.Net
return result;
}
}
public static IEnumerable<Tuple<string, string>> ToIEnumerable(this WebHeaderCollection headers)
{
if (headers == null)
return null;
return Enumerable
.Range(0, headers.Count)
.SelectMany(i => headers.GetValues(i)
.Select(v => Tuple.Create(headers.GetKey(i), v))
);
}
public static async Task<bool> WaitOneAsync(this WaitHandle handle, int millisecondsTimeout, CancellationToken cancellationToken)
{
RegisteredWaitHandle registeredHandle = null;
CancellationTokenRegistration tokenRegistration = default(CancellationTokenRegistration);
try
{
var tcs = new TaskCompletionSource<bool>();
registeredHandle = ThreadPool.RegisterWaitForSingleObject(
handle,
(state, timedOut) => ((TaskCompletionSource<bool>)state).TrySetResult(!timedOut),
tcs,
millisecondsTimeout,
true);
tokenRegistration = cancellationToken.Register(
state => ((TaskCompletionSource<bool>)state).TrySetCanceled(),
tcs);
return await tcs.Task;
}
finally
{
registeredHandle?.Unregister(null);
tokenRegistration.Dispose();
}
}
public static Task<bool> WaitOneAsync(this WaitHandle handle, TimeSpan timeout)
{
return handle.WaitOneAsync((int)timeout.TotalMilliseconds, CancellationToken.None);
}
}
}