wip
This commit is contained in:
@@ -82,7 +82,13 @@ public class Application : INetworkListener
|
||||
|
||||
if (_contexts.Remove(connection.Id, out var context))
|
||||
{
|
||||
context.Room?.RemovePlayer(context.RoomPlayer);
|
||||
var room = context.Room;
|
||||
if (room != null)
|
||||
{
|
||||
room.RemovePlayer(context.RoomPlayer);
|
||||
|
||||
_lobby.RemoveIfEmpty(room);
|
||||
}
|
||||
context.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,16 +20,15 @@ public class EntityList
|
||||
_entitiesMap.Add(entity.Id, entity);
|
||||
}
|
||||
|
||||
public Entity Remove(Entity entity)
|
||||
public bool Remove(Entity entity)
|
||||
{
|
||||
if (_entitiesMap.Remove(entity.Id, out var existEntity))
|
||||
{
|
||||
_staticEntitiesList.Remove(entity);
|
||||
_dynamicEntitiesList.Remove(entity);
|
||||
|
||||
return existEntity;
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+24
-27
@@ -5,11 +5,11 @@ namespace Ragon.Core.Game;
|
||||
|
||||
public class Room: IAction
|
||||
{
|
||||
public string Id { get; }
|
||||
public RoomInformation Info { get; }
|
||||
public RoomPlayer Owner { get; set; }
|
||||
|
||||
public Dictionary<ushort, RoomPlayer> Players { get; }
|
||||
public string Id { get; private set; }
|
||||
public RoomInformation Info { get; private set; }
|
||||
public RoomPlayer Owner { get; private set; }
|
||||
public RagonSerializer Writer { get; }
|
||||
public Dictionary<ushort, RoomPlayer> Players { get; private set; }
|
||||
public List<RoomPlayer> WaitPlayersList { get; private set; }
|
||||
public List<RoomPlayer> ReadyPlayersList { get; private set; }
|
||||
public List<RoomPlayer> PlayerList { get; private set; }
|
||||
@@ -19,10 +19,7 @@ public class Room: IAction
|
||||
public List<Entity> StaticEntitiesList { get; private set; }
|
||||
public List<Entity> EntityList { get; private set; }
|
||||
|
||||
private HashSet<Entity> _entitiesDirtySet;
|
||||
private RagonSerializer _writer;
|
||||
|
||||
public RagonSerializer Writer => _writer;
|
||||
private readonly HashSet<Entity> _entitiesDirtySet;
|
||||
|
||||
public Room(string roomId, RoomInformation info)
|
||||
{
|
||||
@@ -40,7 +37,7 @@ public class Room: IAction
|
||||
EntityList = new List<Entity>();
|
||||
|
||||
_entitiesDirtySet = new HashSet<Entity>();
|
||||
_writer = new RagonSerializer(512);
|
||||
Writer = new RagonSerializer(512);
|
||||
}
|
||||
|
||||
public void AttachEntity(RoomPlayer newOwner, Entity entity)
|
||||
@@ -75,16 +72,16 @@ public class Room: IAction
|
||||
var entities = (ushort) _entitiesDirtySet.Count;
|
||||
if (entities > 0)
|
||||
{
|
||||
_writer.Clear();
|
||||
_writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
_writer.WriteUShort(entities);
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
Writer.WriteUShort(entities);
|
||||
|
||||
foreach (var entity in _entitiesDirtySet)
|
||||
entity.State.Write(_writer);
|
||||
entity.State.Write(Writer);
|
||||
|
||||
_entitiesDirtySet.Clear();
|
||||
|
||||
var sendData = _writer.ToArray();
|
||||
var sendData = Writer.ToArray();
|
||||
foreach (var roomPlayer in ReadyPlayersList)
|
||||
roomPlayer.Connection.UnreliableChannel.Send(sendData);
|
||||
}
|
||||
@@ -108,19 +105,19 @@ public class Room: IAction
|
||||
PlayerList.Remove(player);
|
||||
|
||||
{
|
||||
_writer.Clear();
|
||||
_writer.WriteOperation(RagonOperation.PLAYER_LEAVED);
|
||||
_writer.WriteString(player.Id);
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.PLAYER_LEAVED);
|
||||
Writer.WriteString(player.Id);
|
||||
|
||||
var entitiesToDelete = player.Entities.DynamicList;
|
||||
_writer.WriteUShort((ushort) entitiesToDelete.Count);
|
||||
Writer.WriteUShort((ushort) entitiesToDelete.Count);
|
||||
foreach (var entity in entitiesToDelete)
|
||||
{
|
||||
_writer.WriteUShort(entity.Id);
|
||||
Writer.WriteUShort(entity.Id);
|
||||
EntityList.Remove(entity);
|
||||
}
|
||||
|
||||
var sendData = _writer.ToArray();
|
||||
var sendData = Writer.ToArray();
|
||||
Broadcast(sendData);
|
||||
}
|
||||
|
||||
@@ -132,20 +129,20 @@ public class Room: IAction
|
||||
|
||||
var entitiesToUpdate = roomPlayer.Entities.StaticList;
|
||||
|
||||
_writer.Clear();
|
||||
_writer.WriteOperation(RagonOperation.OWNERSHIP_CHANGED);
|
||||
_writer.WriteString(Owner.Id);
|
||||
_writer.WriteUShort((ushort) entitiesToUpdate.Count);
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.OWNERSHIP_CHANGED);
|
||||
Writer.WriteString(Owner.Id);
|
||||
Writer.WriteUShort((ushort) entitiesToUpdate.Count);
|
||||
|
||||
foreach (var entity in entitiesToUpdate)
|
||||
{
|
||||
_writer.WriteUShort(entity.Id);
|
||||
Writer.WriteUShort(entity.Id);
|
||||
|
||||
entity.SetOwner(nextOwner);
|
||||
nextOwner.Entities.Add(entity);
|
||||
}
|
||||
|
||||
var sendData = _writer.ToArray();
|
||||
var sendData = Writer.ToArray();
|
||||
Broadcast(sendData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ namespace Ragon.Core.Game;
|
||||
|
||||
public class RoomInformation
|
||||
{
|
||||
public string Map { get; set; }
|
||||
public int Min { get; set; }
|
||||
public int Max { get; set; }
|
||||
public string Map { get; init; } = "none";
|
||||
public int Min { get; init; }
|
||||
public int Max { get; init; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
|
||||
@@ -8,5 +8,5 @@ public interface ILobby
|
||||
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out Room room);
|
||||
public bool FindRoomByMap(string map, [MaybeNullWhen(false)] out Room room);
|
||||
public void Persist(Room room);
|
||||
public void Remove(Room room);
|
||||
public void RemoveIfEmpty(Room room);
|
||||
}
|
||||
@@ -47,11 +47,15 @@ public class LobbyInMemory : ILobby
|
||||
_rooms.Add(room);
|
||||
|
||||
foreach (var r in _rooms)
|
||||
_logger.Trace($"{r.Id} {r.Info}");
|
||||
_logger.Trace($"Room: {r.Id} {r.Info} Players: {r.Players.Count}");
|
||||
}
|
||||
|
||||
public void Remove(Room room)
|
||||
public void RemoveIfEmpty(Room room)
|
||||
{
|
||||
if (room.Players.Count == 0)
|
||||
_rooms.Remove(room);
|
||||
|
||||
foreach (var r in _rooms)
|
||||
_logger.Trace($"Room: {r.Id} {r.Info} Players: {r.Players.Count}");
|
||||
}
|
||||
}
|
||||
@@ -8,30 +8,31 @@ namespace Ragon.Server.ENet
|
||||
{
|
||||
public sealed class ENetServer: INetworkServer
|
||||
{
|
||||
public ENetConnection[] Connections;
|
||||
private ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||
private INetworkListener _listener;
|
||||
private readonly Host _host;
|
||||
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
private ENetConnection[] _connections;
|
||||
private uint _protocol;
|
||||
private Host _host;
|
||||
private INetworkListener _listener;
|
||||
private Event _event;
|
||||
private NetworkConfiguration _configuration;
|
||||
|
||||
public ENetServer()
|
||||
{
|
||||
_host = new Host();
|
||||
_connections = Array.Empty<ENetConnection>();
|
||||
}
|
||||
|
||||
public void Start(INetworkListener listener, NetworkConfiguration configuration)
|
||||
{
|
||||
Library.Initialize();
|
||||
|
||||
_connections = new ENetConnection[configuration.LimitConnections];
|
||||
|
||||
_listener = listener;
|
||||
_protocol = configuration.Protocol;
|
||||
|
||||
Connections = new ENetConnection[configuration.LimitConnections];
|
||||
|
||||
var address = new Address { Port = (ushort) configuration.Port };
|
||||
_host.Create(address, Connections.Length, 2, 0, 0, 1024 * 1024);
|
||||
_host.Create(address, _connections.Length, 2, 0, 0, 1024 * 1024);
|
||||
|
||||
var protocolDecoded = RagonVersion.Parse(_protocol);
|
||||
_logger.Info($"Network listening on {configuration.Port}");
|
||||
@@ -60,34 +61,33 @@ namespace Ragon.Server.ENet
|
||||
}
|
||||
case EventType.Connect:
|
||||
{
|
||||
if (IsValidProtocol(_event.Data))
|
||||
if (!IsValidProtocol(_event.Data))
|
||||
{
|
||||
_logger.Warn("Mismatched protocol, close connection");
|
||||
break;
|
||||
}
|
||||
|
||||
var connection = new ENetConnection(_event.Peer);
|
||||
Connections[_event.Peer.ID] = connection;
|
||||
|
||||
_connections[_event.Peer.ID] = connection;
|
||||
_listener.OnConnected(connection);
|
||||
break;
|
||||
}
|
||||
case EventType.Disconnect:
|
||||
{
|
||||
var connection = Connections[_event.Peer.ID];
|
||||
var connection = _connections[_event.Peer.ID];
|
||||
_listener.OnDisconnected(connection);
|
||||
break;
|
||||
}
|
||||
case EventType.Timeout:
|
||||
{
|
||||
var connection = Connections[_event.Peer.ID];
|
||||
var connection = _connections[_event.Peer.ID];
|
||||
_listener.OnTimeout(connection);
|
||||
break;
|
||||
}
|
||||
case EventType.Receive:
|
||||
{
|
||||
var peerId = (ushort) _event.Peer.ID;
|
||||
var connection = Connections[peerId];
|
||||
var connection = _connections[peerId];
|
||||
var dataRaw = new byte[_event.Packet.Length];
|
||||
|
||||
_event.Packet.CopyTo(dataRaw);
|
||||
@@ -109,7 +109,8 @@ namespace Ragon.Server.ENet
|
||||
|
||||
private bool IsValidProtocol(uint protocol)
|
||||
{
|
||||
return protocol == _configuration.Protocol;
|
||||
Console.WriteLine($"{protocol} {_protocol}");
|
||||
return protocol == _protocol;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user