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
@@ -3,17 +3,18 @@ using Newtonsoft.Json;
using Ragon.Server; using Ragon.Server;
using Ragon.Server.Plugin; using Ragon.Server.Plugin;
namespace Ragon.Relay; namespace Ragon.Relay
public class RelayServerPlugin: BaseServerPlugin
{ {
public class RelayServerPlugin : BaseServerPlugin
{
public override bool OnCommand(string command, string payload) public override bool OnCommand(string command, string payload)
{ {
Console.WriteLine(command); Console.WriteLine(command);
if (command == "kick-player") if (command == "kick-player")
{ {
var commandPayload = JsonConvert.DeserializeObject<KickPlayerCommand>(payload); var commandPayload = JsonConvert.DeserializeObject<KickPlayerCommand>(payload);
var player = Server.GetPlayerById(commandPayload.Id); var player = Server.GetContextById(commandPayload.Id);
if (player != null) if (player != null)
player.Connection.Close(); player.Connection.Close();
else else
@@ -27,4 +28,5 @@ public class RelayServerPlugin: BaseServerPlugin
{ {
return new RelayRoomPlugin(); return new RelayRoomPlugin();
} }
}
} }
+2
View File
@@ -42,6 +42,7 @@ namespace Ragon.Relay
INetworkServer networkServer = new ENetServer(); INetworkServer networkServer = new ENetServer();
IServerPlugin plugin = new RelayServerPlugin(); IServerPlugin plugin = new RelayServerPlugin();
switch (serverType) switch (serverType)
{ {
case ServerType.ENET: case ServerType.ENET:
@@ -63,6 +64,7 @@ namespace Ragon.Relay
ServerKey = configuration.ServerKey, ServerKey = configuration.ServerKey,
ServerTickRate = configuration.ServerTickRate, ServerTickRate = configuration.ServerTickRate,
}; };
var relay = new RagonServer(networkServer, plugin, serverConfiguration); var relay = new RagonServer(networkServer, plugin, serverConfiguration);
relay.Start(); relay.Start();
while (relay.IsRunning) while (relay.IsRunning)
@@ -15,6 +15,5 @@ namespace Ragon.Relay
public int LimitRooms; public int LimitRooms;
public int LimitBufferedEvents; public int LimitBufferedEvents;
public int LimitUserData; public int LimitUserData;
} }
} }
@@ -26,12 +26,14 @@ namespace Ragon.Server.Handler
{ {
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(AuthorizationOperation)); private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(AuthorizationOperation));
private readonly IServerPlugin _serverPlugin; private readonly IServerPlugin _serverPlugin;
private readonly IRagonServer _server;
private readonly RagonContextObserver _observer; private readonly RagonContextObserver _observer;
private readonly RagonServerConfiguration _configuration; private readonly RagonServerConfiguration _configuration;
private readonly RagonBuffer _writer; private readonly RagonBuffer _writer;
public AuthorizationOperation(RagonBuffer reader, public AuthorizationOperation(RagonBuffer reader,
RagonBuffer writer, RagonBuffer writer,
IRagonServer server,
IServerPlugin serverPlugin, IServerPlugin serverPlugin,
RagonContextObserver observer, RagonContextObserver observer,
RagonServerConfiguration configuration) : base(reader, writer) RagonServerConfiguration configuration) : base(reader, writer)
@@ -40,6 +42,7 @@ namespace Ragon.Server.Handler
_configuration = configuration; _configuration = configuration;
_observer = observer; _observer = observer;
_writer = writer; _writer = writer;
_server = server;
} }
public override void Handle(RagonContext context, NetworkChannel channel) public override void Handle(RagonContext context, NetworkChannel channel)
@@ -63,14 +66,12 @@ namespace Ragon.Server.Handler
if (key == configuration.ServerKey) if (key == configuration.ServerKey)
{ {
var authorizeViaWebHook = _serverPlugin.OnAuthorize(new ConnectionRequest() { PeerID = context.Connection.Id }); var authorizeViaPlugin = _serverPlugin.OnAuthorize(new ConnectionRequest(_server, context.Connection.Id, payload));
if (authorizeViaWebHook) if (authorizeViaPlugin)
return; return;
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload); var id = Guid.NewGuid().ToString();
context.SetPlayer(lobbyPlayer); Approve(context, new ConnectionResponse(id, name, payload));
Approve(context);
} }
else 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; context.ConnectionStatus = ConnectionStatus.Authorized;
_observer.OnAuthorized(context); _observer.OnAuthorized(context);
@@ -49,7 +49,6 @@ public sealed class RoomJoinOperation : BaseOperation
JoinSuccess(context, existsRoom, Writer); JoinSuccess(context, existsRoom, Writer);
existsRoom.RestoreBufferedEvents(player); existsRoom.RestoreBufferedEvents(player);
existsRoom.Plugin.OnPlayerJoined(player); existsRoom.Plugin.OnPlayerJoined(player);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to {existsRoom.Id}"); _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 public interface IRagonServer
{ {
BaseOperation ResolveHandler(RagonOperation operation); BaseOperation ResolveHandler(RagonOperation operation);
RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection); public RagonContext? GetContextByConnectionId(ushort peerId);
RagonLobbyPlayer? GetPlayerById(string id); public RagonContext? GetContextById(string playerId);
} }
@@ -14,6 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
using Ragon.Protocol;
using Ragon.Server.Handler;
using Ragon.Server.IO; using Ragon.Server.IO;
using Ragon.Server.Lobby; using Ragon.Server.Lobby;
using Ragon.Server.Room; using Ragon.Server.Room;
@@ -22,17 +24,58 @@ namespace Ragon.Server.Plugin
{ {
public class ConnectionRequest public class ConnectionRequest
{ {
public int PeerID { get; set; } public ConnectionRequest(
IRagonServer server,
public void Approve() 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() 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 class BaseServerPlugin : IServerPlugin
{ {
public IRagonServer Server { get; protected set; } public IRagonServer Server { get; protected set; }
@@ -44,7 +87,6 @@ namespace Ragon.Server.Plugin
public virtual void OnDetached() public virtual void OnDetached()
{ {
} }
public virtual bool OnAuthorize(ConnectionRequest request) public virtual bool OnAuthorize(ConnectionRequest request)
+7 -7
View File
@@ -34,7 +34,6 @@ public class RagonServer : IRagonServer, INetworkListener
private readonly BaseOperation[] _handlers; private readonly BaseOperation[] _handlers;
private readonly IRagonLobby _lobby; private readonly IRagonLobby _lobby;
private readonly IServerPlugin _serverPlugin; private readonly IServerPlugin _serverPlugin;
private readonly Thread _dedicatedThread;
private readonly RagonServerConfiguration _configuration; private readonly RagonServerConfiguration _configuration;
private readonly RagonBuffer _reader; private readonly RagonBuffer _reader;
private readonly RagonBuffer _writer; private readonly RagonBuffer _writer;
@@ -75,7 +74,7 @@ public class RagonServer : IRagonServer, INetworkListener
_serverPlugin.OnAttached(this); _serverPlugin.OnAttached(this);
_handlers = new BaseOperation[byte.MaxValue]; _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.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(_reader, _writer, plugin, _configuration);
_handlers[(byte)RagonOperation.CREATE_ROOM] = new RoomCreateOperation(_reader, _writer, plugin, _configuration); _handlers[(byte)RagonOperation.CREATE_ROOM] = new RoomCreateOperation(_reader, _writer, plugin, _configuration);
_handlers[(byte)RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_reader, _writer); _handlers[(byte)RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_reader, _writer);
@@ -136,7 +135,6 @@ public class RagonServer : IRagonServer, INetworkListener
{ {
_serverPlugin.OnDetached(); _serverPlugin.OnDetached();
_server.Stop(); _server.Stop();
_dedicatedThread.Interrupt();
} }
public void OnConnected(INetworkConnection connection) public void OnConnected(INetworkConnection connection)
@@ -158,6 +156,7 @@ public class RagonServer : IRagonServer, INetworkListener
_lobby.RemoveIfEmpty(room); _lobby.RemoveIfEmpty(room);
} }
_contextsByPlayerId.Remove(context.LobbyPlayer.Id);
_logger.Trace($"Disconnected: {connection.Id}"); _logger.Trace($"Disconnected: {connection.Id}");
} }
@@ -177,6 +176,7 @@ public class RagonServer : IRagonServer, INetworkListener
room.DetachPlayer(context.RoomPlayer); room.DetachPlayer(context.RoomPlayer);
_lobby.RemoveIfEmpty(room); _lobby.RemoveIfEmpty(room);
} }
_contextsByPlayerId.Remove(context.LobbyPlayer.Id);
_logger.Trace($"Timeout: {connection.Id}|{context.LobbyPlayer.Name}|{context.LobbyPlayer.Id}"); _logger.Trace($"Timeout: {connection.Id}|{context.LobbyPlayer.Name}|{context.LobbyPlayer.Id}");
} }
@@ -275,14 +275,14 @@ public class RagonServer : IRagonServer, INetworkListener
return _handlers[(byte)operation]; 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() private void CopyrightInfo()