Created
January 21, 2018 23:00
-
-
Save ProximaB/11f1e5ebeafd3166e237108104ded15b to your computer and use it in GitHub Desktop.
Canselation Token
This file contains hidden or 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
| public void Stop() | |
| { | |
| this.Status = ServerStatus.Stopping; | |
| this.listener.Stop(); | |
| this.cancellationTokenSource.Cancel(); | |
| this.Status = ServerStatus.Stopped; | |
| } | |
| private async void ListenForConnections(CancellationToken cancellationToken) | |
| { | |
| try | |
| { | |
| while (this.Status == ServerStatus.Running) | |
| { | |
| var socketTask = this.listener.AcceptSocketAsync(); | |
| var tcs = new TaskCompletionSource<bool>(); | |
| using (cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs)) | |
| { | |
| if (socketTask != await Task.WhenAny(socketTask, tcs.Task).ConfigureAwait(false)) | |
| break; | |
| } | |
| var context = new TcpContext(socketTask.Result); | |
| this.OnConnectionReceived(context); | |
| } | |
| } | |
| catch (ObjectDisposedException) | |
| { | |
| // Closed | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment