feat: plugin api for replication of data
This commit is contained in:
@@ -36,8 +36,11 @@ internal class RoomDataHandler: IHandler
|
|||||||
{
|
{
|
||||||
var rawData = reader.RawData;
|
var rawData = reader.RawData;
|
||||||
var peerId = (ushort)(rawData[1] + (rawData[2] << 8));
|
var peerId = (ushort)(rawData[1] + (rawData[2] << 8));
|
||||||
var player = _playerCache.GetPlayerByPeer(peerId);
|
|
||||||
|
|
||||||
|
RagonPlayer player = null;
|
||||||
|
if (peerId != 10000)
|
||||||
|
{
|
||||||
|
player = _playerCache.GetPlayerByPeer(peerId);
|
||||||
if (player == null)
|
if (player == null)
|
||||||
{
|
{
|
||||||
RagonLog.Error($"Player with peerId:{peerId} not found");
|
RagonLog.Error($"Player with peerId:{peerId} not found");
|
||||||
@@ -45,6 +48,7 @@ internal class RoomDataHandler: IHandler
|
|||||||
_playerCache.Dump();
|
_playerCache.Dump();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var headerSize = 3;
|
var headerSize = 3;
|
||||||
var payload = new byte[rawData.Length - headerSize];
|
var payload = new byte[rawData.Length - headerSize];
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using NLog;
|
||||||
using Ragon.Server.Entity;
|
using Ragon.Server.Entity;
|
||||||
|
using Ragon.Server.IO;
|
||||||
using Ragon.Server.Plugin;
|
using Ragon.Server.Plugin;
|
||||||
using Ragon.Server.Room;
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
@@ -7,47 +11,28 @@ namespace Ragon.Relay;
|
|||||||
|
|
||||||
public class RelayRoomPlugin : BaseRoomPlugin
|
public class RelayRoomPlugin : BaseRoomPlugin
|
||||||
{
|
{
|
||||||
public void Tick(float dt)
|
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnAttached()
|
|
||||||
{
|
|
||||||
Console.WriteLine("Room attached");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnDetached()
|
|
||||||
{
|
|
||||||
Console.WriteLine("Room detached");
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Entity created: {entity.Id}");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool OnEntityRemove(RagonRoomPlayer destroyer, RagonEntity entity)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Entity destroyed: {entity.Id}");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool OnPlayerJoined(RagonRoomPlayer player)
|
public override bool OnPlayerJoined(RagonRoomPlayer player)
|
||||||
{
|
{
|
||||||
|
// _logger.Trace($"Player {player.Name}|{player.Connection.Id} joined");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool OnPlayerLeaved(RagonRoomPlayer player)
|
public override bool OnPlayerLeaved(RagonRoomPlayer player)
|
||||||
{
|
{
|
||||||
|
// _logger.Trace($"Player {player.Name}|{player.Connection.Id} leaved");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool OnData(RagonRoomPlayer player, byte[] data)
|
public override bool OnData(RagonRoomPlayer player, byte[] data)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Data received");
|
// _logger.Trace($"Data received from {player.Name}|{player.Connection.Id}");
|
||||||
|
|
||||||
|
// All Players
|
||||||
|
// Room.ReplicateData(new Byte[] { 30, 40, 50 }, NetworkChannel.RELIABLE);
|
||||||
|
// Selected Player
|
||||||
|
// Room.ReplicateData(new byte[] { 10, 30, 40 }, new List<RagonRoomPlayer> { player }, NetworkChannel.RELIABLE);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ public sealed class RoomCreateOperation : BaseOperation
|
|||||||
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
|
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
|
||||||
var room = new RagonRoom(roomId, information, roomPlugin);
|
var room = new RagonRoom(roomId, information, roomPlugin);
|
||||||
|
|
||||||
|
room.Plugin.OnAttached(room);
|
||||||
roomPlayer.OnAttached(room);
|
roomPlayer.OnAttached(room);
|
||||||
|
|
||||||
context.Scheduler.Run(room);
|
context.Scheduler.Run(room);
|
||||||
|
|||||||
@@ -42,8 +42,11 @@ public sealed class RoomDataOperation : BaseOperation
|
|||||||
sendData[1] = (byte)peerId;
|
sendData[1] = (byte)peerId;
|
||||||
sendData[2] = (byte)(peerId >> 8);
|
sendData[2] = (byte)(peerId >> 8);
|
||||||
|
|
||||||
Array.Copy(data, 1, sendData, headerSize, dataSize);
|
var pluginData = new byte[dataSize];
|
||||||
|
Array.Copy(data, 1, pluginData, 0, dataSize);
|
||||||
|
room.Plugin.OnData(player, pluginData);
|
||||||
|
|
||||||
room.Broadcast(sendData, channel);
|
Array.Copy(data, 1, sendData, headerSize, dataSize);
|
||||||
|
room.Broadcast(sendData, room.ReadyPlayersList, NetworkChannel.RELIABLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,6 +79,8 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
|
|||||||
|
|
||||||
_ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
|
_ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
|
||||||
|
|
||||||
|
room.Plugin.OnAttached(room);
|
||||||
|
|
||||||
context.Lobby.Persist(room);
|
context.Lobby.Persist(room);
|
||||||
context.Scheduler.Run(room);
|
context.Scheduler.Run(room);
|
||||||
context.SetRoom(room, roomPlayer);
|
context.SetRoom(room, roomPlayer);
|
||||||
|
|||||||
@@ -28,4 +28,5 @@ public interface IRoomPlugin
|
|||||||
bool OnPlayerLeaved(RagonRoomPlayer player);
|
bool OnPlayerLeaved(RagonRoomPlayer player);
|
||||||
bool OnEntityCreate(RagonRoomPlayer player, IRagonEntity entity);
|
bool OnEntityCreate(RagonRoomPlayer player, IRagonEntity entity);
|
||||||
bool OnEntityRemove(RagonRoomPlayer player, IRagonEntity entity);
|
bool OnEntityRemove(RagonRoomPlayer player, IRagonEntity entity);
|
||||||
|
bool OnData(RagonRoomPlayer player, byte[] data);
|
||||||
}
|
}
|
||||||
@@ -29,6 +29,9 @@ public interface IRagonRoom
|
|||||||
public int PlayerCount { get; }
|
public int PlayerCount { get; }
|
||||||
public RagonData UserData { get; }
|
public RagonData UserData { get; }
|
||||||
|
|
||||||
|
public void ReplicateData(byte[] data, NetworkChannel channel);
|
||||||
|
public void ReplicateData(byte[] data, List<RagonRoomPlayer> player, NetworkChannel channel);
|
||||||
|
|
||||||
RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection);
|
RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection);
|
||||||
RagonRoomPlayer GetPlayerById(string id);
|
RagonRoomPlayer GetPlayerById(string id);
|
||||||
IRagonEntity GetEntityById(ushort id);
|
IRagonEntity GetEntityById(ushort id);
|
||||||
|
|||||||
@@ -144,7 +144,8 @@ public class RagonRoom : IRagonRoom, IRagonAction
|
|||||||
RagonTarget targetMode
|
RagonTarget targetMode
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner && _bufferedEvents.Count < _limitBufferedEvents)
|
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner &&
|
||||||
|
_bufferedEvents.Count < _limitBufferedEvents)
|
||||||
{
|
{
|
||||||
_bufferedEvents.Add(evnt);
|
_bufferedEvents.Add(evnt);
|
||||||
}
|
}
|
||||||
@@ -192,6 +193,28 @@ public class RagonRoom : IRagonRoom, IRagonAction
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void ReplicateData(byte[] data, NetworkChannel channel)
|
||||||
|
{
|
||||||
|
ReplicateData(data, ReadyPlayersList, channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ReplicateData(byte[] data, List<RagonRoomPlayer> receivers,
|
||||||
|
NetworkChannel channel = NetworkChannel.RELIABLE)
|
||||||
|
{
|
||||||
|
var dataSize = data.Length;
|
||||||
|
var headerSize = 3;
|
||||||
|
var size = headerSize + dataSize;
|
||||||
|
var sendData = new byte[size];
|
||||||
|
var peerId = 10000; // Server Peer
|
||||||
|
|
||||||
|
sendData[0] = (byte)RagonOperation.REPLICATE_RAW_DATA;
|
||||||
|
sendData[1] = (byte)peerId;
|
||||||
|
sendData[2] = (byte)(peerId >> 8);
|
||||||
|
|
||||||
|
Array.Copy(data, 0, sendData, headerSize, dataSize);
|
||||||
|
|
||||||
|
Broadcast(sendData, receivers, channel);
|
||||||
|
}
|
||||||
|
|
||||||
public void Tick(float dt)
|
public void Tick(float dt)
|
||||||
{
|
{
|
||||||
@@ -275,6 +298,8 @@ public class RagonRoom : IRagonRoom, IRagonAction
|
|||||||
player.OnDetached();
|
player.OnDetached();
|
||||||
|
|
||||||
UpdateReadyPlayerList();
|
UpdateReadyPlayerList();
|
||||||
|
|
||||||
|
Plugin.OnPlayerLeaved(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,6 +342,20 @@ public class RagonRoom : IRagonRoom, IRagonAction
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Broadcast(byte[] data, List<RagonRoomPlayer> players, NetworkChannel channel = NetworkChannel.RELIABLE)
|
||||||
|
{
|
||||||
|
if (channel == NetworkChannel.RELIABLE)
|
||||||
|
{
|
||||||
|
foreach (var p in players)
|
||||||
|
p.Connection.Reliable.Send(data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (var p in players)
|
||||||
|
p.Connection.Unreliable.Send(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection)
|
public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection)
|
||||||
{
|
{
|
||||||
return Players[connection.Id];
|
return Players[connection.Id];
|
||||||
|
|||||||
Reference in New Issue
Block a user