wip
This commit is contained in:
@@ -38,7 +38,7 @@ internal class AuthorizeSuccessHandler: IHandler
|
|||||||
var playerName = reader.ReadString();
|
var playerName = reader.ReadString();
|
||||||
var playerPayload = reader.ReadString();
|
var playerPayload = reader.ReadString();
|
||||||
|
|
||||||
_client.SetStatus(RagonStatus.LOBBY);
|
_client.UpdateState(RagonState.LOBBY);
|
||||||
_listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload);
|
_listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,16 +42,15 @@ internal class JoinSuccessHandler : IHandler
|
|||||||
private readonly RagonListenerList _listenerList;
|
private readonly RagonListenerList _listenerList;
|
||||||
private readonly RagonPlayerCache _playerCache;
|
private readonly RagonPlayerCache _playerCache;
|
||||||
private readonly RagonClient _client;
|
private readonly RagonClient _client;
|
||||||
|
private readonly RagonRoom _room;
|
||||||
|
|
||||||
public JoinSuccessHandler(
|
public JoinSuccessHandler(
|
||||||
RagonClient client,
|
RagonClient client,
|
||||||
RagonListenerList listenerList,
|
RagonRoom room
|
||||||
RagonPlayerCache playerCache
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_listenerList = listenerList;
|
_room = room;
|
||||||
_playerCache = playerCache;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonStream reader)
|
public void Handle(RagonStream reader)
|
||||||
@@ -59,17 +58,14 @@ internal class JoinSuccessHandler : IHandler
|
|||||||
var roomId = reader.ReadString();
|
var roomId = reader.ReadString();
|
||||||
var min = reader.ReadUShort();
|
var min = reader.ReadUShort();
|
||||||
var max = reader.ReadUShort();
|
var max = reader.ReadUShort();
|
||||||
var sceneName = reader.ReadString();
|
|
||||||
var localId = reader.ReadString();
|
var localId = reader.ReadString();
|
||||||
var ownerId = reader.ReadString();
|
var ownerId = reader.ReadString();
|
||||||
|
var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max);
|
||||||
|
|
||||||
_playerCache.SetOwnerAndLocal(ownerId, localId);
|
_playerCache.SetOwnerAndLocal(ownerId, localId);
|
||||||
|
_room.Reset(roomInfo);
|
||||||
var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max);
|
_room.UserData.Read(reader);
|
||||||
var room = new RagonRoom(_client, _playerCache, roomInfo);
|
|
||||||
|
|
||||||
room.UserData.Read(reader);
|
|
||||||
|
|
||||||
var playersCount = reader.ReadUShort();
|
var playersCount = reader.ReadUShort();
|
||||||
RagonLog.Trace("Players: " + playersCount);
|
RagonLog.Trace("Players: " + playersCount);
|
||||||
for (var i = 0; i < playersCount; i++)
|
for (var i = 0; i < playersCount; i++)
|
||||||
@@ -79,15 +75,14 @@ internal class JoinSuccessHandler : IHandler
|
|||||||
var playerName = reader.ReadString();
|
var playerName = reader.ReadString();
|
||||||
|
|
||||||
var player = _playerCache.AddPlayer(playerPeerId, playerId, playerName);
|
var player = _playerCache.AddPlayer(playerPeerId, playerId, playerName);
|
||||||
|
|
||||||
player.UserData.Read(reader);
|
player.UserData.Read(reader);
|
||||||
|
|
||||||
RagonLog.Trace($"Player {playerPeerId} - {playerId} - {playerName}");
|
RagonLog.Trace($"Player {playerPeerId} - {playerId} - {playerName}");
|
||||||
}
|
}
|
||||||
|
|
||||||
_client.AssignRoom(room);
|
_client.UpdateState(RagonState.ROOM);
|
||||||
_client.SetStatus(RagonStatus.ROOM);
|
|
||||||
|
|
||||||
_listenerList.OnJoined();
|
_listenerList.OnJoined();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,6 +35,7 @@ internal class LeaveRoomHandler : IHandler
|
|||||||
public void Handle(RagonStream reader)
|
public void Handle(RagonStream reader)
|
||||||
{
|
{
|
||||||
_listenerList.OnLeft();
|
_listenerList.OnLeft();
|
||||||
_client.Room.Cleanup();
|
|
||||||
|
_client.Room.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
using Ragon.Protocol;
|
|
||||||
|
|
||||||
namespace Ragon.Client;
|
|
||||||
|
|
||||||
internal class RoomDataHandler: IHandler
|
|
||||||
{
|
|
||||||
private readonly RagonListenerList _listeners;
|
|
||||||
private readonly RagonPlayerCache _playerCache;
|
|
||||||
|
|
||||||
public RoomDataHandler(
|
|
||||||
RagonPlayerCache playerCache,
|
|
||||||
RagonListenerList listeners)
|
|
||||||
{
|
|
||||||
_playerCache = playerCache;
|
|
||||||
_listeners = listeners;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Handle(RagonStream reader)
|
|
||||||
{
|
|
||||||
var rawData = reader.ReadBinary(reader.Lenght);
|
|
||||||
var peerId = (ushort)(rawData[1] + (rawData[2] << 8));
|
|
||||||
|
|
||||||
RagonPlayer player = null;
|
|
||||||
if (peerId != 10000)
|
|
||||||
{
|
|
||||||
player = _playerCache.GetPlayerByPeer(peerId);
|
|
||||||
if (player == null)
|
|
||||||
{
|
|
||||||
RagonLog.Error($"Player with peerId:{peerId} not found");
|
|
||||||
|
|
||||||
_playerCache.Dump();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var headerSize = 3;
|
|
||||||
var payload = new byte[rawData.Length - headerSize];
|
|
||||||
|
|
||||||
Array.Copy(rawData, headerSize, payload, 0, payload.Length);
|
|
||||||
|
|
||||||
_listeners.OnData(player, payload);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -36,13 +36,13 @@ public class RoomEventHandler : IHandler
|
|||||||
{
|
{
|
||||||
var eventCode = buffer.ReadUShort();
|
var eventCode = buffer.ReadUShort();
|
||||||
var peerId = buffer.ReadUShort();
|
var peerId = buffer.ReadUShort();
|
||||||
var executionMode = (RagonReplicationMode)buffer.ReadByte();
|
var executionMode = (RagonReplicationMode) buffer.ReadByte();
|
||||||
|
|
||||||
var player = _playerCache.GetPlayerByPeer(peerId);
|
var player = _playerCache.GetPlayerByPeer(peerId);
|
||||||
if (player == null)
|
if (player == null)
|
||||||
{
|
{
|
||||||
RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}");
|
RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}");
|
||||||
|
|
||||||
_playerCache.Dump();
|
_playerCache.Dump();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ public class TimestampHandler: IHandler
|
|||||||
var timestamp1 = (uint)buffer.ReadInt();
|
var timestamp1 = (uint)buffer.ReadInt();
|
||||||
var value = new DoubleToUInt { Int0 = timestamp0, Int1 = timestamp1 };
|
var value = new DoubleToUInt { Int0 = timestamp0, Int1 = timestamp1 };
|
||||||
|
|
||||||
_client.SetTimestamp(value.Double);
|
_client.UpdateTimestamp(value.Double);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,6 +20,5 @@ namespace Ragon.Client
|
|||||||
{
|
{
|
||||||
public interface IRagonEvent: IRagonSerializable
|
public interface IRagonEvent: IRagonSerializable
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -22,7 +22,6 @@ namespace Ragon.Client
|
|||||||
IRagonFailedListener,
|
IRagonFailedListener,
|
||||||
IRagonJoinListener,
|
IRagonJoinListener,
|
||||||
IRagonLeftListener,
|
IRagonLeftListener,
|
||||||
IRagonSceneListener,
|
|
||||||
IRagonOwnershipChangedListener,
|
IRagonOwnershipChangedListener,
|
||||||
IRagonPlayerJoinListener,
|
IRagonPlayerJoinListener,
|
||||||
IRagonPlayerLeftListener,
|
IRagonPlayerLeftListener,
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Ragon.Client;
|
|
||||||
|
|
||||||
public interface IRagonSceneListener
|
|
||||||
{
|
|
||||||
void OnSceneLoaded(RagonClient client);
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
namespace Ragon.Client;
|
|
||||||
|
|
||||||
public interface IRagonSceneRequestListener
|
|
||||||
{
|
|
||||||
void OnRequestScene(RagonClient client, string sceneName);
|
|
||||||
}
|
|
||||||
@@ -31,7 +31,7 @@ namespace Ragon.Client
|
|||||||
private RagonListenerList _listeners;
|
private RagonListenerList _listeners;
|
||||||
private RagonPlayerCache _playerCache;
|
private RagonPlayerCache _playerCache;
|
||||||
private RagonEventCache _eventCache;
|
private RagonEventCache _eventCache;
|
||||||
private RagonStatus _status;
|
private RagonState _state;
|
||||||
|
|
||||||
private double _serverTimestamp;
|
private double _serverTimestamp;
|
||||||
private float _replicationRate = 0;
|
private float _replicationRate = 0;
|
||||||
@@ -39,7 +39,7 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public double ServerTimestamp => _serverTimestamp;
|
public double ServerTimestamp => _serverTimestamp;
|
||||||
public IRagonConnection Connection => _connection;
|
public IRagonConnection Connection => _connection;
|
||||||
public RagonStatus Status => _status;
|
public RagonState State => _state;
|
||||||
public RagonSession Session => _session;
|
public RagonSession Session => _session;
|
||||||
public RagonEventCache Event => _eventCache;
|
public RagonEventCache Event => _eventCache;
|
||||||
public NetworkStatistics Statistics => _stats;
|
public NetworkStatistics Statistics => _stats;
|
||||||
@@ -64,22 +64,18 @@ namespace Ragon.Client
|
|||||||
_replicationTime = 0;
|
_replicationTime = 0;
|
||||||
|
|
||||||
_eventCache = new RagonEventCache();
|
_eventCache = new RagonEventCache();
|
||||||
_stats = new NetworkStatistics();
|
|
||||||
_status = RagonStatus.DISCONNECTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void Connect(string address, ushort port, string protocol)
|
|
||||||
{
|
|
||||||
_writeBuffer = new RagonStream();
|
_writeBuffer = new RagonStream();
|
||||||
_readBuffer = new RagonStream();
|
_readBuffer = new RagonStream();
|
||||||
_playerCache = new RagonPlayerCache();
|
_playerCache = new RagonPlayerCache();
|
||||||
_session = new RagonSession(this, _writeBuffer);
|
_session = new RagonSession(this, _writeBuffer);
|
||||||
|
_room = new RagonRoom(this, _playerCache);
|
||||||
|
_stats = new NetworkStatistics();
|
||||||
|
_state = RagonState.DISCONNECTED;
|
||||||
|
|
||||||
_handlers = new IHandler[byte.MaxValue];
|
_handlers = new IHandler[byte.MaxValue];
|
||||||
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, _listeners);
|
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, _listeners);
|
||||||
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listeners);
|
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listeners);
|
||||||
_handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, _listeners, _playerCache);
|
_handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, _room);
|
||||||
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listeners);
|
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listeners);
|
||||||
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners);
|
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners);
|
||||||
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = new OwnershipRoomHandler(_listeners, _playerCache);
|
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = new OwnershipRoomHandler(_listeners, _playerCache);
|
||||||
@@ -87,19 +83,22 @@ namespace Ragon.Client
|
|||||||
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_playerCache, _listeners);
|
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_playerCache, _listeners);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache);
|
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache);
|
||||||
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampHandler(this);
|
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampHandler(this);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataHandler(_playerCache, _listeners);
|
|
||||||
_handlers[(byte)RagonOperation.ROOM_LIST_UPDATED] = new RoomListHandler(_session, _listeners);
|
_handlers[(byte)RagonOperation.ROOM_LIST_UPDATED] = new RoomListHandler(_session, _listeners);
|
||||||
_handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataHandler(this, _listeners);
|
_handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataHandler(this, _listeners);
|
||||||
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataHandler(_playerCache, _listeners);
|
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataHandler(_playerCache, _listeners);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Connect(string address, ushort port, string protocol)
|
||||||
|
{
|
||||||
var protocolRaw = RagonVersion.Parse(protocol);
|
var protocolRaw = RagonVersion.Parse(protocol);
|
||||||
_connection.Connect(address, port, protocolRaw);
|
_connection.Connect(address, port, protocolRaw);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Disconnect()
|
public void Disconnect()
|
||||||
{
|
{
|
||||||
_status = RagonStatus.DISCONNECTED;
|
_state = RagonState.DISCONNECTED;
|
||||||
_room?.Cleanup();
|
_room.Clear();
|
||||||
|
|
||||||
_connection.Disconnect();
|
_connection.Disconnect();
|
||||||
|
|
||||||
OnDisconnected(RagonDisconnect.MANUAL);
|
OnDisconnected(RagonDisconnect.MANUAL);
|
||||||
@@ -107,14 +106,13 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public void Update(float dt)
|
public void Update(float dt)
|
||||||
{
|
{
|
||||||
if (_status != RagonStatus.DISCONNECTED)
|
if (_state != RagonState.DISCONNECTED)
|
||||||
{
|
{
|
||||||
_replicationTime += dt;
|
_replicationTime += dt;
|
||||||
if (_replicationTime >= _replicationRate)
|
if (_replicationTime >= _replicationRate)
|
||||||
{
|
{
|
||||||
_replicationTime = 0;
|
_replicationTime = 0;
|
||||||
// _entityCache.WriteState(_writeBuffer);
|
|
||||||
|
|
||||||
SendTimestamp();
|
SendTimestamp();
|
||||||
SendRoomUserData();
|
SendRoomUserData();
|
||||||
SendPlayerUserData();
|
SendPlayerUserData();
|
||||||
@@ -129,9 +127,9 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (_status != RagonStatus.DISCONNECTED)
|
if (_state != RagonState.DISCONNECTED)
|
||||||
{
|
{
|
||||||
_status = RagonStatus.DISCONNECTED;
|
_state = RagonState.DISCONNECTED;
|
||||||
_connection.Disconnect();
|
_connection.Disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +145,6 @@ namespace Ragon.Client
|
|||||||
public void AddListener(IRagonOwnershipChangedListener listener) => _listeners.Add(listener);
|
public void AddListener(IRagonOwnershipChangedListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonPlayerJoinListener listener) => _listeners.Add(listener);
|
public void AddListener(IRagonPlayerJoinListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonPlayerLeftListener listener) => _listeners.Add(listener);
|
public void AddListener(IRagonPlayerLeftListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonDataListener listener) => _listeners.Add(listener);
|
|
||||||
public void AddListener(IRagonRoomListListener listener) => _listeners.Add(listener);
|
public void AddListener(IRagonRoomListListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonPlayerUserDataListener listener) => _listeners.Add(listener);
|
public void AddListener(IRagonPlayerUserDataListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonRoomUserDataListener listener) => _listeners.Add(listener);
|
public void AddListener(IRagonRoomUserDataListener listener) => _listeners.Add(listener);
|
||||||
@@ -160,7 +157,6 @@ namespace Ragon.Client
|
|||||||
public void RemoveListener(IRagonOwnershipChangedListener listener) => _listeners.Remove(listener);
|
public void RemoveListener(IRagonOwnershipChangedListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonPlayerJoinListener listener) => _listeners.Remove(listener);
|
public void RemoveListener(IRagonPlayerJoinListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonPlayerLeftListener listener) => _listeners.Remove(listener);
|
public void RemoveListener(IRagonPlayerLeftListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonDataListener listener) => _listeners.Remove(listener);
|
|
||||||
public void RemoveListener(IRagonRoomListListener listener) => _listeners.Remove(listener);
|
public void RemoveListener(IRagonRoomListListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonRoomUserDataListener listener) => _listeners.Remove(listener);
|
public void RemoveListener(IRagonRoomUserDataListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonPlayerUserDataListener listener) => _listeners.Remove(listener);
|
public void RemoveListener(IRagonPlayerUserDataListener listener) => _listeners.Remove(listener);
|
||||||
@@ -168,19 +164,13 @@ namespace Ragon.Client
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region INTERNAL
|
#region INTERNAL
|
||||||
|
|
||||||
internal void AssignRoom(RagonRoom room)
|
internal void UpdateState(RagonState state)
|
||||||
{
|
{
|
||||||
_room?.Dispose();
|
_state = state;
|
||||||
_room = room;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SetStatus(RagonStatus status)
|
internal void UpdateTimestamp(double time)
|
||||||
{
|
|
||||||
_status = status;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void SetTimestamp(double time)
|
|
||||||
{
|
{
|
||||||
_serverTimestamp = time;
|
_serverTimestamp = time;
|
||||||
}
|
}
|
||||||
@@ -240,7 +230,7 @@ namespace Ragon.Client
|
|||||||
RagonLog.Trace("Connected");
|
RagonLog.Trace("Connected");
|
||||||
|
|
||||||
_listeners.OnConnected();
|
_listeners.OnConnected();
|
||||||
_status = RagonStatus.CONNECTED;
|
_state = RagonState.CONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDisconnected(RagonDisconnect reason)
|
private void OnDisconnected(RagonDisconnect reason)
|
||||||
@@ -248,7 +238,7 @@ namespace Ragon.Client
|
|||||||
RagonLog.Trace($"Disconnected: {reason}");
|
RagonLog.Trace($"Disconnected: {reason}");
|
||||||
|
|
||||||
_listeners.OnDisconnected(reason);
|
_listeners.OnDisconnected(reason);
|
||||||
_status = RagonStatus.DISCONNECTED;
|
_state = RagonState.DISCONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnData(byte[] data)
|
private void OnData(byte[] data)
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ namespace Ragon.Client
|
|||||||
private readonly List<IRagonOwnershipChangedListener> _ownershipChangedListeners = new();
|
private readonly List<IRagonOwnershipChangedListener> _ownershipChangedListeners = new();
|
||||||
private readonly List<IRagonPlayerJoinListener> _playerJoinListeners = new();
|
private readonly List<IRagonPlayerJoinListener> _playerJoinListeners = new();
|
||||||
private readonly List<IRagonPlayerLeftListener> _playerLeftListeners = new();
|
private readonly List<IRagonPlayerLeftListener> _playerLeftListeners = new();
|
||||||
private readonly List<IRagonDataListener> _dataListeners = new();
|
|
||||||
private readonly List<IRagonRoomListListener> _roomListListeners = new();
|
private readonly List<IRagonRoomListListener> _roomListListeners = new();
|
||||||
private readonly List<IRagonRoomUserDataListener> _roomUserDataListeners = new();
|
private readonly List<IRagonRoomUserDataListener> _roomUserDataListeners = new();
|
||||||
private readonly List<IRagonPlayerUserDataListener> _playerUserDataListeners = new();
|
private readonly List<IRagonPlayerUserDataListener> _playerUserDataListeners = new();
|
||||||
@@ -79,11 +78,6 @@ namespace Ragon.Client
|
|||||||
_delayedActions.Clear();
|
_delayedActions.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add(IRagonDataListener dataListener)
|
|
||||||
{
|
|
||||||
_dataListeners.Add(dataListener);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Add(IRagonAuthorizationListener listener)
|
public void Add(IRagonAuthorizationListener listener)
|
||||||
{
|
{
|
||||||
_authorizationListeners.Add(listener);
|
_authorizationListeners.Add(listener);
|
||||||
@@ -139,11 +133,6 @@ namespace Ragon.Client
|
|||||||
_playerUserDataListeners.Add(listener);
|
_playerUserDataListeners.Add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(IRagonDataListener listener)
|
|
||||||
{
|
|
||||||
_delayedActions.Add(() => _dataListeners.Remove(listener));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Remove(IRagonAuthorizationListener listener)
|
public void Remove(IRagonAuthorizationListener listener)
|
||||||
{
|
{
|
||||||
_delayedActions.Add(() => _authorizationListeners.Remove(listener));
|
_delayedActions.Add(() => _authorizationListeners.Remove(listener));
|
||||||
@@ -259,12 +248,6 @@ namespace Ragon.Client
|
|||||||
listener.OnDisconnected(_client, disconnect);
|
listener.OnDisconnected(_client, disconnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnData(RagonPlayer player, byte[] data)
|
|
||||||
{
|
|
||||||
foreach (var listener in _dataListeners)
|
|
||||||
listener.OnData(_client, player, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnRoomList(RagonRoomInformation[] roomInfos)
|
public void OnRoomList(RagonRoomInformation[] roomInfos)
|
||||||
{
|
{
|
||||||
foreach (var listListener in _roomListListeners)
|
foreach (var listListener in _roomListListeners)
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client
|
namespace Ragon.Client
|
||||||
{
|
{
|
||||||
public class RagonRoom : IDisposable
|
public class RagonRoom
|
||||||
{
|
{
|
||||||
private class EventSubscription : IDisposable
|
private class EventSubscription : IDisposable
|
||||||
{
|
{
|
||||||
@@ -51,13 +51,12 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
private readonly RagonClient _client;
|
private readonly RagonClient _client;
|
||||||
private readonly RagonPlayerCache _playerCache;
|
private readonly RagonPlayerCache _playerCache;
|
||||||
private readonly RoomParameters _parameters;
|
private RoomParameters _parameters;
|
||||||
private readonly RagonUserData _userData;
|
private RagonUserData _userData;
|
||||||
|
|
||||||
public string Id => _parameters.RoomId;
|
public string Id => _parameters.RoomId;
|
||||||
public int MinPlayers => _parameters.Min;
|
public int MinPlayers => _parameters.Min;
|
||||||
public int MaxPlayers => _parameters.Max;
|
public int MaxPlayers => _parameters.Max;
|
||||||
public string Scene => "none";
|
|
||||||
|
|
||||||
public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
|
public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
|
||||||
public RagonPlayer Local => _playerCache.Local;
|
public RagonPlayer Local => _playerCache.Local;
|
||||||
@@ -69,27 +68,23 @@ namespace Ragon.Client
|
|||||||
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new();
|
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new();
|
||||||
|
|
||||||
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new();
|
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new();
|
||||||
|
|
||||||
public RagonRoom(RagonClient client,
|
public RagonRoom(RagonClient client, RagonPlayerCache playerCache)
|
||||||
RagonPlayerCache playerCache,
|
|
||||||
RoomParameters parameters)
|
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_parameters = parameters;
|
|
||||||
_playerCache = playerCache;
|
_playerCache = playerCache;
|
||||||
_userData = new RagonUserData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Cleanup()
|
public void Reset(RoomParameters parameters)
|
||||||
{
|
{
|
||||||
|
Clear();
|
||||||
|
|
||||||
|
_userData = new RagonUserData();
|
||||||
|
_parameters = parameters;
|
||||||
_playerCache.Cleanup();
|
_playerCache.Cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Update(string sceneName)
|
|
||||||
{
|
|
||||||
// _scene.Update(sceneName);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void HandleEvent(ushort eventCode, RagonPlayer caller, RagonStream buffer)
|
internal void HandleEvent(ushort eventCode, RagonPlayer caller, RagonStream buffer)
|
||||||
{
|
{
|
||||||
if (_events.TryGetValue(eventCode, out var evnt))
|
if (_events.TryGetValue(eventCode, out var evnt))
|
||||||
@@ -108,7 +103,7 @@ namespace Ragon.Client
|
|||||||
var t = new TEvent();
|
var t = new TEvent();
|
||||||
var eventCode = _client.Event.GetEventCode(t);
|
var eventCode = _client.Event.GetEventCode(t);
|
||||||
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
|
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
|
||||||
|
|
||||||
if (!_listeners.TryGetValue(eventCode, out var callbacks))
|
if (!_listeners.TryGetValue(eventCode, out var callbacks))
|
||||||
{
|
{
|
||||||
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
||||||
@@ -194,22 +189,8 @@ namespace Ragon.Client
|
|||||||
_client.Reliable.Send(sendData);
|
_client.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ReplicateData(byte[] data, bool reliable)
|
public void Clear()
|
||||||
{
|
{
|
||||||
var sendData = new byte[data.Length + 1];
|
|
||||||
sendData[0] = (byte)RagonOperation.REPLICATE_RAW_DATA;
|
|
||||||
Array.Copy(data, 0, sendData, 1, data.Length);
|
|
||||||
|
|
||||||
if (reliable)
|
|
||||||
_client.Reliable.Send(sendData);
|
|
||||||
else
|
|
||||||
_client.Unreliable.Send(sendData);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
Cleanup();
|
|
||||||
|
|
||||||
_events.Clear();
|
_events.Clear();
|
||||||
_listeners.Clear();
|
_listeners.Clear();
|
||||||
_localListeners.Clear();
|
_localListeners.Clear();
|
||||||
|
|||||||
@@ -21,20 +21,21 @@ namespace Ragon.Client
|
|||||||
public class RagonSession
|
public class RagonSession
|
||||||
{
|
{
|
||||||
private readonly RagonClient _client;
|
private readonly RagonClient _client;
|
||||||
private readonly RagonStream _buffer;
|
|
||||||
|
|
||||||
|
private readonly RagonStream _buffer;
|
||||||
|
|
||||||
public RagonSession(RagonClient client, RagonStream buffer)
|
public RagonSession(RagonClient client, RagonStream buffer)
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_buffer = buffer;
|
_buffer = buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CreateOrJoin(string sceneName, int minPlayers, int maxPlayers)
|
public void CreateOrJoin(string sessionName, int minPlayers, int maxPlayers)
|
||||||
{
|
{
|
||||||
var parameters = new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers};
|
var parameters = new RagonRoomParameters() { Min = minPlayers, Max = maxPlayers };
|
||||||
CreateOrJoin(parameters);
|
CreateOrJoin(parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CreateOrJoin(RagonRoomParameters parameters)
|
public void CreateOrJoin(RagonRoomParameters parameters)
|
||||||
{
|
{
|
||||||
_buffer.Clear();
|
_buffer.Clear();
|
||||||
@@ -46,16 +47,16 @@ namespace Ragon.Client
|
|||||||
_client.Reliable.Send(sendData);
|
_client.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Create(string sceneName, int minPlayers, int maxPlayers)
|
public void Create(string sessionName, int minPlayers, int maxPlayers)
|
||||||
{
|
{
|
||||||
Create(null, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
|
Create(null, new RagonRoomParameters() { Min = minPlayers, Max = maxPlayers });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Create(string roomId, string sceneName, int minPlayers, int maxPlayers)
|
public void Create(string roomId, string sessionName, int minPlayers, int maxPlayers)
|
||||||
{
|
{
|
||||||
Create(roomId, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
|
Create(roomId, new RagonRoomParameters() { Min = minPlayers, Max = maxPlayers });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Create(string roomId, RagonRoomParameters parameters)
|
public void Create(string roomId, RagonRoomParameters parameters)
|
||||||
{
|
{
|
||||||
_buffer.Clear();
|
_buffer.Clear();
|
||||||
@@ -76,14 +77,14 @@ namespace Ragon.Client
|
|||||||
var sendData = _buffer.ToArray();
|
var sendData = _buffer.ToArray();
|
||||||
_client.Reliable.Send(sendData);
|
_client.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Leave()
|
public void Leave()
|
||||||
{
|
{
|
||||||
var sendData = new[] {(byte) RagonOperation.LEAVE_ROOM};
|
var sendData = new[] { (byte)RagonOperation.LEAVE_ROOM };
|
||||||
_client.Reliable.Send(sendData);
|
_client.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Join(string roomId)
|
public void Join(string roomId)
|
||||||
{
|
{
|
||||||
_buffer.Clear();
|
_buffer.Clear();
|
||||||
_buffer.WriteOperation(RagonOperation.JOIN_ROOM);
|
_buffer.WriteOperation(RagonOperation.JOIN_ROOM);
|
||||||
@@ -93,7 +94,7 @@ namespace Ragon.Client
|
|||||||
_client.Reliable.Send(sendData);
|
_client.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AuthorizeWithKey(string key, string playerName, string payload = "")
|
public void AuthorizeWithKey(string key, string playerName, string payload = "")
|
||||||
{
|
{
|
||||||
_buffer.Clear();
|
_buffer.Clear();
|
||||||
_buffer.WriteOperation(RagonOperation.AUTHORIZE);
|
_buffer.WriteOperation(RagonOperation.AUTHORIZE);
|
||||||
@@ -104,6 +105,5 @@ namespace Ragon.Client
|
|||||||
var sendData = _buffer.ToArray();
|
var sendData = _buffer.ToArray();
|
||||||
_client.Reliable.Send(sendData);
|
_client.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
namespace Ragon.Client
|
namespace Ragon.Client
|
||||||
{
|
{
|
||||||
public enum RagonStatus
|
public enum RagonState
|
||||||
{
|
{
|
||||||
DISCONNECTED,
|
DISCONNECTED,
|
||||||
CONNECTED,
|
CONNECTED,
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
namespace Ragon.Protocol
|
namespace Ragon.Protocol
|
||||||
{
|
{
|
||||||
public enum RagonOperation: byte
|
public enum RagonOperation : byte
|
||||||
{
|
{
|
||||||
AUTHORIZE = 1,
|
AUTHORIZE = 1,
|
||||||
AUTHORIZED_SUCCESS = 2,
|
AUTHORIZED_SUCCESS = 2,
|
||||||
@@ -26,23 +26,13 @@ namespace Ragon.Protocol
|
|||||||
CREATE_ROOM = 5,
|
CREATE_ROOM = 5,
|
||||||
JOIN_ROOM = 6,
|
JOIN_ROOM = 6,
|
||||||
LEAVE_ROOM = 7,
|
LEAVE_ROOM = 7,
|
||||||
OWNERSHIP_ENTITY_CHANGED = 8,
|
OWNERSHIP_ROOM_CHANGED = 9,
|
||||||
OWNERSHIP_ROOM_CHANGED= 9,
|
|
||||||
JOIN_SUCCESS = 10,
|
JOIN_SUCCESS = 10,
|
||||||
JOIN_FAILED = 11,
|
JOIN_FAILED = 11,
|
||||||
LOAD_SCENE = 12,
|
|
||||||
SCENE_LOADED = 13,
|
|
||||||
PLAYER_JOINED = 14,
|
PLAYER_JOINED = 14,
|
||||||
PLAYER_LEAVED = 15,
|
PLAYER_LEAVED = 15,
|
||||||
CREATE_ENTITY = 16,
|
|
||||||
REMOVE_ENTITY = 17,
|
|
||||||
SNAPSHOT = 18,
|
|
||||||
REPLICATE_ENTITY_STATE = 19,
|
|
||||||
REPLICATE_ENTITY_EVENT = 20,
|
|
||||||
REPLICATE_RAW_DATA = 21,
|
|
||||||
REPLICATE_ROOM_EVENT = 22,
|
REPLICATE_ROOM_EVENT = 22,
|
||||||
TRANSFER_ROOM_OWNERSHIP = 23,
|
TRANSFER_ROOM_OWNERSHIP = 23,
|
||||||
TRANSFER_ENTITY_OWNERSHIP = 24,
|
|
||||||
TIMESTAMP_SYNCHRONIZATION = 25,
|
TIMESTAMP_SYNCHRONIZATION = 25,
|
||||||
ROOM_LIST_UPDATED = 26,
|
ROOM_LIST_UPDATED = 26,
|
||||||
PLAYER_DATA_UPDATED = 27,
|
PLAYER_DATA_UPDATED = 27,
|
||||||
|
|||||||
@@ -19,20 +19,17 @@ namespace Ragon.Protocol
|
|||||||
{
|
{
|
||||||
public class RagonRoomParameters
|
public class RagonRoomParameters
|
||||||
{
|
{
|
||||||
public string Scene { get; set; }
|
|
||||||
public int Min { get; set; }
|
public int Min { get; set; }
|
||||||
public int Max { get; set; }
|
public int Max { get; set; }
|
||||||
|
|
||||||
public void Serialize(RagonStream buffer)
|
public void Serialize(RagonStream buffer)
|
||||||
{
|
{
|
||||||
buffer.WriteString(Scene);
|
|
||||||
buffer.WriteInt(Min);
|
buffer.WriteInt(Min);
|
||||||
buffer.WriteInt(Max);
|
buffer.WriteInt(Max);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Deserialize(RagonStream buffer)
|
public void Deserialize(RagonStream buffer)
|
||||||
{
|
{
|
||||||
Scene = buffer.ReadString();
|
|
||||||
Min = buffer.ReadInt();
|
Min = buffer.ReadInt();
|
||||||
Max = buffer.ReadInt();
|
Max = buffer.ReadInt();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,8 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="ENet-CSharp" Version="2.4.8" />
|
<PackageReference Include="ENet-CSharp" Version="2.4.8" />
|
||||||
|
<PackageReference Include="Google.Protobuf" Version="3.29.0" />
|
||||||
|
<PackageReference Include="MsgPack.Cli" Version="1.0.1" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
<PackageReference Include="NLog" Version="5.3.2" />
|
<PackageReference Include="NLog" Version="5.3.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -9,10 +9,11 @@ public class RelayRoomPlugin: BaseRoomPlugin
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnAttached()
|
public void OnAttached()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Room attached");
|
Console.WriteLine("Room attached");
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnDetached()
|
public void OnDetached()
|
||||||
|
|||||||
@@ -1,10 +1,35 @@
|
|||||||
using Ragon.Server;
|
using Ragon.Server;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
using Ragon.Server.Plugin;
|
using Ragon.Server.Plugin;
|
||||||
|
using Ragon.Server.Time;
|
||||||
|
|
||||||
namespace Ragon.Relay
|
namespace Ragon.Relay
|
||||||
{
|
{
|
||||||
public class RelayServerPlugin : BaseServerPlugin
|
public class RelayServerPlugin : BaseServerPlugin
|
||||||
{
|
{
|
||||||
|
private RelayConfiguration _relayConfiguration;
|
||||||
|
private RagonScheduler _scheduler;
|
||||||
|
private RagonConnectionRegistry _connectionRegistry;
|
||||||
|
private IRagonLobby _lobby;
|
||||||
|
private Reporter _reporter;
|
||||||
|
|
||||||
|
public RelayServerPlugin(RelayConfiguration config)
|
||||||
|
{
|
||||||
|
_relayConfiguration = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnAttached(IRagonServer server)
|
||||||
|
{
|
||||||
|
base.OnAttached(server);
|
||||||
|
|
||||||
|
_lobby = server.Lobby;
|
||||||
|
_connectionRegistry = server.ConnectionRegistry;
|
||||||
|
_scheduler = server.Scheduler;
|
||||||
|
_reporter = new Reporter(_relayConfiguration, server, "127.0.0.1", 5000);
|
||||||
|
|
||||||
|
server.Scheduler.Run(new RagonActionTimer(() => _reporter.Done(), 1, -1));
|
||||||
|
}
|
||||||
|
|
||||||
public override bool OnCommand(string command, string payload)
|
public override bool OnCommand(string command, string payload)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using Ragon.Server.Logging;
|
||||||
|
|
||||||
|
namespace Ragon.Relay;
|
||||||
|
|
||||||
|
public class Client
|
||||||
|
{
|
||||||
|
private readonly UdpClient _udpClient;
|
||||||
|
private readonly IPEndPoint _endpoint;
|
||||||
|
private readonly IRagonLogger _logger;
|
||||||
|
|
||||||
|
public Client(string host, int port)
|
||||||
|
{
|
||||||
|
_logger = LoggerManager.GetLogger("Client");
|
||||||
|
_udpClient = new UdpClient();
|
||||||
|
_endpoint = new IPEndPoint(IPAddress.Parse(host), port);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Send(byte[] data)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_udpClient.BeginSend(data, data.Length, _endpoint, SendCallback, null);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SendCallback(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_udpClient.EndSend(ar);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,56 @@
|
|||||||
|
using Google.Protobuf;
|
||||||
|
using Google.Protobuf.Collections;
|
||||||
|
using Ragon.Server;
|
||||||
|
|
||||||
|
namespace Ragon.Relay;
|
||||||
|
|
||||||
|
public class Reporter
|
||||||
|
{
|
||||||
|
private readonly Client _client;
|
||||||
|
private readonly IRagonServer _server;
|
||||||
|
private readonly RelayConfiguration _configuration;
|
||||||
|
|
||||||
|
public Reporter(
|
||||||
|
RelayConfiguration relayConfiguration,
|
||||||
|
IRagonServer server,
|
||||||
|
string host,
|
||||||
|
int port
|
||||||
|
)
|
||||||
|
{
|
||||||
|
_client = new Client(host, port);
|
||||||
|
_server = server;
|
||||||
|
_configuration = relayConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Done()
|
||||||
|
{
|
||||||
|
for (var i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
var message = new Data();
|
||||||
|
message.Statistics = new Statistics()
|
||||||
|
{
|
||||||
|
Connections = _server.ConnectionRegistry.Contexts.Count,
|
||||||
|
ConnectionsLimit = _configuration.LimitConnections,
|
||||||
|
Rooms = _server.Lobby.Rooms.Count,
|
||||||
|
RoomsLimit = _configuration.LimitRooms,
|
||||||
|
};
|
||||||
|
|
||||||
|
var room = new Room()
|
||||||
|
{
|
||||||
|
Id = $"Room ID {i}",
|
||||||
|
};
|
||||||
|
|
||||||
|
for (var j = 0; j < 10; j++)
|
||||||
|
{
|
||||||
|
room.Players.Add(new Player()
|
||||||
|
{
|
||||||
|
Id = $"Player ID {i}",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
message.Room = room;
|
||||||
|
|
||||||
|
_client.Send(message.ToByteArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -158,13 +158,11 @@ public class WebSocketServer : INetworkServer
|
|||||||
_sequencer.Push(0);
|
_sequencer.Push(0);
|
||||||
|
|
||||||
_connections = new WebSocketConnection[configuration.LimitConnections];
|
_connections = new WebSocketConnection[configuration.LimitConnections];
|
||||||
|
|
||||||
_httpListener = new HttpListener();
|
_httpListener = new HttpListener();
|
||||||
_httpListener.Prefixes.Add($"http://{configuration.Address}:{configuration.Port}/");
|
_httpListener.Prefixes.Add($"http://{configuration.Address}:{configuration.Port}/");
|
||||||
_httpListener.Start();
|
_httpListener.Start();
|
||||||
|
|
||||||
// _executor.Run(() => StartAccept(_cancellationTokenSource.Token));
|
|
||||||
|
|
||||||
var protocolDecoded = RagonVersion.Parse(configuration.Protocol);
|
var protocolDecoded = RagonVersion.Parse(configuration.Protocol);
|
||||||
_logger.Info($"Listen at http://{configuration.Address}:{configuration.Port}/");
|
_logger.Info($"Listen at http://{configuration.Address}:{configuration.Port}/");
|
||||||
_logger.Info($"Protocol: {protocolDecoded}");
|
_logger.Info($"Protocol: {protocolDecoded}");
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ public class RagonEvent
|
|||||||
|
|
||||||
public void Read(RagonStream buffer)
|
public void Read(RagonStream buffer)
|
||||||
{
|
{
|
||||||
// _size = buffer.Capacity;
|
// _size = buffer.Capacity;
|
||||||
// buffer.ReadArray(_data, _size);
|
// buffer.ReadArray(_data, _size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Write(RagonStream buffer)
|
public void Write(RagonStream buffer)
|
||||||
|
|||||||
@@ -76,11 +76,10 @@ namespace Ragon.Server.Handler
|
|||||||
|
|
||||||
var information = new RoomInformation()
|
var information = new RoomInformation()
|
||||||
{
|
{
|
||||||
Scene = _roomParameters.Scene,
|
|
||||||
Max = _roomParameters.Max,
|
Max = _roomParameters.Max,
|
||||||
Min = _roomParameters.Min,
|
Min = _roomParameters.Min,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (information.Max > _configuration.LimitPlayersPerRoom)
|
if (information.Max > _configuration.LimitPlayersPerRoom)
|
||||||
information.Max = _configuration.LimitPlayersPerRoom;
|
information.Max = _configuration.LimitPlayersPerRoom;
|
||||||
|
|
||||||
@@ -97,8 +96,7 @@ namespace Ragon.Server.Handler
|
|||||||
context.Lobby.Persist(room);
|
context.Lobby.Persist(room);
|
||||||
context.SetRoom(room, roomPlayer);
|
context.SetRoom(room, roomPlayer);
|
||||||
|
|
||||||
_logger.Trace(
|
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id}");
|
||||||
$"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} with scene {information.Scene}");
|
|
||||||
|
|
||||||
JoinSuccess(roomPlayer, room, Writer);
|
JoinSuccess(roomPlayer, room, Writer);
|
||||||
|
|
||||||
@@ -114,7 +112,6 @@ namespace Ragon.Server.Handler
|
|||||||
writer.WriteString(room.Id);
|
writer.WriteString(room.Id);
|
||||||
writer.WriteUShort((ushort)room.PlayerMin);
|
writer.WriteUShort((ushort)room.PlayerMin);
|
||||||
writer.WriteUShort((ushort)room.PlayerMax);
|
writer.WriteUShort((ushort)room.PlayerMax);
|
||||||
writer.WriteString(room.Scene);
|
|
||||||
writer.WriteString(player.Id);
|
writer.WriteString(player.Id);
|
||||||
writer.WriteString(room.Owner.Id);
|
writer.WriteString(room.Owner.Id);
|
||||||
|
|
||||||
|
|||||||
@@ -1,52 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
using Ragon.Protocol;
|
|
||||||
using Ragon.Server.IO;
|
|
||||||
|
|
||||||
namespace Ragon.Server.Handler;
|
|
||||||
|
|
||||||
public sealed class RoomDataOperation : BaseOperation
|
|
||||||
{
|
|
||||||
public RoomDataOperation(RagonStream reader, RagonStream writer) : base(reader, writer)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Handle(RagonContext context, NetworkChannel channel)
|
|
||||||
{
|
|
||||||
var player = context.RoomPlayer;
|
|
||||||
var room = context.Room;
|
|
||||||
|
|
||||||
|
|
||||||
var data = Reader.ReadBinary(Reader.Lenght);
|
|
||||||
var dataSize = data.Length - 1;
|
|
||||||
var headerSize = 3;
|
|
||||||
var size = headerSize + dataSize;
|
|
||||||
var sendData = new byte[size];
|
|
||||||
var peerId = player.Connection.Id;
|
|
||||||
|
|
||||||
sendData[0] = (byte)RagonOperation.REPLICATE_RAW_DATA;
|
|
||||||
sendData[1] = (byte)peerId;
|
|
||||||
sendData[2] = (byte)(peerId >> 8);
|
|
||||||
|
|
||||||
var pluginData = new byte[dataSize];
|
|
||||||
Array.Copy(data, 1, pluginData, 0, dataSize);
|
|
||||||
room.Plugin.OnData(player, pluginData);
|
|
||||||
|
|
||||||
Array.Copy(data, 1, sendData, headerSize, dataSize);
|
|
||||||
room.Broadcast(sendData, room.ReadyPlayersList, NetworkChannel.RELIABLE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -61,7 +61,6 @@ public sealed class RoomJoinOperation : BaseOperation
|
|||||||
writer.WriteString(room.Id);
|
writer.WriteString(room.Id);
|
||||||
writer.WriteUShort((ushort)room.PlayerMin);
|
writer.WriteUShort((ushort)room.PlayerMin);
|
||||||
writer.WriteUShort((ushort)room.PlayerMax);
|
writer.WriteUShort((ushort)room.PlayerMax);
|
||||||
writer.WriteString(room.Scene);
|
|
||||||
writer.WriteString(context.RoomPlayer.Id);
|
writer.WriteString(context.RoomPlayer.Id);
|
||||||
writer.WriteString(room.Owner.Id);
|
writer.WriteString(room.Owner.Id);
|
||||||
|
|
||||||
|
|||||||
@@ -30,11 +30,11 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
|
|||||||
private readonly RagonRoomParameters _roomParameters = new();
|
private readonly RagonRoomParameters _roomParameters = new();
|
||||||
private readonly IServerPlugin _serverPlugin;
|
private readonly IServerPlugin _serverPlugin;
|
||||||
private readonly RagonServerConfiguration _configuration;
|
private readonly RagonServerConfiguration _configuration;
|
||||||
|
|
||||||
public RoomJoinOrCreateOperation(
|
public RoomJoinOrCreateOperation(
|
||||||
RagonStream reader,
|
RagonStream reader,
|
||||||
RagonStream writer,
|
RagonStream writer,
|
||||||
IServerPlugin serverPlugin,
|
IServerPlugin serverPlugin,
|
||||||
RagonServerConfiguration configuration
|
RagonServerConfiguration configuration
|
||||||
) : base(reader, writer)
|
) : base(reader, writer)
|
||||||
{
|
{
|
||||||
@@ -52,52 +52,35 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
|
|||||||
|
|
||||||
var roomId = Guid.NewGuid().ToString();
|
var roomId = Guid.NewGuid().ToString();
|
||||||
var lobbyPlayer = context.LobbyPlayer;
|
var lobbyPlayer = context.LobbyPlayer;
|
||||||
|
|
||||||
_roomParameters.Deserialize(Reader);
|
_roomParameters.Deserialize(Reader);
|
||||||
|
|
||||||
if (context.Lobby.FindRoomByScene(_roomParameters.Scene, out var existsRoom))
|
var information = new RoomInformation()
|
||||||
{
|
{
|
||||||
var player = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
|
Max = _roomParameters.Max,
|
||||||
|
Min = _roomParameters.Min,
|
||||||
|
};
|
||||||
|
|
||||||
context.SetRoom(existsRoom, player);
|
if (information.Max > _configuration.LimitPlayersPerRoom)
|
||||||
|
information.Max = _configuration.LimitPlayersPerRoom;
|
||||||
|
|
||||||
JoinSuccess(player, existsRoom, Writer);
|
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||||
|
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
|
||||||
|
var room = new RagonRoom(roomId, information, roomPlugin);
|
||||||
|
|
||||||
existsRoom.RestoreBufferedEvents(player);
|
_serverPlugin.OnRoomCreate(lobbyPlayer, room);
|
||||||
|
|
||||||
existsRoom.Plugin.OnPlayerJoined(player);
|
room.Plugin.OnAttached(room);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var information = new RoomInformation()
|
|
||||||
{
|
|
||||||
Scene = _roomParameters.Scene,
|
|
||||||
Max = _roomParameters.Max,
|
|
||||||
Min = _roomParameters.Min,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (information.Max > _configuration.LimitPlayersPerRoom)
|
context.Lobby.Persist(room);
|
||||||
information.Max = _configuration.LimitPlayersPerRoom;
|
context.Scheduler.Run(room);
|
||||||
|
context.SetRoom(room, roomPlayer);
|
||||||
|
|
||||||
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
|
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id}");
|
||||||
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
|
|
||||||
var room = new RagonRoom(roomId, information, roomPlugin);
|
|
||||||
|
|
||||||
_serverPlugin.OnRoomCreate(lobbyPlayer, room);
|
JoinSuccess(roomPlayer, room, Writer);
|
||||||
|
|
||||||
room.Plugin.OnAttached(room);
|
room.Plugin.OnPlayerJoined(roomPlayer);
|
||||||
|
|
||||||
context.Lobby.Persist(room);
|
|
||||||
context.Scheduler.Run(room);
|
|
||||||
context.SetRoom(room, roomPlayer);
|
|
||||||
|
|
||||||
_logger.Trace(
|
|
||||||
$"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} with scene {information.Scene}");
|
|
||||||
|
|
||||||
JoinSuccess(roomPlayer, room, Writer);
|
|
||||||
|
|
||||||
room.Plugin.OnPlayerJoined(roomPlayer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonStream writer)
|
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonStream writer)
|
||||||
@@ -107,7 +90,6 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
|
|||||||
writer.WriteString(room.Id);
|
writer.WriteString(room.Id);
|
||||||
writer.WriteUShort((ushort)room.PlayerMin);
|
writer.WriteUShort((ushort)room.PlayerMin);
|
||||||
writer.WriteUShort((ushort)room.PlayerMax);
|
writer.WriteUShort((ushort)room.PlayerMax);
|
||||||
writer.WriteString(room.Scene);
|
|
||||||
writer.WriteString(player.Id);
|
writer.WriteString(player.Id);
|
||||||
writer.WriteString(room.Owner.Id);
|
writer.WriteString(room.Owner.Id);
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ public sealed class RoomLeaveOperation: BaseOperation
|
|||||||
|
|
||||||
public RoomLeaveOperation(RagonStream reader, RagonStream writer): base(reader, writer)
|
public RoomLeaveOperation(RagonStream reader, RagonStream writer): base(reader, writer)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Handle(RagonContext context, NetworkChannel channel)
|
public override void Handle(RagonContext context, NetworkChannel channel)
|
||||||
|
|||||||
@@ -18,14 +18,15 @@ namespace Ragon.Server;
|
|||||||
|
|
||||||
public class RagonContextObserver
|
public class RagonContextObserver
|
||||||
{
|
{
|
||||||
private Dictionary<string, RagonContext> _contexts;
|
private readonly RagonConnectionRegistry _registry;
|
||||||
public RagonContextObserver(Dictionary<string, RagonContext> contexts)
|
|
||||||
|
public RagonContextObserver(RagonConnectionRegistry registry)
|
||||||
{
|
{
|
||||||
_contexts = contexts;
|
_registry = registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnAuthorized(RagonContext context)
|
public void OnAuthorized(RagonContext context)
|
||||||
{
|
{
|
||||||
_contexts.Add(context.LobbyPlayer.Id, context);
|
_registry.Add(context.LobbyPlayer.Id, context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,14 +16,15 @@
|
|||||||
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
using Ragon.Server.Handler;
|
using Ragon.Server.Handler;
|
||||||
using Ragon.Server.IO;
|
|
||||||
using Ragon.Server.Lobby;
|
using Ragon.Server.Lobby;
|
||||||
|
using Ragon.Server.Time;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server;
|
||||||
|
|
||||||
public interface IRagonServer
|
public interface IRagonServer
|
||||||
{
|
{
|
||||||
BaseOperation ResolveHandler(RagonOperation operation);
|
BaseOperation ResolveHandler(RagonOperation operation);
|
||||||
public RagonContext? GetContextByConnectionId(ushort peerId);
|
public RagonConnectionRegistry ConnectionRegistry { get; }
|
||||||
public RagonContext? GetContextById(string playerId);
|
public RagonScheduler Scheduler { get; }
|
||||||
|
public IRagonLobby Lobby { get; }
|
||||||
}
|
}
|
||||||
@@ -23,7 +23,6 @@ public interface IRagonLobby
|
|||||||
{
|
{
|
||||||
public IReadOnlyList<IRagonRoom> Rooms { get; }
|
public IReadOnlyList<IRagonRoom> Rooms { get; }
|
||||||
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room);
|
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room);
|
||||||
public bool FindRoomByScene(string sceneName, [MaybeNullWhen(false)] out RagonRoom room);
|
|
||||||
public void Persist(RagonRoom room);
|
public void Persist(RagonRoom room);
|
||||||
public bool RemoveIfEmpty(RagonRoom room);
|
public bool RemoveIfEmpty(RagonRoom room);
|
||||||
}
|
}
|
||||||
@@ -24,7 +24,6 @@ public class RagonLobbyDispatcher
|
|||||||
var room = rooms[i];
|
var room = rooms[i];
|
||||||
|
|
||||||
writer.WriteString(room.Id);
|
writer.WriteString(room.Id);
|
||||||
writer.WriteString(room.Scene);
|
|
||||||
writer.WriteUShort((ushort)room.PlayerMax);
|
writer.WriteUShort((ushort)room.PlayerMax);
|
||||||
writer.WriteUShort((ushort)room.PlayerMin);
|
writer.WriteUShort((ushort)room.PlayerMin);
|
||||||
writer.WriteUShort((ushort)room.PlayerCount);
|
writer.WriteUShort((ushort)room.PlayerCount);
|
||||||
|
|||||||
@@ -49,35 +49,7 @@ public class LobbyInMemory : IRagonLobby
|
|||||||
room = default;
|
room = default;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool FindRoomByScene(string sceneName, [MaybeNullWhen(false)] out RagonRoom room)
|
|
||||||
{
|
|
||||||
foreach (var existsRoom in _rooms)
|
|
||||||
{
|
|
||||||
if (existsRoom.Scene == sceneName)
|
|
||||||
{
|
|
||||||
if (existsRoom.PlayerCount >= existsRoom.PlayerMax)
|
|
||||||
{
|
|
||||||
_logger.Warning($"Room with scene {sceneName} fulfilled");
|
|
||||||
|
|
||||||
room = default;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
room = existsRoom;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
room = default;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool FindRoomByProperties(Dictionary<string, object> props)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Persist(RagonRoom room)
|
public void Persist(RagonRoom room)
|
||||||
{
|
{
|
||||||
room.Attach();
|
room.Attach();
|
||||||
@@ -86,7 +58,7 @@ public class LobbyInMemory : IRagonLobby
|
|||||||
_logger.Trace($"New room: {room.Id}");
|
_logger.Trace($"New room: {room.Id}");
|
||||||
|
|
||||||
foreach (var r in _rooms)
|
foreach (var r in _rooms)
|
||||||
_logger.Trace($"Room: {r.Id} Scene: {r.Scene} Players: {r.Players.Count}");
|
_logger.Trace($"Room: {r.Id} ID: {r.Id} Players: {r.Players.Count}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool RemoveIfEmpty(RagonRoom room)
|
public bool RemoveIfEmpty(RagonRoom room)
|
||||||
@@ -103,7 +75,7 @@ public class LobbyInMemory : IRagonLobby
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach (var r in _rooms)
|
foreach (var r in _rooms)
|
||||||
_logger.Trace($"Room: {r.Id} Scene: {r.Scene} Players: {r.Players.Count}");
|
_logger.Trace($"Room: {r.Id} ID: {r.Id} Players: {r.Players.Count}");
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
using Ragon.Server.Handler;
|
using Ragon.Server.Handler;
|
||||||
using Ragon.Server.IO;
|
|
||||||
using Ragon.Server.Lobby;
|
using Ragon.Server.Lobby;
|
||||||
using Ragon.Server.Room;
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
@@ -42,7 +41,7 @@ namespace Ragon.Server.Plugin
|
|||||||
|
|
||||||
public void Approve(string id, string name, string payload)
|
public void Approve(string id, string name, string payload)
|
||||||
{
|
{
|
||||||
var ctx = _server.GetContextByConnectionId(PeerID);
|
var ctx = _server.ConnectionRegistry.GetContextByConnectionId(PeerID);
|
||||||
if (ctx == null)
|
if (ctx == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -52,7 +51,7 @@ namespace Ragon.Server.Plugin
|
|||||||
|
|
||||||
public void Reject()
|
public void Reject()
|
||||||
{
|
{
|
||||||
var ctx = _server.GetContextByConnectionId(PeerID);
|
var ctx = _server.ConnectionRegistry.GetContextByConnectionId(PeerID);
|
||||||
if (ctx == null)
|
if (ctx == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -80,7 +79,9 @@ namespace Ragon.Server.Plugin
|
|||||||
{
|
{
|
||||||
public IRagonServer Server { get; protected set; }
|
public IRagonServer Server { get; protected set; }
|
||||||
|
|
||||||
public virtual void OnAttached(IRagonServer server)
|
public virtual void OnAttached(
|
||||||
|
IRagonServer server
|
||||||
|
)
|
||||||
{
|
{
|
||||||
Server = server;
|
Server = server;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
using Ragon.Server.Lobby;
|
using Ragon.Server.Lobby;
|
||||||
using Ragon.Server.Room;
|
using Ragon.Server.Room;
|
||||||
|
using Ragon.Server.Time;
|
||||||
|
|
||||||
namespace Ragon.Server.Plugin
|
namespace Ragon.Server.Plugin
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
namespace Ragon.Server;
|
||||||
|
|
||||||
|
public class RagonConnectionRegistry
|
||||||
|
{
|
||||||
|
private readonly Dictionary<ushort, RagonContext> _contextsByConnection = new();
|
||||||
|
private readonly Dictionary<string, RagonContext> _contextsByPlayerId = new();
|
||||||
|
private readonly List<RagonContext> _contexts = new();
|
||||||
|
private readonly List<RagonContext> _playerContexts = new();
|
||||||
|
|
||||||
|
public void Add(string playerId, RagonContext context)
|
||||||
|
{
|
||||||
|
_contextsByPlayerId.Add(playerId, context);
|
||||||
|
_playerContexts.Add(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(ushort connectionId, RagonContext context)
|
||||||
|
{
|
||||||
|
_contextsByConnection.Add(connectionId, context);
|
||||||
|
_contexts.Add(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public bool Remove(ushort connectionId, out RagonContext o)
|
||||||
|
{
|
||||||
|
if (_contextsByConnection.Remove(connectionId, out var context))
|
||||||
|
{
|
||||||
|
_contexts.Remove(context);
|
||||||
|
o = context;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
o = null;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove(string playerId)
|
||||||
|
{
|
||||||
|
if (_contextsByPlayerId.Remove(playerId, out var context))
|
||||||
|
{
|
||||||
|
_playerContexts.Remove(context);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetValue(ushort connectionId, out RagonContext o)
|
||||||
|
{
|
||||||
|
return _contextsByConnection.TryGetValue(connectionId, out o);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IReadOnlyList<RagonContext> Contexts => _contexts;
|
||||||
|
public IReadOnlyList<RagonContext> PlayerContexts => _playerContexts;
|
||||||
|
|
||||||
|
public RagonContext? GetContextByConnectionId(ushort peerId) => _contextsByConnection.GetValueOrDefault(peerId);
|
||||||
|
public RagonContext? GetContextById(string playerId) => _contextsByPlayerId.GetValueOrDefault(playerId);
|
||||||
|
}
|
||||||
@@ -38,8 +38,7 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
private readonly RagonStream _reader;
|
private readonly RagonStream _reader;
|
||||||
private readonly RagonStream _writer;
|
private readonly RagonStream _writer;
|
||||||
private readonly RagonScheduler _scheduler;
|
private readonly RagonScheduler _scheduler;
|
||||||
private readonly Dictionary<ushort, RagonContext> _contextsByConnection;
|
private readonly RagonConnectionRegistry _connectionRegistry;
|
||||||
private readonly Dictionary<string, RagonContext> _contextsByPlayerId;
|
|
||||||
private readonly Stopwatch _timer;
|
private readonly Stopwatch _timer;
|
||||||
private readonly RagonLobbyDispatcher _lobbySerializer;
|
private readonly RagonLobbyDispatcher _lobbySerializer;
|
||||||
private readonly long _tickRate = 0;
|
private readonly long _tickRate = 0;
|
||||||
@@ -55,8 +54,7 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
_server = server;
|
_server = server;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
_serverPlugin = plugin;
|
_serverPlugin = plugin;
|
||||||
_contextsByConnection = new Dictionary<ushort, RagonContext>();
|
_connectionRegistry = new RagonConnectionRegistry();
|
||||||
_contextsByPlayerId = new Dictionary<string, RagonContext>();
|
|
||||||
_lobby = new LobbyInMemory();
|
_lobby = new LobbyInMemory();
|
||||||
_lobbySerializer = new RagonLobbyDispatcher(_lobby);
|
_lobbySerializer = new RagonLobbyDispatcher(_lobby);
|
||||||
_scheduler = new RagonScheduler();
|
_scheduler = new RagonScheduler();
|
||||||
@@ -65,13 +63,11 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
_tickRate = 1000 / _configuration.ServerTickRate;
|
_tickRate = 1000 / _configuration.ServerTickRate;
|
||||||
_timer = new Stopwatch();
|
_timer = new Stopwatch();
|
||||||
|
|
||||||
var contextObserver = new RagonContextObserver(_contextsByPlayerId);
|
var contextObserver = new RagonContextObserver(_connectionRegistry);
|
||||||
_scheduler.Run(new RagonActionTimer(SendRoomList, 2.0f));
|
_scheduler.Run(new RagonActionTimer(SendRoomList, 2.0f));
|
||||||
_scheduler.Run(new RagonActionTimer(SendPlayerUserData, 0.1f));
|
_scheduler.Run(new RagonActionTimer(SendPlayerUserData, 0.1f));
|
||||||
_scheduler.Run(new RagonActionTimer(SendRoomUserData, 0.1f));
|
_scheduler.Run(new RagonActionTimer(SendRoomUserData, 0.1f));
|
||||||
|
|
||||||
_serverPlugin.OnAttached(this);
|
|
||||||
|
|
||||||
_handlers = new BaseOperation[byte.MaxValue];
|
_handlers = new BaseOperation[byte.MaxValue];
|
||||||
_handlers[(byte)RagonOperation.AUTHORIZE] = new AuthorizationOperation(_reader, _writer, this, _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);
|
||||||
@@ -80,7 +76,6 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(_reader, _writer);
|
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(_reader, _writer);
|
||||||
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampSyncOperation(_reader, _writer);
|
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampSyncOperation(_reader, _writer);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventOperation(_reader, _writer);
|
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventOperation(_reader, _writer);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataOperation(_reader, _writer);
|
|
||||||
_handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataOperation(_reader, _writer, _configuration.LimitUserDataSize);
|
_handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataOperation(_reader, _writer, _configuration.LimitUserDataSize);
|
||||||
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataOperation(_reader, _writer, _configuration.LimitUserDataSize);
|
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataOperation(_reader, _writer, _configuration.LimitUserDataSize);
|
||||||
}
|
}
|
||||||
@@ -133,12 +128,12 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
var context = new RagonContext(connection, _lobby, _scheduler, _configuration.LimitBufferedEvents);
|
var context = new RagonContext(connection, _lobby, _scheduler, _configuration.LimitBufferedEvents);
|
||||||
|
|
||||||
_logger.Trace($"Connected: {connection.Id}");
|
_logger.Trace($"Connected: {connection.Id}");
|
||||||
_contextsByConnection.Add(connection.Id, context);
|
_connectionRegistry.Add(connection.Id, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnDisconnected(INetworkConnection connection)
|
public void OnDisconnected(INetworkConnection connection)
|
||||||
{
|
{
|
||||||
if (_contextsByConnection.Remove(connection.Id, out var context))
|
if (_connectionRegistry.Remove(connection.Id, out var context))
|
||||||
{
|
{
|
||||||
var room = context.Room;
|
var room = context.Room;
|
||||||
if (room != null)
|
if (room != null)
|
||||||
@@ -149,7 +144,7 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (context.ConnectionStatus == ConnectionStatus.Authorized)
|
if (context.ConnectionStatus == ConnectionStatus.Authorized)
|
||||||
_contextsByPlayerId.Remove(context.LobbyPlayer.Id);
|
_connectionRegistry.Remove(context.LobbyPlayer.Id);
|
||||||
|
|
||||||
_logger.Trace($"Disconnected: {connection.Id}");
|
_logger.Trace($"Disconnected: {connection.Id}");
|
||||||
}
|
}
|
||||||
@@ -161,7 +156,7 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
|
|
||||||
public void OnTimeout(INetworkConnection connection)
|
public void OnTimeout(INetworkConnection connection)
|
||||||
{
|
{
|
||||||
if (_contextsByConnection.Remove(connection.Id, out var context) && context.ConnectionStatus == ConnectionStatus.Authorized)
|
if (_connectionRegistry.Remove(connection.Id, out var context) && context.ConnectionStatus == ConnectionStatus.Authorized)
|
||||||
{
|
{
|
||||||
var room = context.Room;
|
var room = context.Room;
|
||||||
if (room != null)
|
if (room != null)
|
||||||
@@ -171,7 +166,7 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (context.ConnectionStatus == ConnectionStatus.Authorized)
|
if (context.ConnectionStatus == ConnectionStatus.Authorized)
|
||||||
_contextsByPlayerId.Remove(context.LobbyPlayer.Id);
|
_connectionRegistry.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}");
|
||||||
}
|
}
|
||||||
@@ -185,7 +180,7 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (_contextsByConnection.TryGetValue(connection.Id, out var context))
|
if (_connectionRegistry.TryGetValue(connection.Id, out var context))
|
||||||
{
|
{
|
||||||
_writer.Clear();
|
_writer.Clear();
|
||||||
_reader.Clear();
|
_reader.Clear();
|
||||||
@@ -223,24 +218,24 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
_lobbySerializer.Write(_writer);
|
_lobbySerializer.Write(_writer);
|
||||||
|
|
||||||
var sendData = _writer.ToArray();
|
var sendData = _writer.ToArray();
|
||||||
foreach (var (_, value) in _contextsByPlayerId)
|
foreach (var ctx in _connectionRegistry.Contexts)
|
||||||
{
|
{
|
||||||
if (value.Room == null) // If only in lobby, then send room list data
|
if (ctx.Room == null) // If only in lobby, then send room list data
|
||||||
value.Connection.Reliable.Send(sendData);
|
ctx.Connection.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendPlayerUserData()
|
public void SendPlayerUserData()
|
||||||
{
|
{
|
||||||
foreach (var (_, value) in _contextsByPlayerId)
|
foreach (var playerContext in _connectionRegistry.PlayerContexts)
|
||||||
{
|
{
|
||||||
if (value.UserData.IsDirty)
|
if (playerContext.UserData.IsDirty)
|
||||||
{
|
{
|
||||||
_writer.Clear();
|
_writer.Clear();
|
||||||
_writer.WriteOperation(RagonOperation.PLAYER_DATA_UPDATED);
|
_writer.WriteOperation(RagonOperation.PLAYER_DATA_UPDATED);
|
||||||
_writer.WriteUShort(value.Connection.Id);
|
_writer.WriteUShort(playerContext.Connection.Id);
|
||||||
|
|
||||||
value.UserData.Write(_writer);
|
playerContext.UserData.Write(_writer);
|
||||||
|
|
||||||
var sendData = _writer.ToArray();
|
var sendData = _writer.ToArray();
|
||||||
_server.Broadcast(sendData, NetworkChannel.RELIABLE);
|
_server.Broadcast(sendData, NetworkChannel.RELIABLE);
|
||||||
@@ -264,22 +259,18 @@ public class RagonServer : IRagonServer, INetworkListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public BaseOperation ResolveHandler(RagonOperation operation)
|
public BaseOperation ResolveHandler(RagonOperation operation)
|
||||||
{
|
{
|
||||||
return _handlers[(byte)operation];
|
return _handlers[(byte)operation];
|
||||||
}
|
}
|
||||||
|
|
||||||
public RagonContext? GetContextByConnectionId(ushort peerId)
|
|
||||||
{
|
|
||||||
return _contextsByConnection.TryGetValue(peerId, out var context) ? context : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RagonContext? GetContextById(string playerId)
|
public RagonConnectionRegistry ConnectionRegistry => _connectionRegistry;
|
||||||
{
|
|
||||||
return _contextsByPlayerId.TryGetValue(playerId, out var context) ? context : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public RagonScheduler Scheduler => _scheduler;
|
||||||
|
|
||||||
|
public IRagonLobby Lobby => _lobby;
|
||||||
|
|
||||||
private void CopyrightInfo()
|
private void CopyrightInfo()
|
||||||
{
|
{
|
||||||
_logger.Info($"Server Version: {ServerVersion}");
|
_logger.Info($"Server Version: {ServerVersion}");
|
||||||
|
|||||||
@@ -22,17 +22,12 @@ namespace Ragon.Server.Room;
|
|||||||
public interface IRagonRoom
|
public interface IRagonRoom
|
||||||
{
|
{
|
||||||
public string Id { get; }
|
public string Id { get; }
|
||||||
public string Scene { get; }
|
|
||||||
public int PlayerMin { get; }
|
public int PlayerMin { get; }
|
||||||
public int PlayerMax { get; }
|
public int PlayerMax { get; }
|
||||||
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);
|
|
||||||
public void ReplicateData(RagonRoomPlayer invoker, byte[] data, List<RagonRoomPlayer> receivers,
|
|
||||||
NetworkChannel channel = NetworkChannel.RELIABLE);
|
|
||||||
|
|
||||||
RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection);
|
RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection);
|
||||||
RagonRoomPlayer GetPlayerById(string id);
|
RagonRoomPlayer GetPlayerById(string id);
|
||||||
|
IReadOnlyList<RagonRoomPlayer> GetPlayers();
|
||||||
}
|
}
|
||||||
@@ -26,10 +26,10 @@ namespace Ragon.Server.Room;
|
|||||||
public class RagonRoom : IRagonRoom, IRagonAction
|
public class RagonRoom : IRagonRoom, IRagonAction
|
||||||
{
|
{
|
||||||
public string Id { get; private set; }
|
public string Id { get; private set; }
|
||||||
public string Scene { get; private set; }
|
|
||||||
public int PlayerMax { get; private set; }
|
public int PlayerMax { get; private set; }
|
||||||
public int PlayerMin { get; private set; }
|
public int PlayerMin { get; private set; }
|
||||||
public int PlayerCount => WaitPlayersList.Count;
|
public int PlayerCount => WaitPlayersList.Count;
|
||||||
|
public IReadOnlyList<RagonRoomPlayer> GetPlayers() => PlayerList;
|
||||||
|
|
||||||
public bool IsDone { get; private set; }
|
public bool IsDone { get; private set; }
|
||||||
|
|
||||||
@@ -49,7 +49,6 @@ public class RagonRoom : IRagonRoom, IRagonAction
|
|||||||
public RagonRoom(string roomId, RoomInformation info, IRoomPlugin roomPlugin)
|
public RagonRoom(string roomId, RoomInformation info, IRoomPlugin roomPlugin)
|
||||||
{
|
{
|
||||||
Id = roomId;
|
Id = roomId;
|
||||||
Scene = info.Scene;
|
|
||||||
PlayerMax = info.Max;
|
PlayerMax = info.Max;
|
||||||
PlayerMin = info.Min;
|
PlayerMin = info.Min;
|
||||||
Plugin = roomPlugin;
|
Plugin = roomPlugin;
|
||||||
@@ -66,8 +65,6 @@ public class RagonRoom : IRagonRoom, IRagonAction
|
|||||||
Writer = new RagonStream();
|
Writer = new RagonStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer)
|
public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer)
|
||||||
{
|
{
|
||||||
foreach (var evnt in _bufferedEvents)
|
foreach (var evnt in _bufferedEvents)
|
||||||
@@ -163,47 +160,7 @@ 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 ReplicateData(RagonRoomPlayer invoker, 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 = invoker.Connection.Id;
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -247,16 +204,6 @@ public class RagonRoom : IRagonRoom, IRagonAction
|
|||||||
ReadyPlayersList = PlayerList.Where(p => p.IsLoaded).ToList();
|
ReadyPlayersList = PlayerList.Where(p => p.IsLoaded).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateMap(string sceneName)
|
|
||||||
{
|
|
||||||
Scene = sceneName;
|
|
||||||
|
|
||||||
foreach (var player in PlayerList)
|
|
||||||
player.UnsetReady();
|
|
||||||
|
|
||||||
UpdateReadyPlayerList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Broadcast(byte[] data, NetworkChannel channel = NetworkChannel.RELIABLE)
|
public void Broadcast(byte[] data, NetworkChannel channel = NetworkChannel.RELIABLE)
|
||||||
{
|
{
|
||||||
if (channel == NetworkChannel.RELIABLE)
|
if (channel == NetworkChannel.RELIABLE)
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ namespace Ragon.Server;
|
|||||||
|
|
||||||
public ref struct RoomInformation
|
public ref struct RoomInformation
|
||||||
{
|
{
|
||||||
public string Scene;
|
|
||||||
public int Min;
|
public int Min;
|
||||||
public int Max;
|
public int Max;
|
||||||
public int BufferedEventsLimit;
|
public int BufferedEventsLimit;
|
||||||
|
|||||||
Reference in New Issue
Block a user