Compare commits
3 Commits
v1.0.11-rc
...
v1.0.14-rc
| Author | SHA1 | Date | |
|---|---|---|---|
| 51c0482a40 | |||
| 9e16c53cad | |||
| 6ec29e5dcd |
@@ -20,11 +20,9 @@ namespace Ragon.Common
|
|||||||
PLAYER_JOINED,
|
PLAYER_JOINED,
|
||||||
PLAYER_LEAVED,
|
PLAYER_LEAVED,
|
||||||
|
|
||||||
CREATE_STATES,
|
|
||||||
CREATE_ENTITY,
|
CREATE_ENTITY,
|
||||||
|
CREATE_SCENE_ENTITY,
|
||||||
DESTROY_ENTITY,
|
DESTROY_ENTITY,
|
||||||
CREATE_STATIC_ENTITY,
|
|
||||||
|
|
||||||
SNAPSHOT,
|
SNAPSHOT,
|
||||||
|
|
||||||
REPLICATE_ENTITY_STATE,
|
REPLICATE_ENTITY_STATE,
|
||||||
|
|||||||
@@ -45,12 +45,18 @@ namespace Ragon.Common
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteByte(byte value)
|
public void AddOffset(int offset)
|
||||||
|
{
|
||||||
|
_offset += offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteByte(byte value)
|
||||||
{
|
{
|
||||||
ResizeIfNeed(1);
|
ResizeIfNeed(1);
|
||||||
|
|
||||||
_data[_offset] = value;
|
_data[_offset] = value;
|
||||||
_offset += 1;
|
_offset += 1;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
@@ -62,12 +68,12 @@ namespace Ragon.Common
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteBool(bool value)
|
public int WriteBool(bool value)
|
||||||
{
|
{
|
||||||
ResizeIfNeed(1);
|
ResizeIfNeed(1);
|
||||||
|
|
||||||
_data[_offset] = value ? (byte) 1 : (byte) 0;
|
_data[_offset] = value ? (byte) 1 : (byte) 0;
|
||||||
_offset += 1;
|
_offset += 1;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -81,7 +87,7 @@ namespace Ragon.Common
|
|||||||
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteInt(int value)
|
public int WriteInt(int value)
|
||||||
{
|
{
|
||||||
ResizeIfNeed(4);
|
ResizeIfNeed(4);
|
||||||
var converter = new ValueConverter() {Int = value};
|
var converter = new ValueConverter() {Int = value};
|
||||||
@@ -90,6 +96,19 @@ namespace Ragon.Common
|
|||||||
_data[_offset + 2] = converter.Byte2;
|
_data[_offset + 2] = converter.Byte2;
|
||||||
_data[_offset + 3] = converter.Byte3;
|
_data[_offset + 3] = converter.Byte3;
|
||||||
_offset += 4;
|
_offset += 4;
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteInt(int value, int offset)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(4);
|
||||||
|
var converter = new ValueConverter() {Int = value};
|
||||||
|
_data[offset] = converter.Byte0;
|
||||||
|
_data[offset + 1] = converter.Byte1;
|
||||||
|
_data[offset + 2] = converter.Byte2;
|
||||||
|
_data[offset + 3] = converter.Byte3;
|
||||||
|
return 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
@@ -109,7 +128,7 @@ namespace Ragon.Common
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteLong(long value, int offset)
|
public int WriteLong(long value, int offset)
|
||||||
{
|
{
|
||||||
var converter = new ValueConverter() {Long = value};
|
var converter = new ValueConverter() {Long = value};
|
||||||
_data[offset] = converter.Byte0;
|
_data[offset] = converter.Byte0;
|
||||||
@@ -120,6 +139,7 @@ namespace Ragon.Common
|
|||||||
_data[offset + 5] = converter.Byte5;
|
_data[offset + 5] = converter.Byte5;
|
||||||
_data[offset + 6] = converter.Byte6;
|
_data[offset + 6] = converter.Byte6;
|
||||||
_data[offset + 7] = converter.Byte7;
|
_data[offset + 7] = converter.Byte7;
|
||||||
|
return 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
@@ -141,10 +161,11 @@ namespace Ragon.Common
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteFloat(float value)
|
public int WriteFloat(float value)
|
||||||
{
|
{
|
||||||
var converter = new ValueConverter() {Float = value};
|
var converter = new ValueConverter() {Float = value};
|
||||||
WriteInt(converter.Int);
|
WriteInt(converter.Int);
|
||||||
|
return 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
@@ -157,39 +178,16 @@ namespace Ragon.Common
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteString(string value, ushort size)
|
public int WriteString(string value)
|
||||||
{
|
{
|
||||||
var stringRaw = Encoding.UTF8.GetBytes(value).AsSpan();
|
var rawData = Encoding.UTF8.GetBytes(value).AsSpan();
|
||||||
ResizeIfNeed(2 + size);
|
ResizeIfNeed(2 + rawData.Length);
|
||||||
|
WriteUShort((ushort) rawData.Length);
|
||||||
|
var data = _data.AsSpan().Slice(_offset, rawData.Length);
|
||||||
|
rawData.CopyTo(data);
|
||||||
|
_offset += rawData.Length;
|
||||||
|
|
||||||
WriteUShort((ushort) stringRaw.Length);
|
return rawData.Length + 2;
|
||||||
|
|
||||||
var data = _data.AsSpan().Slice(_offset, size);
|
|
||||||
stringRaw.CopyTo(data);
|
|
||||||
_offset += stringRaw.Length;
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public string ReadString(ushort size)
|
|
||||||
{
|
|
||||||
var lenght = ReadUShort();
|
|
||||||
var stringRaw = _data.AsSpan().Slice(_offset, size);
|
|
||||||
var strData = stringRaw.Slice(0, lenght);
|
|
||||||
var str = Encoding.UTF8.GetString(strData);
|
|
||||||
_offset += size;
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public void WriteString(string value)
|
|
||||||
{
|
|
||||||
var stringRaw = Encoding.UTF8.GetBytes(value).AsSpan();
|
|
||||||
ResizeIfNeed(2 + stringRaw.Length);
|
|
||||||
|
|
||||||
WriteUShort((ushort) stringRaw.Length);
|
|
||||||
var data = _data.AsSpan().Slice(_offset, stringRaw.Length);
|
|
||||||
stringRaw.CopyTo(data);
|
|
||||||
_offset += stringRaw.Length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
@@ -213,7 +211,7 @@ namespace Ragon.Common
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteData(ref ReadOnlySpan<byte> payload)
|
public int WriteData(ref ReadOnlySpan<byte> payload)
|
||||||
{
|
{
|
||||||
ResizeIfNeed(payload.Length);
|
ResizeIfNeed(payload.Length);
|
||||||
|
|
||||||
@@ -222,6 +220,7 @@ namespace Ragon.Common
|
|||||||
|
|
||||||
payload.CopyTo(payloadData);
|
payload.CopyTo(payloadData);
|
||||||
_offset += payload.Length;
|
_offset += payload.Length;
|
||||||
|
return payload.Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
@@ -237,12 +236,14 @@ namespace Ragon.Common
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteOperation(RagonOperation ragonOperation)
|
public int WriteOperation(RagonOperation ragonOperation)
|
||||||
{
|
{
|
||||||
ResizeIfNeed(1);
|
ResizeIfNeed(1);
|
||||||
|
|
||||||
_data[_offset] = (byte) ragonOperation;
|
_data[_offset] = (byte) ragonOperation;
|
||||||
_offset += 1;
|
_offset += 1;
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
@@ -254,13 +255,24 @@ namespace Ragon.Common
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteUShort(ushort value)
|
public int WriteUShort(ushort value)
|
||||||
{
|
{
|
||||||
ResizeIfNeed(2);
|
ResizeIfNeed(2);
|
||||||
|
|
||||||
_data[_offset] = (byte) (value & 0x00FF);
|
_data[_offset] = (byte) (value & 0x00FF);
|
||||||
_data[_offset + 1] = (byte) ((value & 0xFF00) >> 8);
|
_data[_offset + 1] = (byte) ((value & 0xFF00) >> 8);
|
||||||
_offset += 2;
|
_offset += 2;
|
||||||
|
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteUShort(ushort value, int offset)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(2);
|
||||||
|
_data[offset] = (byte) (value & 0x00FF);
|
||||||
|
_data[offset + 1] = (byte) ((value & 0xFF00) >> 8);
|
||||||
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public class AuthorizationProviderByKey: IAuthorizationProvider
|
|||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task OnAuthorizationRequest(string key, string name, byte protocol, byte[] additionalData, Action<string, string> accept, Action<uint> reject)
|
public async Task OnAuthorizationRequest(string key, string name, byte[] additionalData, Action<string, string> accept, Action<uint> reject)
|
||||||
{
|
{
|
||||||
if (key == _configuration.Key)
|
if (key == _configuration.Key)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"key": "defaultkey",
|
"key": "defaultkey",
|
||||||
|
"protocol": "1.0.0",
|
||||||
"statisticsInterval": 5,
|
"statisticsInterval": 5,
|
||||||
"sendRate": 30,
|
"sendRate": 30,
|
||||||
"port": 4444,
|
"port": 4444,
|
||||||
|
|||||||
@@ -25,11 +25,11 @@ public class AuthorizationManager : IAuthorizationManager
|
|||||||
_playersByPeers = new Dictionary<uint, Player>();
|
_playersByPeers = new Dictionary<uint, Player>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnAuthorization(uint peerId, string key, string name, byte protocol)
|
public void OnAuthorization(uint peerId, string key, string name)
|
||||||
{
|
{
|
||||||
var dispatcher = _gameThread.ThreadDispatcher;
|
var dispatcher = _gameThread.ThreadDispatcher;
|
||||||
|
|
||||||
_provider.OnAuthorizationRequest(key, name, protocol, Array.Empty<byte>(),
|
_provider.OnAuthorizationRequest(key, name, Array.Empty<byte>(),
|
||||||
(playerId, playerName) => { dispatcher.Dispatch(() => Accepted(peerId, playerId, playerName)); },
|
(playerId, playerName) => { dispatcher.Dispatch(() => Accepted(peerId, playerId, playerName)); },
|
||||||
(errorCode) => { dispatcher.Dispatch(() => Rejected(peerId, errorCode)); });
|
(errorCode) => { dispatcher.Dispatch(() => Rejected(peerId, errorCode)); });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,5 +5,5 @@ namespace Ragon.Core;
|
|||||||
|
|
||||||
public interface IAuthorizationProvider
|
public interface IAuthorizationProvider
|
||||||
{
|
{
|
||||||
Task OnAuthorizationRequest(string key, string playerName, byte protocol, byte[] additionalData, Action<string, string> Accept, Action<uint> Reject);
|
Task OnAuthorizationRequest(string key, string playerName, byte[] additionalData, Action<string, string> Accept, Action<uint> Reject);
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ namespace Ragon.Core
|
|||||||
public struct Configuration
|
public struct Configuration
|
||||||
{
|
{
|
||||||
public string Key;
|
public string Key;
|
||||||
|
public string Protocol;
|
||||||
public int StatisticsInterval;
|
public int StatisticsInterval;
|
||||||
public ushort SendRate;
|
public ushort SendRate;
|
||||||
public ushort Port;
|
public ushort Port;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace Ragon.Core
|
|||||||
public static class ConfigurationLoader
|
public static class ConfigurationLoader
|
||||||
{
|
{
|
||||||
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
private static readonly string _serverVersion = "1.0.9-rc";
|
private static readonly string _serverVersion = "1.0.13-rc";
|
||||||
|
|
||||||
private static void CopyrightInfo()
|
private static void CopyrightInfo()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,33 +1,39 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using Ragon.Common;
|
||||||
|
|
||||||
namespace Ragon.Core;
|
namespace Ragon.Core;
|
||||||
|
|
||||||
public class EntityProperty
|
public class EntityProperty
|
||||||
{
|
{
|
||||||
public int Size => _data.Length;
|
public int Size { get; set; }
|
||||||
public bool IsDirty => _dirty;
|
public bool IsDirty { get; private set; }
|
||||||
|
public bool IsFixed { get; private set; }
|
||||||
private bool _dirty;
|
|
||||||
private byte[] _data;
|
private byte[] _data;
|
||||||
|
|
||||||
public EntityProperty(int size)
|
public EntityProperty(int size, bool isFixed)
|
||||||
{
|
{
|
||||||
_data = new byte[size];
|
_data = new byte[512];
|
||||||
|
|
||||||
|
Size = size;
|
||||||
|
IsFixed = isFixed;
|
||||||
|
IsDirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReadOnlySpan<byte> Read()
|
public ReadOnlySpan<byte> Read()
|
||||||
{
|
{
|
||||||
return _data.AsSpan();
|
var dataSpan = _data.AsSpan();
|
||||||
|
|
||||||
|
return dataSpan.Slice(0, Size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Write(ref ReadOnlySpan<byte> src)
|
public void Write(ref ReadOnlySpan<byte> src)
|
||||||
{
|
{
|
||||||
src.CopyTo(_data);
|
src.CopyTo(_data);
|
||||||
_dirty = true;
|
IsDirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
_dirty = false;
|
IsDirty = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,7 @@ namespace Ragon.Core
|
|||||||
public int PlayersMin { get; private set; }
|
public int PlayersMin { get; private set; }
|
||||||
public int PlayersMax { get; private set; }
|
public int PlayersMax { get; private set; }
|
||||||
public int PlayersCount => _players.Count;
|
public int PlayersCount => _players.Count;
|
||||||
|
public int EntitiesCount => _entities.Count;
|
||||||
public string Id { get; private set; }
|
public string Id { get; private set; }
|
||||||
public string Map { get; private set; }
|
public string Map { get; private set; }
|
||||||
|
|
||||||
@@ -116,6 +117,20 @@ namespace Ragon.Core
|
|||||||
Broadcast(_readyPlayers, sendData);
|
Broadcast(_readyPlayers, sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_allPlayers.Length > 0 && player.PeerId == _owner)
|
||||||
|
{
|
||||||
|
var nextOwnerId = _allPlayers[0];
|
||||||
|
_owner = nextOwnerId;
|
||||||
|
var nextOwner = _players[nextOwnerId];
|
||||||
|
|
||||||
|
_serializer.Clear();
|
||||||
|
_serializer.WriteOperation(RagonOperation.OWNERSHIP_CHANGED);
|
||||||
|
_serializer.WriteString(nextOwner.Id);
|
||||||
|
|
||||||
|
var sendData = _serializer.ToArray();
|
||||||
|
Broadcast(_readyPlayers, sendData);
|
||||||
|
}
|
||||||
|
|
||||||
_entitiesAll = _entities.Values.ToArray();
|
_entitiesAll = _entities.Values.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,29 +144,40 @@ namespace Ragon.Core
|
|||||||
{
|
{
|
||||||
case RagonOperation.REPLICATE_ENTITY_STATE:
|
case RagonOperation.REPLICATE_ENTITY_STATE:
|
||||||
{
|
{
|
||||||
var entityId = _serializer.ReadUShort();
|
var entitiesCount = _serializer.ReadUShort();
|
||||||
if (_entities.TryGetValue(entityId, out var ent))
|
for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++)
|
||||||
{
|
{
|
||||||
if (ent.OwnerId != peerId)
|
var entityId = _serializer.ReadUShort();
|
||||||
|
if (_entities.TryGetValue(entityId, out var ent))
|
||||||
{
|
{
|
||||||
_logger.Warn($"Not owner can't change properties of object {entityId}");
|
if (ent.OwnerId != peerId)
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var mask = _serializer.ReadLong();
|
|
||||||
for (var i = 0; i < ent.Properties.Length; i++)
|
|
||||||
{
|
|
||||||
if (((mask >> i) & 1) == 1)
|
|
||||||
{
|
{
|
||||||
var propertyPayload = _serializer.ReadData(ent.Properties[i].Size);
|
_logger.Warn($"Not owner can't change properties of object {entityId}");
|
||||||
ent.Properties[i].Write(ref propertyPayload);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < ent.Properties.Length; i++)
|
||||||
|
{
|
||||||
|
if (_serializer.ReadBool())
|
||||||
|
{
|
||||||
|
var property = ent.Properties[i];
|
||||||
|
if (!property.IsFixed)
|
||||||
|
property.Size = _serializer.ReadUShort();
|
||||||
|
|
||||||
|
var propertyPayload = _serializer.ReadData(property.Size);
|
||||||
|
property.Write(ref propertyPayload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_entitiesDirtySet.Add(ent))
|
||||||
|
_entitiesDirty.Add(ent);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.Error($"Entity with Id {entityId} not found, replication interrupted");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_entitiesDirtySet.Add(ent))
|
|
||||||
_entitiesDirty.Add(ent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RagonOperation.REPLICATE_ENTITY_EVENT:
|
case RagonOperation.REPLICATE_ENTITY_EVENT:
|
||||||
@@ -182,6 +208,7 @@ namespace Ragon.Core
|
|||||||
_serializer.WriteByte(evntMode);
|
_serializer.WriteByte(evntMode);
|
||||||
_serializer.WriteUShort(entityId);
|
_serializer.WriteUShort(entityId);
|
||||||
_serializer.WriteData(ref payload);
|
_serializer.WriteData(ref payload);
|
||||||
|
|
||||||
var sendData = _serializer.ToArray();
|
var sendData = _serializer.ToArray();
|
||||||
|
|
||||||
switch (targetMode)
|
switch (targetMode)
|
||||||
@@ -273,22 +300,18 @@ namespace Ragon.Core
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RagonOperation.CREATE_STATIC_ENTITY:
|
case RagonOperation.CREATE_SCENE_ENTITY:
|
||||||
{
|
{
|
||||||
var entityType = _serializer.ReadUShort();
|
var entityType = _serializer.ReadUShort();
|
||||||
var staticId = _serializer.ReadUShort();
|
var staticId = _serializer.ReadUShort();
|
||||||
var propertiesCount = _serializer.ReadUShort();
|
var propertiesCount = _serializer.ReadUShort();
|
||||||
if (propertiesCount > 63)
|
|
||||||
{
|
|
||||||
_logger.Warn($"Allowed only 64 properties per entity. EntityType(Static) {entityType}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var entity = new Entity(peerId, entityType, staticId, RagonAuthority.ALL, RagonAuthority.OWNER_ONLY, propertiesCount);
|
var entity = new Entity(peerId, entityType, staticId, RagonAuthority.ALL, RagonAuthority.OWNER_ONLY, propertiesCount);
|
||||||
for (var i = 0; i < propertiesCount; i++)
|
for (var i = 0; i < propertiesCount; i++)
|
||||||
{
|
{
|
||||||
|
var propertyType = _serializer.ReadBool();
|
||||||
var propertySize = _serializer.ReadUShort();
|
var propertySize = _serializer.ReadUShort();
|
||||||
entity.Properties[i] = new EntityProperty(propertySize);
|
entity.Properties[i] = new EntityProperty(propertySize, propertyType);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -308,7 +331,7 @@ namespace Ragon.Core
|
|||||||
_plugin.OnEntityCreated(player, entity);
|
_plugin.OnEntityCreated(player, entity);
|
||||||
|
|
||||||
_serializer.Clear();
|
_serializer.Clear();
|
||||||
_serializer.WriteOperation(RagonOperation.CREATE_STATIC_ENTITY);
|
_serializer.WriteOperation(RagonOperation.CREATE_SCENE_ENTITY);
|
||||||
_serializer.WriteUShort(entityType);
|
_serializer.WriteUShort(entityType);
|
||||||
_serializer.WriteUShort(entity.EntityId);
|
_serializer.WriteUShort(entity.EntityId);
|
||||||
_serializer.WriteUShort(staticId);
|
_serializer.WriteUShort(staticId);
|
||||||
@@ -328,16 +351,13 @@ namespace Ragon.Core
|
|||||||
var entityType = _serializer.ReadUShort();
|
var entityType = _serializer.ReadUShort();
|
||||||
var propertiesCount = _serializer.ReadUShort();
|
var propertiesCount = _serializer.ReadUShort();
|
||||||
var entity = new Entity(peerId, entityType, 0, RagonAuthority.ALL, RagonAuthority.ALL, propertiesCount);
|
var entity = new Entity(peerId, entityType, 0, RagonAuthority.ALL, RagonAuthority.ALL, propertiesCount);
|
||||||
if (propertiesCount > 63)
|
// _logger.Trace("Created entity with properties: " + propertiesCount);
|
||||||
{
|
|
||||||
_logger.Warn($"Allowed only 64 properties per entity. EntityType(Static) {entityType}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < propertiesCount; i++)
|
for (var i = 0; i < propertiesCount; i++)
|
||||||
{
|
{
|
||||||
|
var propertyType = _serializer.ReadBool();
|
||||||
var propertySize = _serializer.ReadUShort();
|
var propertySize = _serializer.ReadUShort();
|
||||||
entity.Properties[i] = new EntityProperty(propertySize);
|
entity.Properties[i] = new EntityProperty(propertySize, propertyType);
|
||||||
|
// _logger.Trace($"Property: {i} Size: {propertySize} IsFixed: {propertyType}");
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -461,38 +481,35 @@ namespace Ragon.Core
|
|||||||
_serializer.Clear();
|
_serializer.Clear();
|
||||||
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||||
_serializer.WriteUShort((ushort) _entitiesDirty.Count);
|
_serializer.WriteUShort((ushort) _entitiesDirty.Count);
|
||||||
|
|
||||||
for (var entityIndex = 0; entityIndex < _entitiesDirty.Count; entityIndex++)
|
for (var entityIndex = 0; entityIndex < _entitiesDirty.Count; entityIndex++)
|
||||||
{
|
{
|
||||||
var entity = _entitiesDirty[entityIndex];
|
var entity = _entitiesDirty[entityIndex];
|
||||||
var mask = 0L;
|
|
||||||
|
|
||||||
_serializer.WriteUShort(entity.EntityId);
|
_serializer.WriteUShort(entity.EntityId);
|
||||||
|
|
||||||
var offset = _serializer.Lenght;
|
|
||||||
_serializer.WriteLong(mask);
|
|
||||||
|
|
||||||
for (int propertyIndex = 0; propertyIndex < entity.Properties.Length; propertyIndex++)
|
for (int propertyIndex = 0; propertyIndex < entity.Properties.Length; propertyIndex++)
|
||||||
{
|
{
|
||||||
var property = entity.Properties[propertyIndex];
|
var property = entity.Properties[propertyIndex];
|
||||||
if (property.IsDirty)
|
if (property.IsDirty)
|
||||||
{
|
{
|
||||||
mask |= (uint) (1 << propertyIndex);
|
_serializer.WriteBool(true);
|
||||||
|
|
||||||
var span = _serializer.GetWritableData(property.Size);
|
var span = _serializer.GetWritableData(property.Size);
|
||||||
var data = property.Read();
|
var data = property.Read();
|
||||||
data.CopyTo(span);
|
data.CopyTo(span);
|
||||||
property.Clear();
|
property.Clear();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_serializer.WriteBool(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_serializer.WriteLong(mask, offset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_entitiesDirty.Clear();
|
_entitiesDirty.Clear();
|
||||||
_entitiesDirtySet.Clear();
|
_entitiesDirtySet.Clear();
|
||||||
|
|
||||||
var sendData = _serializer.ToArray();
|
var sendData = _serializer.ToArray();
|
||||||
Broadcast(_readyPlayers, sendData, DeliveryType.Unreliable);
|
Broadcast(_readyPlayers, sendData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Ragon.Common;
|
using Ragon.Common;
|
||||||
using ENet;
|
using ENet;
|
||||||
@@ -11,36 +10,33 @@ namespace Ragon.Core
|
|||||||
{
|
{
|
||||||
private readonly RoomManager _roomManager;
|
private readonly RoomManager _roomManager;
|
||||||
private readonly Thread _thread;
|
private readonly Thread _thread;
|
||||||
private readonly Stopwatch _gameLoopTimer;
|
|
||||||
private readonly Lobby _lobby;
|
private readonly Lobby _lobby;
|
||||||
|
private readonly ISocketServer _server;
|
||||||
|
private readonly IDispatcherInternal _dispatcherInternal;
|
||||||
|
private readonly IDispatcher _dispatcher;
|
||||||
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
|
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||||
private readonly float _deltaTime = 0.0f;
|
private readonly float _deltaTime = 0.0f;
|
||||||
private readonly Stopwatch _statisticsTimer;
|
|
||||||
private readonly Configuration _configuration;
|
private readonly Configuration _configuration;
|
||||||
private readonly IDispatcherInternal _dispatcherInternal;
|
|
||||||
|
|
||||||
public IDispatcher ThreadDispatcher { get; private set; }
|
public ISocketServer Server => _server;
|
||||||
public ISocketServer Server { get; private set; }
|
public IDispatcher ThreadDispatcher => _dispatcher;
|
||||||
|
|
||||||
public GameThread(PluginFactory factory, Configuration configuration)
|
public GameThread(PluginFactory factory, Configuration configuration)
|
||||||
{
|
{
|
||||||
|
var authorizationProvider = factory.CreateAuthorizationProvider(configuration);
|
||||||
|
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
|
|
||||||
var authorizationProvider = factory.CreateAuthorizationProvider(configuration);
|
|
||||||
var dispatcher = new Dispatcher();
|
var dispatcher = new Dispatcher();
|
||||||
_dispatcherInternal = dispatcher;
|
_dispatcherInternal = dispatcher;
|
||||||
|
_dispatcher = dispatcher;
|
||||||
|
|
||||||
ThreadDispatcher = dispatcher;
|
_server = new ENetServer(this);
|
||||||
Server = new ENetServer(this);
|
|
||||||
|
|
||||||
_deltaTime = 1000.0f / configuration.SendRate;
|
_deltaTime = 1000.0f / configuration.SendRate;
|
||||||
|
|
||||||
_roomManager = new RoomManager(factory, this);
|
_roomManager = new RoomManager(factory, this);
|
||||||
_lobby = new Lobby(authorizationProvider, _roomManager, this);
|
_lobby = new Lobby(authorizationProvider, _roomManager, this);
|
||||||
|
|
||||||
_gameLoopTimer = new Stopwatch();
|
|
||||||
_statisticsTimer = new Stopwatch();
|
|
||||||
|
|
||||||
_thread = new Thread(Execute);
|
_thread = new Thread(Execute);
|
||||||
_thread.Name = "Game Thread";
|
_thread.Name = "Game Thread";
|
||||||
_thread.IsBackground = true;
|
_thread.IsBackground = true;
|
||||||
@@ -48,19 +44,33 @@ namespace Ragon.Core
|
|||||||
|
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
Server.Start(_configuration.Port, _configuration.MaxConnections);
|
var strings = _configuration.Protocol.Split(".");
|
||||||
|
if (strings.Length < 3)
|
||||||
|
{
|
||||||
|
_logger.Error("Wrong protocol passed to connect method");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_gameLoopTimer.Start();
|
var parts = new uint[] {0, 0, 0};
|
||||||
_statisticsTimer.Start();
|
for (int i = 0; i < parts.Length; i++)
|
||||||
|
{
|
||||||
|
if (!uint.TryParse(strings[i], out var v))
|
||||||
|
{
|
||||||
|
_logger.Error("Wrong protocol");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
parts[i] = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint encoded = (parts[0] << 16) | (parts[1] << 8) | parts[2];
|
||||||
|
_server.Start(_configuration.Port, _configuration.MaxConnections, encoded);
|
||||||
_thread.Start();
|
_thread.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
Server.Stop();
|
_server.Stop();
|
||||||
|
|
||||||
_gameLoopTimer.Stop();
|
|
||||||
_statisticsTimer.Stop();
|
|
||||||
_thread.Interrupt();
|
_thread.Interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,23 +78,11 @@ namespace Ragon.Core
|
|||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
Server.Process();
|
_server.Process();
|
||||||
|
|
||||||
_dispatcherInternal.Process();
|
_dispatcherInternal.Process();
|
||||||
|
_roomManager.Tick(_deltaTime);
|
||||||
|
|
||||||
var elapsedMilliseconds = _gameLoopTimer.ElapsedMilliseconds;
|
Thread.Sleep((int) _deltaTime);
|
||||||
if (elapsedMilliseconds > _deltaTime)
|
|
||||||
{
|
|
||||||
_roomManager.Tick(elapsedMilliseconds / 1000.0f);
|
|
||||||
_gameLoopTimer.Restart();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_statisticsTimer.Elapsed.Seconds > _configuration.StatisticsInterval && _roomManager.RoomsBySocket.Count > 0)
|
|
||||||
{
|
|
||||||
_logger.Trace($"Rooms: {_roomManager.Rooms.Count} Clients: {_roomManager.RoomsBySocket.Count}");
|
|
||||||
_statisticsTimer.Restart();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,8 +32,7 @@ public class Lobby : ILobby
|
|||||||
{
|
{
|
||||||
var key = _serializer.ReadString();
|
var key = _serializer.ReadString();
|
||||||
var playerName = _serializer.ReadString();
|
var playerName = _serializer.ReadString();
|
||||||
var protocol = _serializer.ReadByte();
|
_authorizationManager.OnAuthorization(peerId, key, playerName);
|
||||||
_authorizationManager.OnAuthorization(peerId, key, playerName, protocol);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,46 +1,42 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Timers;
|
||||||
using ENet;
|
using ENet;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
|
||||||
namespace Ragon.Core
|
namespace Ragon.Core
|
||||||
{
|
{
|
||||||
public enum Status
|
|
||||||
{
|
|
||||||
Stopped,
|
|
||||||
Listening,
|
|
||||||
Disconnecting,
|
|
||||||
Connecting,
|
|
||||||
Assigning,
|
|
||||||
Connected
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ENetServer : ISocketServer
|
public class ENetServer : ISocketServer
|
||||||
{
|
{
|
||||||
public Status Status { get; private set; }
|
|
||||||
|
|
||||||
private ILogger _logger = LogManager.GetCurrentClassLogger();
|
private ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||||
private Host _host;
|
private Host _host;
|
||||||
|
private uint _protocol;
|
||||||
private Address _address;
|
private Address _address;
|
||||||
private Event _netEvent;
|
private Event _netEvent;
|
||||||
private Peer[] _peers;
|
private Peer[] _peers;
|
||||||
private IHandler _handler;
|
private IHandler _handler;
|
||||||
|
private Stopwatch _timer;
|
||||||
|
|
||||||
public ENetServer(IHandler handler)
|
public ENetServer(IHandler handler)
|
||||||
{
|
{
|
||||||
_handler = handler;
|
_handler = handler;
|
||||||
|
_timer = Stopwatch.StartNew();
|
||||||
|
_peers = Array.Empty<Peer>();
|
||||||
|
_host = new Host();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start(ushort port, int connections)
|
public void Start(ushort port, int connections, uint protocol)
|
||||||
{
|
{
|
||||||
_address = default;
|
_address = default;
|
||||||
_address.Port = port;
|
_address.Port = port;
|
||||||
_peers = new Peer[connections];
|
_peers = new Peer[connections];
|
||||||
|
_protocol = protocol;
|
||||||
_host = new Host();
|
|
||||||
_host.Create(_address, connections, 2, 0, 0, 1024 * 1024);
|
_host.Create(_address, connections, 2, 0, 0, 1024 * 1024);
|
||||||
|
|
||||||
Status = Status.Listening;
|
|
||||||
|
var protocolDecoded = (protocol >> 16 & 0xFF) + "." + (protocol >> 8 & 0xFF) + "." + (protocol & 0xFF);
|
||||||
_logger.Info($"Network listening on {port}");
|
_logger.Info($"Network listening on {port}");
|
||||||
|
_logger.Info($"Protocol: {protocolDecoded}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Broadcast(uint[] peersIds, byte[] data, DeliveryType type)
|
public void Broadcast(uint[] peersIds, byte[] data, DeliveryType type)
|
||||||
@@ -57,7 +53,7 @@ namespace Ragon.Core
|
|||||||
else if (type == DeliveryType.Unreliable)
|
else if (type == DeliveryType.Unreliable)
|
||||||
{
|
{
|
||||||
channel = 1;
|
channel = 1;
|
||||||
packetFlags = PacketFlags.None;
|
packetFlags = PacketFlags.UnreliableFragmented;
|
||||||
}
|
}
|
||||||
|
|
||||||
newPacket.Create(data, data.Length, packetFlags);
|
newPacket.Create(data, data.Length, packetFlags);
|
||||||
@@ -98,7 +94,7 @@ namespace Ragon.Core
|
|||||||
{
|
{
|
||||||
if (_host.CheckEvents(out _netEvent) <= 0)
|
if (_host.CheckEvents(out _netEvent) <= 0)
|
||||||
{
|
{
|
||||||
if (_host.Service(15, out _netEvent) <= 0)
|
if (_host.Service(0, out _netEvent) <= 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
polled = true;
|
polled = true;
|
||||||
@@ -107,11 +103,17 @@ namespace Ragon.Core
|
|||||||
switch (_netEvent.Type)
|
switch (_netEvent.Type)
|
||||||
{
|
{
|
||||||
case EventType.None:
|
case EventType.None:
|
||||||
Console.WriteLine("None event");
|
{
|
||||||
|
_logger.Trace("None event");
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case EventType.Connect:
|
case EventType.Connect:
|
||||||
{
|
{
|
||||||
|
// if (IsValidProtocol(_netEvent.Data))
|
||||||
|
// {
|
||||||
|
// _logger.Warn("Mismatched protocol, close connection");
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
_peers[_netEvent.Peer.ID] = _netEvent.Peer;
|
_peers[_netEvent.Peer.ID] = _netEvent.Peer;
|
||||||
_handler.OnEvent(_netEvent);
|
_handler.OnEvent(_netEvent);
|
||||||
break;
|
break;
|
||||||
@@ -140,5 +142,10 @@ namespace Ragon.Core
|
|||||||
{
|
{
|
||||||
_host?.Dispose();
|
_host?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool IsValidProtocol(uint protocol)
|
||||||
|
{
|
||||||
|
return protocol == _protocol;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@ namespace Ragon.Core;
|
|||||||
|
|
||||||
public interface ISocketServer
|
public interface ISocketServer
|
||||||
{
|
{
|
||||||
public void Start(ushort port, int connections);
|
public void Start(ushort port, int connections, uint protocol);
|
||||||
public void Process();
|
public void Process();
|
||||||
public void Stop();
|
public void Stop();
|
||||||
public void Send(uint peerId, byte[] data, DeliveryType type);
|
public void Send(uint peerId, byte[] data, DeliveryType type);
|
||||||
|
|||||||
Reference in New Issue
Block a user