From 053d5c93835273a189ab0dcbea2e2d270ec6fd71 Mon Sep 17 00:00:00 2001 From: Edmand46 Date: Sat, 14 May 2022 10:35:17 +0400 Subject: [PATCH] chore: update naming game -> simple server --- Ragon/Sources/{ => Server}/ENetServer.cs | 2 +- Ragon/Sources/Server/WebsocketServer.cs | 60 +++++++++++++++++++ Ragon/Sources/Storage/Storage.cs | 7 +-- Ragon/Sources/WebsocketServer.cs | 6 -- {Game => SimpleServer}/NLog.config | 0 {Game => SimpleServer}/Program.cs | 7 +-- .../SimpleServer.csproj | 8 +++ .../Source/AuthorizerByKey.cs | 8 +-- .../Source/Events/SimpleEvent.cs | 8 +-- SimpleServer/Source/Plugins/SimplePlugin.cs | 31 ++++++++++ .../Source/SimplePluginFactory.cs | 11 ++-- 11 files changed, 121 insertions(+), 27 deletions(-) rename Ragon/Sources/{ => Server}/ENetServer.cs (98%) create mode 100644 Ragon/Sources/Server/WebsocketServer.cs delete mode 100644 Ragon/Sources/WebsocketServer.cs rename {Game => SimpleServer}/NLog.config (100%) rename {Game => SimpleServer}/Program.cs (68%) rename Game/Game.csproj => SimpleServer/SimpleServer.csproj (72%) rename Game/Source/GameAuthorizer.cs => SimpleServer/Source/AuthorizerByKey.cs (56%) rename Game/Source/Events/TestEvent.cs => SimpleServer/Source/Events/SimpleEvent.cs (58%) create mode 100755 SimpleServer/Source/Plugins/SimplePlugin.cs rename Game/Source/GameFactory.cs => SimpleServer/Source/SimplePluginFactory.cs (50%) diff --git a/Ragon/Sources/ENetServer.cs b/Ragon/Sources/Server/ENetServer.cs similarity index 98% rename from Ragon/Sources/ENetServer.cs rename to Ragon/Sources/Server/ENetServer.cs index 097cd09..d195ba2 100755 --- a/Ragon/Sources/ENetServer.cs +++ b/Ragon/Sources/Server/ENetServer.cs @@ -54,7 +54,7 @@ namespace Ragon.Core _thread = new Thread(Execute); _thread.Name = "NetworkThread"; _thread.Start(); - _logger.Info($"Socket Server Started at port {port}"); + _logger.Info($"ENet Server Started at port {port}"); } private void Execute() diff --git a/Ragon/Sources/Server/WebsocketServer.cs b/Ragon/Sources/Server/WebsocketServer.cs new file mode 100644 index 0000000..99915a5 --- /dev/null +++ b/Ragon/Sources/Server/WebsocketServer.cs @@ -0,0 +1,60 @@ +using System; +using System.Net; +using System.Net.WebSockets; +using System.Threading; +using DisruptorUnity3d; +using NLog; + +namespace Ragon.Core; + +public class WebsocketServer : IDisposable +{ + private HttpListener _httpListener; + private ILogger _logger = LogManager.GetCurrentClassLogger(); + private Thread _thread; + private ENet.Event _netEvent; + + private RingBuffer _receiveBuffer; + private RingBuffer _sendBuffer; + + public void WriteEvent(Event evnt) => _sendBuffer.Enqueue(evnt); + public bool ReadEvent(out Event evnt) => _receiveBuffer.TryDequeue(out evnt); + + public void Start(ushort port) + { + // _httpListener = new HttpListener(); + // _httpListener.Prefixes.Add("http://localhost/"); + // _httpListener.Start(); + // + // _thread = new Thread(Execute); + // _thread.Name = "NetworkThread"; + // _thread.Start(); + // _logger.Info($"Socket Server Started at port {port}"); + } + + public void Execute() + { + + } + + public async void ExecuteAsync() + { + // while (true) + // { + // HttpListenerContext context = await _httpListener.GetContextAsync(); + // if (context.Request.IsWebSocketRequest) + // { + // HttpListenerWebSocketContext webSocketContext = await context.AcceptWebSocketAsync(null); + // WebSocket webSocket = webSocketContext.WebSocket; + // while (webSocket.State == WebSocketState.Open) + // { + // await webSocket.SendAsync(... ); + // } + // } + // } + } + + public void Dispose() + { + } +} \ No newline at end of file diff --git a/Ragon/Sources/Storage/Storage.cs b/Ragon/Sources/Storage/Storage.cs index 939b344..012c962 100644 --- a/Ragon/Sources/Storage/Storage.cs +++ b/Ragon/Sources/Storage/Storage.cs @@ -1,19 +1,18 @@ -using StackExchange.Redis; namespace Ragon.Core.Storage; public class Storage { - private ConnectionMultiplexer _connection; + // private ConnectionMultiplexer _connection; public Storage(Configuration _configuration) { - _connection = ConnectionMultiplexer.Connect(_configuration.ApiKey); + // _connection = ConnectionMultiplexer.Connect(_configuration.Key); } public void UpdateEntity(int entityId) { - var db = _connection.GetDatabase(); + // var db = _connection.GetDatabase(); // db.set("entity_", ) } diff --git a/Ragon/Sources/WebsocketServer.cs b/Ragon/Sources/WebsocketServer.cs deleted file mode 100644 index 1fb1f50..0000000 --- a/Ragon/Sources/WebsocketServer.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Ragon.Core; - -public class WebsocketServer -{ - -} \ No newline at end of file diff --git a/Game/NLog.config b/SimpleServer/NLog.config similarity index 100% rename from Game/NLog.config rename to SimpleServer/NLog.config diff --git a/Game/Program.cs b/SimpleServer/Program.cs similarity index 68% rename from Game/Program.cs rename to SimpleServer/Program.cs index 04fae28..aebd55e 100755 --- a/Game/Program.cs +++ b/SimpleServer/Program.cs @@ -1,17 +1,16 @@ using System; using Game.Source; -using NetStack.Serialization; using Ragon.Core; -namespace Game +namespace SimpleServer { class Program { static void Main(string[] args) { var bootstrap = new Bootstrap(); - bootstrap.Configure(new GameFactory()); - + bootstrap.Configure(new SimplePluginFactory()); + Console.Read(); } } diff --git a/Game/Game.csproj b/SimpleServer/SimpleServer.csproj similarity index 72% rename from Game/Game.csproj rename to SimpleServer/SimpleServer.csproj index daa608d..cd51211 100755 --- a/Game/Game.csproj +++ b/SimpleServer/SimpleServer.csproj @@ -6,6 +6,10 @@ Game + + true + + Always @@ -19,4 +23,8 @@ + + + + diff --git a/Game/Source/GameAuthorizer.cs b/SimpleServer/Source/AuthorizerByKey.cs similarity index 56% rename from Game/Source/GameAuthorizer.cs rename to SimpleServer/Source/AuthorizerByKey.cs index cbac446..eacb357 100644 --- a/Game/Source/GameAuthorizer.cs +++ b/SimpleServer/Source/AuthorizerByKey.cs @@ -4,17 +4,17 @@ using Ragon.Core; namespace Game.Source; -public class GameAuthorizer: AuthorizationManager +public class AuthorizerByKey: AuthorizationManager { private Configuration _configuration; - public GameAuthorizer(Configuration configuration) + public AuthorizerByKey(Configuration configuration) { _configuration = configuration; } public override bool OnAuthorize(uint peerId, ref ReadOnlySpan payload) { - var apiKey = Encoding.UTF8.GetString(payload); - return _configuration.ApiKey == apiKey; + var key = Encoding.UTF8.GetString(payload); + return _configuration.Key == key; } } \ No newline at end of file diff --git a/Game/Source/Events/TestEvent.cs b/SimpleServer/Source/Events/SimpleEvent.cs similarity index 58% rename from Game/Source/Events/TestEvent.cs rename to SimpleServer/Source/Events/SimpleEvent.cs index 1c8040d..32402a7 100644 --- a/Game/Source/Events/TestEvent.cs +++ b/SimpleServer/Source/Events/SimpleEvent.cs @@ -3,17 +3,17 @@ using Ragon.Common; namespace Game.Source.Events; -public class TestEvent: IRagonSerializable +public class SimpleEvent: IRagonSerializable { - public string TestData; + public string Name; public void Serialize(BitBuffer buffer) { - buffer.AddString(TestData); + buffer.AddString(Name); } public void Deserialize(BitBuffer buffer) { - TestData = buffer.ReadString(); + Name = buffer.ReadString(); } } \ No newline at end of file diff --git a/SimpleServer/Source/Plugins/SimplePlugin.cs b/SimpleServer/Source/Plugins/SimplePlugin.cs new file mode 100755 index 0000000..caabe32 --- /dev/null +++ b/SimpleServer/Source/Plugins/SimplePlugin.cs @@ -0,0 +1,31 @@ +using System.Runtime.InteropServices; +using Game.Source.Events; +using NLog; +using Ragon.Common; +using Ragon.Core; + +namespace Game.Source +{ + public class SimplePlugin: PluginBase + { + public override void OnStart() + { + _logger.Info("Plugin started"); + } + + public override void OnStop() + { + _logger.Info("Plugin stopped"); + } + + public override void OnPlayerJoined(Player player) + { + _logger.Info("Player joined " + player.PlayerName); + } + + public override void OnPlayerLeaved(Player player) + { + _logger.Info("Player leaved " + player.PlayerName); + } + } +} \ No newline at end of file diff --git a/Game/Source/GameFactory.cs b/SimpleServer/Source/SimplePluginFactory.cs similarity index 50% rename from Game/Source/GameFactory.cs rename to SimpleServer/Source/SimplePluginFactory.cs index 02d4111..ed1281f 100644 --- a/Game/Source/GameFactory.cs +++ b/SimpleServer/Source/SimplePluginFactory.cs @@ -3,15 +3,18 @@ using Ragon.Core; namespace Game.Source { - public class GameFactory : PluginFactory + public class SimplePluginFactory : PluginFactory { - public string PluginName { get; set; } = "ExamplePlugin"; + public string PluginName { get; set; } = "SimplePlugin"; public PluginBase CreatePlugin(string map) { - return new ExamplePlugin(); + return new SimplePlugin(); } - public AuthorizationManager CreateManager(Configuration configuration) => new GameAuthorizer(configuration); + public AuthorizationManager CreateManager(Configuration configuration) + { + return new AuthorizerByKey(configuration); + } } } \ No newline at end of file