]> git.deb.at Git - rhonda/impostor.git/commitdiff
Feature/gamemanager improvement (#65)
authorJonas Kamsker <11245306+JKamsker@users.noreply.github.com>
Wed, 21 Oct 2020 22:02:27 +0000 (00:02 +0200)
committerGitHub <noreply@github.com>
Wed, 21 Oct 2020 22:02:27 +0000 (00:02 +0200)
* Ignoring .Vs folder

* Improved Gamemanager
- Retry code creation
- Async Node Lookup

* Switched back to old using order

* Removed IAsyncNodeLocator

* Found one more use of find

* deleted submodule

Co-authored-by: Weirdo <git.jonas@kamsker.at>
.gitignore
src/Impostor.Server/Extensions/NodeLocatorExtensions.cs [new file with mode: 0644]
src/Impostor.Server/Net/Manager/GameManager.cs
src/Impostor.Server/Net/Redirector/ClientRedirector.cs
src/Impostor.Server/Net/Redirector/INodeLocator.cs
src/Impostor.Server/Net/Redirector/NodeLocatorNoOp.cs
src/Impostor.Server/Net/Redirector/NodeLocatorRedis.cs
src/Impostor.Server/Net/Redirector/NodeLocatorUDP.cs

index e5150c9b83034018fb696206f97e2fa2190572c0..95e2d57dc6b8d0aaa922c15ba8944e428989033f 100644 (file)
@@ -1,2 +1,4 @@
 /.vscode
-/build
\ No newline at end of file
+/build
+/.vs
+/src/Impostor.Plugins.Debugger/Properties/launchSettings.json
diff --git a/src/Impostor.Server/Extensions/NodeLocatorExtensions.cs b/src/Impostor.Server/Extensions/NodeLocatorExtensions.cs
new file mode 100644 (file)
index 0000000..671017d
--- /dev/null
@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Text;
+using System.Threading.Tasks;
+
+using Impostor.Server.Net.Redirector;
+
+namespace Impostor.Server.Extensions
+{
+    public static class NodeLocatorExtensions
+    {
+        public static async ValueTask<bool> ExistsAsync(this INodeLocator nodeLocator, string gameCode)
+        {
+            return await nodeLocator.FindAsync(gameCode) != null;
+        }
+    }
+}
\ No newline at end of file
index c71a56d88fe77571139edb5808ff98f965d13ba3..ccb5d3766044ca52ff61a8e00790134a5113ec57 100644 (file)
@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Net;
 using System.Threading.Tasks;
+
 using Impostor.Api;
 using Impostor.Api.Events;
 using Impostor.Api.Events.Managers;
@@ -11,8 +12,10 @@ using Impostor.Api.Games;
 using Impostor.Api.Innersloth;
 using Impostor.Api.Innersloth.Data;
 using Impostor.Server.Data;
+using Impostor.Server.Extensions;
 using Impostor.Server.Net.Redirector;
 using Impostor.Server.Net.State;
+
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Logging;
 using Microsoft.Extensions.Options;
@@ -41,21 +44,38 @@ namespace Impostor.Server.Net.Manager
         public async ValueTask<Game> CreateAsync(GameOptionsData options)
         {
             // TODO: Prevent duplicates when using server redirector using INodeProvider.
+            var (success, game) = await TryCreateAsync(options);
+
+            for (int i = 0; i < 10 && !success; i++)
+            {
+                (success, game) = await TryCreateAsync(options);
+            }
+
+            if (!success)
+            {
+                throw new ImpostorException("Could not create new game"); // TODO: Fix generic exception.
+            }
+
+            return game;
+        }
+
+        public async ValueTask<(bool success, Game game)> TryCreateAsync(GameOptionsData options)
+        {
             var gameCode = GameCode.Create();
             var gameCodeStr = gameCode.Code;
             var game = ActivatorUtilities.CreateInstance<Game>(_serviceProvider, _publicIp, gameCode, options);
 
-            if (_nodeLocator.Find(gameCodeStr) != null || !_games.TryAdd(gameCode, game))
+            if (await _nodeLocator.ExistsAsync(gameCodeStr) || !_games.TryAdd(gameCode, game))
             {
-                throw new ImpostorException("Could not create new game"); // TODO: Fix generic exception.
+                return (false, null);
             }
 
-            _nodeLocator.Save(gameCodeStr, _publicIp);
+            await _nodeLocator.SaveAsync(gameCodeStr, _publicIp);
             _logger.LogDebug("Created game with code {0}.", game.Code);
 
             await _eventManager.CallAsync(new GameCreatedEvent(game));
 
-            return game;
+            return (true, game);
         }
 
         public Game Find(GameCode code)
@@ -119,7 +139,7 @@ namespace Impostor.Server.Net.Manager
             }
 
             _logger.LogDebug("Remove game with code {0} ({1}).", GameCodeParser.IntToGameName(gameCode), gameCode);
-            _nodeLocator.Remove(GameCodeParser.IntToGameName(gameCode));
+            await _nodeLocator.RemoveAsync(GameCodeParser.IntToGameName(gameCode));
 
             await _eventManager.CallAsync(new GameDestroyedEvent(game));
         }
index 418e79c44d4cbb5795852399def8fe2a299de99b..0c6d4680a68a283a5a8ed8aa3a9e18370f2c935d 100644 (file)
@@ -58,7 +58,7 @@ namespace Impostor.Server.Net.Redirector
                         out _);
 
                     using var packet = MessageWriter.Get(MessageType.Reliable);
