chore: update naming game -> simple server

This commit is contained in:
2022-05-14 10:35:17 +04:00
parent 185b5b7ca1
commit 053d5c9383
11 changed files with 121 additions and 27 deletions
@@ -54,7 +54,7 @@ namespace Ragon.Core
_thread = new Thread(Execute); _thread = new Thread(Execute);
_thread.Name = "NetworkThread"; _thread.Name = "NetworkThread";
_thread.Start(); _thread.Start();
_logger.Info($"Socket Server Started at port {port}"); _logger.Info($"ENet Server Started at port {port}");
} }
private void Execute() private void Execute()
+60
View File
@@ -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<Event> _receiveBuffer;
private RingBuffer<Event> _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()
{
}
}
+3 -4
View File
@@ -1,19 +1,18 @@
using StackExchange.Redis;
namespace Ragon.Core.Storage; namespace Ragon.Core.Storage;
public class Storage public class Storage
{ {
private ConnectionMultiplexer _connection; // private ConnectionMultiplexer _connection;
public Storage(Configuration _configuration) public Storage(Configuration _configuration)
{ {
_connection = ConnectionMultiplexer.Connect(_configuration.ApiKey); // _connection = ConnectionMultiplexer.Connect(_configuration.Key);
} }
public void UpdateEntity(int entityId) public void UpdateEntity(int entityId)
{ {
var db = _connection.GetDatabase(); // var db = _connection.GetDatabase();
// db.set("entity_", ) // db.set("entity_", )
} }
-6
View File
@@ -1,6 +0,0 @@
namespace Ragon.Core;
public class WebsocketServer
{
}
+2 -3
View File
@@ -1,16 +1,15 @@
using System; using System;
using Game.Source; using Game.Source;
using NetStack.Serialization;
using Ragon.Core; using Ragon.Core;
namespace Game namespace SimpleServer
{ {
class Program class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
var bootstrap = new Bootstrap(); var bootstrap = new Bootstrap();
bootstrap.Configure(new GameFactory()); bootstrap.Configure(new SimplePluginFactory());
Console.Read(); Console.Read();
} }
@@ -6,6 +6,10 @@
<RootNamespace>Game</RootNamespace> <RootNamespace>Game</RootNamespace>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<None Update="NLog.config"> <None Update="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
@@ -19,4 +23,8 @@
<ProjectReference Include="..\Ragon\Ragon.csproj" /> <ProjectReference Include="..\Ragon\Ragon.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Source\Managers" />
</ItemGroup>
</Project> </Project>
@@ -4,17 +4,17 @@ using Ragon.Core;
namespace Game.Source; namespace Game.Source;
public class GameAuthorizer: AuthorizationManager public class AuthorizerByKey: AuthorizationManager
{ {
private Configuration _configuration; private Configuration _configuration;
public GameAuthorizer(Configuration configuration) public AuthorizerByKey(Configuration configuration)
{ {
_configuration = configuration; _configuration = configuration;
} }
public override bool OnAuthorize(uint peerId, ref ReadOnlySpan<byte> payload) public override bool OnAuthorize(uint peerId, ref ReadOnlySpan<byte> payload)
{ {
var apiKey = Encoding.UTF8.GetString(payload); var key = Encoding.UTF8.GetString(payload);
return _configuration.ApiKey == apiKey; return _configuration.Key == key;
} }
} }
@@ -3,17 +3,17 @@ using Ragon.Common;
namespace Game.Source.Events; namespace Game.Source.Events;
public class TestEvent: IRagonSerializable public class SimpleEvent: IRagonSerializable
{ {
public string TestData; public string Name;
public void Serialize(BitBuffer buffer) public void Serialize(BitBuffer buffer)
{ {
buffer.AddString(TestData); buffer.AddString(Name);
} }
public void Deserialize(BitBuffer buffer) public void Deserialize(BitBuffer buffer)
{ {
TestData = buffer.ReadString(); Name = buffer.ReadString();
} }
} }
+31
View File
@@ -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);
}
}
}
@@ -3,15 +3,18 @@ using Ragon.Core;
namespace Game.Source 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) 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);
}
} }
} }