-
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.
- Loading branch information
1 parent
734395b
commit 72b4728
Showing
7 changed files
with
275 additions
and
44 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace VaettirNet.PixelsDice.Net; | ||
|
||
internal sealed class AsyncAutoResetEvent | ||
{ | ||
private readonly Queue<TaskCompletionSource> _queue = new(); | ||
|
||
private bool _signaled; | ||
|
||
public AsyncAutoResetEvent(bool signaled) | ||
{ | ||
_signaled = signaled; | ||
} | ||
|
||
public Task WaitAsync(CancellationToken cancellationToken = default) | ||
{ | ||
lock (_queue) | ||
{ | ||
if (_signaled) | ||
{ | ||
_signaled = false; | ||
return Task.CompletedTask; | ||
} | ||
else | ||
{ | ||
var tcs = new TaskCompletionSource(); | ||
if (cancellationToken.CanBeCanceled) | ||
{ | ||
// If the token is cancelled, cancel the waiter. | ||
var registration = cancellationToken.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: false); | ||
|
||
// If the waiter completes or faults, unregister our interest in cancellation. | ||
tcs.Task.ContinueWith( | ||
_ => registration.Unregister(), | ||
cancellationToken, | ||
TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.NotOnFaulted, | ||
TaskScheduler.Default); | ||
} | ||
_queue.Enqueue(tcs); | ||
return tcs.Task; | ||
} | ||
} | ||
} | ||
|
||
public void Set() | ||
{ | ||
TaskCompletionSource toRelease = null; | ||
|
||
lock (_queue) | ||
{ | ||
if (_queue.Count > 0) | ||
{ | ||
toRelease = _queue.Dequeue(); | ||
} | ||
else if (!_signaled) | ||
{ | ||
_signaled = true; | ||
} | ||
} | ||
|
||
// It's possible that the TCS has already been cancelled. | ||
toRelease?.TrySetResult(); | ||
} | ||
} |
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,8 @@ | ||
namespace VaettirNet.PixelsDice.Net; | ||
|
||
public enum ConnectionState | ||
{ | ||
Disconnected, | ||
Connected, | ||
Reconnecting, | ||
} |
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
Oops, something went wrong.