feat: improved server plugin api
This commit is contained in:
@@ -3,7 +3,8 @@ using Newtonsoft.Json;
|
||||
using Ragon.Server;
|
||||
using Ragon.Server.Plugin;
|
||||
|
||||
namespace Ragon.Relay;
|
||||
namespace Ragon.Relay
|
||||
{
|
||||
|
||||
public class RelayServerPlugin : BaseServerPlugin
|
||||
{
|
||||
@@ -13,7 +14,7 @@ public class RelayServerPlugin: BaseServerPlugin
|
||||
if (command == "kick-player")
|
||||
{
|
||||
var commandPayload = JsonConvert.DeserializeObject<KickPlayerCommand>(payload);
|
||||
var player = Server.GetPlayerById(commandPayload.Id);
|
||||
var player = Server.GetContextById(commandPayload.Id);
|
||||
if (player != null)
|
||||
player.Connection.Close();
|
||||
else
|
||||
@@ -28,3 +29,4 @@ public class RelayServerPlugin: BaseServerPlugin
|
||||
return new RelayRoomPlugin();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,7 @@ namespace Ragon.Relay
|
||||
|
||||
INetworkServer networkServer = new ENetServer();
|
||||
IServerPlugin plugin = new RelayServerPlugin();
|
||||
|
||||
switch (serverType)
|
||||
{
|
||||
case ServerType.ENET:
|
||||
@@ -63,6 +64,7 @@ namespace Ragon.Relay
|
||||
ServerKey = configuration.ServerKey,
|
||||
ServerTickRate = configuration.ServerTickRate,
|
||||
};
|
||||
|
||||
var relay = new RagonServer(networkServer, plugin, serverConfiguration);
|
||||
relay.Start();
|
||||
while (relay.IsRunning)
|
||||
|
||||
@@ -15,6 +15,5 @@ namespace Ragon.Relay
|
||||
public int LimitRooms;
|
||||
public int LimitBufferedEvents;
|
||||
public int LimitUserData;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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}");
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,6 +176,7 @@ 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}");
|
||||
}
|
||||
@@ -275,14 +275,14 @@ 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()
|
||||
|
||||
Reference in New Issue
Block a user