wip
This commit is contained in:
@@ -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<byte> data) {
|
||||
@@ -31,6 +31,5 @@ namespace Ragon.Core
|
||||
{
|
||||
return (ushort)(data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,8 @@
|
||||
<PackageReference Include="ENet-CSharp" Version="2.4.8" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<PackageReference Include="NLog" Version="5.0.0-rc2" />
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.5.61" />
|
||||
<PackageReference Include="StackExchange.Redis.Extensions.Core" Version="8.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -28,6 +30,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Sources\Protocol" />
|
||||
<Folder Include="Sources\Storage" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+39
-30
@@ -57,19 +57,30 @@ namespace Ragon.Core
|
||||
|
||||
_allPlayers = _players.Select(p => p.Key).ToArray();
|
||||
|
||||
Span<byte> data = stackalloc byte[10];
|
||||
Span<byte> operationData = data.Slice(0, 2);
|
||||
Span<byte> peerData = data.Slice(2, 4);
|
||||
Span<byte> ownerData = data.Slice(4, 4);
|
||||
{
|
||||
Span<byte> data = stackalloc byte[10];
|
||||
Span<byte> operationData = data.Slice(0, 2);
|
||||
Span<byte> peerData = data.Slice(2, 4);
|
||||
Span<byte> 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<byte> data = stackalloc byte[sceneRawData.Length + 2];
|
||||
Span<byte> operationData = data.Slice(0, 2);
|
||||
Span<byte> 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)
|
||||
@@ -83,8 +94,8 @@ namespace Ragon.Core
|
||||
Span<byte> 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,7 +113,7 @@ 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();
|
||||
@@ -111,16 +122,17 @@ namespace Ragon.Core
|
||||
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,13 +157,8 @@ namespace Ragon.Core
|
||||
case RagonOperation.REPLICATE_EVENT:
|
||||
case RagonOperation.REPLICATE_ENTITY_EVENT:
|
||||
{
|
||||
Span<byte> data = stackalloc byte[rawData.Length + 4];
|
||||
Span<byte> peerData = data.Slice(2, 4);
|
||||
Span<byte> rawDataSlice = data.Slice(4, rawData.Length);
|
||||
|
||||
rawData.CopyTo(rawDataSlice);
|
||||
ProtocolHeader.WriteInt((int) peerId, ref peerData);
|
||||
|
||||
Span<byte> data = stackalloc byte[rawData.Length];
|
||||
rawData.CopyTo(data);
|
||||
Broadcast(_readyPlayers, data, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
@@ -177,9 +184,9 @@ namespace Ragon.Core
|
||||
|
||||
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;
|
||||
@@ -187,7 +194,7 @@ namespace Ragon.Core
|
||||
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)
|
||||
@@ -221,14 +228,16 @@ namespace Ragon.Core
|
||||
Span<byte> ownerData = sendData.Slice(6, 4);
|
||||
Span<byte> 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;
|
||||
}
|
||||
@@ -280,7 +289,7 @@ namespace Ragon.Core
|
||||
public void Send(uint peerId, RagonOperation operation, DeliveryType deliveryType = DeliveryType.Unreliable)
|
||||
{
|
||||
Span<byte> data = stackalloc byte[2];
|
||||
ProtocolHeader.WriteUShort((ushort) operation, ref data);
|
||||
RagonHeader.WriteUShort((ushort) operation, ref data);
|
||||
|
||||
var bytes = data.ToArray();
|
||||
_roomThread.WriteOutEvent(new Event()
|
||||
|
||||
@@ -57,7 +57,7 @@ namespace Ragon.Core
|
||||
if (_manager.OnAuthorize(peerId, payload))
|
||||
{
|
||||
Span<byte> 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<byte> 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()
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace Ragon.Core
|
||||
if (evnt.Type == EventType.DATA)
|
||||
{
|
||||
ReadOnlySpan<byte> 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);
|
||||
|
||||
Reference in New Issue
Block a user