From 0253e20e5a89578ca351179e5b435976cbad8eba Mon Sep 17 00:00:00 2001 From: Edmand46 Date: Sat, 30 Apr 2022 16:35:12 +0400 Subject: [PATCH] wip --- .../{ProtocolHeader.cs => RagonHeader.cs} | 3 +- .../Protocol/{Opcode.cs => RagonOperation.cs} | 0 Ragon/Ragon.csproj | 3 + Ragon/Sources/Plugin/PluginBase.cs | 4 +- Ragon/Sources/Rooms/Room.cs | 101 ++++++++++-------- Ragon/Sources/Rooms/RoomManager.cs | 4 +- Ragon/Sources/Rooms/RoomThread.cs | 4 +- 7 files changed, 65 insertions(+), 54 deletions(-) rename Ragon.Common/Protocol/{ProtocolHeader.cs => RagonHeader.cs} (96%) rename Ragon.Common/Protocol/{Opcode.cs => RagonOperation.cs} (100%) diff --git a/Ragon.Common/Protocol/ProtocolHeader.cs b/Ragon.Common/Protocol/RagonHeader.cs similarity index 96% rename from Ragon.Common/Protocol/ProtocolHeader.cs rename to Ragon.Common/Protocol/RagonHeader.cs index ec6222f..2819f29 100644 --- a/Ragon.Common/Protocol/ProtocolHeader.cs +++ b/Ragon.Common/Protocol/RagonHeader.cs @@ -4,7 +4,7 @@ using NetStack.Buffers; namespace Ragon.Core { - public static class ProtocolHeader + public static class RagonHeader { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteUShort(ushort id, ref Span data) { @@ -31,6 +31,5 @@ namespace Ragon.Core { return (ushort)(data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24)); } - } } \ No newline at end of file diff --git a/Ragon.Common/Protocol/Opcode.cs b/Ragon.Common/Protocol/RagonOperation.cs similarity index 100% rename from Ragon.Common/Protocol/Opcode.cs rename to Ragon.Common/Protocol/RagonOperation.cs diff --git a/Ragon/Ragon.csproj b/Ragon/Ragon.csproj index 2740052..9169aa0 100755 --- a/Ragon/Ragon.csproj +++ b/Ragon/Ragon.csproj @@ -20,6 +20,8 @@ + + @@ -28,6 +30,7 @@ + diff --git a/Ragon/Sources/Plugin/PluginBase.cs b/Ragon/Sources/Plugin/PluginBase.cs index f3545e7..307827a 100755 --- a/Ragon/Sources/Plugin/PluginBase.cs +++ b/Ragon/Sources/Plugin/PluginBase.cs @@ -83,7 +83,7 @@ namespace Ragon.Core _buffer.ToSpan(ref bufferSpan); - ProtocolHeader.WriteUShort((ushort) operation, ref data); + RagonHeader.WriteUShort((ushort) operation, ref data); Room.Send(peerId, data); } @@ -99,7 +99,7 @@ namespace Ragon.Core _buffer.ToSpan(ref bufferSpan); - ProtocolHeader.WriteUShort((ushort) operation, ref data); + RagonHeader.WriteUShort((ushort) operation, ref data); Room.Broadcast(peersIds, data); } diff --git a/Ragon/Sources/Rooms/Room.cs b/Ragon/Sources/Rooms/Room.cs index f476288..a5afd0d 100755 --- a/Ragon/Sources/Rooms/Room.cs +++ b/Ragon/Sources/Rooms/Room.cs @@ -25,7 +25,7 @@ namespace Ragon.Core // Cache private uint[] _readyPlayers = Array.Empty(); private uint[] _allPlayers = Array.Empty(); - private Entity[] _entitiesAll = Array.Empty(); + private Entity[] _entitiesAll = Array.Empty(); public Room(RoomThread roomThread, PluginBase pluginBase, string map) { @@ -54,22 +54,33 @@ namespace Ragon.Core }; _players.Add(peerId, player); - + _allPlayers = _players.Select(p => p.Key).ToArray(); - Span data = stackalloc byte[10]; - Span operationData = data.Slice(0, 2); - Span peerData = data.Slice(2, 4); - Span ownerData = data.Slice(4, 4); + { + Span data = stackalloc byte[10]; + Span operationData = data.Slice(0, 2); + Span peerData = data.Slice(2, 4); + Span ownerData = data.Slice(4, 4); - ProtocolHeader.WriteUShort((ushort) RagonOperation.JOIN_ROOM, ref operationData); - ProtocolHeader.WriteInt((int) peerId, ref peerData); - ProtocolHeader.WriteInt((int) _owner, ref ownerData); + RagonHeader.WriteUShort((ushort) RagonOperation.JOIN_ROOM, ref operationData); + RagonHeader.WriteInt((int) peerId, ref peerData); + RagonHeader.WriteInt((int) _owner, ref ownerData); - Send(peerId, data); + Send(peerId, data); + } - // var sceneRawData = Encoding.UTF8.GetBytes(_map); - // Send(peerId, RagonOperation.LOAD_SCENE, sceneRawData); + { + var sceneRawData = Encoding.UTF8.GetBytes(_map).AsSpan(); + Span data = stackalloc byte[sceneRawData.Length + 2]; + Span operationData = data.Slice(0, 2); + Span sceneData = data.Slice(2, sceneRawData.Length); + + RagonHeader.WriteUShort((ushort) RagonOperation.LOAD_SCENE, ref operationData); + sceneRawData.CopyTo(sceneData); + + Send(peerId, data, DeliveryType.Reliable); + } } public void Leave(uint peerId) @@ -77,14 +88,14 @@ namespace Ragon.Core if (_players.Remove(peerId, out var player)) { _allPlayers = _players.Select(p => p.Key).ToArray(); - + foreach (var entityId in player.EntitiesIds) { Span entityData = stackalloc byte[6]; var operationData = entityData.Slice(0, 2); - - ProtocolHeader.WriteUShort((ushort) RagonOperation.DESTROY_ENTITY, ref operationData); - ProtocolHeader.WriteInt(entityId, ref entityData); + + RagonHeader.WriteUShort((ushort) RagonOperation.DESTROY_ENTITY, ref operationData); + RagonHeader.WriteInt(entityId, ref entityData); Broadcast(_allPlayers, entityData); @@ -102,25 +113,26 @@ namespace Ragon.Core case RagonOperation.REPLICATE_ENTITY_STATE: { var entityData = rawData.Slice(2, 4); - var entityId = ProtocolHeader.ReadInt(ref entityData); + var entityId = RagonHeader.ReadInt(ref entityData); if (_entities.TryGetValue(entityId, out var ent)) { ent.State = rawData.Slice(6, rawData.Length - 6).ToArray(); - + Span data = stackalloc byte[rawData.Length]; rawData.CopyTo(data); Broadcast(_readyPlayers, data); } + break; } case RagonOperation.REPLICATE_ENTITY_PROPERTY: { var entityData = rawData.Slice(2, 4); - var entityId = ProtocolHeader.ReadInt(ref entityData); + var entityId = RagonHeader.ReadInt(ref entityData); if (_entities.TryGetValue(entityId, out var ent)) { var propertyData = rawData.Slice(6, 4); - var propertyId = ProtocolHeader.ReadInt(ref propertyData); + var propertyId = RagonHeader.ReadInt(ref propertyData); var payload = rawData.Slice(10, rawData.Length - 10).ToArray(); var props = _entities[entityId].Properties; @@ -145,20 +157,15 @@ namespace Ragon.Core case RagonOperation.REPLICATE_EVENT: case RagonOperation.REPLICATE_ENTITY_EVENT: { - Span data = stackalloc byte[rawData.Length + 4]; - Span peerData = data.Slice(2, 4); - Span rawDataSlice = data.Slice(4, rawData.Length); - - rawData.CopyTo(rawDataSlice); - ProtocolHeader.WriteInt((int) peerId, ref peerData); - + Span data = stackalloc byte[rawData.Length]; + rawData.CopyTo(data); Broadcast(_readyPlayers, data, DeliveryType.Reliable); break; } case RagonOperation.CREATE_ENTITY: { var entity = new Entity(peerId); - var entityPayload = rawData.Slice(2, rawData.Length - 2); + var entityPayload = rawData.Slice(2, rawData.Length - 2); entity.State = entityPayload.ToArray(); entity.Properties = new Dictionary(); @@ -168,26 +175,26 @@ namespace Ragon.Core _entities.Add(entity.EntityId, entity); _entitiesAll = _entities.Values.ToArray(); - + Span data = stackalloc byte[entityPayload.Length + 10]; var operationData = data.Slice(0, 2); var entityData = data.Slice(2, 4); var peerData = data.Slice(6, 4); var payload = data.Slice(10, entityPayload.Length); - + entityPayload.CopyTo(payload); - - ProtocolHeader.WriteUShort((ushort) RagonOperation.CREATE_ENTITY, ref operationData); - ProtocolHeader.WriteInt(entity.EntityId, ref entityData); - ProtocolHeader.WriteInt((int) peerId, ref peerData); - + + RagonHeader.WriteUShort((ushort) RagonOperation.CREATE_ENTITY, ref operationData); + RagonHeader.WriteInt(entity.EntityId, ref entityData); + RagonHeader.WriteInt((int) peerId, ref peerData); + Broadcast(_allPlayers, data, DeliveryType.Reliable); break; } case RagonOperation.DESTROY_ENTITY: { var entityData = rawData.Slice(2, 4); - var entityId = ProtocolHeader.ReadInt(ref entityData); + var entityId = RagonHeader.ReadInt(ref entityData); if (_entities.TryGetValue(entityId, out var entity)) { if (entity.OwnerId == peerId) @@ -214,21 +221,23 @@ namespace Ragon.Core foreach (var entity in _entities.Values) { var entityState = entity.State.AsSpan(); - + Span sendData = stackalloc byte[entity.State.Length + 10]; Span operationData = sendData.Slice(0, 2); Span entityData = sendData.Slice(2, 4); Span ownerData = sendData.Slice(6, 4); Span entityStateData = sendData.Slice(10, entity.State.Length); - ProtocolHeader.WriteUShort((ushort) RagonOperation.CREATE_ENTITY, ref operationData);; - ProtocolHeader.WriteInt(entity.EntityId, ref entityData); - ProtocolHeader.WriteInt((int) entity.OwnerId, ref ownerData); - + RagonHeader.WriteUShort((ushort) RagonOperation.CREATE_ENTITY, ref operationData); + ; + RagonHeader.WriteInt(entity.EntityId, ref entityData); + RagonHeader.WriteInt((int) entity.OwnerId, ref ownerData); + entityState.CopyTo(entityStateData); - + Send(peerId, sendData, DeliveryType.Reliable); } + Send(peerId, RagonOperation.RESTORE_END); break; } @@ -245,7 +254,7 @@ namespace Ragon.Core { _ticks++; _plugin.OnTick(_ticks, deltaTime); - + // for (var i = 0; i < _entitiesAll.Length; i++) // { // var entity = _entities[i]; @@ -280,8 +289,8 @@ namespace Ragon.Core public void Send(uint peerId, RagonOperation operation, DeliveryType deliveryType = DeliveryType.Unreliable) { Span data = stackalloc byte[2]; - ProtocolHeader.WriteUShort((ushort) operation, ref data); - + RagonHeader.WriteUShort((ushort) operation, ref data); + var bytes = data.ToArray(); _roomThread.WriteOutEvent(new Event() { @@ -291,7 +300,7 @@ namespace Ragon.Core Delivery = deliveryType, }); } - + public void Send(uint peerId, Span payload, DeliveryType deliveryType = DeliveryType.Unreliable) { var bytes = payload.ToArray(); diff --git a/Ragon/Sources/Rooms/RoomManager.cs b/Ragon/Sources/Rooms/RoomManager.cs index 9975c86..8a09c25 100644 --- a/Ragon/Sources/Rooms/RoomManager.cs +++ b/Ragon/Sources/Rooms/RoomManager.cs @@ -57,7 +57,7 @@ namespace Ragon.Core if (_manager.OnAuthorize(peerId, payload)) { Span data = stackalloc byte[2]; - ProtocolHeader.WriteUShort((ushort) RagonOperation.AUTHORIZED_SUCCESS, ref data); + RagonHeader.WriteUShort((ushort) RagonOperation.AUTHORIZED_SUCCESS, ref data); var bytes = data.ToArray(); _roomThread.WriteOutEvent(new Event() @@ -71,7 +71,7 @@ namespace Ragon.Core else { Span data = stackalloc byte[2]; - ProtocolHeader.WriteUShort((ushort) RagonOperation.AUTHORIZED_FAILED, ref data); + RagonHeader.WriteUShort((ushort) RagonOperation.AUTHORIZED_FAILED, ref data); var bytes = data.ToArray(); _roomThread.WriteOutEvent(new Event() diff --git a/Ragon/Sources/Rooms/RoomThread.cs b/Ragon/Sources/Rooms/RoomThread.cs index 27da35c..87c7ee8 100755 --- a/Ragon/Sources/Rooms/RoomThread.cs +++ b/Ragon/Sources/Rooms/RoomThread.cs @@ -15,7 +15,7 @@ namespace Ragon.Core private readonly Dictionary _socketByRooms; private readonly Thread _thread; private readonly Stopwatch _timer; - + private RingBuffer _receiveBuffer = new RingBuffer(8192 + 8192); private RingBuffer _sendBuffer = new RingBuffer(8192 + 8192); @@ -70,7 +70,7 @@ namespace Ragon.Core if (evnt.Type == EventType.DATA) { ReadOnlySpan data = evnt.Data.AsSpan(); - var operation = (RagonOperation) ProtocolHeader.ReadUShort(ref data); + var operation = (RagonOperation) RagonHeader.ReadUShort(ref data); if (_socketByRooms.TryGetValue(evnt.PeerId, out var room)) { room.ProcessEvent(operation, evnt.PeerId, evnt.Data);