-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use value tasks for async network stream (closes #4)
- Loading branch information
Showing
12 changed files
with
638 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,41 @@ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Console; | ||
|
||
namespace Codestellation.SolarWind.Tests | ||
{ | ||
public static class TestContext | ||
{ | ||
public static readonly ILoggerFactory LoggerFactory = | ||
new LoggerFactory(new ILoggerProvider[] {new ConsoleLoggerProvider((s, level) => true, false)}); | ||
public static readonly ILoggerFactory LoggerFactory = new ConsoleLoggerFactory(); | ||
|
||
public class ConsoleLoggerFactory : ILoggerFactory | ||
{ | ||
public void Dispose() => throw new NotImplementedException(); | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
=> ConsoleLogger.Instance; | ||
|
||
public void AddProvider(ILoggerProvider provider) => throw new NotImplementedException(); | ||
} | ||
|
||
public class ConsoleLogger : ILogger | ||
{ | ||
public static readonly ConsoleLogger Instance = new ConsoleLogger(); | ||
|
||
private ConsoleLogger() | ||
{ | ||
} | ||
|
||
public void Log<TState>( | ||
LogLevel logLevel, | ||
EventId eventId, | ||
TState state, | ||
Exception exception, | ||
Func<TState, Exception, string> formatter) | ||
=> Console.WriteLine(formatter(state, exception)); | ||
|
||
public bool IsEnabled(LogLevel logLevel) => true; | ||
|
||
public IDisposable BeginScope<TState>(TState state) => throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 26 additions & 15 deletions
41
src/SolarWind/Internals/CompletionSourceAsyncEventArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,38 @@ | ||
using System; | ||
using System.Net.Sockets; | ||
using System.Threading; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading.Tasks; | ||
using System.Threading.Tasks.Sources; | ||
using Codestellation.SolarWind.Threading; | ||
|
||
namespace Codestellation.SolarWind.Internals | ||
{ | ||
internal class CompletionSourceAsyncEventArgs : SocketAsyncEventArgs | ||
internal class CompletionSourceAsyncEventArgs : SocketAsyncEventArgs, IValueTaskSource | ||
{ | ||
private TaskCompletionSource<int> _source; | ||
private SyncValueTaskSourceCore _valueSource = new SyncValueTaskSourceCore(); | ||
|
||
public TaskCompletionSource<int> CompletionSource | ||
public ValueTask Task | ||
{ | ||
get | ||
{ | ||
if (_source != null) | ||
{ | ||
return _source; | ||
} | ||
// Here's possible multiple creation of TaskCompletionSource, but it's unlikely to happen; | ||
// However it allows thread-safe assigning without locking. | ||
Interlocked.CompareExchange(ref _source, new TaskCompletionSource<int>(), null); | ||
return _source; | ||
} | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => new ValueTask(this, _valueSource.Version); | ||
} | ||
|
||
public void Reset() | ||
=> _valueSource.Reset(); | ||
|
||
public void SetException(Exception exception) | ||
=> _valueSource.SetException(exception); | ||
|
||
public void SetResult() | ||
=> _valueSource.SetResult(); | ||
|
||
public ValueTaskSourceStatus GetStatus(short token) | ||
=> _valueSource.GetStatus(token); | ||
|
||
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) | ||
=> _valueSource.OnCompleted(continuation, state, token, flags); | ||
|
||
public void GetResult(short token) | ||
=> _valueSource.GetResult(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#if NETSTANDARD2_0 | ||
using Microsoft.Extensions.ObjectPool; | ||
|
||
namespace Codestellation.SolarWind.Internals | ||
{ | ||
internal class SocketEventArgsPool : DefaultObjectPool<CompletionSourceAsyncEventArgs> | ||
{ | ||
public static readonly SocketEventArgsPool Instance = new SocketEventArgsPool(); | ||
|
||
private class Policy : IPooledObjectPolicy<CompletionSourceAsyncEventArgs> | ||
{ | ||
public CompletionSourceAsyncEventArgs Create() | ||
{ | ||
var result = new CompletionSourceAsyncEventArgs(); | ||
result.Completed += AsyncNetworkStream.HandleAsyncResult; | ||
return result; | ||
} | ||
|
||
public bool Return(CompletionSourceAsyncEventArgs obj) => true; | ||
} | ||
|
||
public SocketEventArgsPool() | ||
: base(new Policy()) | ||
{ | ||
} | ||
|
||
public SocketEventArgsPool(int maximumRetained) | ||
: base(new Policy(), maximumRetained) | ||
{ | ||
} | ||
|
||
public override CompletionSourceAsyncEventArgs Get() | ||
{ | ||
CompletionSourceAsyncEventArgs result = base.Get(); | ||
result.Reset(); | ||
return result; | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.