feat: improved server plugin api

This commit is contained in:
2024-05-19 11:28:36 +03:00
parent 6199af3d1e
commit 7cf1353869
9 changed files with 92 additions and 45 deletions
@@ -26,12 +26,14 @@ namespace Ragon.Server.Handler
{
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(AuthorizationOperation));
private readonly IServerPlugin _serverPlugin;
private readonly IRagonServer _server;
private readonly RagonContextObserver _observer;
private readonly RagonServerConfiguration _configuration;
private readonly RagonBuffer _writer;
public AuthorizationOperation(RagonBuffer reader,
RagonBuffer writer,
IRagonServer server,
IServerPlugin serverPlugin,
RagonContextObserver observer,
RagonServerConfiguration configuration) : base(reader, writer)
@@ -40,6 +42,7 @@ namespace Ragon.Server.Handler
_configuration = configuration;
_observer = observer;
_writer = writer;
_server = server;
}
public override void Handle(RagonContext context, NetworkChannel channel)
@@ -63,14 +66,12 @@ namespace Ragon.Server.Handler
if (key == configuration.ServerKey)
{
var authorizeViaWebHook = _serverPlugin.OnAuthorize(new ConnectionRequest() { PeerID = context.Connection.Id });
if (authorizeViaWebHook)
var authorizeViaPlugin = _serverPlugin.OnAuthorize(new ConnectionRequest(_server, context.Connection.Id, payload));
if (authorizeViaPlugin)
return;
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload);
context.SetPlayer(lobbyPlayer);
Approve(context);
var id = Guid.NewGuid().ToString();
Approve(context, new ConnectionResponse(id, name, payload));
}
else
{
@@ -78,8 +79,10 @@ namespace Ragon.Server.Handler
}
}
public void Approve(RagonContext context)
public void Approve(RagonContext context, ConnectionResponse result)
{
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, result.Id, result.Name, result.Payload);
context.SetPlayer(lobbyPlayer);
context.ConnectionStatus = ConnectionStatus.Authorized;
_observer.OnAuthorized(context);
@@ -49,7 +49,6 @@ public sealed class RoomJoinOperation : BaseOperation
JoinSuccess(context, existsRoom, Writer);
existsRoom.RestoreBufferedEvents(player);
existsRoom.Plugin.OnPlayerJoined(player);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to {existsRoom.Id}");
+2 -2
View File
@@ -24,6 +24,6 @@ namespace Ragon.Server;
public interface IRagonServer
{
BaseOperation ResolveHandler(RagonOperation operation);
RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection);
RagonLobbyPlayer? GetPlayerById(string id);
public RagonContext? GetContextByConnectionId(ushort peerId);
public RagonContext? GetContextById(string playerId);
}
@@ -14,6 +14,8 @@
* limitations under the License.
*/
using Ragon.Protocol;
using Ragon.Server.Handler;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Room;
@@ -22,17 +24,58 @@ namespace Ragon.Server.Plugin
{
public class ConnectionRequest
{
public int PeerID { get; set; }
public void Approve()
public ConnectionRequest(
IRagonServer server,
ushort connectionId,
string payload
)
{
_server = server;
PeerID = connectionId;
Payload = payload;
}
public ushort PeerID { get; private set; }
public string Payload { get; private set; }
private readonly IRagonServer _server;
public void Approve(string id, string name, string payload)
{
var ctx = _server.GetContextByConnectionId(PeerID);
if (ctx == null)
return;
var operation = (AuthorizationOperation)_server.ResolveHandler(RagonOperation.AUTHORIZE);
operation.Approve(ctx, new ConnectionResponse(id, name, payload));
}
public void Reject()
{
var ctx = _server.GetContextByConnectionId(PeerID);
if (ctx == null)
return;
var operation = (AuthorizationOperation)_server.ResolveHandler(RagonOperation.AUTHORIZE);
operation.Reject(ctx);
}
}
public class ConnectionResponse
{
public string Id { get; }
public string Name { get; }
public string Payload { get; }
public ConnectionResponse(string id, string name, string payload)
{
Id = id;
Name = name;
Payload = payload;
}
}
public class BaseServerPlugin : IServerPlugin
{
public IRagonServer Server { get; protected set; }
@@ -44,7 +87,6 @@ namespace Ragon.Server.Plugin
public virtual void OnDetached()
{
}
public virtual bool OnAuthorize(ConnectionRequest request)
+9 -9
View File
@@ -34,7 +34,6 @@ public class RagonServer : IRagonServer, INetworkListener
private readonly BaseOperation[] _handlers;
private readonly IRagonLobby _lobby;
private readonly IServerPlugin _serverPlugin;
private readonly Thread _dedicatedThread;
private readonly RagonServerConfiguration _configuration;
private readonly RagonBuffer _reader;
private readonly RagonBuffer _writer;
@@ -75,7 +74,7 @@ public class RagonServer : IRagonServer, INetworkListener
_serverPlugin.OnAttached(this);
_handlers = new BaseOperation[byte.MaxValue];
_handlers[(byte)RagonOperation.AUTHORIZE] = new AuthorizationOperation(_reader, _writer, _serverPlugin, contextObserver, configuration);
_handlers[(byte)RagonOperation.AUTHORIZE] = new AuthorizationOperation(_reader, _writer, this, _serverPlugin, contextObserver, configuration);
_handlers[(byte)RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(_reader, _writer, plugin, _configuration);
_handlers[(byte)RagonOperation.CREATE_ROOM] = new RoomCreateOperation(_reader, _writer, plugin, _configuration);
_handlers[(byte)RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_reader, _writer);
@@ -136,7 +135,6 @@ public class RagonServer : IRagonServer, INetworkListener
{
_serverPlugin.OnDetached();
_server.Stop();
_dedicatedThread.Interrupt();
}
public void OnConnected(INetworkConnection connection)
@@ -158,6 +156,7 @@ public class RagonServer : IRagonServer, INetworkListener
_lobby.RemoveIfEmpty(room);
}
_contextsByPlayerId.Remove(context.LobbyPlayer.Id);
_logger.Trace($"Disconnected: {connection.Id}");
}
@@ -177,7 +176,8 @@ public class RagonServer : IRagonServer, INetworkListener
room.DetachPlayer(context.RoomPlayer);
_lobby.RemoveIfEmpty(room);
}
_contextsByPlayerId.Remove(context.LobbyPlayer.Id);
_logger.Trace($"Timeout: {connection.Id}|{context.LobbyPlayer.Name}|{context.LobbyPlayer.Id}");
}
else
@@ -274,15 +274,15 @@ public class RagonServer : IRagonServer, INetworkListener
{
return _handlers[(byte)operation];
}
public RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection)
public RagonContext? GetContextByConnectionId(ushort peerId)
{
return _contextsByConnection.TryGetValue(connection.Id, out var context) ? context.LobbyPlayer : null;
return _contextsByConnection.TryGetValue(peerId, out var context) ? context : null;
}
public RagonLobbyPlayer? GetPlayerById(string playerId)
public RagonContext? GetContextById(string playerId)
{
return _contextsByPlayerId.TryGetValue(playerId, out var context) ? context.LobbyPlayer : null;
return _contextsByPlayerId.TryGetValue(playerId, out var context) ? context : null;
}
private void CopyrightInfo()