Compare commits

..

3 Commits

Author SHA1 Message Date
edmand46 f1c5c99417 chore: update version 2025-10-04 17:33:58 +03:00
edmand46 e52e940fda feat: multi project support, maintaince 2025-10-04 16:21:30 +03:00
edmand46 e78e8048ff feat: multi project support, maintaince 2025-10-04 16:17:41 +03:00
142 changed files with 4779 additions and 2040 deletions
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Ragon.Client\Ragon.Client.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,60 @@
/*
* 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.Property
{
[Serializable]
public class RagonBool : RagonProperty
{
public bool Value
{
get => _value;
set
{
_value = value;
MarkAsChanged();
}
}
private bool _value;
public RagonBool(
bool initialValue,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_value = initialValue;
SetFixedSize(1);
}
public override void Serialize(RagonBuffer buffer)
{
buffer.WriteBool(_value);
}
public override void Deserialize(RagonBuffer buffer)
{
_value = buffer.ReadBool();
InvokeChanged();
}
}
}
+104
View File
@@ -0,0 +1,104 @@
/*
* 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
{
[Serializable]
public class RagonFloat : RagonProperty
{
public float Value
{
get => _value;
set
{
_value = value;
if (_value < _min)
_value = _min;
else if (_value > _max)
_value = _max;
MarkAsChanged();
}
}
private float _value;
private float _min;
private float _max;
private int _requiredBits;
private float _precision;
private uint _mask;
public RagonFloat(
float initialValue,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_value = initialValue;
_min = -1024.0f;
_max = 1024.0f;
_precision = 0.01f;
_requiredBits = DeBruijn.Log2((uint)((_max - _min) * (1.0f / _precision) + 0.5f)) + 1;
_mask = (uint)((1L << _requiredBits) - 1);
SetFixedSize(_requiredBits);
}
public RagonFloat(
float initialValue,
float min = -1024.0f,
float max = 1024.0f,
float precision = 0.01f,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_value = initialValue;
_min = min;
_max = max;
_precision = precision;
_requiredBits = DeBruijn.Log2((uint)((_max - _min) * (1.0f / _precision) + 0.5f)) + 1;
_mask = (uint)((1L << _requiredBits) - 1);
SetFixedSize(_requiredBits);
}
public override void Serialize(RagonBuffer buffer)
{
var compressedValue = (uint)((_value - _min) * (1f / _precision) + 0.5f) & _mask;
buffer.Write(compressedValue, _requiredBits);
}
public override void Deserialize(RagonBuffer buffer)
{
var compressedValue = buffer.Read(_requiredBits);
_value = compressedValue * _precision + _min;
if (_value < _min)
_value = _min;
else if (_value > _max)
_value = _max;
InvokeChanged();
}
}
}
+87
View File
@@ -0,0 +1,87 @@
/*
* 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.Property
{
[Serializable]
public class RagonInt : RagonProperty
{
public int Value
{
get => _value;
set
{
_value = value;
MarkAsChanged();
}
}
private int _min;
private int _max;
private int _requiredBits;
private int _value;
public RagonInt(
int initialValue,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_value = initialValue;
_min = -1000;
_max = 1000;
_max = Math.Max(Math.Abs(_min), Math.Abs(_max));
_requiredBits = Bits.Compute(_max);
SetFixedSize(_requiredBits);
}
public RagonInt(
int initialValue,
int min = -1000,
int max = 1000,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_value = initialValue;
_min = min;
_max = max;
_requiredBits = Bits.Compute(_max);
SetFixedSize(_requiredBits);
}
public override void Serialize(RagonBuffer buffer)
{
uint compressedValue = (uint)((_value << 1) ^ (_value >> 31));
buffer.Write(compressedValue, _requiredBits);
}
public override void Deserialize(RagonBuffer buffer)
{
var compressedValue = buffer.Read(_requiredBits);
_value = (int)((compressedValue >> 1) ^ -(int)(compressedValue & 1));
InvokeChanged();
}
}
}
@@ -0,0 +1,76 @@
/*
* 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 System.Numerics;
using Ragon.Client.Compressor;
using Ragon.Protocol;
namespace Ragon.Client.Property;
public class RagonQuaternion : RagonProperty
{
private Quaternion _value;
public Quaternion Value
{
get => _value;
set
{
_value = value;
MarkAsChanged();
}
}
private readonly FloatCompressor _compressor;
public RagonQuaternion(bool invokeLocal = false, int priority = 0) : base(priority, invokeLocal)
{
_compressor = new FloatCompressor(-1.0f, 1f, 0.01f);
SetFixedSize(_compressor.RequiredBits * 4);
}
public override void Serialize(RagonBuffer buffer)
{
var compressedX = _compressor.Compress(_value.X);
var compressedY = _compressor.Compress(_value.Y);
var compressedZ = _compressor.Compress(_value.Z);
var compressedW = _compressor.Compress(_value.W);
buffer.Write(compressedX, _compressor.RequiredBits);
buffer.Write(compressedY, _compressor.RequiredBits);
buffer.Write(compressedZ, _compressor.RequiredBits);
buffer.Write(compressedW, _compressor.RequiredBits);
}
public override void Deserialize(RagonBuffer buffer)
{
var compressedX = buffer.Read(_compressor.RequiredBits);
var compressedY = buffer.Read(_compressor.RequiredBits);
var compressedZ = buffer.Read(_compressor.RequiredBits);
var compressedW = buffer.Read(_compressor.RequiredBits);
var x = _compressor.Decompress(compressedX);
var y = _compressor.Decompress(compressedY);
var z = _compressor.Decompress(compressedZ);
var w = _compressor.Decompress(compressedW);
_value = new Quaternion(x, y, z, w);
InvokeChanged();
}
}
@@ -0,0 +1,68 @@
/*
* 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 System.Text;
using Ragon.Protocol;
namespace Ragon.Client.Property
{
[Serializable]
public class RagonString : RagonProperty
{
public string Value
{
get => _value;
set
{
_value = value;
MarkAsChanged();
}
}
private string _value;
private readonly UTF8Encoding _utf8Encoding = new UTF8Encoding(false, true);
public RagonString(
string initialValue,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_value = initialValue;
}
public override void Serialize(RagonBuffer buffer)
{
var data = _utf8Encoding.GetBytes(_value);
var len = (uint) data.Length;
buffer.Write(len, 16);
buffer.WriteBytes(data);
}
public override void Deserialize(RagonBuffer buffer)
{
var len = (int) buffer.Read(16);
var data = buffer.ReadBytes(len);
_value = _utf8Encoding.GetString(data);
InvokeChanged();
}
}
}
@@ -0,0 +1,330 @@
/*
* 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 System;
using System.Numerics;
using Ragon.Client.Compressor;
using Ragon.Protocol;
namespace Ragon.Client.Property
{
[Serializable]
public enum RagonAxis
{
XYZ,
XY,
YZ,
XZ,
X,
Y,
Z
}
[Serializable]
public class RagonVector3 : RagonProperty
{
private Vector3 _value;
public Vector3 Value
{
get => _value;
set
{
_value = value;
MarkAsChanged();
}
}
private RagonAxis _axis;
private FloatCompressor _compressorX;
private FloatCompressor _compressorY;
private FloatCompressor _compressorZ;
public RagonVector3(
RagonAxis axis = RagonAxis.XYZ,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_axis = axis;
var defaultCompressor = new FloatCompressor(-1024.0f, 1024f, 0.01f);
_compressorX = defaultCompressor;
_compressorY = defaultCompressor;
_compressorZ = defaultCompressor;
switch (_axis)
{
case RagonAxis.XYZ:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.XY:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits);
break;
case RagonAxis.XZ:
SetFixedSize(_compressorX.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.YZ:
SetFixedSize(_compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.X:
SetFixedSize(_compressorX.RequiredBits);
break;
case RagonAxis.Y:
SetFixedSize(_compressorY.RequiredBits);
break;
case RagonAxis.Z:
SetFixedSize(_compressorZ.RequiredBits);
break;
}
}
public RagonVector3(
Vector3 initialValue,
RagonAxis axis = RagonAxis.XYZ,
float min = -1024.0f,
float max = 1024.0f,
float precision = 0.1f,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_value = initialValue;
_axis = axis;
var defaultCompressor = new FloatCompressor(min, max, precision);
_compressorX = defaultCompressor;
_compressorY = defaultCompressor;
_compressorZ = defaultCompressor;
switch (_axis)
{
case RagonAxis.XYZ:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.XY:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits);
break;
case RagonAxis.XZ:
SetFixedSize(_compressorX.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.YZ:
SetFixedSize(_compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.X:
SetFixedSize(_compressorX.RequiredBits);
break;
case RagonAxis.Y:
SetFixedSize(_compressorY.RequiredBits);
break;
case RagonAxis.Z:
SetFixedSize(_compressorZ.RequiredBits);
break;
}
}
public RagonVector3(
RagonAxis axis = RagonAxis.XYZ,
FloatCompressor compressorX = null,
FloatCompressor compressorY = null,
FloatCompressor compressorZ = null,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_axis = axis;
var defaultCompressor = new FloatCompressor(-1024.0f, 1024f, 0.01f);
_compressorX = defaultCompressor;
_compressorY = defaultCompressor;
_compressorZ = defaultCompressor;
if (compressorX != null)
_compressorX = compressorX;
if (compressorY != null)
_compressorY = compressorY;
if (compressorZ != null)
_compressorZ = compressorZ;
switch (_axis)
{
case RagonAxis.XYZ:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.XY:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits);
break;
case RagonAxis.XZ:
SetFixedSize(_compressorX.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.YZ:
SetFixedSize(_compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.X:
SetFixedSize(_compressorX.RequiredBits);
break;
case RagonAxis.Y:
SetFixedSize(_compressorY.RequiredBits);
break;
case RagonAxis.Z:
SetFixedSize(_compressorZ.RequiredBits);
break;
}
}
public override void Serialize(RagonBuffer buffer)
{
switch (_axis)
{
case RagonAxis.XYZ:
{
var compressedX = _compressorX.Compress(_value.X);
var compressedY = _compressorY.Compress(_value.Y);
var compressedZ = _compressorZ.Compress(_value.Z);
buffer.Write(compressedX, _compressorX.RequiredBits);
buffer.Write(compressedY, _compressorY.RequiredBits);
buffer.Write(compressedZ, _compressorZ.RequiredBits);
}
break;
case RagonAxis.XY:
{
var compressedX = _compressorX.Compress(_value.X);
var compressedY = _compressorY.Compress(_value.Y);
buffer.Write(compressedX, _compressorX.RequiredBits);
buffer.Write(compressedY, _compressorY.RequiredBits);
}
break;
case RagonAxis.XZ:
{
var compressedX = _compressorX.Compress(_value.X);
var compressedZ = _compressorZ.Compress(_value.Z);
buffer.Write(compressedX, _compressorX.RequiredBits);
buffer.Write(compressedZ, _compressorZ.RequiredBits);
break;
}
case RagonAxis.YZ:
{
var compressedY = _compressorY.Compress(_value.Y);
var compressedZ = _compressorZ.Compress(_value.Z);
buffer.Write(compressedY, _compressorY.RequiredBits);
buffer.Write(compressedZ, _compressorZ.RequiredBits);
break;
}
case RagonAxis.X:
{
var compressedX = _compressorX.Compress(_value.X);
buffer.Write(compressedX, _compressorX.RequiredBits);
break;
}
case RagonAxis.Y:
{
var compressedY = _compressorY.Compress(_value.Y);
buffer.Write(compressedY, _compressorY.RequiredBits);
break;
}
case RagonAxis.Z:
{
var compressedZ = _compressorZ.Compress(_value.Z);
buffer.Write(compressedZ, _compressorZ.RequiredBits);
break;
}
}
}
public override void Deserialize(RagonBuffer buffer)
{
switch (_axis)
{
case RagonAxis.XYZ:
{
var compressedX = buffer.Read(_compressorX.RequiredBits);
var compressedY = buffer.Read(_compressorY.RequiredBits);
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
_value.X = _compressorX.Decompress(compressedX);
_value.Y = _compressorY.Decompress(compressedY);
_value.Z = _compressorZ.Decompress(compressedZ);
break;
}
case RagonAxis.XY:
{
var compressedX = buffer.Read(_compressorX.RequiredBits);
var compressedY = buffer.Read(_compressorY.RequiredBits);
_value.X = _compressorX.Decompress(compressedX);
_value.Z = _compressorY.Decompress(compressedY);
break;
}
case RagonAxis.XZ:
{
var compressedX = buffer.Read(_compressorX.RequiredBits);
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
_value.X = _compressorX.Decompress(compressedX);
_value.Z = _compressorZ.Decompress(compressedZ);
break;
}
case RagonAxis.YZ:
{
var compressedY = buffer.Read(_compressorY.RequiredBits);
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
_value.Y = _compressorY.Decompress(compressedY);
_value.Z = _compressorZ.Decompress(compressedZ);
break;
}
case RagonAxis.X:
{
var compressedX = buffer.Read(_compressorX.RequiredBits);
_value.X = _compressorX.Decompress(compressedX);
break;
}
case RagonAxis.Y:
{
var compressedY = buffer.Read(_compressorY.RequiredBits);
_value.Y = _compressorY.Decompress(compressedY);
break;
}
case RagonAxis.Z:
{
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
_value.Z = _compressorZ.Decompress(compressedZ);
break;
}
}
InvokeChanged();
}
}
}
-1
View File
@@ -1,4 +1,3 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
@@ -0,0 +1,57 @@
/*
* 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.Compressor;
public class FloatCompressor
{
public float Min { get; private set; }
public float Max { get; private set; }
public float Precision { get; private set; }
public int RequiredBits { get; private set; }
private uint _mask;
public FloatCompressor(float min = -1024.0f, float max = 1024.0f, float precision = 0.01f)
{
Min = min;
Max = max;
Precision = precision;
RequiredBits = DeBruijn.Log2((uint)((Max - Min) * (1f / Precision) + 0.5f)) + 1;
_mask = (uint)((1L << RequiredBits) - 1);
}
public uint Compress(float value)
{
return (uint)((value - Min) * 1f / Precision + 0.5f) & _mask;
}
public float Decompress(uint value)
{
var result = value * Precision + Min;
if (result < Min)
result = Min;
else if (result > Max)
result = Max;
return result;
}
}
@@ -0,0 +1,44 @@
/*
* 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.Compressor;
public class IntCompressor
{
public int Min { get; private set; }
public int Max { get; private set; }
public int RequiredBits { get; private set; }
public IntCompressor(int min = -1000, int max = 1000)
{
Min = min;
Max = max;
RequiredBits = Bits.Compute(Max);
}
public uint Compress(int value)
{
return (uint)((value << 1) ^ (value >> 31));;
}
public int Decompress(uint value)
{
return (int)((value >> 1) ^ -(int)(value & 1));
}
}
+310
View File
@@ -0,0 +1,310 @@
/*
* 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
{
public sealed class RagonEntity : IDisposable
{
private class EventSubscription : IDisposable
{
private List<Action<RagonPlayer, IRagonEvent>> _callbacks;
private List<Action<RagonPlayer, IRagonEvent>> _localCallbacks;
private Action<RagonPlayer, IRagonEvent> _callback;
public EventSubscription(
List<Action<RagonPlayer, IRagonEvent>> callbacks,
List<Action<RagonPlayer, IRagonEvent>> localCallbacks,
Action<RagonPlayer, IRagonEvent> callback)
{
_callbacks = callbacks;
_localCallbacks = localCallbacks;
_callback = callback;
}
public void Dispose()
{
_callbacks?.Remove(_callback);
_localCallbacks?.Remove(_callback);
_callbacks = null!;
_localCallbacks = null!;
_callback = null!;
}
}
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
private RagonClient _client;
public ushort Id { get; private set; }
public ushort Type { get; private set; }
public RagonAuthority Authority { get; private set; }
public RagonPlayer Owner { get; private set; }
public RagonEntityState State { get; private set; }
public bool IsStatic => SceneId > 0;
public bool IsReplicated { get; private set; }
public bool IsAttached { get; private set; }
public bool HasAuthority { get; private set; }
public event Action<RagonEntity> Attached;
public event Action Detached;
public event Action<RagonPlayer, RagonPlayer> OwnershipChanged;
internal bool PropertiesChanged => _propertiesChanged;
internal ushort SceneId => _sceneId;
private readonly ushort _sceneId;
private bool _propertiesChanged;
private RagonPayload _spawnPayload;
private RagonPayload _destroyPayload;
private readonly Dictionary<int, OnEventDelegate> _events = new();
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new();
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new();
public RagonEntity(ushort type = 0, ushort sceneId = 0, bool replicated = true)
{
State = new RagonEntityState(this);
Type = type;
IsReplicated = replicated;
_spawnPayload = new RagonPayload(0);
_destroyPayload = new RagonPayload(0);
_sceneId = sceneId;
}
internal void Attach()
{
IsAttached = true;
Attached?.Invoke(this);
}
public void SetReplication(bool enabled)
{
IsReplicated = enabled;
}
internal void Detach(RagonPayload payload)
{
_destroyPayload = payload;
Detached?.Invoke();
}
internal T GetPayload<T>(RagonPayload data) where T : IRagonPayload, new()
{
var payload = new T();
if (data.Size <= 0) return payload;
var buffer = new RagonBuffer();
data.Write(buffer);
payload.Deserialize(buffer);
return payload;
}
public void Prepare(
RagonClient client,
ushort entityId,
ushort entityType,
bool hasAuthority,
RagonPlayer player,
RagonPayload payload
)
{
Type = entityType;
Id = entityId;
HasAuthority = hasAuthority;
_client = client;
_spawnPayload = payload;
Owner = player;
}
public T GetAttachPayload<T>() where T : IRagonPayload, new()
{
return GetPayload<T>(_spawnPayload);
}
public T GetDetachPayload<T>() where T : IRagonPayload, new()
{
return GetPayload<T>(_destroyPayload);
}
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
where TEvent : IRagonEvent, new()
{
if (!IsAttached)
{
RagonLog.Error("Entity not attached");
return;
}
var evntId = _client.Event.GetEventCode(evnt);
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
buffer.WriteUShort(Id);
buffer.WriteUShort(evntId);
buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte)RagonTarget.Player);
buffer.WriteUShort(target.PeerId);
evnt.Serialize(buffer);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
public void ReplicateEvent<TEvent>(
TEvent evnt,
RagonTarget target = RagonTarget.All,
RagonReplicationMode replicationMode = RagonReplicationMode.Server)
where TEvent : IRagonEvent, new()
{
if (!IsAttached)
{
RagonLog.Error("Entity not attached");
return;
}
var eventCode = _client.Event.GetEventCode(evnt);
if (target != RagonTarget.ExceptOwner)
{
if (replicationMode == RagonReplicationMode.Local)
{
var localListeners = _localListeners[eventCode];
foreach (var listener in localListeners)
listener.Invoke(_client.Room.Local, evnt);
return;
}
if (replicationMode == RagonReplicationMode.LocalAndServer)
{
var localListeners = _localListeners[eventCode];
foreach (var listener in localListeners)
listener.Invoke(_client.Room.Local, evnt);
}
}
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
buffer.WriteUShort(Id);
buffer.WriteUShort(eventCode);
buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte)target);
evnt.Serialize(buffer);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
public IDisposable OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
{
var t = new TEvent();
var eventCode = _client.Event.GetEventCode(t);
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
if (!_listeners.TryGetValue(eventCode, out var callbacks))
{
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
_listeners.Add(eventCode, callbacks);
}
if (!_localListeners.TryGetValue(eventCode, out var localCallbacks))
{
localCallbacks = new List<Action<RagonPlayer, IRagonEvent>>();
_localListeners.Add(eventCode, localCallbacks);
}
callbacks.Add(action);
localCallbacks.Add(action);
if (!_events.ContainsKey(eventCode))
{
_events.Add(eventCode, (player, serializer) =>
{
t.Deserialize(serializer);
foreach (var callbackListener in callbacks)
callbackListener.Invoke(player, t);
});
}
return new EventSubscription(callbacks, localCallbacks, action);
}
internal void Write(RagonBuffer buffer)
{
buffer.WriteUShort(Id);
State.WriteState(buffer);
_propertiesChanged = false;
}
internal void Read(RagonBuffer buffer)
{
State.ReadState(buffer);
}
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
{
if (!IsReplicated) return;
if (_events.TryGetValue(eventCode, out var evnt))
evnt?.Invoke(caller, buffer);
else
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
}
internal void TrackChangedProperty(RagonProperty property)
{
_propertiesChanged = true;
}
public void OnOwnershipChanged(RagonPlayer player)
{
var prevOwner = Owner;
Owner = player;
HasAuthority = player.IsLocal;
OwnershipChanged?.Invoke(prevOwner, player);
}
public void Dispose()
{
_events.Clear();
_listeners.Clear();
_localListeners.Clear();
}
}
}
@@ -0,0 +1,77 @@
/*
* 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;
public sealed class RagonEntityState
{
private List<RagonProperty> _properties;
private RagonEntity _entity;
public RagonEntityState(RagonEntity entity)
{
_entity = entity;
_properties = new List<RagonProperty>(6);
}
public void AddProperty(RagonProperty property)
{
_properties.Add(property);
property.AssignEntity(_entity);
}
internal void WriteInfo(RagonBuffer buffer)
{
buffer.WriteUShort((ushort)_properties.Count);
foreach (var property in _properties)
{
buffer.WriteBool(property.IsFixed);
buffer.WriteUShort((ushort)property.Size);
}
}
internal void ReadState(RagonBuffer buffer)
{
foreach (var property in _properties)
{
var changed = buffer.ReadBool();
if (changed)
property.Read(buffer);
}
}
internal void WriteState(RagonBuffer buffer)
{
foreach (var prop in _properties)
{
if (prop.IsDirty)
{
buffer.WriteBool(true);
prop.Write(buffer);
prop.Flush();
}
else
{
prop.AddTick();
buffer.WriteBool(false);
}
}
}
}
@@ -0,0 +1,49 @@
/*
* 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;
public class RagonPayload
{
public static RagonPayload Empty = new RagonPayload(0);
private readonly uint[] _data = new uint[128];
private readonly int _size = 0;
public RagonPayload(int capacity)
{
_size = capacity;
}
public int Size => _size;
public void Read(RagonBuffer buffer)
{
buffer.ReadArray(_data, _size);
}
public void Write(RagonBuffer buffer)
{
buffer.WriteArray(_data, _size);
}
public override string ToString()
{
return $"Payload Size: {_size}";
}
}
@@ -0,0 +1,146 @@
/*
* 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
{
[Serializable]
public class RagonProperty
{
public string Name => _name;
public RagonEntity Entity => _entity;
public event Action Changed;
public bool IsDirty => _dirty && _ticks >= _priority;
public bool IsFixed => _fixed;
public int Size => _size;
private RagonBuffer _propertyBuffer;
private RagonEntity _entity;
private bool _dirty;
private int _size;
private int _ticks;
private int _priority;
private bool _fixed;
private string _name;
protected bool InvokeLocal;
protected RagonProperty(int priority, bool invokeLocal)
{
_size = 0;
_priority = priority;
_fixed = false;
_propertyBuffer = new RagonBuffer();
InvokeLocal = invokeLocal;
}
public void SetName(string name)
{
_name = name;
}
protected void SetFixedSize(int size)
{
_size = size;
_fixed = true;
}
protected void InvokeChanged()
{
if (_entity.HasAuthority)
return;
Changed?.Invoke();
}
protected void MarkAsChanged()
{
if (InvokeLocal)
Changed?.Invoke();
if (_dirty || _entity == null)
return;
_dirty = true;
_entity.TrackChangedProperty(this);
}
internal void Flush()
{
_dirty = false;
_ticks = 0;
}
internal void AddTick()
{
_ticks++;
}
internal void AssignEntity(RagonEntity ent)
{
_entity = ent;
Changed?.Invoke();
}
internal void Write(RagonBuffer buffer)
{
_propertyBuffer.Clear();
if (_fixed)
{
Serialize(_propertyBuffer);
buffer.CopyFrom(_propertyBuffer, _size);
return;
}
Serialize(_propertyBuffer);
var propertySize = (ushort)_propertyBuffer.WriteOffset;
buffer.WriteUShort(propertySize);
buffer.CopyFrom(_propertyBuffer, propertySize);
}
internal void Read(RagonBuffer buffer)
{
_propertyBuffer.Clear();
if (_fixed)
{
buffer.ToBuffer(_propertyBuffer, _size);
Deserialize(_propertyBuffer);
return;
}
var propSize = buffer.ReadUShort();
buffer.ToBuffer(_propertyBuffer, propSize);
Deserialize(_propertyBuffer);
}
public virtual void Serialize(RagonBuffer buffer)
{
}
public virtual void Deserialize(RagonBuffer buffer)
{
}
}
}
@@ -27,7 +27,7 @@ internal class AuthorizeFailedHandler: IHandler
_listenerList = list; _listenerList = list;
} }
public void Handle(RagonStream reader) public void Handle(RagonBuffer reader)
{ {
var message = reader.ReadString(); var message = reader.ReadString();
_listenerList.OnAuthorizationFailed(message); _listenerList.OnAuthorizationFailed(message);
@@ -32,13 +32,13 @@ internal class AuthorizeSuccessHandler: IHandler
_listenerList = listenerList; _listenerList = listenerList;
} }
public void Handle(RagonStream reader) public void Handle(RagonBuffer reader)
{ {
var playerId = reader.ReadString(); var playerId = reader.ReadString();
var playerName = reader.ReadString(); var playerName = reader.ReadString();
var playerPayload = reader.ReadString(); var playerPayload = reader.ReadString();
_client.UpdateState(RagonState.LOBBY); _client.SetStatus(RagonStatus.LOBBY);
_listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload); _listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload);
} }
} }
@@ -0,0 +1,69 @@
/*
* 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 EntityCreateHandler : IHandler
{
private readonly RagonClient _client;
private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache;
private readonly IRagonEntityListener _entityListener;
public EntityCreateHandler(
RagonClient client,
RagonPlayerCache playerCache,
RagonEntityCache entityCache,
IRagonEntityListener entityListener
)
{
_client = client;
_entityCache = entityCache;
_playerCache = playerCache;
_entityListener = entityListener;
}
public void Handle(RagonBuffer reader)
{
var attachId = reader.ReadUShort();
var entityType = reader.ReadUShort();
var entityId = reader.ReadUShort();
var ownerId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(ownerId);
var payload = new RagonPayload(reader.Capacity);
payload.Read(reader);
if (player == null)
{
RagonLog.Warn($"Owner {ownerId}|{player.Name} not found in players");
_playerCache.Dump();
return;
}
var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.TryGetEntity(attachId, entityType, 0, entityId, hasAuthority, out var hasCreated);
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
if (hasCreated)
_entityListener.OnEntityCreated(entity);
entity.Attach();
}
}
@@ -0,0 +1,56 @@
/*
* 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 EntityEventHandler : IHandler
{
private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache;
public EntityEventHandler(
RagonPlayerCache playerCache,
RagonEntityCache entityCache
)
{
_playerCache = playerCache;
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
{
var eventCode = reader.ReadUShort();
var peerId = reader.ReadUShort();
var executionMode = (RagonReplicationMode)reader.ReadByte();
var entityId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(peerId);
if (player == null)
{
RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}");
_playerCache.Dump();
return;
}
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
return;
_entityCache.OnEvent(player, entityId, eventCode, reader);
}
}
@@ -0,0 +1,60 @@
/*
* 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 EntityOwnershipHandler: IHandler
{
private readonly RagonListenerList _listenerList;
private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache;
public EntityOwnershipHandler(
RagonListenerList listenerList,
RagonPlayerCache playerCache,
RagonEntityCache entityCache)
{
_listenerList = listenerList;
_playerCache = playerCache;
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
{
var newOwnerId = reader.ReadUShort();
var entities = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(newOwnerId);
if (player == null)
{
RagonLog.Error($"Player with Id:{newOwnerId} not found in cache");
_playerCache.Dump();
return;
}
for (var i = 0; i < entities; i++)
{
var entityId = reader.ReadUShort();
_entityCache.OnOwnershipChanged(player, entityId);
RagonLog.Trace("Entity changed owner: " + entityId);
}
}
}
@@ -0,0 +1,39 @@
/*
* 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 EntityRemoveHandler: IHandler
{
private readonly RagonEntityCache _entityCache;
public EntityRemoveHandler(RagonEntityCache entityCache)
{
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
{
var entityId = reader.ReadUShort();
var payload = new RagonPayload(reader.Capacity);
payload.Read(reader);
_entityCache.OnDestroy(entityId, payload);
}
}
@@ -0,0 +1,39 @@
/*
* 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 StateEntityHandler: IHandler
{
private readonly RagonEntityCache _entityCache;
public StateEntityHandler(RagonEntityCache entityCache)
{
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
{
var entitiesCount = reader.ReadUShort();
for (var i = 0; i < entitiesCount; i++)
{
var entityId = reader.ReadUShort();
_entityCache.OnState(entityId, reader);
}
}
}
+1 -1
View File
@@ -21,5 +21,5 @@ namespace Ragon.Client;
public interface IHandler public interface IHandler
{ {
public void Handle(RagonStream reader); public void Handle(RagonBuffer reader);
} }
@@ -28,7 +28,7 @@ internal class JoinFailedHandler: IHandler
_listenerList = listenerList; _listenerList = listenerList;
} }
public void Handle(RagonStream reader) public void Handle(RagonBuffer reader)
{ {
var message = reader.ReadString(); var message = reader.ReadString();
_listenerList.OnFailed(message); _listenerList.OnFailed(message);
@@ -41,30 +41,38 @@ internal class JoinSuccessHandler : IHandler
{ {
private readonly RagonListenerList _listenerList; private readonly RagonListenerList _listenerList;
private readonly RagonPlayerCache _playerCache; private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache;
private readonly RagonClient _client; private readonly RagonClient _client;
private readonly RagonRoom _room;
public JoinSuccessHandler( public JoinSuccessHandler(
RagonClient client, RagonClient client,
RagonRoom room RagonListenerList listenerList,
RagonPlayerCache playerCache,
RagonEntityCache entityCache
) )
{ {
_client = client; _client = client;
_room = room; _listenerList = listenerList;
_entityCache = entityCache;
_playerCache = playerCache;
} }
public void Handle(RagonStream reader) public void Handle(RagonBuffer reader)
{ {
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);
_room.UserData.Read(reader); var scene = new RagonScene(_client, _playerCache, _entityCache, sceneName);
var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max);
var room = new RagonRoom(_client, _entityCache, _playerCache, roomInfo, scene);
room.UserData.Read(reader);
var playersCount = reader.ReadUShort(); var playersCount = reader.ReadUShort();
RagonLog.Trace("Players: " + playersCount); RagonLog.Trace("Players: " + playersCount);
@@ -81,8 +89,10 @@ internal class JoinSuccessHandler : IHandler
RagonLog.Trace($"Player {playerPeerId} - {playerId} - {playerName}"); RagonLog.Trace($"Player {playerPeerId} - {playerId} - {playerName}");
} }
_client.UpdateState(RagonState.ROOM); _client.AssignRoom(room);
_client.SetStatus(RagonStatus.ROOM);
_listenerList.OnJoined(); _listenerList.OnJoined();
_listenerList.OnSceneRequest(sceneName);
} }
} }
@@ -23,19 +23,22 @@ internal class LeaveRoomHandler : IHandler
{ {
private readonly RagonClient _client; private readonly RagonClient _client;
private readonly RagonListenerList _listenerList; private readonly RagonListenerList _listenerList;
private readonly RagonEntityCache _entityCache;
public LeaveRoomHandler( public LeaveRoomHandler(
RagonClient client, RagonClient client,
RagonListenerList listenerList) RagonListenerList listenerList,
RagonEntityCache entityCache)
{ {
_client = client; _client = client;
_listenerList = listenerList; _listenerList = listenerList;
_entityCache = entityCache;
} }
public void Handle(RagonStream reader) public void Handle(RagonBuffer reader)
{ {
_listenerList.OnLeft(); _listenerList.OnLeft();
_entityCache.Cleanup();
_client.Room.Clear(); _client.Room.Cleanup();
} }
} }
@@ -0,0 +1,45 @@
/*
* 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 SceneLoadHandler: IHandler
{
private readonly RagonClient _client;
private readonly RagonListenerList _listenerList;
public SceneLoadHandler(
RagonClient ragonClient,
RagonListenerList listenerList
)
{
_client = ragonClient;
_listenerList = listenerList;
}
public void Handle(RagonBuffer reader)
{
var sceneName = reader.ReadString();
var room = _client.Room;
room.Cleanup();
room.Update(sceneName);
_listenerList.OnSceneRequest(sceneName);
}
}
@@ -23,16 +23,19 @@ internal class OwnershipRoomHandler : IHandler
{ {
private readonly RagonListenerList _listenerList; private readonly RagonListenerList _listenerList;
private readonly RagonPlayerCache _playerCache; private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache;
public OwnershipRoomHandler( public OwnershipRoomHandler(
RagonListenerList listenerList, RagonListenerList listenerList,
RagonPlayerCache playerCache) RagonPlayerCache playerCache,
RagonEntityCache entityCache)
{ {
_listenerList = listenerList; _listenerList = listenerList;
_playerCache = playerCache; _playerCache = playerCache;
_entityCache = entityCache;
} }
public void Handle(RagonStream reader) public void Handle(RagonBuffer reader)
{ {
var newOwnerId = reader.ReadUShort(); var newOwnerId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(newOwnerId); var player = _playerCache.GetPlayerByPeer(newOwnerId);
@@ -33,7 +33,7 @@ internal class PlayerJoinHandler : IHandler
_listenerList = listenerList; _listenerList = listenerList;
} }
public void Handle(RagonStream reader) public void Handle(RagonBuffer reader)
{ {
var playerPeerId = reader.ReadUShort(); var playerPeerId = reader.ReadUShort();
var playerId = reader.ReadString(); var playerId = reader.ReadString();
@@ -22,18 +22,21 @@ namespace Ragon.Client;
internal class PlayerLeftHandler : IHandler internal class PlayerLeftHandler : IHandler
{ {
private RagonPlayerCache _playerCache; private RagonPlayerCache _playerCache;
private RagonEntityCache _entityCache;
private RagonListenerList _listenerList; private RagonListenerList _listenerList;
public PlayerLeftHandler( public PlayerLeftHandler(
RagonEntityCache entityCache,
RagonPlayerCache playerCache, RagonPlayerCache playerCache,
RagonListenerList listenerList RagonListenerList listenerList
) )
{ {
_entityCache = entityCache;
_playerCache = playerCache; _playerCache = playerCache;
_listenerList = listenerList; _listenerList = listenerList;
} }
public void Handle(RagonStream reader) public void Handle(RagonBuffer reader)
{ {
var playerId = reader.ReadString(); var playerId = reader.ReadString();
var player = _playerCache.GetPlayerById(playerId); var player = _playerCache.GetPlayerById(playerId);
@@ -50,10 +53,9 @@ internal class PlayerLeftHandler : IHandler
toDeleteIds[i] = entityId; toDeleteIds[i] = entityId;
} }
var emptyPayload = new RagonPayload(0);
// var emptyPayload = new RagonPayload(0); foreach (var id in toDeleteIds)
// foreach (var id in toDeleteIds) _entityCache.OnDestroy(id, emptyPayload);
// _entityCache.OnDestroy(id, emptyPayload);
} }
else else
{ {
@@ -32,7 +32,7 @@ namespace Ragon.Client
_playerCache = playerCache; _playerCache = playerCache;
_listenerList = listenerList; _listenerList = listenerList;
} }
public void Handle(RagonStream reader) public void Handle(RagonBuffer reader)
{ {
var playerPeerId = reader.ReadUShort(); var playerPeerId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(playerPeerId); var player = _playerCache.GetPlayerByPeer(playerPeerId);
@@ -0,0 +1,60 @@
/*
* 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(RagonBuffer reader)
{
var rawData = reader.RawData;
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);
}
}
@@ -32,11 +32,11 @@ public class RoomEventHandler : IHandler
_playerCache = playerCache; _playerCache = playerCache;
} }
public void Handle(RagonStream buffer) public void Handle(RagonBuffer buffer)
{ {
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)
@@ -13,7 +13,7 @@ internal class RoomListHandler: IHandler
_listenerList = list; _listenerList = list;
} }
public void Handle(RagonStream reader) public void Handle(RagonBuffer reader)
{ {
var roomCount = reader.ReadUShort(); var roomCount = reader.ReadUShort();
var roomList = new RagonRoomInformation[roomCount]; var roomList = new RagonRoomInformation[roomCount];
@@ -29,7 +29,7 @@ namespace Ragon.Client
_listenerList = listenerList; _listenerList = listenerList;
} }
public void Handle(RagonStream reader) public void Handle(RagonBuffer reader)
{ {
var changes = _client.Room?.UserData.Read(reader); var changes = _client.Room?.UserData.Read(reader);
_listenerList.OnRoomUserData(changes); _listenerList.OnRoomUserData(changes);
@@ -0,0 +1,118 @@
/*
* 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 System.Diagnostics;
using Ragon.Protocol;
namespace Ragon.Client;
internal class SnapshotHandler : IHandler
{
private readonly IRagonEntityListener _entityListener;
private readonly RagonClient _client;
private readonly RagonListenerList _listenerList;
private readonly RagonEntityCache _entityCache;
private readonly RagonPlayerCache _playerCache;
public SnapshotHandler(
RagonClient ragonClient,
RagonListenerList listenerList,
RagonEntityCache entityCache,
RagonPlayerCache playerCache,
IRagonEntityListener entityListener
)
{
_client = ragonClient;
_entityListener = entityListener;
_listenerList = listenerList;
_entityCache = entityCache;
_playerCache = playerCache;
}
public void Handle(RagonBuffer buffer)
{
var entities = new List<RagonEntity>();
var dynamicEntities = buffer.ReadUShort();
RagonLog.Trace("Dynamic Entities: " + dynamicEntities);
for (var i = 0; i < dynamicEntities; i++)
{
var entityType = buffer.ReadUShort();
var entityId = buffer.ReadUShort();
var ownerPeerId = buffer.ReadUShort();
var payloadSize = buffer.ReadUShort();
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
if (player == null)
{
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
_playerCache.Dump();
return;
}
var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.TryGetEntity(0, entityType, 0, entityId, hasAuthority, out _);
var payload = RagonPayload.Empty;
if (payloadSize > 0)
{
payload = new RagonPayload(payloadSize);
payload.Read(buffer);
}
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
_entityListener.OnEntityCreated(entity);
entity.Read(buffer);
entities.Add(entity);
}
var staticEntities = buffer.ReadUShort();
RagonLog.Trace("Scene Entities: " + staticEntities);
for (var i = 0; i < staticEntities; i++)
{
var entityType = buffer.ReadUShort();
var entityId = buffer.ReadUShort();
var staticId = buffer.ReadUShort();
var ownerPeerId = buffer.ReadUShort();
var payloadSize = buffer.ReadUShort();
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
if (player == null)
{
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
_playerCache.Dump();
return;
}
var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _);
entity.Prepare(_client, entityId, entityType, hasAuthority, player, RagonPayload.Empty);
entity.Read(buffer);
entities.Add(entity);
}
foreach (var entity in entities)
entity.Attach();
_listenerList.OnSceneLoaded();
}
}
@@ -10,12 +10,12 @@ public class TimestampHandler: IHandler
_client = client; _client = client;
} }
public void Handle(RagonStream buffer) public void Handle(RagonBuffer buffer)
{ {
var timestamp0 = (uint)buffer.ReadInt(); var timestamp0 = buffer.Read(32);
var timestamp1 = (uint)buffer.ReadInt(); var timestamp1 = buffer.Read(32);
var value = new DoubleToUInt { Int0 = timestamp0, Int1 = timestamp1 }; var value = new DoubleToUInt { Int0 = timestamp0, Int1 = timestamp1 };
_client.UpdateTimestamp(value.Double); _client.SetTimestamp(value.Double);
} }
} }
@@ -0,0 +1,22 @@
/*
* 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 IRagonEntityListener
{
public void OnEntityCreated(RagonEntity entity);
}
+1
View File
@@ -20,5 +20,6 @@ namespace Ragon.Client
{ {
public interface IRagonEvent: IRagonSerializable public interface IRagonEvent: IRagonSerializable
{ {
} }
} }
+25
View File
@@ -0,0 +1,25 @@
/*
* 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
{
public interface IRagonPayload: IRagonSerializable
{
}
}
@@ -0,0 +1,22 @@
/*
* 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 IRagonSceneCollector
{
public RagonEntity[] Collect();
}
+2 -2
View File
@@ -22,6 +22,6 @@ public interface IUserData
{ {
public byte[] this[string key] { get; set; } public byte[] this[string key] { get; set; }
bool Dirty { get; } bool Dirty { get; }
IReadOnlyList<string> Read(RagonStream buffer); IReadOnlyList<string> Read(RagonBuffer buffer);
void Write(RagonStream buffer); void Write(RagonBuffer buffer);
} }
@@ -22,6 +22,7 @@ namespace Ragon.Client
IRagonFailedListener, IRagonFailedListener,
IRagonJoinListener, IRagonJoinListener,
IRagonLeftListener, IRagonLeftListener,
IRagonSceneListener,
IRagonOwnershipChangedListener, IRagonOwnershipChangedListener,
IRagonPlayerJoinListener, IRagonPlayerJoinListener,
IRagonPlayerLeftListener, IRagonPlayerLeftListener,
@@ -0,0 +1,22 @@
/*
* 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);
}
@@ -0,0 +1,6 @@
namespace Ragon.Client;
public interface IRagonSceneRequestListener
{
void OnRequestScene(RagonClient client, string sceneName);
}
+91 -39
View File
@@ -22,16 +22,18 @@ namespace Ragon.Client
{ {
private readonly INetworkConnection _connection; private readonly INetworkConnection _connection;
private readonly NetworkStatistics _stats; private readonly NetworkStatistics _stats;
private IRagonEntityListener _entityListener;
private IRagonSceneCollector _sceneCollector;
private IHandler[] _handlers; private IHandler[] _handlers;
private RagonStream _readBuffer; private RagonBuffer _readBuffer;
private RagonStream _writeBuffer; private RagonBuffer _writeBuffer;
private RagonRoom _room; private RagonRoom _room;
private RagonSession _session; private RagonSession _session;
private RagonListenerList _listeners; private RagonListenerList _listeners;
private RagonPlayerCache _playerCache; private RagonPlayerCache _playerCache;
private RagonEntityCache _entityCache;
private RagonEventCache _eventCache; private RagonEventCache _eventCache;
private RagonState _state; private RagonStatus _status;
private double _serverTimestamp; private double _serverTimestamp;
private float _replicationRate = 0; private float _replicationRate = 0;
@@ -39,13 +41,14 @@ namespace Ragon.Client
public double ServerTimestamp => _serverTimestamp; public double ServerTimestamp => _serverTimestamp;
public IRagonConnection Connection => _connection; public IRagonConnection Connection => _connection;
public RagonState State => _state; public RagonStatus Status => _status;
public RagonSession Session => _session; public RagonSession Session => _session;
public RagonEventCache Event => _eventCache; public RagonEventCache Event => _eventCache;
public RagonEntityCache Entity => _entityCache;
public NetworkStatistics Statistics => _stats; public NetworkStatistics Statistics => _stats;
public RagonRoom Room => _room; public RagonRoom Room => _room;
internal RagonStream Buffer => _writeBuffer; internal RagonBuffer Buffer => _writeBuffer;
internal INetworkChannel Reliable => _connection.Reliable; internal INetworkChannel Reliable => _connection.Reliable;
internal INetworkChannel Unreliable => _connection.Unreliable; internal INetworkChannel Unreliable => _connection.Unreliable;
@@ -64,41 +67,77 @@ namespace Ragon.Client
_replicationTime = 0; _replicationTime = 0;
_eventCache = new RagonEventCache(); _eventCache = new RagonEventCache();
_writeBuffer = new RagonStream();
_readBuffer = new RagonStream();
_playerCache = new RagonPlayerCache();
_session = new RagonSession(this, _writeBuffer);
_room = new RagonRoom(this, _playerCache);
_stats = new NetworkStatistics(); _stats = new NetworkStatistics();
_state = RagonState.DISCONNECTED; _status = RagonStatus.DISCONNECTED;
}
_handlers = new IHandler[byte.MaxValue];
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, _listeners); public void Configure(IRagonSceneCollector sceneCollector)
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listeners); {
_handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, _room); _sceneCollector = sceneCollector;
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listeners); }
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners);
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = new OwnershipRoomHandler(_listeners, _playerCache); public void Configure(IRagonEntityListener listener)
_handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, _listeners); {
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_playerCache, _listeners); _entityListener = listener;
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache);
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampHandler(this);
_handlers[(byte)RagonOperation.ROOM_LIST_UPDATED] = new RoomListHandler(_session, _listeners);
_handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataHandler(this, _listeners);
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataHandler(_playerCache, _listeners);
} }
public void Connect(string address, ushort port, string protocol) public void Connect(string address, ushort port, string protocol)
{ {
if (_sceneCollector == null)
{
RagonLog.Error("Scene collector is not defined!");
return;
}
if (_entityListener == null)
{
RagonLog.Error("Entity Listener is not defined!");
return;
}
_writeBuffer = new RagonBuffer();
_readBuffer = new RagonBuffer();
_playerCache = new RagonPlayerCache();
_session = new RagonSession(this, _writeBuffer);
_entityCache = new RagonEntityCache(this, _playerCache, _sceneCollector);
_handlers = new IHandler[byte.MaxValue];
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, _listeners);
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listeners);
_handlers[(byte)RagonOperation.JOIN_SUCCESS] =
new JoinSuccessHandler(this, _listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listeners);
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners, _entityCache);
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] =
new OwnershipRoomHandler(_listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.OWNERSHIP_ENTITY_CHANGED] =
new EntityOwnershipHandler(_listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, _listeners);
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, _listeners);
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, _listeners);
_handlers[(byte)RagonOperation.CREATE_ENTITY] =
new EntityCreateHandler(this, _playerCache, _entityCache, _entityListener);
_handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityRemoveHandler(_entityCache);
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache);
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventHandler(_playerCache, _entityCache);
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache);
_handlers[(byte)RagonOperation.SNAPSHOT] =
new SnapshotHandler(this, _listeners, _entityCache, _playerCache, _entityListener);
_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_DATA_UPDATED] = new RoomUserDataHandler(this, _listeners);
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataHandler(_playerCache, _listeners);
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()
{ {
_state = RagonState.DISCONNECTED; _status = RagonStatus.DISCONNECTED;
_room.Clear(); _room.Cleanup();
_connection.Disconnect(); _connection.Disconnect();
OnDisconnected(RagonDisconnect.MANUAL); OnDisconnected(RagonDisconnect.MANUAL);
@@ -106,12 +145,13 @@ namespace Ragon.Client
public void Update(float dt) public void Update(float dt)
{ {
if (_state != RagonState.DISCONNECTED) if (_status != RagonStatus.DISCONNECTED)
{ {
_replicationTime += dt; _replicationTime += dt;
if (_replicationTime >= _replicationRate) if (_replicationTime >= _replicationRate)
{ {
_replicationTime = 0; _replicationTime = 0;
_entityCache.WriteState(_writeBuffer);
SendTimestamp(); SendTimestamp();
SendRoomUserData(); SendRoomUserData();
@@ -127,9 +167,9 @@ namespace Ragon.Client
public void Dispose() public void Dispose()
{ {
if (_state != RagonState.DISCONNECTED) if (_status != RagonStatus.DISCONNECTED)
{ {
_state = RagonState.DISCONNECTED; _status = RagonStatus.DISCONNECTED;
_connection.Disconnect(); _connection.Disconnect();
} }
@@ -145,6 +185,9 @@ 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(IRagonSceneListener listener) => _listeners.Add(listener);
public void AddListener(IRagonSceneRequestListener 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);
@@ -157,6 +200,9 @@ 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(IRagonSceneListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonSceneRequestListener 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);
@@ -165,12 +211,18 @@ namespace Ragon.Client
#region INTERNAL #region INTERNAL
internal void UpdateState(RagonState state) internal void AssignRoom(RagonRoom room)
{ {
_state = state; _room?.Dispose();
_room = room;
} }
internal void UpdateTimestamp(double time) internal void SetStatus(RagonStatus status)
{
_status = status;
}
internal void SetTimestamp(double time)
{ {
_serverTimestamp = time; _serverTimestamp = time;
} }
@@ -189,8 +241,8 @@ namespace Ragon.Client
_writeBuffer.Clear(); _writeBuffer.Clear();
_writeBuffer.WriteOperation(RagonOperation.TIMESTAMP_SYNCHRONIZATION); _writeBuffer.WriteOperation(RagonOperation.TIMESTAMP_SYNCHRONIZATION);
_writeBuffer.WriteInt((int)value.Int0); _writeBuffer.Write(value.Int0, 32);
_writeBuffer.WriteInt((int)value.Int1); _writeBuffer.Write(value.Int1, 32);
} }
private void SendRoomUserData() private void SendRoomUserData()
@@ -230,7 +282,7 @@ namespace Ragon.Client
RagonLog.Trace("Connected"); RagonLog.Trace("Connected");
_listeners.OnConnected(); _listeners.OnConnected();
_state = RagonState.CONNECTED; _status = RagonStatus.CONNECTED;
} }
private void OnDisconnected(RagonDisconnect reason) private void OnDisconnected(RagonDisconnect reason)
@@ -238,7 +290,7 @@ namespace Ragon.Client
RagonLog.Trace($"Disconnected: {reason}"); RagonLog.Trace($"Disconnected: {reason}");
_listeners.OnDisconnected(reason); _listeners.OnDisconnected(reason);
_state = RagonState.DISCONNECTED; _status = RagonStatus.DISCONNECTED;
} }
private void OnData(byte[] data) private void OnData(byte[] data)
+257
View File
@@ -0,0 +1,257 @@
/*
* 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;
public sealed class RagonEntityCache
{
private readonly List<RagonEntity> _entityList = new();
private readonly Dictionary<uint, RagonEntity> _entityMap = new();
private readonly Dictionary<uint, RagonEntity> _pendingEntities = new();
private readonly Dictionary<uint, RagonEntity> _sceneEntities = new();
private readonly RagonClient _client;
private readonly IRagonSceneCollector _sceneCollector;
private readonly RagonPlayerCache _playerCache;
private int _localEntitiesCounter = 0;
public RagonEntityCache(
RagonClient client,
RagonPlayerCache playerCache,
IRagonSceneCollector sceneCollector
)
{
_client = client;
_sceneCollector = sceneCollector;
_playerCache = playerCache;
}
public bool TryGetEntity(ushort id, out RagonEntity entity)
{
return _entityMap.TryGetValue(id, out entity);
}
public void Create(RagonEntity entity, RagonPayload spawnPayload)
{
var attachId = (ushort)(_playerCache.Local.PeerId + _localEntitiesCounter++);
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.CREATE_ENTITY);
buffer.WriteUShort(attachId);
buffer.WriteUShort(entity.Type);
buffer.WriteByte((byte)entity.Authority);
entity.State.WriteInfo(buffer);
spawnPayload?.Write(buffer);
_pendingEntities.Add(attachId, entity);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
public void Transfer(RagonEntity entity, RagonPlayer player)
{
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.TRANSFER_ENTITY_OWNERSHIP);
buffer.WriteUShort(entity.Id);
buffer.WriteUShort(player.PeerId);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
public void Destroy(RagonEntity entity, RagonPayload destroyPayload)
{
if (!entity.IsAttached && !entity.HasAuthority)
{
RagonLog.Warn("Can't destroy object");
return;
}
entity.SetReplication(false);
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REMOVE_ENTITY);
buffer.WriteUShort(entity.Id);
destroyPayload?.Write(buffer);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
internal void WriteState(RagonBuffer buffer)
{
var changedEntities = 0u;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
var offset = buffer.WriteOffset;
buffer.Write(0, 16);
foreach (var ent in _entityList)
{
if (!ent.IsAttached ||
!ent.IsReplicated ||
!ent.PropertiesChanged) continue;
ent.Write(buffer);
changedEntities++;
}
if (changedEntities <= 0) return;
buffer.Write(changedEntities, 16, offset);
var data = buffer.ToArray();
_client.Unreliable.Send(data);
}
internal void WriteScene(RagonBuffer buffer)
{
_sceneEntities.Clear();
var entities = _sceneCollector.Collect();
buffer.WriteUShort((ushort)entities.Length);
foreach (var entity in entities)
{
buffer.WriteUShort(entity.Type);
buffer.WriteByte((byte)entity.Authority);
buffer.WriteUShort(entity.SceneId);
entity.State.WriteInfo(buffer);
_sceneEntities.Add(entity.SceneId, entity);
}
}
internal void CacheScene()
{
_sceneEntities.Clear();
var entities = _sceneCollector.Collect();
foreach (var entity in entities)
_sceneEntities.Add(entity.SceneId, entity);
}
internal void Cleanup()
{
var payload = new RagonPayload(0);
foreach (var ent in _entityList)
ent.Detach(payload);
_entityMap.Clear();
_entityList.Clear();
}
internal RagonEntity TryGetEntity(ushort attachId, ushort entityType, ushort sceneId, ushort entityId, bool hasAuthority, out bool hasCreated)
{
if (sceneId > 0)
{
if (_sceneEntities.TryGetValue(sceneId, out var sceneEntity))
{
_entityMap.Add(entityId, sceneEntity);
if (hasAuthority)
_entityList.Add(sceneEntity);
hasCreated = false;
return sceneEntity;
}
}
if (_pendingEntities.TryGetValue(attachId, out var pendingEntity) && hasAuthority)
{
_pendingEntities.Remove(attachId);
_entityMap.Add(entityId, pendingEntity);
_entityList.Add(pendingEntity);
hasCreated = false;
return pendingEntity;
}
var entity = new RagonEntity(entityType, sceneId);
_entityMap.Add(entityId, entity);
if (hasAuthority)
_entityList.Add(entity);
hasCreated = true;
return entity;
}
internal void OnDestroy(ushort entityId, RagonPayload payload)
{
if (_entityMap.TryGetValue(entityId, out var entity))
{
_entityMap.Remove(entityId);
_entityList.Remove(entity);
entity.Detach(payload);
entity.Dispose();
}
}
internal void OnState(ushort entityId, RagonBuffer buffer)
{
if (_entityMap.TryGetValue(entityId, out var entity))
entity.Read(buffer);
else
RagonLog.Warn($"Entity {entityId} not found!");
}
internal void OnEvent(RagonPlayer player, ushort entityId, ushort eventCode, RagonBuffer buffer)
{
if (_entityMap.TryGetValue(entityId, out var entity))
entity.Event(eventCode, player, buffer);
else
RagonLog.Warn($"Entity {entityId} not found!");
}
internal void OnOwnershipChanged(RagonPlayer player, ushort entityId)
{
if (_entityMap.TryGetValue(entityId, out var entity))
{
if (player.IsLocal)
_entityList.Add(entity);
else
_entityList.Remove(entity);
entity.OnOwnershipChanged(player);
}
else
{
RagonLog.Warn($"Entity {entityId} not found!");
}
}
}
-2
View File
@@ -26,13 +26,11 @@ public class RagonEventCache
public ushort GetEventCode<TEvent>(TEvent _) where TEvent : IRagonEvent public ushort GetEventCode<TEvent>(TEvent _) where TEvent : IRagonEvent
{ {
var type = typeof(TEvent); var type = typeof(TEvent);
if (!_eventsRegistryByType.TryGetValue(type, out var eventCode)) if (!_eventsRegistryByType.TryGetValue(type, out var eventCode))
{ {
RagonLog.Error($"Event with type {type} not registered"); RagonLog.Error($"Event with type {type} not registered");
return 0; return 0;
} }
return eventCode; return eventCode;
} }
+53
View File
@@ -26,9 +26,12 @@ namespace Ragon.Client
private readonly List<IRagonFailedListener> _failedListeners = new(); private readonly List<IRagonFailedListener> _failedListeners = new();
private readonly List<IRagonJoinListener> _joinListeners = new(); private readonly List<IRagonJoinListener> _joinListeners = new();
private readonly List<IRagonLeftListener> _leftListeners = new(); private readonly List<IRagonLeftListener> _leftListeners = new();
private readonly List<IRagonSceneListener> _sceneListeners = new();
private readonly List<IRagonSceneRequestListener> _sceneRequestListeners = new();
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();
@@ -46,6 +49,7 @@ namespace Ragon.Client
_failedListeners.Add(listener); _failedListeners.Add(listener);
_joinListeners.Add(listener); _joinListeners.Add(listener);
_leftListeners.Add(listener); _leftListeners.Add(listener);
_sceneListeners.Add(listener);
_ownershipChangedListeners.Add(listener); _ownershipChangedListeners.Add(listener);
_playerJoinListeners.Add(listener); _playerJoinListeners.Add(listener);
_playerLeftListeners.Add(listener); _playerLeftListeners.Add(listener);
@@ -62,6 +66,7 @@ namespace Ragon.Client
_failedListeners.Remove(listener); _failedListeners.Remove(listener);
_joinListeners.Remove(listener); _joinListeners.Remove(listener);
_leftListeners.Remove(listener); _leftListeners.Remove(listener);
_sceneListeners.Remove(listener);
_ownershipChangedListeners.Remove(listener); _ownershipChangedListeners.Remove(listener);
_playerJoinListeners.Remove(listener); _playerJoinListeners.Remove(listener);
_playerLeftListeners.Remove(listener); _playerLeftListeners.Remove(listener);
@@ -78,11 +83,21 @@ 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);
} }
public void Add(IRagonSceneRequestListener listener)
{
_sceneRequestListeners.Add(listener);
}
public void Add(IRagonConnectionListener listener) public void Add(IRagonConnectionListener listener)
{ {
_connectionListeners.Add(listener); _connectionListeners.Add(listener);
@@ -103,6 +118,11 @@ namespace Ragon.Client
_leftListeners.Add(listener); _leftListeners.Add(listener);
} }
public void Add(IRagonSceneListener listener)
{
_sceneListeners.Add(listener);
}
public void Add(IRagonOwnershipChangedListener listener) public void Add(IRagonOwnershipChangedListener listener)
{ {
_ownershipChangedListeners.Add(listener); _ownershipChangedListeners.Add(listener);
@@ -133,6 +153,16 @@ namespace Ragon.Client
_playerUserDataListeners.Add(listener); _playerUserDataListeners.Add(listener);
} }
public void Remove(IRagonDataListener listener)
{
_delayedActions.Add(() => _dataListeners.Remove(listener));
}
public void Remove(IRagonSceneRequestListener listener)
{
_delayedActions.Add(() => _sceneRequestListeners.Remove(listener));
}
public void Remove(IRagonAuthorizationListener listener) public void Remove(IRagonAuthorizationListener listener)
{ {
_delayedActions.Add(() => _authorizationListeners.Remove(listener)); _delayedActions.Add(() => _authorizationListeners.Remove(listener));
@@ -158,6 +188,11 @@ namespace Ragon.Client
_delayedActions.Add(() => _leftListeners.Remove(listener)); _delayedActions.Add(() => _leftListeners.Remove(listener));
} }
public void Remove(IRagonSceneListener listener)
{
_delayedActions.Add(() => _sceneListeners.Remove(listener));
}
public void Remove(IRagonOwnershipChangedListener listener) public void Remove(IRagonOwnershipChangedListener listener)
{ {
_delayedActions.Add(() => _ownershipChangedListeners.Remove(listener)); _delayedActions.Add(() => _ownershipChangedListeners.Remove(listener));
@@ -230,6 +265,18 @@ namespace Ragon.Client
listener.OnPlayerJoined(_client, player); listener.OnPlayerJoined(_client, player);
} }
public void OnSceneLoaded()
{
foreach (var listener in _sceneListeners)
listener.OnSceneLoaded(_client);
}
public void OnSceneRequest(string sceneName)
{
foreach (var listener in _sceneRequestListeners)
listener.OnRequestScene(_client, sceneName);
}
public void OnJoined() public void OnJoined()
{ {
foreach (var listener in _joinListeners) foreach (var listener in _joinListeners)
@@ -248,6 +295,12 @@ 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)
+50 -72
View File
@@ -18,7 +18,7 @@ using Ragon.Protocol;
namespace Ragon.Client namespace Ragon.Client
{ {
public class RagonRoom public class RagonRoom : IDisposable
{ {
private class EventSubscription : IDisposable private class EventSubscription : IDisposable
{ {
@@ -47,45 +47,59 @@ namespace Ragon.Client
} }
} }
private delegate void OnEventDelegate(RagonPlayer player, RagonStream serializer); private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
private readonly RagonClient _client; private readonly RagonClient _client;
private readonly RagonScene _scene;
private readonly RagonEntityCache _entityCache;
private readonly RagonPlayerCache _playerCache; private readonly RagonPlayerCache _playerCache;
private RoomParameters _parameters; private readonly RoomParameters _parameters;
private RagonUserData _userData; private readonly 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 => _scene.Name;
public IReadOnlyList<RagonPlayer> Players => _playerCache.Players; public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
public RagonPlayer Local => _playerCache.Local; public RagonPlayer Local => _playerCache.Local;
public RagonPlayer Owner => _playerCache.Owner; public RagonPlayer Owner => _playerCache.Owner;
public RagonUserData UserData => _userData; public RagonUserData UserData => _userData;
private readonly Dictionary<int, OnEventDelegate> _events = new(); private readonly Dictionary<int, OnEventDelegate> _events = new Dictionary<int, OnEventDelegate>();
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new(); private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners =
new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new(); private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners =
new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
public RagonRoom(RagonClient client, RagonPlayerCache playerCache) public RagonRoom(RagonClient client,
RagonEntityCache entityCache,
RagonPlayerCache playerCache,
RoomParameters parameters,
RagonScene scene)
{ {
_client = client; _client = client;
_parameters = parameters;
_entityCache = entityCache;
_playerCache = playerCache; _playerCache = playerCache;
_scene = scene;
_userData = new RagonUserData();
} }
public void Reset(RoomParameters parameters) internal void Cleanup()
{ {
Clear(); _entityCache.Cleanup();
_userData = new RagonUserData();
_parameters = parameters;
_playerCache.Cleanup(); _playerCache.Cleanup();
} }
internal void HandleEvent(ushort eventCode, RagonPlayer caller, RagonStream buffer) internal void Update(string sceneName)
{
_scene.Update(sceneName);
}
internal void HandleEvent(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
{ {
if (_events.TryGetValue(eventCode, out var evnt)) if (_events.TryGetValue(eventCode, out var evnt))
evnt?.Invoke(caller, buffer); evnt?.Invoke(caller, buffer);
@@ -93,7 +107,7 @@ namespace Ragon.Client
RagonLog.Warn($"Handler event {Id} with eventCode {eventCode} not defined"); RagonLog.Warn($"Handler event {Id} with eventCode {eventCode} not defined");
} }
internal void HandleUserData(RagonStream buffer) internal void HandleUserData(RagonBuffer buffer)
{ {
_userData.Read(buffer); _userData.Read(buffer);
} }
@@ -133,64 +147,28 @@ namespace Ragon.Client
return new EventSubscription(callbacks, localCallbacks, action); return new EventSubscription(callbacks, localCallbacks, action);
} }
public void ReplicateEvent<TEvent>(TEvent evnt, RagonTarget target, RagonReplicationMode replicationMode) public void LoadScene(string sceneName) => _scene.Load(sceneName);
where TEvent : IRagonEvent, new() public void SceneLoaded() => _scene.SceneLoaded();
public void ReplicateEvent<TEvent>(TEvent evnt, RagonTarget target, RagonReplicationMode mode)
where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode mode)
where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
public void ReplicateData(byte[] data, bool reliable = false) => _scene.ReplicateData(data, reliable);
public void CreateEntity(RagonEntity entity) => CreateEntity(entity, null);
public void CreateEntity(RagonEntity entity, RagonPayload payload) => _entityCache.Create(entity, payload);
public void TransferEntity(RagonEntity entity, RagonPlayer player) => _entityCache.Transfer(entity, player);
public void DestroyEntity(RagonEntity entityId) => DestroyEntity(entityId, null);
public void DestroyEntity(RagonEntity entityId, RagonPayload payload) => _entityCache.Destroy(entityId, payload);
public void Dispose()
{ {
var evntId = _client.Event.GetEventCode(evnt); Cleanup();
var buffer = _client.Buffer;
{
if (replicationMode == RagonReplicationMode.Local &&
_localListeners.TryGetValue(evntId, out var localListeners))
{
foreach (var listener in localListeners)
listener.Invoke(_client.Room.Local, evnt);
return;
}
}
{
if (replicationMode == RagonReplicationMode.LocalAndServer &&
_localListeners.TryGetValue(evntId, out var localListeners))
{
foreach (var listener in localListeners)
listener.Invoke(_client.Room.Local, evnt);
}
}
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
buffer.WriteUShort(evntId);
buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte)target);
evnt.Serialize(buffer);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
where TEvent : IRagonEvent, new()
{
var evntId = _client.Event.GetEventCode(evnt);
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
buffer.WriteUShort(evntId);
buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte)RagonTarget.Player);
buffer.WriteUShort(target.PeerId);
evnt.Serialize(buffer);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
public void Clear()
{
_events.Clear(); _events.Clear();
_listeners.Clear(); _listeners.Clear();
_localListeners.Clear(); _localListeners.Clear();
+119
View File
@@ -0,0 +1,119 @@
/*
* 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;
public class RagonScene
{
public string Name { get; private set; }
private readonly RagonClient _client;
private readonly RagonEntityCache _entityCache;
private readonly RagonPlayerCache _playerCache;
public RagonScene(RagonClient client, RagonPlayerCache playerCache, RagonEntityCache entityCache, string sceneName)
{
Name = sceneName;
_client = client;
_playerCache = playerCache;
_entityCache = entityCache;
}
internal void Update(string scene)
{
Name = scene;
}
internal void Load(string sceneName)
{
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.LOAD_SCENE);
buffer.WriteString(sceneName);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
internal void SceneLoaded()
{
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.SCENE_LOADED);
if (_playerCache.IsRoomOwner)
_entityCache.WriteScene(buffer);
else
_entityCache.CacheScene();
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
internal void ReplicateEvent<TEvent>(TEvent evnt, RagonTarget target, RagonReplicationMode replicationMode)
where TEvent : IRagonEvent, new()
{
var evntId = _client.Event.GetEventCode(evnt);
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
buffer.WriteUShort(evntId);
buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte)target);
evnt.Serialize(buffer);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
internal void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
where TEvent : IRagonEvent, new()
{
var evntId = _client.Event.GetEventCode(evnt);
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
buffer.WriteUShort(evntId);
buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte)RagonTarget.Player);
buffer.WriteUShort(target.PeerId);
evnt.Serialize(buffer);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
public void ReplicateData(byte[] data, bool reliable)
{
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);
}
}
+10 -10
View File
@@ -21,18 +21,17 @@ namespace Ragon.Client
public class RagonSession public class RagonSession
{ {
private readonly RagonClient _client; private readonly RagonClient _client;
private readonly RagonBuffer _buffer;
private readonly RagonStream _buffer; public RagonSession(RagonClient client, RagonBuffer buffer)
public RagonSession(RagonClient client, RagonStream buffer)
{ {
_client = client; _client = client;
_buffer = buffer; _buffer = buffer;
} }
public void CreateOrJoin(string sessionName, int minPlayers, int maxPlayers) public void CreateOrJoin(string sceneName, int minPlayers, int maxPlayers)
{ {
var parameters = new RagonRoomParameters() { Min = minPlayers, Max = maxPlayers }; var parameters = new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers};
CreateOrJoin(parameters); CreateOrJoin(parameters);
} }
@@ -47,14 +46,14 @@ namespace Ragon.Client
_client.Reliable.Send(sendData); _client.Reliable.Send(sendData);
} }
public void Create(string sessionName, int minPlayers, int maxPlayers) public void Create(string sceneName, int minPlayers, int maxPlayers)
{ {
Create(null, new RagonRoomParameters() { Min = minPlayers, Max = maxPlayers }); Create(null, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
} }
public void Create(string roomId, string sessionName, int minPlayers, int maxPlayers) public void Create(string roomId, string sceneName, int minPlayers, int maxPlayers)
{ {
Create(roomId, new RagonRoomParameters() { Min = minPlayers, Max = maxPlayers }); Create(roomId, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
} }
public void Create(string roomId, RagonRoomParameters parameters) public void Create(string roomId, RagonRoomParameters parameters)
@@ -80,7 +79,7 @@ namespace Ragon.Client
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);
} }
@@ -105,5 +104,6 @@ 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 RagonState public enum RagonStatus
{ {
DISCONNECTED, DISCONNECTED,
CONNECTED, CONNECTED,
+4 -4
View File
@@ -57,7 +57,7 @@ namespace Ragon.Client
{ {
} }
public IReadOnlyList<string> Read(RagonStream buffer) public IReadOnlyList<string> Read(RagonBuffer buffer)
{ {
var len = buffer.ReadUShort(); var len = buffer.ReadUShort();
var changes = new List<string>(len); var changes = new List<string>(len);
@@ -67,7 +67,7 @@ namespace Ragon.Client
var valueSize = buffer.ReadUShort(); var valueSize = buffer.ReadUShort();
if (valueSize > 0) if (valueSize > 0)
{ {
var value = buffer.ReadBinary(valueSize); var value = buffer.ReadBytes(valueSize);
_properties[key] = value; _properties[key] = value;
} }
else else
@@ -81,7 +81,7 @@ namespace Ragon.Client
return changes; return changes;
} }
public void Write(RagonStream buffer) public void Write(RagonBuffer buffer)
{ {
buffer.WriteUShort((ushort)_localChanges.Count); buffer.WriteUShort((ushort)_localChanges.Count);
foreach (var propertyChanged in _localChanges) foreach (var propertyChanged in _localChanges)
@@ -90,7 +90,7 @@ namespace Ragon.Client
if (_properties.TryGetValue(propertyChanged, out var property)) if (_properties.TryGetValue(propertyChanged, out var property))
{ {
buffer.WriteUShort((ushort)property.Length); buffer.WriteUShort((ushort)property.Length);
buffer.WriteBinary(property); buffer.WriteBytes(property);
} }
else else
{ {
@@ -35,12 +35,12 @@ public class RagonUserDataReadOnly : IUserData
{ {
} }
public void Write(RagonStream buffer) public void Write(RagonBuffer buffer)
{ {
} }
public IReadOnlyList<string> Read(RagonStream buffer) public IReadOnlyList<string> Read(RagonBuffer buffer)
{ {
var len = buffer.ReadUShort(); var len = buffer.ReadUShort();
var changes = new List<string>(len); var changes = new List<string>(len);
@@ -50,7 +50,7 @@ public class RagonUserDataReadOnly : IUserData
var valueSize = buffer.ReadUShort(); var valueSize = buffer.ReadUShort();
if (valueSize > 0) if (valueSize > 0)
{ {
var value = buffer.ReadBinary(valueSize); var value = buffer.ReadBytes(valueSize);
_properties[key] = value; _properties[key] = value;
} }
@@ -0,0 +1,27 @@
using Ragon.Client.Compressor;
using Ragon.Protocol;
namespace Ragon.Client.Utils;
public static class CompressorExtension
{
public static float Read(this FloatCompressor compressor, RagonBuffer buffer)
{
return compressor.Decompress(buffer.Read(compressor.RequiredBits));
}
public static void Write(this FloatCompressor compressor, RagonBuffer buffer, float value)
{
buffer.Write(compressor.Compress(value), compressor.RequiredBits);
}
public static float Read(this IntCompressor compressor, RagonBuffer buffer)
{
return compressor.Decompress(buffer.Read(compressor.RequiredBits));
}
public static void Write(this IntCompressor compressor, RagonBuffer buffer, int value)
{
buffer.Write(compressor.Compress(value), compressor.RequiredBits);
}
}
+16 -1
View File
@@ -65,6 +65,8 @@ namespace Ragon.Protocol
{ {
public class RagonBuffer public class RagonBuffer
{ {
private const int MaxBufferSize = 1024 * 1024; // 1MB max buffer size
private int _read; private int _read;
private int _write; private int _write;
private uint[] _buckets; private uint[] _buckets;
@@ -404,6 +406,12 @@ namespace Ragon.Protocol
public void FromArray(byte[] data) public void FromArray(byte[] data)
{ {
var length = data.Length; var length = data.Length;
if (length > MaxBufferSize)
{
throw new InvalidOperationException($"Input data exceeds maximum buffer size: {length} bytes > {MaxBufferSize} bytes");
}
var bucketsCount = length / 4 + 1; var bucketsCount = length / 4 + 1;
if (_buckets.Length < bucketsCount) if (_buckets.Length < bucketsCount)
@@ -493,7 +501,14 @@ namespace Ragon.Protocol
private void Resize(int capacity) private void Resize(int capacity)
{ {
var buckets = new uint[_buckets.Length * 2 + capacity]; var newSize = _buckets.Length * 2 + capacity;
if (newSize * 4 > MaxBufferSize)
{
throw new InvalidOperationException($"Buffer size limit exceeded: {newSize * 4} bytes > {MaxBufferSize} bytes");
}
var buckets = new uint[newSize];
Array.Copy(_buckets, buckets, _buckets.Length); Array.Copy(_buckets, buckets, _buckets.Length);
_buckets = buckets; _buckets = buckets;
} }
+12 -2
View File
@@ -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,13 +26,23 @@ namespace Ragon.Protocol
CREATE_ROOM = 5, CREATE_ROOM = 5,
JOIN_ROOM = 6, JOIN_ROOM = 6,
LEAVE_ROOM = 7, LEAVE_ROOM = 7,
OWNERSHIP_ROOM_CHANGED = 9, OWNERSHIP_ENTITY_CHANGED = 8,
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,
+10 -7
View File
@@ -17,21 +17,24 @@
namespace Ragon.Protocol namespace Ragon.Protocol
{ {
public class RagonRoomParameters public class RagonRoomParameters: IRagonSerializable
{ {
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(RagonBuffer buffer)
{ {
buffer.WriteInt(Min); buffer.WriteString(Scene);
buffer.WriteInt(Max); buffer.WriteInt(Min, 1, 32);
buffer.WriteInt(Max, 1, 32);
} }
public void Deserialize(RagonStream buffer) public void Deserialize(RagonBuffer buffer)
{ {
Min = buffer.ReadInt(); Scene = buffer.ReadString();
Max = buffer.ReadInt(); Min = buffer.ReadInt(1, 32);
Max = buffer.ReadInt(1, 32);
} }
} }
} }
+2 -2
View File
@@ -19,7 +19,7 @@ namespace Ragon.Protocol
{ {
public interface IRagonSerializable public interface IRagonSerializable
{ {
public void Serialize(RagonStream buffer); public void Serialize(RagonBuffer buffer);
public void Deserialize(RagonStream buffer); public void Deserialize(RagonBuffer buffer);
} }
} }
+3 -4
View File
@@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<RootNamespace>Ragon.Relay</RootNamespace> <RootNamespace>Ragon.Relay</RootNamespace>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
@@ -23,13 +23,12 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Ragon.Server.ENetServer\Ragon.Server.ENetServer.csproj" />
<ProjectReference Include="..\Ragon.Server.WebSocketServer\Ragon.Server.WebSocketServer.csproj" />
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" /> <ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<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>
@@ -0,0 +1,6 @@
namespace Ragon.Relay;
public class KickPlayerCommand
{
public string Id;
}
+16 -2
View File
@@ -1,5 +1,8 @@
using System; using System;
using Ragon.Server;
using Ragon.Server.Entity;
using Ragon.Server.Plugin; using Ragon.Server.Plugin;
using Ragon.Server.Room;
namespace Ragon.Relay; namespace Ragon.Relay;
@@ -9,15 +12,26 @@ public class RelayRoomPlugin: BaseRoomPlugin
{ {
} }
public void OnAttached() public void OnAttached()
{ {
Console.WriteLine("Room attached"); Console.WriteLine("Room attached");
} }
public void OnDetached() public void OnDetached()
{ {
Console.WriteLine("Room detached"); Console.WriteLine("Room detached");
} }
public bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity)
{
Console.WriteLine($"Entity created: {entity.Id}");
return true;
}
public bool OnEntityRemove(RagonRoomPlayer destroyer, RagonEntity entity)
{
Console.WriteLine($"Entity destroyed: {entity.Id}");
return true;
}
} }
+14 -25
View File
@@ -1,37 +1,26 @@
using System;
using Newtonsoft.Json;
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)
{ {
Console.WriteLine(command);
if (command == "kick-player")
{
var commandPayload = JsonConvert.DeserializeObject<KickPlayerCommand>(payload);
var player = Server.GetContextById(commandPayload.Id);
if (player != null)
player.Connection.Close();
else
Console.WriteLine($"Player not found with Id {commandPayload.Id}");
}
return true; return true;
} }
+3 -2
View File
@@ -14,15 +14,16 @@
* limitations under the License. * limitations under the License.
*/ */
using System;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using Newtonsoft.Json; using Newtonsoft.Json;
using Ragon.Server; using Ragon.Server;
using Ragon.Server.ENetServer;
using Ragon.Server.IO; using Ragon.Server.IO;
using Ragon.Server.Logging; using Ragon.Server.Logging;
using Ragon.Server.Plugin; using Ragon.Server.Plugin;
using Ragon.Server.WebSocketServer; using Ragon.Server.WebSocketServer;
using Ragon.Transport;
namespace Ragon.Relay namespace Ragon.Relay
{ {
@@ -68,7 +69,7 @@ namespace Ragon.Relay
}; };
var relay = new RagonServer(networkServer, plugin, serverConfiguration); var relay = new RagonServer(networkServer, plugin, serverConfiguration);
relay.Listen(); relay.Start();
while (relay.IsRunning) while (relay.IsRunning)
{ {
relay.Tick(); relay.Tick();
-44
View File
@@ -1,44 +0,0 @@
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
-56
View File
@@ -1,56 +0,0 @@
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());
}
}
}
@@ -1,141 +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 System;
using ENet;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Transport;
public sealed class ENetServer : INetworkServer
{
private readonly Host _host = new();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(ENetServer));
private ENetConnection[] _connections = Array.Empty<ENetConnection>();
private INetworkListener _listener;
private uint _protocol;
private Event _event;
public void Listen(INetworkListener listener, NetworkConfiguration configuration)
{
Library.Initialize();
_connections = new ENetConnection[configuration.LimitConnections];
_listener = listener;
_protocol = configuration.Protocol;
var address = new Address
{
Port = (ushort)configuration.Port,
};
_host.Create(address, _connections.Length, 2, 0, 0, 1024 * 1024);
var protocolDecoded = RagonVersion.Parse(_protocol);
_logger.Info($"Listen at {configuration.Address}:{configuration.Port}");
_logger.Info($"Protocol: {protocolDecoded}");
}
public void Update()
{
bool polled = false;
while (!polled)
{
if (_host.CheckEvents(out _event) <= 0)
{
if (_host.Service(0, out _event) <= 0)
break;
polled = true;
}
switch (_event.Type)
{
case EventType.None:
{
_logger.Trace("None event");
break;
}
case EventType.Connect:
{
if (!IsValidProtocol(_event.Data))
{
_logger.Warning(
$"Mismatched protocol Server: {RagonVersion.Parse(_protocol)} Client: {RagonVersion.Parse(_event.Data)}, close connection");
_event.Peer.DisconnectNow(0);
break;
}
var connection = new ENetConnection(_event.Peer);
_connections[_event.Peer.ID] = connection;
_listener.OnConnected(connection);
break;
}
case EventType.Disconnect:
{
var connection = _connections[_event.Peer.ID];
_listener.OnDisconnected(connection);
break;
}
case EventType.Timeout:
{
var connection = _connections[_event.Peer.ID];
_listener.OnTimeout(connection);
break;
}
case EventType.Receive:
{
var peerId = (ushort)_event.Peer.ID;
var connection = _connections[peerId];
var dataRaw = new byte[_event.Packet.Length];
_event.Packet.CopyTo(dataRaw);
_event.Packet.Dispose();
_listener.OnData(connection, (NetworkChannel)_event.ChannelID, dataRaw);
break;
}
}
}
}
public void Broadcast(byte[] data, NetworkChannel channel)
{
var packet = new Packet();
var flag = channel == NetworkChannel.RELIABLE ? PacketFlags.Reliable : PacketFlags.None;
packet.Create(data, flag);
_host.Broadcast((byte)channel, ref packet);
}
public void Stop()
{
_host?.Dispose();
Library.Deinitialize();
}
private bool IsValidProtocol(uint protocol)
{
return protocol == _protocol;
}
}
+2 -1
View File
@@ -10,5 +10,6 @@
"limitRooms": 200, "limitRooms": 200,
"limitBufferedEvents": 50, "limitBufferedEvents": 50,
"limitUserDataSize": 1024, "limitUserDataSize": 1024,
"limitPropertySize": 512 "limitPropertySize": 512,
"limitConnectionsPerProject": 100
} }
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Ragon.ENet</RootNamespace>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Eduard Kargin</Copyright>
<Authors>Eduard Kargin</Authors>
<Version>1.4.0</Version>
<Title>Ragon Server ENet</Title>
<Description>Ragon Server ENet transport</Description>
<PackageProjectUrl>https://ragon.io</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<RepositoryUrl>https://github.com/edmand46/Ragon</RepositoryUrl>
<RepositoryType>Source</RepositoryType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ENet-CSharp" Version="2.4.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
</ItemGroup>
</Project>
@@ -17,7 +17,7 @@
using ENet; using ENet;
using Ragon.Server.IO; using Ragon.Server.IO;
namespace Ragon.Transport; namespace Ragon.Server.ENetServer;
public sealed class ENetConnection: INetworkConnection public sealed class ENetConnection: INetworkConnection
{ {
@@ -14,12 +14,12 @@
* limitations under the License. * limitations under the License.
*/ */
using System.Net;
using ENet; using ENet;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO; using Ragon.Server.IO;
namespace Ragon.Transport; namespace Ragon.Server.ENetServer;
public sealed class ENetReliableChannel: INetworkChannel public sealed class ENetReliableChannel: INetworkChannel
{ {
@@ -42,12 +42,12 @@ public sealed class ENetReliableChannel: INetworkChannel
_peer.Send(_channelId, ref newPacket); _peer.Send(_channelId, ref newPacket);
} }
public void Send(RagonStream buffer) public void Send(RagonBuffer buffer)
{ {
_data = buffer.ToArray(); buffer.ToArray(_data);
var newPacket = new Packet(); var newPacket = new Packet();
newPacket.Create(_data, _data.Length, PacketFlags.Reliable); newPacket.Create(_data, buffer.Length, PacketFlags.Reliable);
_peer.Send(_channelId, ref newPacket); _peer.Send(_channelId, ref newPacket);
} }
@@ -0,0 +1,141 @@
/*
* 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 ENet;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Server.ENetServer
{
public sealed class ENetServer : INetworkServer
{
private readonly Host _host = new();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(ENetServer));
private ENetConnection[] _connections = Array.Empty<ENetConnection>();
private INetworkListener _listener;
private uint _protocol;
private ENet.Event _event;
public void Listen(INetworkListener listener, NetworkConfiguration configuration)
{
Library.Initialize();
_connections = new ENetConnection[configuration.LimitConnections];
_listener = listener;
_protocol = configuration.Protocol;
var address = new Address
{
Port = (ushort)configuration.Port,
};
_host.Create(address, _connections.Length, 2, 0, 0, 1024 * 1024);
var protocolDecoded = RagonVersion.Parse(_protocol);
_logger.Info($"Listen at {configuration.Address}:{configuration.Port}");
_logger.Info($"Protocol: {protocolDecoded}");
}
public void Update()
{
bool polled = false;
while (!polled)
{
if (_host.CheckEvents(out _event) <= 0)
{
if (_host.Service(0, out _event) <= 0)
break;
polled = true;
}
switch (_event.Type)
{
case EventType.None:
{
_logger.Trace("None event");
break;
}
case EventType.Connect:
{
if (!IsValidProtocol(_event.Data))
{
_logger.Warning($"Mismatched protocol Server: {RagonVersion.Parse(_protocol)} Client: {RagonVersion.Parse(_event.Data)}, close connection");
_event.Peer.DisconnectNow(0);
break;
}
var connection = new ENetConnection(_event.Peer);
_connections[_event.Peer.ID] = connection;
_listener.OnConnected(connection);
break;
}
case EventType.Disconnect:
{
var connection = _connections[_event.Peer.ID];
_listener.OnDisconnected(connection);
break;
}
case EventType.Timeout:
{
var connection = _connections[_event.Peer.ID];
_listener.OnTimeout(connection);
break;
}
case EventType.Receive:
{
var peerId = (ushort)_event.Peer.ID;
var connection = _connections[peerId];
var dataRaw = new byte[_event.Packet.Length];
_event.Packet.CopyTo(dataRaw);
_event.Packet.Dispose();
_listener.OnData(connection, (NetworkChannel)_event.ChannelID, dataRaw);
break;
}
}
}
}
public void Broadcast(byte[] data, NetworkChannel channel)
{
var packet = new Packet();
var flag = channel == NetworkChannel.RELIABLE? PacketFlags.Reliable: PacketFlags.None;
packet.Create(data, flag);
_host.Broadcast((byte)channel, ref packet);
}
public void Stop()
{
_host?.Dispose();
Library.Deinitialize();
}
private bool IsValidProtocol(uint protocol)
{
return protocol == _protocol;
}
}
}
@@ -18,7 +18,7 @@ using ENet;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO; using Ragon.Server.IO;
namespace Ragon.Transport; namespace Ragon.Server.ENetServer;
public sealed class ENetUnreliableChannel: INetworkChannel public sealed class ENetUnreliableChannel: INetworkChannel
{ {
@@ -40,12 +40,12 @@ public sealed class ENetUnreliableChannel: INetworkChannel
_peer.Send(_channelId, ref newPacket); _peer.Send(_channelId, ref newPacket);
} }
public void Send(RagonStream buffer) public void Send(RagonBuffer buffer)
{ {
_data = buffer.ToArray(); buffer.ToArray(_data);
var newPacket = new Packet(); var newPacket = new Packet();
newPacket.Create(_data, _data.Length, PacketFlags.None); newPacket.Create(_data, buffer.Length, PacketFlags.None);
_peer.Send(_channelId, ref newPacket); _peer.Send(_channelId, ref newPacket);
} }
@@ -21,8 +21,4 @@
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" /> <ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Sources\" />
</ItemGroup>
</Project> </Project>
@@ -14,21 +14,18 @@
* limitations under the License. * limitations under the License.
*/ */
using System;
using System.Collections.Generic;
using System.Threading.Channels; using System.Threading.Channels;
using System.Threading.Tasks;
namespace Ragon.Server.IO; namespace Ragon.Server.IO;
public class Executor : TaskScheduler public class Executor : TaskScheduler, IExecutor
{ {
private readonly ChannelReader<Task> _reader; private readonly ChannelReader<Task> _reader;
private readonly ChannelWriter<Task> _writer; private readonly ChannelWriter<Task> _writer;
private readonly Queue<Task> _pendingTasks; private readonly Queue<Task> _pendingTasks;
private readonly TaskFactory _taskFactory; private readonly TaskFactory _taskFactory;
public Task Run(Action action, TaskCreationOptions task) public Task Run(Action action, TaskCreationOptions task = TaskCreationOptions.None)
{ {
return _taskFactory.StartNew(action, task); return _taskFactory.StartNew(action, task);
} }
@@ -0,0 +1,22 @@
/*
* 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.Server.IO;
public interface IExecutor
{
public Task Run(Action action, TaskCreationOptions task = TaskCreationOptions.None);
}
@@ -14,10 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
using System;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using Ragon.Server.IO; using Ragon.Server.IO;
using Ragon.Server.Logging; using Ragon.Server.Logging;
@@ -14,10 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
using System.Collections.Generic;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO; using Ragon.Server.IO;
@@ -39,7 +36,7 @@ public class WebSocketReliableChannel : INetworkChannel
_queue.Enqueue(data); _queue.Enqueue(data);
} }
public void Send(RagonStream buffer) public void Send(RagonBuffer buffer)
{ {
var sendData = buffer.ToArray(); var sendData = buffer.ToArray();
_queue.Enqueue(sendData); _queue.Enqueue(sendData);
@@ -14,12 +14,9 @@
* limitations under the License. * limitations under the License.
*/ */
using System;
using System.Buffers; using System.Buffers;
using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO; using Ragon.Server.IO;
using Ragon.Server.Logging; using Ragon.Server.Logging;
@@ -46,7 +43,7 @@ public class WebSocketServer : INetworkServer
_executor = new Executor(); _executor = new Executor();
} }
public async void StartAccept(CancellationToken cancellationToken) public async ValueTask StartAccept(CancellationToken cancellationToken)
{ {
while (!cancellationToken.IsCancellationRequested) while (!cancellationToken.IsCancellationRequested)
{ {
@@ -76,11 +73,11 @@ public class WebSocketServer : INetworkServer
_connections[connection.Id] = connection; _connections[connection.Id] = connection;
StartListen(connection, cancellationToken); _ = StartListen(connection, cancellationToken);
} }
} }
async void StartListen(WebSocketConnection connection, CancellationToken cancellationToken) async ValueTask StartListen(WebSocketConnection connection, CancellationToken cancellationToken)
{ {
_activeConnections.Add(connection); _activeConnections.Add(connection);
_networkListener.OnConnected(connection); _networkListener.OnConnected(connection);
@@ -128,7 +125,7 @@ public class WebSocketServer : INetworkServer
{ {
_executor.Update(); _executor.Update();
Flush(); _ = Flush();
} }
public void Broadcast(byte[] data, NetworkChannel channel) public void Broadcast(byte[] data, NetworkChannel channel)
@@ -137,7 +134,7 @@ public class WebSocketServer : INetworkServer
activeConnection.Reliable.Send(data); activeConnection.Reliable.Send(data);
} }
public async void Flush() public async ValueTask Flush()
{ {
foreach (var conn in _activeConnections) foreach (var conn in _activeConnections)
await conn.Flush(); await conn.Flush();
@@ -163,6 +160,8 @@ public class WebSocketServer : INetworkServer
_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}");
+18 -8
View File
@@ -12,16 +12,28 @@ public class RagonData
{ {
} }
public void Read(RagonStream buffer) public void Read(RagonBuffer buffer, int maxSize = 0)
{ {
var len = buffer.ReadUShort(); var len = buffer.ReadUShort();
var totalSize = 0;
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
{ {
var key = buffer.ReadString(); var key = buffer.ReadString();
var valueSize = buffer.ReadUShort(); var valueSize = buffer.ReadUShort();
if (maxSize > 0)
{
totalSize += valueSize;
if (totalSize > maxSize)
{
throw new InvalidOperationException($"User data exceeds limit: {totalSize} > {maxSize}");
}
}
if (valueSize > 0) if (valueSize > 0)
{ {
var value = buffer.ReadBinary(valueSize); var value = buffer.ReadBytes(valueSize);
_data[key] = value; _data[key] = value;
} }
else else
@@ -31,14 +43,14 @@ public class RagonData
} }
} }
public void Write(RagonStream buffer) public void Write(RagonBuffer buffer)
{ {
buffer.WriteUShort((ushort)_data.Count); buffer.WriteUShort((ushort)_data.Count);
foreach (var prop in _data) foreach (var prop in _data)
{ {
buffer.WriteString(prop.Key); buffer.WriteString(prop.Key);
buffer.WriteUShort((ushort)prop.Value.Length); buffer.WriteUShort((ushort)prop.Value.Length);
buffer.WriteBinary(prop.Value); buffer.WriteBytes(prop.Value);
} }
var toDelete = _data var toDelete = _data
@@ -51,16 +63,14 @@ public class RagonData
IsDirty = false; IsDirty = false;
} }
public void Snapshot(RagonStream buffer) public void Snapshot(RagonBuffer buffer)
{ {
buffer.WriteUShort((ushort)_data.Count); buffer.WriteUShort((ushort)_data.Count);
foreach (var prop in _data) foreach (var prop in _data)
{ {
buffer.WriteString(prop.Key); buffer.WriteString(prop.Key);
buffer.WriteUShort((ushort)prop.Value.Length); buffer.WriteUShort((ushort)prop.Value.Length);
buffer.WriteBinary(prop.Value); buffer.WriteBytes(prop.Value);
Console.WriteLine($"Key: {prop.Key} Value: {prop.Value.Length}");
} }
} }
} }
@@ -0,0 +1,16 @@
using Ragon.Protocol;
using Ragon.Server.Room;
namespace Ragon.Server.Entity;
public interface IRagonEntity
{
public ushort Id { get; }
public ushort Type { get; }
public ushort StaticId { get; }
public ushort AttachId { get; }
public RagonRoomPlayer Owner { get; }
public RagonAuthority Authority { get; }
public RagonPayload Payload { get; }
public IRagonEntityState State { get; }
}
@@ -0,0 +1,8 @@
using Ragon.Protocol;
namespace Ragon.Server.Entity;
public interface IRagonEntityState
{
}
+246
View File
@@ -0,0 +1,246 @@
/*
* 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.Event;
using Ragon.Server.Room;
namespace Ragon.Server.Entity;
public class RagonEntity : IRagonEntity
{
private static ushort _idGenerator = 100;
public ushort Id { get; private set; }
public ushort Type { get; private set; }
public ushort StaticId { get; private set; }
public ushort AttachId { get; private set; }
public RagonRoomPlayer Owner { get; private set; }
public RagonAuthority Authority { get; private set; }
public RagonPayload Payload { get; private set; }
public IRagonEntityState State => _state;
private readonly List<RagonEvent> _bufferedEvents;
private readonly int _limitBufferedEvents;
private readonly RagonEntityState _state;
public RagonEntity(RagonEntityParameters parameters)
{
Id = _idGenerator++;
StaticId = parameters.StaticId;
Type = parameters.Type;
AttachId = parameters.AttachId;
Authority = parameters.Authority;
Payload = new RagonPayload();
_state = new RagonEntityState(this);
_bufferedEvents = new List<RagonEvent>();
_limitBufferedEvents = parameters.BufferedEvents;
}
public void Attach(RagonRoomPlayer owner)
{
Owner = owner;
}
public void Detach()
{
}
public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer, RagonBuffer writer)
{
foreach (var evnt in _bufferedEvents)
{
writer.Clear();
writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
writer.WriteUShort(evnt.EventCode);
writer.WriteUShort(evnt.Invoker.Connection.Id);
writer.WriteByte((byte)RagonReplicationMode.Server);
writer.WriteUShort(Id);
evnt.Write(writer);
var sendData = writer.ToArray();
roomPlayer.Connection.Reliable.Send(sendData);
}
}
public void Create()
{
var room = Owner.Room;
var buffer = room.Writer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.CREATE_ENTITY);
buffer.WriteUShort(AttachId);
buffer.WriteUShort(Type);
buffer.WriteUShort(Id);
buffer.WriteUShort(Owner.Connection.Id);
Payload.Write(buffer);
var sendData = buffer.ToArray();
foreach (var player in room.ReadyPlayersList)
player.Connection.Reliable.Send(sendData);
}
public void Destroy()
{
var room = Owner.Room;
var buffer = room.Writer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REMOVE_ENTITY);
buffer.WriteUShort(Id);
Payload.Write(buffer);
var sendData = buffer.ToArray();
foreach (var player in room.ReadyPlayersList)
player.Connection.Reliable.Send(sendData);
}
public void Snapshot(RagonBuffer buffer)
{
buffer.WriteUShort(Type);
buffer.WriteUShort(Id);
if (StaticId != 0)
buffer.WriteUShort(StaticId);
buffer.WriteUShort(Owner.Connection.Id);
buffer.WriteUShort(Payload.Size);
Payload.Write(buffer);
_state.Snapshot(buffer);
}
public void ReplicateEvent(
RagonRoomPlayer invoker,
RagonEvent evnt,
RagonReplicationMode eventMode,
RagonRoomPlayer targetPlayer
)
{
if (Authority == RagonAuthority.OwnerOnly && invoker.Connection.Id != Owner.Connection.Id)
{
return;
}
var room = Owner.Room;
var buffer = room.Writer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
buffer.WriteUShort(evnt.EventCode);
buffer.WriteUShort(invoker.Connection.Id);
buffer.WriteByte((byte)eventMode);
buffer.WriteUShort(Id);
evnt.Write(buffer);
var sendData = buffer.ToArray();
targetPlayer.Connection.Reliable.Send(sendData);
}
public void ReplicateEvent(
RagonRoomPlayer invoker,
RagonEvent evnt,
RagonReplicationMode eventMode,
RagonTarget targetMode
)
{
if (Authority == RagonAuthority.OwnerOnly && invoker.Connection.Id != Owner.Connection.Id)
{
return;
}
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner && _bufferedEvents.Count < _limitBufferedEvents)
{
_bufferedEvents.Add(evnt);
}
var room = Owner.Room;
var buffer = room.Writer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
buffer.WriteUShort(evnt.EventCode);
buffer.WriteUShort(invoker.Connection.Id);
buffer.WriteByte((byte)eventMode);
buffer.WriteUShort(Id);
evnt.Write(buffer);
var sendData = buffer.ToArray();
switch (targetMode)
{
case RagonTarget.Owner:
{
Owner.Connection.Reliable.Send(sendData);
break;
}
case RagonTarget.ExceptOwner:
{
foreach (var roomPlayer in room.ReadyPlayersList)
{
if (roomPlayer.Connection.Id != Owner.Connection.Id)
roomPlayer.Connection.Reliable.Send(sendData);
}
break;
}
case RagonTarget.ExceptInvoker:
{
foreach (var roomPlayer in room.ReadyPlayersList)
{
if (roomPlayer.Connection.Id != invoker.Connection.Id)
roomPlayer.Connection.Reliable.Send(sendData);
}
break;
}
case RagonTarget.All:
{
foreach (var roomPlayer in room.ReadyPlayersList)
roomPlayer.Connection.Reliable.Send(sendData);
break;
}
}
}
public void AddProperty(RagonProperty property)
{
_state.AddProperty(property);
}
public void WriteState(RagonBuffer writer)
{
_state.Write(writer);
}
public bool TryReadState(RagonRoomPlayer player, RagonBuffer reader)
{
if (Owner.Connection.Id != player.Connection.Id)
return false;
_state.Read(reader);
return true;
}
}
@@ -0,0 +1,28 @@
/*
* 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.Server.Entity;
public ref struct RagonEntityParameters
{
public ushort Type;
public ushort StaticId;
public ushort AttachId;
public RagonAuthority Authority;
public int BufferedEvents;
}
@@ -0,0 +1,77 @@
/*
* 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.Server.Entity;
public class RagonEntityState: IRagonEntityState
{
private readonly List<RagonProperty> _properties;
private readonly RagonEntity _entity;
public RagonEntityState(RagonEntity entity, int capacity = 10)
{
_entity = entity;
_properties = new List<RagonProperty>(capacity);
}
public void AddProperty(RagonProperty property)
{
_properties.Add(property);
}
public void Write(RagonBuffer buffer)
{
buffer.WriteUShort(_entity.Id);
foreach (var property in _properties)
{
if (property.IsDirty)
{
buffer.WriteBool(true);
property.Write(buffer);
property.Clear();
continue;
}
buffer.WriteBool(false);
}
}
public void Read(RagonBuffer buffer)
{
foreach (var property in _properties)
{
if (buffer.ReadBool())
property.Read(buffer);
}
}
public void Snapshot(RagonBuffer buffer)
{
foreach (var property in _properties)
{
if (property.HasData)
{
buffer.WriteBool(true);
property.Write(buffer);
continue;
}
buffer.WriteBool(false);
}
}
}
@@ -0,0 +1,40 @@
/*
* 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.Server.Entity;
public class RagonPayload
{
private uint[] _data = new uint[128];
private int _size = 0;
public ushort Size => (ushort) _size;
public void Read(RagonBuffer buffer)
{
_size = buffer.Capacity;
buffer.ReadArray(_data, _size);
}
public void Write(RagonBuffer buffer)
{
if (_size == 0) return;
buffer.WriteArray(_data, _size);
}
}
@@ -0,0 +1,76 @@
/*
* 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.Server.Entity;
public class RagonProperty : RagonPayload
{
public int Size { get; set; }
public bool IsDirty { get; private set; }
public bool IsFixed { get; private set; }
public bool HasData { get; private set; }
private uint[] _data;
public RagonProperty(int size, bool isFixed, int limit)
{
Size = size;
IsFixed = isFixed;
IsDirty = false;
_data = new uint[limit / 4 + 1];
}
public void Read(RagonBuffer buffer)
{
if (IsFixed)
{
buffer.ReadArray(_data, Size);
}
else
{
Size = (int) buffer.Read();
buffer.ReadArray(_data, Size);
}
HasData = true;
IsDirty = true;
}
public void Write(RagonBuffer buffer)
{
if (IsFixed)
{
buffer.WriteArray(_data, Size);
return;
}
buffer.Write((ushort) Size);
buffer.WriteArray(_data, Size);
}
public void Clear()
{
IsDirty = false;
}
public void Dump()
{
Console.WriteLine( $"[{Size.ToString("00")}] {string.Join("", _data.Take(8).Reverse().Select(b => Convert.ToString(b, 2).PadLeft(32, '0')))}");
}
}
+7 -7
View File
@@ -23,7 +23,7 @@ public class RagonEvent
{ {
public RagonRoomPlayer Invoker { get; private set; } public RagonRoomPlayer Invoker { get; private set; }
public ushort EventCode { get; private set; } public ushort EventCode { get; private set; }
public ushort Size => (ushort)_size; public ushort Size => (ushort) _size;
private uint[] _data = new uint[128]; private uint[] _data = new uint[128];
private int _size = 0; private int _size = 0;
@@ -37,15 +37,15 @@ public class RagonEvent
EventCode = eventCode; EventCode = eventCode;
} }
public void Read(RagonStream buffer) public void Read(RagonBuffer buffer)
{ {
// _size = buffer.Capacity; _size = buffer.Capacity;
// buffer.ReadArray(_data, _size); buffer.ReadArray(_data, _size);
} }
public void Write(RagonStream buffer) public void Write(RagonBuffer buffer)
{ {
// if (_size <= 0) return; if (_size <= 0) return;
// buffer.WriteArray(_data, _size); buffer.WriteArray(_data, _size);
} }
} }
@@ -19,6 +19,7 @@ using Ragon.Server.IO;
using Ragon.Server.Lobby; using Ragon.Server.Lobby;
using Ragon.Server.Logging; using Ragon.Server.Logging;
using Ragon.Server.Plugin; using Ragon.Server.Plugin;
using Ragon.Server.Project;
namespace Ragon.Server.Handler namespace Ragon.Server.Handler
{ {
@@ -29,20 +30,23 @@ namespace Ragon.Server.Handler
private readonly IRagonServer _server; private readonly IRagonServer _server;
private readonly RagonContextObserver _observer; private readonly RagonContextObserver _observer;
private readonly RagonServerConfiguration _configuration; private readonly RagonServerConfiguration _configuration;
private readonly RagonStream _writer; private readonly RagonBuffer _writer;
private readonly ProjectRegistry _projectRegistry;
public AuthorizationOperation(RagonStream reader, public AuthorizationOperation(RagonBuffer reader,
RagonStream writer, RagonBuffer writer,
IRagonServer server, IRagonServer server,
IServerPlugin serverPlugin, IServerPlugin serverPlugin,
RagonContextObserver observer, RagonContextObserver observer,
RagonServerConfiguration configuration) : base(reader, writer) RagonServerConfiguration configuration,
ProjectRegistry projectRegistry) : base(reader, writer)
{ {
_serverPlugin = serverPlugin; _serverPlugin = serverPlugin;
_configuration = configuration; _configuration = configuration;
_observer = observer; _observer = observer;
_writer = writer; _writer = writer;
_server = server; _server = server;
_projectRegistry = projectRegistry;
} }
public override void Handle(RagonContext context, NetworkChannel channel) public override void Handle(RagonContext context, NetworkChannel channel)
@@ -59,31 +63,45 @@ namespace Ragon.Server.Handler
return; return;
} }
var configuration = _configuration; var projectKey = Reader.ReadString();
var key = Reader.ReadString();
var name = Reader.ReadString(); var name = Reader.ReadString();
var payload = Reader.ReadString(); var payload = Reader.ReadString();
if (key == configuration.ServerKey) if (!_projectRegistry.ValidateKey(projectKey))
{ {
_logger.Warning($"Invalid project key from connection {context.Connection.Id}");
Reject(context);
return;
}
if (!_projectRegistry.CanConnect(projectKey))
{
_logger.Warning($"Connection limit reached for project key: {projectKey}");
Reject(context);
return;
}
var authorizeViaPlugin = _serverPlugin.OnAuthorize(new ConnectionRequest(_server, context.Connection.Id, payload)); var authorizeViaPlugin = _serverPlugin.OnAuthorize(new ConnectionRequest(_server, context.Connection.Id, payload));
if (authorizeViaPlugin) if (authorizeViaPlugin)
return; return;
var id = Guid.NewGuid().ToString(); var project = _projectRegistry.GetOrCreateProject(projectKey);
Approve(context, new ConnectionResponse(id, name, payload)); if (project == null)
}
else
{ {
_logger.Warning($"Invalid key for connection {context.Connection.Id}"); _logger.Warning($"Failed to create project for key: {projectKey}");
Reject(context); Reject(context);
} return;
} }
public void Approve(RagonContext context, ConnectionResponse result) var id = Guid.NewGuid().ToString();
Approve(context, new ConnectionResponse(id, name, payload), project.Id);
_projectRegistry.RegisterConnection(project.Id);
}
public void Approve(RagonContext context, ConnectionResponse result, int projectId)
{ {
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, result.Id, result.Name, result.Payload); var lobbyPlayer = new RagonLobbyPlayer(context.Connection, result.Id, result.Name, result.Payload, projectId);
context.SetPlayer(lobbyPlayer); context.SetPlayer(lobbyPlayer);
context.ConnectionStatus = ConnectionStatus.Authorized; context.ConnectionStatus = ConnectionStatus.Authorized;
@@ -102,7 +120,7 @@ namespace Ragon.Server.Handler
var sendData = _writer.ToArray(); var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData); context.Connection.Reliable.Send(sendData);
_logger.Trace($"Approved {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name}"); _logger.Trace($"Approved {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name} for project {projectId}");
} }
public void Reject(RagonContext context) public void Reject(RagonContext context)
@@ -21,10 +21,10 @@ namespace Ragon.Server.Handler;
public abstract class BaseOperation public abstract class BaseOperation
{ {
protected readonly RagonStream Reader; protected readonly RagonBuffer Reader;
protected readonly RagonStream Writer; protected readonly RagonBuffer Writer;
public BaseOperation(RagonStream reader, RagonStream writer) public BaseOperation(RagonBuffer reader, RagonBuffer writer)
{ {
Reader = reader; Reader = reader;
Writer = writer; Writer = writer;
@@ -0,0 +1,79 @@
/*
* 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.Entity;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class EntityCreateOperation : BaseOperation
{
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityCreateOperation));
private RagonServerConfiguration _configuration;
public EntityCreateOperation(RagonBuffer reader, RagonBuffer writer, RagonServerConfiguration configuration) :
base(reader, writer)
{
_configuration = configuration;
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
var player = context.RoomPlayer;
var room = context.Room;
var attachId = Reader.ReadUShort();
var entityType = Reader.ReadUShort();
var eventAuthority = (RagonAuthority) Reader.ReadByte();
var propertiesCount = Reader.ReadUShort();
var entityParameters = new RagonEntityParameters()
{
Type = entityType,
Authority = eventAuthority,
AttachId = attachId,
StaticId = 0,
BufferedEvents = context.LimitBufferedEvents,
};
var entity = new RagonEntity(entityParameters);
for (var i = 0; i < propertiesCount; i++)
{
var propertyType = Reader.ReadBool();
var propertySize = Reader.ReadUShort();
entity.AddProperty(new RagonProperty(propertySize, propertyType, _configuration.LimitPropertySize));
}
if (Reader.Capacity > 0)
entity.Payload.Read(Reader);
var plugin = room.Plugin;
if (!plugin.OnEntityCreate(player, entity))
return;
entity.Attach(player);
room.AttachEntity(entity);
player.AttachEntity(entity);
entity.Create();
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} created entity {entity.Id}:{entity.Type}");
}
}
@@ -0,0 +1,63 @@
/*
* 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.Event;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class EntityEventOperation : BaseOperation
{
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityEventOperation));
public EntityEventOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
var player = context.RoomPlayer;
var room = context.Room;
var entityId = Reader.ReadUShort();
if (!room.Entities.TryGetValue(entityId, out var ent))
{
_logger.Warning($"Entity not found for event with Id {entityId}");
return;
}
var eventId = Reader.ReadUShort();
var eventMode = (RagonReplicationMode)Reader.ReadByte();
var targetMode = (RagonTarget)Reader.ReadByte();
var targetPlayerPeerId = (ushort)0;
if (targetMode == RagonTarget.Player)
targetPlayerPeerId = Reader.ReadUShort();
var @event = new RagonEvent(player, eventId);
@event.Read(Reader);
if (targetMode == RagonTarget.Player && room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer))
{
ent.ReplicateEvent(player, @event, eventMode, targetPlayer);
return;
}
ent.ReplicateEvent(player, @event, eventMode, targetMode);
}
}
@@ -0,0 +1,75 @@
/*
* 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;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class EntityOwnershipOperation : BaseOperation
{
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityOwnershipOperation));
public EntityOwnershipOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
var currentOwner = context.RoomPlayer;
var room = context.Room;
var entityId = Reader.ReadUShort();
var playerPeerId = Reader.ReadUShort();
if (!room.Entities.TryGetValue(entityId, out var entity))
{
_logger.Error($"Entity not found with id {entityId}");
return;
}
if (entity.Owner.Connection.Id != currentOwner.Connection.Id)
{
_logger.Error($"Player not owner of entity with id {entityId}");
return;
}
if (!room.Players.TryGetValue(playerPeerId, out var nextOwner))
{
_logger.Error($"Player not found with id {playerPeerId}");
return;
}
currentOwner.Entities.Remove(entity);
nextOwner.Entities.Add(entity);
entity.Attach(nextOwner);
_logger.Trace($"Entity {entity.Id} next owner {nextOwner.Connection.Id}");
Writer.Clear();
Writer.WriteOperation(RagonOperation.OWNERSHIP_ENTITY_CHANGED);
Writer.WriteUShort(playerPeerId);
Writer.WriteUShort(1);
Writer.WriteUShort(entity.Id);
var sendData = Writer.ToArray();
foreach (var player in room.PlayerList)
player.Connection.Reliable.Send(sendData);
}
}
@@ -0,0 +1,55 @@
/*
* 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.Entity;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class EntityDestroyOperation: BaseOperation
{
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityDestroyOperation));
public EntityDestroyOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
var player = context.RoomPlayer;
var room = context.Room;
var entityId = Reader.ReadUShort();
if (room.Entities.TryGetValue(entityId, out var entity) && entity.Owner.Connection.Id == player.Connection.Id)
{
var payload = new RagonPayload();
payload.Read(Reader);
room.DetachEntity(entity);
player.DetachEntity(entity);
entity.Destroy();
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} destoyed entity {entity.Id}");
}
else
{
_logger.Trace($"Entity {entity.Id} not found or Player {context.Connection.Id}|{context.LobbyPlayer.Name} have not authority");
}
}
}
@@ -0,0 +1,50 @@
/*
* 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;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class EntityStateOperation: BaseOperation
{
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityStateOperation));
public EntityStateOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
var room = context.Room;
var player = context.RoomPlayer;
var entitiesCount = Reader.ReadUShort();
for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++)
{
var entityId = Reader.ReadUShort();
if (room.Entities.TryGetValue(entityId, out var entity) && entity.TryReadState(player, Reader))
{
room.Track(entity);
}
else
{
_logger.Error($"Entity with Id {entityId} not found, replication interrupted");
}
}
}
}
@@ -11,8 +11,8 @@ namespace Ragon.Server.Handler
private readonly int _userDataLimit; private readonly int _userDataLimit;
public PlayerUserDataOperation( public PlayerUserDataOperation(
RagonStream reader, RagonBuffer reader,
RagonStream writer, RagonBuffer writer,
int userDataLimit int userDataLimit
) : base(reader, writer) ) : base(reader, writer)
{ {
@@ -27,7 +27,7 @@ namespace Ragon.Server.Handler
return; return;
} }
context.UserData.Read(Reader); context.UserData.Read(Reader, _userDataLimit);
} }
} }
} }
@@ -32,8 +32,8 @@ namespace Ragon.Server.Handler
private readonly RagonServerConfiguration _configuration; private readonly RagonServerConfiguration _configuration;
public RoomCreateOperation( public RoomCreateOperation(
RagonStream reader, RagonBuffer reader,
RagonStream writer, RagonBuffer writer,
IServerPlugin serverPlugin, IServerPlugin serverPlugin,
RagonServerConfiguration configuration RagonServerConfiguration configuration
) : base(reader, ) : base(reader,
@@ -76,6 +76,7 @@ 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,
}; };
@@ -87,7 +88,7 @@ namespace Ragon.Server.Handler
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name); var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information); var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin); var room = new RagonRoom(roomId, information, roomPlugin, lobbyPlayer.ProjectId);
room.Plugin.OnAttached(room); room.Plugin.OnAttached(room);
roomPlayer.OnAttached(room); roomPlayer.OnAttached(room);
@@ -96,7 +97,8 @@ namespace Ragon.Server.Handler
context.Lobby.Persist(room); context.Lobby.Persist(room);
context.SetRoom(room, roomPlayer); context.SetRoom(room, roomPlayer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id}"); _logger.Trace(
$"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} with scene {information.Scene}");
JoinSuccess(roomPlayer, room, Writer); JoinSuccess(roomPlayer, room, Writer);
@@ -105,13 +107,14 @@ namespace Ragon.Server.Handler
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}");
} }
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonStream writer) private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer)
{ {
writer.Clear(); writer.Clear();
writer.WriteOperation(RagonOperation.JOIN_SUCCESS); writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
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);
@@ -0,0 +1,51 @@
/*
* 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(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
var player = context.RoomPlayer;
var room = context.Room;
var data = Reader.RawData;
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);
}
}
@@ -10,7 +10,7 @@ public class RoomEventOperation : BaseOperation
{ {
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomEventOperation)); private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomEventOperation));
public RoomEventOperation(RagonStream reader, RagonStream writer) : base(reader, writer) public RoomEventOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{ {
} }

Some files were not shown because too many files have changed in this diff Show More