From f8cc21e7db6416afabb670727017e851e7c110bd Mon Sep 17 00:00:00 2001 From: Alexandru Macocian Date: Sun, 2 Jan 2022 17:14:01 +0200 Subject: [PATCH 1/2] Introduce cancellation tokens for server Update pipelines to .net6 Deprecate netcoreapp2.1 build --- .github/workflows/cd.yaml | 2 +- .github/workflows/ci.yaml | 2 +- MTSC.UnitTests/ServerTests.cs | 19 +++++++++++++++++-- MTSC/MTSC.csproj | 8 ++++---- MTSC/ServerSide/Server.cs | 26 +++++++++++++++++++++++--- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml index 111e8d7..5d998f5 100644 --- a/.github/workflows/cd.yaml +++ b/.github/workflows/cd.yaml @@ -31,7 +31,7 @@ jobs: - name: Install .NET Core uses: actions/setup-dotnet@v1 with: - dotnet-version: '5.0.202' + dotnet-version: '6.0.x' - name: Setup MSBuild.exe uses: microsoft/setup-msbuild@v1.0.1 diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5c6b2ac..c8aeef5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -33,7 +33,7 @@ jobs: - name: Install .NET Core uses: actions/setup-dotnet@v1 with: - dotnet-version: '5.0.202' + dotnet-version: '6.0.x' - name: Setup MSBuild.exe uses: microsoft/setup-msbuild@v1.0.1 diff --git a/MTSC.UnitTests/ServerTests.cs b/MTSC.UnitTests/ServerTests.cs index ec74f52..1d35ef8 100644 --- a/MTSC.UnitTests/ServerTests.cs +++ b/MTSC.UnitTests/ServerTests.cs @@ -1,4 +1,5 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MTSC.ServerSide; using System.Threading; @@ -11,7 +12,7 @@ namespace MTSC.UnitTests [TestMethod] public void Stop() { - var server = new Server(); + var server = new Server(256); server.RunAsync(); Thread.Sleep(1000); Assert.IsTrue(server.Running); @@ -19,5 +20,19 @@ namespace MTSC.UnitTests Thread.Sleep(1000); Assert.IsFalse(server.Running); } + + [TestMethod] + public void CancellationToken_StopsServer() + { + var server = new Server(256); + var cts = new CancellationTokenSource(); + var runningTask = server.RunAsync(cts.Token); + Thread.Sleep(1000); + server.Running.Should().BeTrue(); + + cts.Cancel(); + runningTask.Wait(1000); + runningTask.IsCompleted.Should().BeTrue(); + } } } diff --git a/MTSC/MTSC.csproj b/MTSC/MTSC.csproj index 2c994c7..e8ec1bb 100644 --- a/MTSC/MTSC.csproj +++ b/MTSC/MTSC.csproj @@ -2,10 +2,10 @@ Library - netcoreapp2.1;net48;netstandard2.0;netcoreapp3.1;net5.0 + net48;netstandard2.0;netcoreapp3.1;net5.0;net6.0 - 4.6.0 + 4.7.0 latest Alexandru-Victor Macocian MTSC @@ -27,9 +27,9 @@ - + - + diff --git a/MTSC/ServerSide/Server.cs b/MTSC/ServerSide/Server.cs index b074656..08d1aaf 100644 --- a/MTSC/ServerSide/Server.cs +++ b/MTSC/ServerSide/Server.cs @@ -14,6 +14,7 @@ using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace MTSC.ServerSide @@ -24,6 +25,7 @@ namespace MTSC.ServerSide public sealed class Server { #region Fields + private CancellationToken cancellationToken; private bool running; private X509Certificate2 certificate; private readonly ProducerConsumerQueue addQueue = new(); @@ -491,8 +493,14 @@ namespace MTSC.ServerSide /// /// Blocking method. Runs the server on the current thread. /// - public void Run() + /// Cancellation token used to cancel the server. + public void Run(CancellationToken cancellationToken = default) { + if (this.running) + { + return; + } + this.Listener?.Stop(); if (this.logger is null) { @@ -515,6 +523,7 @@ namespace MTSC.ServerSide this.Listener.Initialize(this.Port, this.IPAddress); this.Listener.Start(); + this.cancellationToken = cancellationToken; this.running = true; this.Log("Server started on: " + this.Listener.LocalEndpoint.ToString()); foreach(var toBeRunOnStartup in this.handlers.OfType()) @@ -526,6 +535,17 @@ namespace MTSC.ServerSide while (this.running) { startLoopTime = DateTime.Now; + + /* + * Check cancellationToken. If cancellation has been requested, stop the loop + * and let the cleanup process deal with the server resources. + */ + if (this.cancellationToken.IsCancellationRequested) + { + this.running = false; + break; + } + /* * Check the client states. If a client is disconnected, * remove it from the list of clients. @@ -636,9 +656,9 @@ namespace MTSC.ServerSide /// /// Runs the server async. /// - public Task RunAsync() + public Task RunAsync(CancellationToken cancellationToken = default) { - return Task.Run(this.Run); + return Task.Run(() => this.Run(cancellationToken)); } /// /// Stop the server. From 0392441b5a6d6e6cf6a8e6f24d8d05c880887ba9 Mon Sep 17 00:00:00 2001 From: Alexandru Macocian Date: Sun, 2 Jan 2022 17:21:29 +0200 Subject: [PATCH 2/2] Upgrade test project to .net 6 --- MTSC.UnitTests/MTSC.UnitTests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MTSC.UnitTests/MTSC.UnitTests.csproj b/MTSC.UnitTests/MTSC.UnitTests.csproj index aa3faaf..1ae2171 100644 --- a/MTSC.UnitTests/MTSC.UnitTests.csproj +++ b/MTSC.UnitTests/MTSC.UnitTests.csproj @@ -1,7 +1,7 @@  - net5.0 + net6.0 false