-                    var endpoint = _nodeLocator.Find(GameCodeParser.IntToGameName(gameCode));
+                    var endpoint = await _nodeLocator.FindAsync(GameCodeParser.IntToGameName(gameCode));
                     if (endpoint == null)
                     {
                         Message01JoinGameS2C.SerializeError(packet, false, DisconnectReason.GameMissing);
index 2dd2f07848da3f1e233906384830a504dcba97ef..12563b9a2949a2c1bd9daeb1f2f9217d91200543 100644 (file)
@@ -1,13 +1,14 @@
 using System.Net;
+using System.Threading.Tasks;
 
 namespace Impostor.Server.Net.Redirector
 {
     public interface INodeLocator
     {
-        IPEndPoint Find(string gameCode);
+        ValueTask<IPEndPoint> FindAsync(string gameCode);
 
-        void Save(string gameCode, IPEndPoint endPoint);
+        ValueTask SaveAsync(string gameCode, IPEndPoint endPoint);
 
-        void Remove(string gameCode);
+        ValueTask RemoveAsync(string gameCode);
     }
 }
\ No newline at end of file
index 21811c82d98190f123159603fb795ebab74c9f1a..fd4cd56f7b309ea71251e577f5c16d51c907ee2b 100644 (file)
@@ -1,23 +1,14 @@
 using System.Net;
+using System.Threading.Tasks;
 
 namespace Impostor.Server.Net.Redirector
 {
     public class NodeLocatorNoOp : INodeLocator
     {
-        public IPEndPoint Find(string gameCode)
-        {
-            // Do nothing.
-            return null;
-        }
+        public ValueTask<IPEndPoint> FindAsync(string gameCode) => ValueTask.FromResult(default(IPEndPoint));
 
-        public void Save(string gameCode, IPEndPoint endPoint)
-        {
-            // Do nothing.
-        }
+        public ValueTask SaveAsync(string gameCode, IPEndPoint endPoint) => ValueTask.CompletedTask;
 
-        public void Remove(string gameCode)
-        {
-            // Do nothing.
-        }
+        public ValueTask RemoveAsync(string gameCode) => ValueTask.CompletedTask;
     }
 }
\ No newline at end of file
index 36632a7106a347bde492b319fd4e7063cfceca09..ec036660fad66aaa8cca0a1fa4e3c2c7586517c1 100644 (file)
@@ -1,5 +1,7 @@
 using System;
 using System.Net;
+using System.Threading.Tasks;
+
 using Microsoft.Extensions.Caching.Distributed;
 using Microsoft.Extensions.Logging;
 
@@ -15,9 +17,9 @@ namespace Impostor.Server.Net.Redirector
             _cache = cache;
         }
 
-        public IPEndPoint Find(string gameCode)
+        public async ValueTask<IPEndPoint> FindAsync(string gameCode)
         {
-            var entry = _cache.GetString(gameCode);
+            var entry = await _cache.GetStringAsync(gameCode);
             if (entry == null)
             {
                 return null;
@@ -26,17 +28,17 @@ namespace Impostor.Server.Net.Redirector
             return IPEndPoint.Parse(entry);
         }
 
-        public void Save(string gameCode, IPEndPoint endPoint)
+        public async ValueTask SaveAsync(string gameCode, IPEndPoint endPoint)
         {
-            _cache.SetString(gameCode, endPoint.ToString(), new DistributedCacheEntryOptions
+            await _cache.SetStringAsync(gameCode, endPoint.ToString(), new DistributedCacheEntryOptions
             {
                 SlidingExpiration = TimeSpan.FromHours(1),
             });
         }
 
-        public void Remove(string gameCode)
+        public async ValueTask RemoveAsync(string gameCode)
         {
-            _cache.Remove(gameCode);
+            await _cache.RemoveAsync(gameCode);
         }
     }
 }
\ No newline at end of file
index b1efb99e7d0369fc73af8a15ad52b1da2eeea7fc..ed19b06e22cf23e5f960d6d58696f7b19ca72cfc 100644 (file)
@@ -3,6 +3,7 @@ using System.Collections.Concurrent;
 using System.Net;
 using System.Net.Sockets;
 using System.Text;
+using System.Threading.Tasks;
 using Impostor.Server.Data;
 using Microsoft.Extensions.Logging;
 using Microsoft.Extensions.Options;
@@ -72,11 +73,11 @@ namespace Impostor.Server.Net.Redirector
             }
         }
 
-        public IPEndPoint Find(string gameCode)
+        public ValueTask<IPEndPoint> FindAsync(string gameCode)
         {
             if (!_isMaster)
             {
-                return null;
+                return ValueTask.FromResult(default(IPEndPoint));
             }
 
             if (_availableNodes.TryGetValue(gameCode, out var node))
@@ -84,29 +85,31 @@ namespace Impostor.Server.Net.Redirector
                 if (node.Expired)
                 {
                     _availableNodes.TryRemove(gameCode, out _);
-                    return null;
+                    return ValueTask.FromResult(default(IPEndPoint));
                 }
 
-                return node.Endpoint;
+                return ValueTask.FromResult(node.Endpoint);
             }
 
-            return null;
+            return ValueTask.FromResult(default(IPEndPoint));
         }
 
-        public void Remove(string gameCode)
+        public ValueTask RemoveAsync(string gameCode)
         {
             if (!_isMaster)
             {
-                return;
+                return ValueTask.CompletedTask;
             }
 
             _availableNodes.TryRemove(gameCode, out _);
+            return ValueTask.CompletedTask;
         }
 
-        public void Save(string gameCode, IPEndPoint endPoint)
+        public ValueTask SaveAsync(string gameCode, IPEndPoint endPoint)
         {
             var data = Encoding.UTF8.GetBytes($"{gameCode},{endPoint}");
             _client.Send(data, data.Length, _server);
+            return ValueTask.CompletedTask;
         }
 
         public void Dispose()