fixed: incorrected snapshot on reconnecting, added resizble serializer
This commit is contained in:
@@ -24,6 +24,8 @@ namespace Ragon.Common
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteByte(byte value)
|
public void WriteByte(byte value)
|
||||||
{
|
{
|
||||||
|
ResizeIfNeed(1);
|
||||||
|
|
||||||
_data[_offset] = value;
|
_data[_offset] = value;
|
||||||
_offset += 1;
|
_offset += 1;
|
||||||
}
|
}
|
||||||
@@ -39,6 +41,8 @@ namespace Ragon.Common
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteInt(int value)
|
public void WriteInt(int value)
|
||||||
{
|
{
|
||||||
|
ResizeIfNeed(4);
|
||||||
|
|
||||||
_data[_offset] = (byte) (value & 0x00FF);
|
_data[_offset] = (byte) (value & 0x00FF);
|
||||||
_data[_offset + 1] = (byte) ((value & 0xFF00) >> 8);
|
_data[_offset + 1] = (byte) ((value & 0xFF00) >> 8);
|
||||||
_data[_offset + 2] = (byte) ((value & 0xFF00) >> 16);
|
_data[_offset + 2] = (byte) ((value & 0xFF00) >> 16);
|
||||||
@@ -58,8 +62,9 @@ namespace Ragon.Common
|
|||||||
public void WriteString(string value)
|
public void WriteString(string value)
|
||||||
{
|
{
|
||||||
var stringRaw = Encoding.UTF8.GetBytes(value).AsSpan();
|
var stringRaw = Encoding.UTF8.GetBytes(value).AsSpan();
|
||||||
WriteUShort((ushort) stringRaw.Length);
|
ResizeIfNeed(2 + stringRaw.Length);
|
||||||
|
|
||||||
|
WriteUShort((ushort) stringRaw.Length);
|
||||||
var data = _data.AsSpan().Slice(_offset, stringRaw.Length);
|
var data = _data.AsSpan().Slice(_offset, stringRaw.Length);
|
||||||
stringRaw.CopyTo(data);
|
stringRaw.CopyTo(data);
|
||||||
_offset += stringRaw.Length;
|
_offset += stringRaw.Length;
|
||||||
@@ -88,6 +93,8 @@ namespace Ragon.Common
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteData(ref ReadOnlySpan<byte> payload)
|
public void WriteData(ref ReadOnlySpan<byte> payload)
|
||||||
{
|
{
|
||||||
|
ResizeIfNeed(payload.Length);
|
||||||
|
|
||||||
var data = _data.AsSpan();
|
var data = _data.AsSpan();
|
||||||
var payloadData = data.Slice(_offset, payload.Length);
|
var payloadData = data.Slice(_offset, payload.Length);
|
||||||
|
|
||||||
@@ -98,6 +105,8 @@ namespace Ragon.Common
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public Span<byte> GetWritableData(int lenght)
|
public Span<byte> GetWritableData(int lenght)
|
||||||
{
|
{
|
||||||
|
ResizeIfNeed(lenght);
|
||||||
|
|
||||||
var data = _data.AsSpan();
|
var data = _data.AsSpan();
|
||||||
var payloadData = data.Slice(_offset, lenght);
|
var payloadData = data.Slice(_offset, lenght);
|
||||||
|
|
||||||
@@ -108,6 +117,8 @@ namespace Ragon.Common
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteOperation(RagonOperation ragonOperation)
|
public void WriteOperation(RagonOperation ragonOperation)
|
||||||
{
|
{
|
||||||
|
ResizeIfNeed(1);
|
||||||
|
|
||||||
_data[_offset] = (byte) ragonOperation;
|
_data[_offset] = (byte) ragonOperation;
|
||||||
_offset += 1;
|
_offset += 1;
|
||||||
}
|
}
|
||||||
@@ -123,6 +134,8 @@ namespace Ragon.Common
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteUShort(ushort value)
|
public void WriteUShort(ushort value)
|
||||||
{
|
{
|
||||||
|
ResizeIfNeed(2);
|
||||||
|
|
||||||
_data[_offset] = (byte) (value & 0x00FF);
|
_data[_offset] = (byte) (value & 0x00FF);
|
||||||
_data[_offset + 1] = (byte) ((value & 0xFF00) >> 8);
|
_data[_offset + 1] = (byte) ((value & 0xFF00) >> 8);
|
||||||
_offset += 2;
|
_offset += 2;
|
||||||
@@ -149,13 +162,6 @@ namespace Ragon.Common
|
|||||||
dataSpan.CopyTo(data);
|
dataSpan.CopyTo(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] ToArray()
|
|
||||||
{
|
|
||||||
var bytes = new byte[_offset];
|
|
||||||
Buffer.BlockCopy(_data, 0, bytes, 0, _offset);
|
|
||||||
return bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void FromSpan(ref ReadOnlySpan<byte> data)
|
public void FromSpan(ref ReadOnlySpan<byte> data)
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
@@ -163,5 +169,24 @@ namespace Ragon.Common
|
|||||||
data.CopyTo(dataSpan);
|
data.CopyTo(dataSpan);
|
||||||
_size = data.Length;
|
_size = data.Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] ToArray()
|
||||||
|
{
|
||||||
|
var bytes = new byte[_offset];
|
||||||
|
Buffer.BlockCopy(_data, 0, bytes, 0, _offset);
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ResizeIfNeed(int lenght)
|
||||||
|
{
|
||||||
|
if (_offset + lenght < _data.Length)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Console.WriteLine("Resize: " + (_data.Length + 512));
|
||||||
|
|
||||||
|
var newData = new byte[_data.Length * 2];
|
||||||
|
Buffer.BlockCopy(_data, 0, newData, 0, _data.Length);
|
||||||
|
_data = newData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Common;
|
using Ragon.Common;
|
||||||
@@ -22,7 +23,7 @@ namespace Ragon.Core
|
|||||||
|
|
||||||
private readonly PluginBase _plugin;
|
private readonly PluginBase _plugin;
|
||||||
private readonly RoomThread _roomThread;
|
private readonly RoomThread _roomThread;
|
||||||
private readonly RagonSerializer _serializer = new(2048);
|
private readonly RagonSerializer _serializer = new(512);
|
||||||
|
|
||||||
// Cache
|
// Cache
|
||||||
private uint[] _readyPlayers = Array.Empty<uint>();
|
private uint[] _readyPlayers = Array.Empty<uint>();
|
||||||
@@ -106,6 +107,8 @@ namespace Ragon.Core
|
|||||||
if (_players.Remove(peerId, out var player))
|
if (_players.Remove(peerId, out var player))
|
||||||
{
|
{
|
||||||
_allPlayers = _players.Select(p => p.Key).ToArray();
|
_allPlayers = _players.Select(p => p.Key).ToArray();
|
||||||
|
_readyPlayers = _players.Where(p => p.Value.IsLoaded).Select(p => p.Key).ToArray();
|
||||||
|
|
||||||
var isOwnershipChange = player.PeerId == _owner;
|
var isOwnershipChange = player.PeerId == _owner;
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -176,7 +179,11 @@ namespace Ragon.Core
|
|||||||
if (ent.Authority == RagonAuthority.OWNER_ONLY && ent.OwnerId != peerId)
|
if (ent.Authority == RagonAuthority.OWNER_ONLY && ent.OwnerId != peerId)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var payload = _serializer.ReadData(_serializer.Size);
|
Span<byte> payloadRaw = stackalloc byte[_serializer.Size];
|
||||||
|
ReadOnlySpan<byte> payload = payloadRaw;
|
||||||
|
var payloadData = _serializer.ReadData(_serializer.Size);
|
||||||
|
payloadData.CopyTo(payloadRaw);
|
||||||
|
|
||||||
if (_plugin.InternalHandle(peerId, entityId, evntId, ref payload))
|
if (_plugin.InternalHandle(peerId, entityId, evntId, ref payload))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user