Compare commits
73 Commits
v1.0.9-rc
...
v1.0.31-rc
| Author | SHA1 | Date | |
|---|---|---|---|
| b793ca3e68 | |||
| 3936e5c95b | |||
| e9418f4b22 | |||
| f34b05e6ff | |||
| a5a67963be | |||
| ab85578ccf | |||
| 13044357a5 | |||
| a9be230960 | |||
| 5a4bf0c24e | |||
| fa6ace4dc8 | |||
| 4d8ed1105a | |||
| e2ef761bd7 | |||
| 828112855f | |||
| 06ff76fe0b | |||
| c92b5a5bc4 | |||
| f83d3ea0c7 | |||
| 3564eb2adc | |||
| 3531432758 | |||
| 6bda468607 | |||
| 7ddd52bf9d | |||
| ecf3631163 | |||
| 8a149eb3b4 | |||
| 14ae5e8189 | |||
| b7e8327ca8 | |||
| b9e79af9d8 | |||
| 545ec02ecc | |||
| 73feb77169 | |||
| cbcf1773aa | |||
| 5519ae7679 | |||
| 1558b5eefb | |||
| aaa0e4a317 | |||
| ff712dc094 | |||
| c2c75cb513 | |||
| 9a22566f79 | |||
| 783d2ce922 | |||
| 5771ec738b | |||
| 85081e1da7 | |||
| dbaa5d9d92 | |||
| 54786c065d | |||
| 0b3a0dd1ac | |||
| 72ff37e94a | |||
| 75dab9d16f | |||
| 25b0e54d13 | |||
| e4f664b557 | |||
| 174a4c4422 | |||
| a51d7c73cd | |||
| 957622a170 | |||
| 4a07424293 | |||
| 568a3282c3 | |||
| 6a71fe5fe0 | |||
| 082e183989 | |||
| 51c0482a40 | |||
| 9e16c53cad | |||
| 6ec29e5dcd | |||
| 65c1d9c6d4 | |||
| f2934bc8ee | |||
| 81879d4fe2 | |||
| 2d89d267f5 | |||
| 34107cbd5f | |||
| ba2ce25aec | |||
| ca17e7de83 | |||
| dff6dbcf97 | |||
| ae485f96d4 | |||
| 74904de16b | |||
| 655cf3503d | |||
| 8fd8558323 | |||
| 944cb65733 | |||
| 877ebdcde2 | |||
| e2032f381a | |||
| 85336f998e | |||
| 16b8d3a062 | |||
| f72b4c0904 | |||
| 19272609ef |
@@ -19,7 +19,7 @@ jobs:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: ${{ github.ref }}
|
||||
release_name: Ragon.SimpleServer-${{ github.ref }}
|
||||
release_name: Ragon.Relay-${{ github.ref }}
|
||||
prerelease: true
|
||||
|
||||
build:
|
||||
@@ -53,10 +53,10 @@ jobs:
|
||||
- name: Build
|
||||
shell: bash
|
||||
run: |
|
||||
release_name="Ragon.SimpleServer-${{ env.TAG }}-${{ matrix.target }}"
|
||||
release_name="Ragon.Relay-${{ env.TAG }}-${{ matrix.target }}"
|
||||
|
||||
# Build everything
|
||||
dotnet publish Ragon.SimpleServer/Ragon.SimpleServer.csproj -c Release --runtime "${{ matrix.target }}" -p:PublishSingleFile=true -o "$release_name"
|
||||
dotnet publish Ragon.Relay/Ragon.Relay.csproj -c Release --runtime "${{ matrix.target }}" -p:PublishSingleFile=true -o "$release_name"
|
||||
|
||||
# Pack files
|
||||
7z a -tzip "${release_name}.zip" "./${release_name}/*"
|
||||
@@ -69,6 +69,6 @@ jobs:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ needs.release.outputs.upload_url }}
|
||||
asset_path: Ragon.SimpleServer-${{ env.TAG }}-${{ matrix.target }}.zip
|
||||
asset_name: Ragon.SimpleServer-${{ env.TAG }}-${{ matrix.target }}.zip
|
||||
asset_path: Ragon.Relay-${{ env.TAG }}-${{ matrix.target }}.zip
|
||||
asset_name: Ragon.Relay-${{ env.TAG }}-${{ matrix.target }}.zip
|
||||
asset_content_type: application/zip
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System.Threading;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace NetStack.Buffers {
|
||||
public abstract class ArrayPool<T> {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
private static ArrayPool<T> s_sharedInstance = null;
|
||||
#else
|
||||
private static volatile ArrayPool<T> s_sharedInstance = null;
|
||||
#endif
|
||||
|
||||
public static ArrayPool<T> Shared {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
[MethodImpl(256)]
|
||||
get {
|
||||
return Volatile.Read(ref s_sharedInstance) ?? EnsureSharedCreated();
|
||||
}
|
||||
#else
|
||||
[MethodImpl(256)]
|
||||
get {
|
||||
return s_sharedInstance ?? EnsureSharedCreated();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#pragma warning disable 420
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static ArrayPool<T> EnsureSharedCreated() {
|
||||
Interlocked.CompareExchange(ref s_sharedInstance, Create(), null);
|
||||
|
||||
return s_sharedInstance;
|
||||
}
|
||||
|
||||
public static ArrayPool<T> Create() {
|
||||
return new DefaultArrayPool<T>();
|
||||
}
|
||||
|
||||
public static ArrayPool<T> Create(int maxArrayLength, int maxArraysPerBucket) {
|
||||
return new DefaultArrayPool<T>(maxArrayLength, maxArraysPerBucket);
|
||||
}
|
||||
|
||||
public abstract T[] Rent(int minimumLength);
|
||||
|
||||
public abstract void Return(T[] array, bool clearArray = false);
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
using UnityEngine;
|
||||
#endif
|
||||
|
||||
namespace NetStack.Buffers {
|
||||
internal sealed class ArrayPoolEventSource {
|
||||
#if NETSTACK_BUFFERS_LOG
|
||||
internal static readonly ArrayPoolEventSource EventLog = new ArrayPoolEventSource();
|
||||
|
||||
internal enum BufferAllocatedReason : int {
|
||||
Pooled,
|
||||
OverMaximumSize,
|
||||
PoolExhausted
|
||||
}
|
||||
|
||||
internal void BufferAllocated(int bufferId, int bufferSize, int poolId, int bucketId, BufferAllocatedReason reason) {
|
||||
var message = "Buffer allocated (Buffer ID: " + bufferId + ", Buffer size: " + bufferSize + ", Pool ID: " + poolId + ", Bucket ID: " + bucketId + ", Reason: " + reason + ")";
|
||||
|
||||
if (reason == BufferAllocatedReason.Pooled)
|
||||
Log.Info("Buffers", message);
|
||||
else
|
||||
Log.Warning("Buffers", message);
|
||||
}
|
||||
|
||||
internal void BufferRented(int bufferId, int bufferSize, int poolId, int bucketId) {
|
||||
Log.Info("Buffers", "Buffer rented (Buffer ID: " + bufferId + ", Buffer size: " + bufferSize + ", Pool ID: " + poolId + ", Bucket ID: " + bucketId + ")");
|
||||
}
|
||||
|
||||
internal void BufferReturned(int bufferId, int bufferSize, int poolId) {
|
||||
Log.Info("Buffers", "Buffer returned (Buffer ID: " + bufferId + ", Buffer size: " + bufferSize + ", Pool ID: " + poolId + ")");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
internal static class Log {
|
||||
private static string Output(string module, string message) {
|
||||
return DateTime.Now.ToString("[HH:mm:ss]") + " [NetStack." + module + "] " + message;
|
||||
}
|
||||
|
||||
public static void Info(string module, string message) {
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
Debug.Log(Output(module, message));
|
||||
#else
|
||||
Console.WriteLine(Output(module, message));
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void Warning(string module, string message) {
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
Debug.LogWarning(Output(module, message));
|
||||
#else
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine(Output(module, message));
|
||||
Console.ResetColor();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
|
||||
namespace NetStack.Buffers {
|
||||
internal sealed partial class DefaultArrayPool<T> : ArrayPool<T> {
|
||||
private const int DefaultMaxArrayLength = 1024 * 1024;
|
||||
private const int DefaultMaxNumberOfArraysPerBucket = 50;
|
||||
private static T[] s_emptyArray;
|
||||
private readonly Bucket[] _buckets;
|
||||
|
||||
internal DefaultArrayPool() : this(DefaultMaxArrayLength, DefaultMaxNumberOfArraysPerBucket) { }
|
||||
|
||||
internal DefaultArrayPool(int maxArrayLength, int maxArraysPerBucket) {
|
||||
if (maxArrayLength <= 0)
|
||||
throw new ArgumentOutOfRangeException("maxArrayLength");
|
||||
|
||||
if (maxArraysPerBucket <= 0)
|
||||
throw new ArgumentOutOfRangeException("maxArraysPerBucket");
|
||||
|
||||
const int MinimumArrayLength = 0x10, MaximumArrayLength = 0x40000000;
|
||||
|
||||
if (maxArrayLength > MaximumArrayLength)
|
||||
maxArrayLength = MaximumArrayLength;
|
||||
else if (maxArrayLength < MinimumArrayLength)
|
||||
maxArrayLength = MinimumArrayLength;
|
||||
|
||||
int poolId = Id;
|
||||
int maxBuckets = Utilities.SelectBucketIndex(maxArrayLength);
|
||||
var buckets = new Bucket[maxBuckets + 1];
|
||||
|
||||
for (int i = 0; i < buckets.Length; i++) {
|
||||
buckets[i] = new Bucket(Utilities.GetMaxSizeForBucket(i), maxArraysPerBucket, poolId);
|
||||
}
|
||||
|
||||
_buckets = buckets;
|
||||
}
|
||||
|
||||
private int Id {
|
||||
get {
|
||||
return GetHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
public override T[] Rent(int minimumLength) {
|
||||
if (minimumLength < 0)
|
||||
throw new ArgumentOutOfRangeException("minimumLength");
|
||||
else if (minimumLength == 0)
|
||||
return s_emptyArray ?? (s_emptyArray = new T[0]);
|
||||
|
||||
#if NETSTACK_BUFFERS_LOG
|
||||
var log = ArrayPoolEventSource.EventLog;
|
||||
#endif
|
||||
|
||||
T[] buffer = null;
|
||||
int index = Utilities.SelectBucketIndex(minimumLength);
|
||||
|
||||
if (index < _buckets.Length) {
|
||||
const int MaxBucketsToTry = 2;
|
||||
|
||||
int i = index;
|
||||
|
||||
do {
|
||||
buffer = _buckets[i].Rent();
|
||||
|
||||
if (buffer != null) {
|
||||
#if NETSTACK_BUFFERS_LOG
|
||||
log.BufferRented(buffer.GetHashCode(), buffer.Length, Id, _buckets[i].Id);
|
||||
#endif
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
while (++i < _buckets.Length && i != index + MaxBucketsToTry);
|
||||
|
||||
buffer = new T[_buckets[index]._bufferLength];
|
||||
} else {
|
||||
buffer = new T[minimumLength];
|
||||
}
|
||||
|
||||
#if NETSTACK_BUFFERS_LOG
|
||||
int bufferId = buffer.GetHashCode(), bucketId = -1;
|
||||
|
||||
log.BufferRented(bufferId, buffer.Length, Id, bucketId);
|
||||
log.BufferAllocated(bufferId, buffer.Length, Id, bucketId, index >= _buckets.Length ? ArrayPoolEventSource.BufferAllocatedReason.OverMaximumSize : ArrayPoolEventSource.BufferAllocatedReason.PoolExhausted);
|
||||
#endif
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public override void Return(T[] array, bool clearArray = false) {
|
||||
if (array == null)
|
||||
throw new ArgumentNullException("array");
|
||||
else if (array.Length == 0)
|
||||
return;
|
||||
|
||||
int bucket = Utilities.SelectBucketIndex(array.Length);
|
||||
|
||||
if (bucket < _buckets.Length) {
|
||||
if (clearArray)
|
||||
Array.Clear(array, 0, array.Length);
|
||||
|
||||
_buckets[bucket].Return(array);
|
||||
}
|
||||
|
||||
#if NETSTACK_BUFFERS_LOG
|
||||
var log = ArrayPoolEventSource.EventLog;
|
||||
|
||||
log.BufferReturned(array.GetHashCode(), array.Length, Id);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
|
||||
namespace NetStack.Buffers {
|
||||
internal sealed partial class DefaultArrayPool<T> : ArrayPool<T> {
|
||||
private sealed class Bucket {
|
||||
internal readonly int _bufferLength;
|
||||
private readonly T[][] _buffers;
|
||||
#if NETSTACK_BUFFERS_LOG
|
||||
private readonly int _poolId;
|
||||
#endif
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
private SpinLock _lock;
|
||||
#else
|
||||
private object _lock;
|
||||
#endif
|
||||
private int _index;
|
||||
|
||||
internal Bucket(int bufferLength, int numberOfBuffers, int poolId) {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
_lock = new SpinLock();
|
||||
#else
|
||||
_lock = new Object();
|
||||
#endif
|
||||
_buffers = new T[numberOfBuffers][];
|
||||
_bufferLength = bufferLength;
|
||||
|
||||
#if NETSTACK_BUFFERS_LOG
|
||||
_poolId = poolId;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
internal int Id => GetHashCode();
|
||||
#else
|
||||
internal int Id {
|
||||
get {
|
||||
return GetHashCode();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
internal T[] Rent() {
|
||||
T[][] buffers = _buffers;
|
||||
T[] buffer = null;
|
||||
bool allocateBuffer = false;
|
||||
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
bool lockTaken = false;
|
||||
|
||||
try {
|
||||
_lock.Enter(ref lockTaken);
|
||||
|
||||
if (_index < buffers.Length) {
|
||||
buffer = buffers[_index];
|
||||
buffers[_index++] = null;
|
||||
allocateBuffer = buffer == null;
|
||||
}
|
||||
}
|
||||
|
||||
finally {
|
||||
if (lockTaken)
|
||||
_lock.Exit(false);
|
||||
}
|
||||
#else
|
||||
try {
|
||||
Monitor.Enter(_lock);
|
||||
|
||||
if (_index < buffers.Length) {
|
||||
buffer = buffers[_index];
|
||||
buffers[_index++] = null;
|
||||
allocateBuffer = buffer == null;
|
||||
}
|
||||
}
|
||||
|
||||
finally {
|
||||
Monitor.Exit(_lock);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (allocateBuffer) {
|
||||
buffer = new T[_bufferLength];
|
||||
|
||||
#if NETSTACK_BUFFERS_LOG
|
||||
var log = ArrayPoolEventSource.EventLog;
|
||||
|
||||
log.BufferAllocated(buffer.GetHashCode(), _bufferLength, _poolId, Id, ArrayPoolEventSource.BufferAllocatedReason.Pooled);
|
||||
#endif
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
internal void Return(T[] array) {
|
||||
if (array.Length != _bufferLength)
|
||||
throw new ArgumentException("BufferNotFromPool", "array");
|
||||
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
bool lockTaken = false;
|
||||
|
||||
try {
|
||||
_lock.Enter(ref lockTaken);
|
||||
|
||||
if (_index != 0)
|
||||
_buffers[--_index] = array;
|
||||
|
||||
}
|
||||
|
||||
finally {
|
||||
if (lockTaken)
|
||||
_lock.Exit(false);
|
||||
}
|
||||
#else
|
||||
try {
|
||||
Monitor.Enter(_lock);
|
||||
|
||||
if (_index != 0)
|
||||
_buffers[--_index] = array;
|
||||
}
|
||||
|
||||
finally {
|
||||
Monitor.Exit(_lock);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
using UnityEngine.Assertions;
|
||||
#endif
|
||||
|
||||
namespace NetStack.Buffers {
|
||||
internal static class Utilities {
|
||||
[MethodImpl(256)]
|
||||
internal static int SelectBucketIndex(int bufferSize) {
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
Assert.IsTrue(bufferSize > 0);
|
||||
#else
|
||||
Debug.Assert(bufferSize > 0);
|
||||
#endif
|
||||
|
||||
uint bitsRemaining = ((uint)bufferSize - 1) >> 4;
|
||||
int poolIndex = 0;
|
||||
|
||||
if (bitsRemaining > 0xFFFF) {
|
||||
bitsRemaining >>= 16;
|
||||
poolIndex = 16;
|
||||
}
|
||||
|
||||
if (bitsRemaining > 0xFF) {
|
||||
bitsRemaining >>= 8;
|
||||
poolIndex += 8;
|
||||
}
|
||||
|
||||
if (bitsRemaining > 0xF) {
|
||||
bitsRemaining >>= 4;
|
||||
poolIndex += 4;
|
||||
}
|
||||
|
||||
if (bitsRemaining > 0x3) {
|
||||
bitsRemaining >>= 2;
|
||||
poolIndex += 2;
|
||||
}
|
||||
|
||||
if (bitsRemaining > 0x1) {
|
||||
bitsRemaining >>= 1;
|
||||
poolIndex += 1;
|
||||
}
|
||||
|
||||
return poolIndex + (int)bitsRemaining;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
internal static int GetMaxSizeForBucket(int binIndex) {
|
||||
int maxSize = 16 << binIndex;
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
Assert.IsTrue(maxSize >= 0);
|
||||
#else
|
||||
Debug.Assert(maxSize >= 0);
|
||||
#endif
|
||||
|
||||
return maxSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,184 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#if !(ENABLE_MONO || ENABLE_IL2CPP)
|
||||
using System.Numerics;
|
||||
#else
|
||||
using UnityEngine;
|
||||
#endif
|
||||
|
||||
namespace NetStack.Quantization {
|
||||
public struct QuantizedVector2 {
|
||||
public uint x;
|
||||
public uint y;
|
||||
|
||||
public QuantizedVector2(uint x, uint y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
public struct QuantizedVector3 {
|
||||
public uint x;
|
||||
public uint y;
|
||||
public uint z;
|
||||
|
||||
public QuantizedVector3(uint x, uint y, uint z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
}
|
||||
|
||||
public struct QuantizedVector4 {
|
||||
public uint x;
|
||||
public uint y;
|
||||
public uint z;
|
||||
public uint w;
|
||||
|
||||
public QuantizedVector4(uint x, uint y, uint z, uint w) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
}
|
||||
|
||||
public static class DeBruijn {
|
||||
public static readonly int[] Lookup = new int[32] {
|
||||
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
||||
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
||||
};
|
||||
}
|
||||
|
||||
public class BoundedRange {
|
||||
private readonly float minValue;
|
||||
private readonly float maxValue;
|
||||
private readonly float precision;
|
||||
private readonly int requiredBits;
|
||||
private readonly uint mask;
|
||||
|
||||
public BoundedRange(float minValue, float maxValue, float precision) {
|
||||
this.minValue = minValue;
|
||||
this.maxValue = maxValue;
|
||||
this.precision = precision;
|
||||
|
||||
requiredBits = Log2((uint)((maxValue - minValue) * (1.0f / precision) + 0.5f)) + 1;
|
||||
mask = (uint)((1L << requiredBits) - 1);
|
||||
}
|
||||
|
||||
private int Log2(uint value) {
|
||||
value |= value >> 1;
|
||||
value |= value >> 2;
|
||||
value |= value >> 4;
|
||||
value |= value >> 8;
|
||||
value |= value >> 16;
|
||||
|
||||
return DeBruijn.Lookup[(value * 0x07C4ACDDU) >> 27];
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public uint Quantize(float value) {
|
||||
if (value < minValue)
|
||||
value = minValue;
|
||||
else if (value > maxValue)
|
||||
value = maxValue;
|
||||
|
||||
return (uint)((float)((value - minValue) * (1f / precision)) + 0.5f) & mask;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public float Dequantize(uint data) {
|
||||
float adjusted = ((float)data * precision) + minValue;
|
||||
|
||||
if (adjusted < minValue)
|
||||
adjusted = minValue;
|
||||
else if (adjusted > maxValue)
|
||||
adjusted = maxValue;
|
||||
|
||||
return adjusted;
|
||||
}
|
||||
|
||||
public static QuantizedVector2 Quantize(Vector2 vector2, BoundedRange[] boundedRange) {
|
||||
QuantizedVector2 data = default(QuantizedVector2);
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
data.x = boundedRange[0].Quantize(vector2.x);
|
||||
data.y = boundedRange[1].Quantize(vector2.y);
|
||||
#else
|
||||
data.x = boundedRange[0].Quantize(vector2.X);
|
||||
data.y = boundedRange[1].Quantize(vector2.Y);
|
||||
#endif
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public static QuantizedVector3 Quantize(Vector3 vector3, BoundedRange[] boundedRange) {
|
||||
QuantizedVector3 data = default(QuantizedVector3);
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
data.x = boundedRange[0].Quantize(vector3.x);
|
||||
data.y = boundedRange[1].Quantize(vector3.y);
|
||||
data.z = boundedRange[2].Quantize(vector3.z);
|
||||
#else
|
||||
data.x = boundedRange[0].Quantize(vector3.X);
|
||||
data.y = boundedRange[1].Quantize(vector3.Y);
|
||||
data.z = boundedRange[2].Quantize(vector3.Z);
|
||||
#endif
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public static QuantizedVector4 Quantize(Vector4 vector4, BoundedRange[] boundedRange) {
|
||||
QuantizedVector4 data = default(QuantizedVector4);
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
data.x = boundedRange[0].Quantize(vector4.x);
|
||||
data.y = boundedRange[1].Quantize(vector4.y);
|
||||
data.z = boundedRange[2].Quantize(vector4.z);
|
||||
data.w = boundedRange[3].Quantize(vector4.w);
|
||||
#else
|
||||
data.x = boundedRange[0].Quantize(vector4.X);
|
||||
data.y = boundedRange[1].Quantize(vector4.Y);
|
||||
data.z = boundedRange[2].Quantize(vector4.Z);
|
||||
data.w = boundedRange[3].Quantize(vector4.W);
|
||||
#endif
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public static Vector2 Dequantize(QuantizedVector2 data, BoundedRange[] boundedRange) {
|
||||
return new Vector2(boundedRange[0].Dequantize(data.x), boundedRange[1].Dequantize(data.y));
|
||||
}
|
||||
|
||||
public static Vector3 Dequantize(QuantizedVector3 data, BoundedRange[] boundedRange) {
|
||||
return new Vector3(boundedRange[0].Dequantize(data.x), boundedRange[1].Dequantize(data.y), boundedRange[2].Dequantize(data.z));
|
||||
}
|
||||
|
||||
public static Vector4 Dequantize(QuantizedVector4 data, BoundedRange[] boundedRange) {
|
||||
return new Vector4(boundedRange[0].Dequantize(data.x), boundedRange[1].Dequantize(data.y), boundedRange[2].Dequantize(data.z), boundedRange[3].Dequantize(data.w));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace NetStack.Quantization {
|
||||
public static class HalfPrecision {
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
private struct Values {
|
||||
[FieldOffset(0)]
|
||||
public float f;
|
||||
[FieldOffset(0)]
|
||||
public int i;
|
||||
[FieldOffset(0)]
|
||||
public uint u;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public static ushort Quantize(float value) {
|
||||
var values = new Values {
|
||||
f = value
|
||||
};
|
||||
|
||||
return Quantize(values.i);
|
||||
}
|
||||
|
||||
public static ushort Quantize(int value) {
|
||||
int s = (value >> 16) & 0x00008000;
|
||||
int e = ((value >> 23) & 0X000000FF) - (127 - 15);
|
||||
int m = value & 0X007FFFFF;
|
||||
|
||||
if (e <= 0) {
|
||||
if (e < -10)
|
||||
return (ushort)s;
|
||||
|
||||
m = m | 0x00800000;
|
||||
|
||||
int t = 14 - e;
|
||||
int a = (1 << (t - 1)) - 1;
|
||||
int b = (m >> t) & 1;
|
||||
|
||||
m = (m + a + b) >> t;
|
||||
|
||||
return (ushort)(s | m);
|
||||
}
|
||||
|
||||
if (e == 0XFF - (127 - 15)) {
|
||||
if (m == 0)
|
||||
return (ushort)(s | 0X7C00);
|
||||
|
||||
m >>= 13;
|
||||
|
||||
return (ushort)(s | 0X7C00 | m | ((m == 0) ? 1 : 0));
|
||||
}
|
||||
|
||||
m = m + 0X00000FFF + ((m >> 13) & 1);
|
||||
|
||||
if ((m & 0x00800000) != 0) {
|
||||
m = 0;
|
||||
e++;
|
||||
}
|
||||
|
||||
if (e > 30)
|
||||
return (ushort)(s | 0X7C00);
|
||||
|
||||
return (ushort)(s | (e << 10) | (m >> 13));
|
||||
}
|
||||
|
||||
public static float Dequantize(ushort value) {
|
||||
uint result;
|
||||
uint mantissa = (uint)(value & 1023);
|
||||
uint exponent = 0XFFFFFFF2;
|
||||
|
||||
if ((value & -33792) == 0) {
|
||||
if (mantissa != 0) {
|
||||
while ((mantissa & 1024) == 0) {
|
||||
exponent--;
|
||||
mantissa = mantissa << 1;
|
||||
}
|
||||
|
||||
mantissa &= 0XFFFFFBFF;
|
||||
result = ((uint)((((uint)value & 0x8000) << 16) | ((exponent + 127) << 23))) | (mantissa << 13);
|
||||
} else {
|
||||
result = (uint)((value & 0x8000) << 16);
|
||||
}
|
||||
} else {
|
||||
result = ((((uint)value & 0x8000) << 16) | ((((((uint)value >> 10) & 0X1F) - 15) + 127) << 23)) | (mantissa << 13);
|
||||
}
|
||||
|
||||
var values = new Values {
|
||||
u = result
|
||||
};
|
||||
|
||||
return values.f;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,223 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Stanislav Denisov, Maxim Munning, Davin Carten
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
|
||||
#if !(ENABLE_MONO || ENABLE_IL2CPP)
|
||||
using System.Numerics;
|
||||
#else
|
||||
using UnityEngine;
|
||||
#endif
|
||||
|
||||
namespace NetStack.Quantization {
|
||||
public struct QuantizedQuaternion {
|
||||
public uint m;
|
||||
public uint a;
|
||||
public uint b;
|
||||
public uint c;
|
||||
|
||||
public QuantizedQuaternion(uint m, uint a, uint b, uint c) {
|
||||
this.m = m;
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.c = c;
|
||||
}
|
||||
}
|
||||
|
||||
public static class SmallestThree {
|
||||
private const float smallestThreeUnpack = 0.70710678118654752440084436210485f + 0.0000001f;
|
||||
private const float smallestThreePack = 1.0f / smallestThreeUnpack;
|
||||
|
||||
public static QuantizedQuaternion Quantize(Quaternion quaternion, int bitsPerElement = 12) {
|
||||
float halfRange = (1 << bitsPerElement - 1);
|
||||
float packer = smallestThreePack * halfRange;
|
||||
float maxValue = float.MinValue;
|
||||
bool signMinus = false;
|
||||
uint m = 0;
|
||||
uint a = 0;
|
||||
uint b = 0;
|
||||
uint c = 0;
|
||||
|
||||
for (uint i = 0; i <= 3; i++) {
|
||||
float element = 0.0f;
|
||||
float abs = 0.0f;
|
||||
|
||||
switch (i) {
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
case 0:
|
||||
element = quaternion.x;
|
||||
|
||||
break;
|
||||
|
||||
case 1:
|
||||
element = quaternion.y;
|
||||
|
||||
break;
|
||||
|
||||
case 2:
|
||||
element = quaternion.z;
|
||||
|
||||
break;
|
||||
|
||||
case 3:
|
||||
element = quaternion.w;
|
||||
|
||||
break;
|
||||
#else
|
||||
case 0:
|
||||
element = quaternion.X;
|
||||
|
||||
break;
|
||||
|
||||
case 1:
|
||||
element = quaternion.Y;
|
||||
|
||||
break;
|
||||
|
||||
case 2:
|
||||
element = quaternion.Z;
|
||||
|
||||
break;
|
||||
|
||||
case 3:
|
||||
element = quaternion.W;
|
||||
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
abs = Math.Abs(element);
|
||||
|
||||
if (abs > maxValue) {
|
||||
signMinus = (element < 0.0f);
|
||||
m = i;
|
||||
maxValue = abs;
|
||||
}
|
||||
}
|
||||
|
||||
float af = 0.0f;
|
||||
float bf = 0.0f;
|
||||
float cf = 0.0f;
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
switch (m) {
|
||||
case 0:
|
||||
af = quaternion.y;
|
||||
bf = quaternion.z;
|
||||
cf = quaternion.w;
|
||||
|
||||
break;
|
||||
case 1:
|
||||
af = quaternion.x;
|
||||
bf = quaternion.z;
|
||||
cf = quaternion.w;
|
||||
|
||||
break;
|
||||
case 2:
|
||||
af = quaternion.x;
|
||||
bf = quaternion.y;
|
||||
cf = quaternion.w;
|
||||
|
||||
break;
|
||||
default:
|
||||
af = quaternion.x;
|
||||
bf = quaternion.y;
|
||||
cf = quaternion.z;
|
||||
|
||||
break;
|
||||
}
|
||||
#else
|
||||
switch (m) {
|
||||
case 0:
|
||||
af = quaternion.Y;
|
||||
bf = quaternion.Z;
|
||||
cf = quaternion.W;
|
||||
|
||||
break;
|
||||
case 1:
|
||||
af = quaternion.X;
|
||||
bf = quaternion.Z;
|
||||
cf = quaternion.W;
|
||||
|
||||
break;
|
||||
case 2:
|
||||
af = quaternion.X;
|
||||
bf = quaternion.Y;
|
||||
cf = quaternion.W;
|
||||
|
||||
break;
|
||||
default:
|
||||
af = quaternion.X;
|
||||
bf = quaternion.Y;
|
||||
cf = quaternion.Z;
|
||||
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (signMinus) {
|
||||
a = (uint)((-af * packer) + halfRange);
|
||||
b = (uint)((-bf * packer) + halfRange);
|
||||
c = (uint)((-cf * packer) + halfRange);
|
||||
} else {
|
||||
a = (uint)((af * packer) + halfRange);
|
||||
b = (uint)((bf * packer) + halfRange);
|
||||
c = (uint)((cf * packer) + halfRange);
|
||||
}
|
||||
|
||||
return new QuantizedQuaternion(m, a, b, c);
|
||||
}
|
||||
|
||||
public static Quaternion Dequantize(QuantizedQuaternion data, int bitsPerElement = 12) {
|
||||
int halfRange = (1 << bitsPerElement - 1);
|
||||
float unpacker = smallestThreeUnpack * (1.0f / halfRange);
|
||||
uint m = data.m;
|
||||
int ai = (int)data.a;
|
||||
int bi = (int)data.b;
|
||||
int ci = (int)data.c;
|
||||
|
||||
ai -= halfRange;
|
||||
bi -= halfRange;
|
||||
ci -= halfRange;
|
||||
|
||||
float a = ai * unpacker;
|
||||
float b = bi * unpacker;
|
||||
float c = ci * unpacker;
|
||||
|
||||
float d = (float)Math.Sqrt(1.0f - ((a * a) + (b * b) + (c * c)));
|
||||
|
||||
switch (m) {
|
||||
case 0:
|
||||
return new Quaternion(d, a, b, c);
|
||||
|
||||
case 1:
|
||||
return new Quaternion(a, d, b, c);
|
||||
|
||||
case 2:
|
||||
return new Quaternion(a, b, d, c);
|
||||
|
||||
default:
|
||||
return new Quaternion(a, b, c, d);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,546 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Stanislav Denisov, Maxim Munnig
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2018 Alexander Shoulson
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
using UnityEngine.Assertions;
|
||||
#endif
|
||||
|
||||
namespace NetStack.Serialization {
|
||||
public class BitBuffer {
|
||||
private const int defaultCapacity = 375; // 375 * 4 = 1500 bytes
|
||||
private const int stringLengthBits = 8;
|
||||
private const int stringLengthMax = (1 << stringLengthBits) - 1; // 255
|
||||
private const int bitsASCII = 7;
|
||||
private const int growFactor = 2;
|
||||
private const int minGrow = 1;
|
||||
private int readPosition;
|
||||
private int nextPosition;
|
||||
private uint[] chunks;
|
||||
|
||||
public BitBuffer(int capacity = defaultCapacity) {
|
||||
readPosition = 0;
|
||||
nextPosition = 0;
|
||||
chunks = new uint[capacity];
|
||||
}
|
||||
|
||||
public int Length {
|
||||
get {
|
||||
return ((nextPosition - 1) >> 3) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsFinished {
|
||||
get {
|
||||
return nextPosition == readPosition;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public void Clear() {
|
||||
readPosition = 0;
|
||||
nextPosition = 0;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public BitBuffer Add(int numBits, uint value) {
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
Assert.IsFalse(numBits < 0); // Pushing negative bits
|
||||
Assert.IsFalse(numBits > 32); // Pushing too many bits
|
||||
#else
|
||||
Debug.Assert(!(numBits < 0));
|
||||
Debug.Assert(!(numBits > 32));
|
||||
#endif
|
||||
|
||||
int index = nextPosition >> 5;
|
||||
int used = nextPosition & 0x0000001F;
|
||||
|
||||
if ((index + 1) >= chunks.Length)
|
||||
ExpandArray();
|
||||
|
||||
ulong chunkMask = ((1UL << used) - 1);
|
||||
ulong scratch = chunks[index] & chunkMask;
|
||||
ulong result = scratch | ((ulong)value << used);
|
||||
|
||||
chunks[index] = (uint)result;
|
||||
chunks[index + 1] = (uint)(result >> 32);
|
||||
nextPosition += numBits;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public uint Read(int numBits) {
|
||||
uint result = Peek(numBits);
|
||||
|
||||
readPosition += numBits;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public uint Peek(int numBits) {
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
Assert.IsFalse(numBits < 0); // Pushing negative bits
|
||||
Assert.IsFalse(numBits > 32); // Pushing too many bits
|
||||
#else
|
||||
Debug.Assert(!(numBits < 0));
|
||||
Debug.Assert(!(numBits > 32));
|
||||
#endif
|
||||
|
||||
int index = readPosition >> 5;
|
||||
int used = readPosition & 0x0000001F;
|
||||
|
||||
ulong chunkMask = ((1UL << numBits) - 1) << used;
|
||||
ulong scratch = (ulong)chunks[index];
|
||||
|
||||
if ((index + 1) < chunks.Length)
|
||||
scratch |= (ulong)chunks[index + 1] << 32;
|
||||
|
||||
ulong result = (scratch & chunkMask) >> used;
|
||||
|
||||
return (uint)result;
|
||||
}
|
||||
|
||||
public int ToArray(byte[] data) {
|
||||
Add(1, 1);
|
||||
|
||||
int numChunks = (nextPosition >> 5) + 1;
|
||||
int length = data.Length;
|
||||
|
||||
for (int i = 0; i < numChunks; i++) {
|
||||
int dataIdx = i * 4;
|
||||
uint chunk = chunks[i];
|
||||
|
||||
if (dataIdx < length)
|
||||
data[dataIdx] = (byte)(chunk);
|
||||
|
||||
if (dataIdx + 1 < length)
|
||||
data[dataIdx + 1] = (byte)(chunk >> 8);
|
||||
|
||||
if (dataIdx + 2 < length)
|
||||
data[dataIdx + 2] = (byte)(chunk >> 16);
|
||||
|
||||
if (dataIdx + 3 < length)
|
||||
data[dataIdx + 3] = (byte)(chunk >> 24);
|
||||
}
|
||||
|
||||
return Length;
|
||||
}
|
||||
|
||||
public void FromArray(byte[] data, int length) {
|
||||
int numChunks = (length / 4) + 1;
|
||||
|
||||
if (chunks.Length < numChunks)
|
||||
chunks = new uint[numChunks];
|
||||
|
||||
for (int i = 0; i < numChunks; i++) {
|
||||
int dataIdx = i * 4;
|
||||
uint chunk = 0;
|
||||
|
||||
if (dataIdx < length)
|
||||
chunk = (uint)data[dataIdx];
|
||||
|
||||
if (dataIdx + 1 < length)
|
||||
chunk = chunk | (uint)data[dataIdx + 1] << 8;
|
||||
|
||||
if (dataIdx + 2 < length)
|
||||
chunk = chunk | (uint)data[dataIdx + 2] << 16;
|
||||
|
||||
if (dataIdx + 3 < length)
|
||||
chunk = chunk | (uint)data[dataIdx + 3] << 24;
|
||||
|
||||
chunks[i] = chunk;
|
||||
}
|
||||
|
||||
int positionInByte = FindHighestBitPosition(data[length - 1]);
|
||||
|
||||
nextPosition = ((length - 1) * 8) + (positionInByte - 1);
|
||||
readPosition = 0;
|
||||
}
|
||||
|
||||
#if NETSTACK_SPAN
|
||||
public int ToSpan(ref Span<byte> data) {
|
||||
Add(1, 1);
|
||||
|
||||
int numChunks = (nextPosition >> 5) + 1;
|
||||
int length = data.Length;
|
||||
|
||||
for (int i = 0; i < numChunks; i++) {
|
||||
int dataIdx = i * 4;
|
||||
uint chunk = chunks[i];
|
||||
|
||||
if (dataIdx < length)
|
||||
data[dataIdx] = (byte)(chunk);
|
||||
|
||||
if (dataIdx + 1 < length)
|
||||
data[dataIdx + 1] = (byte)(chunk >> 8);
|
||||
|
||||
if (dataIdx + 2 < length)
|
||||
data[dataIdx + 2] = (byte)(chunk >> 16);
|
||||
|
||||
if (dataIdx + 3 < length)
|
||||
data[dataIdx + 3] = (byte)(chunk >> 24);
|
||||
}
|
||||
|
||||
return Length;
|
||||
}
|
||||
|
||||
public void FromSpan(ref ReadOnlySpan<byte> data, int length) {
|
||||
int numChunks = (length / 4) + 1;
|
||||
|
||||
if (chunks.Length < numChunks)
|
||||
chunks = new uint[numChunks];
|
||||
|
||||
for (int i = 0; i < numChunks; i++) {
|
||||
int dataIdx = i * 4;
|
||||
uint chunk = 0;
|
||||
|
||||
if (dataIdx < length)
|
||||
chunk = (uint)data[dataIdx];
|
||||
|
||||
if (dataIdx + 1 < length)
|
||||
chunk = chunk | (uint)data[dataIdx + 1] << 8;
|
||||
|
||||
if (dataIdx + 2 < length)
|
||||
chunk = chunk | (uint)data[dataIdx + 2] << 16;
|
||||
|
||||
if (dataIdx + 3 < length)
|
||||
chunk = chunk | (uint)data[dataIdx + 3] << 24;
|
||||
|
||||
chunks[i] = chunk;
|
||||
}
|
||||
|
||||
int positionInByte = FindHighestBitPosition(data[length - 1]);
|
||||
|
||||
nextPosition = ((length - 1) * 8) + (positionInByte - 1);
|
||||
readPosition = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
[MethodImpl(256)]
|
||||
public BitBuffer AddBool(bool value) {
|
||||
Add(1, value ? 1U : 0U);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public bool ReadBool() {
|
||||
return Read(1) > 0;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public bool PeekBool() {
|
||||
return Peek(1) > 0;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public BitBuffer AddByte(byte value) {
|
||||
Add(8, value);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public byte ReadByte() {
|
||||
return (byte)Read(8);
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public byte PeekByte() {
|
||||
return (byte)Peek(8);
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public BitBuffer AddShort(short value) {
|
||||
AddInt(value);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public short ReadShort() {
|
||||
return (short)ReadInt();
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public short PeekShort() {
|
||||
return (short)PeekInt();
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public BitBuffer AddUShort(ushort value) {
|
||||
AddUInt(value);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public ushort ReadUShort() {
|
||||
return (ushort)ReadUInt();
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public ushort PeekUShort() {
|
||||
return (ushort)PeekUInt();
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public BitBuffer AddInt(int value) {
|
||||
uint zigzag = (uint)((value << 1) ^ (value >> 31));
|
||||
|
||||
AddUInt(zigzag);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public int ReadInt() {
|
||||
uint value = ReadUInt();
|
||||
int zagzig = (int)((value >> 1) ^ (-(int)(value & 1)));
|
||||
|
||||
return zagzig;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public int PeekInt() {
|
||||
uint value = PeekUInt();
|
||||
int zagzig = (int)((value >> 1) ^ (-(int)(value & 1)));
|
||||
|
||||
return zagzig;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public BitBuffer AddUInt(uint value) {
|
||||
uint buffer = 0x0u;
|
||||
|
||||
do {
|
||||
buffer = value & 0x7Fu;
|
||||
value >>= 7;
|
||||
|
||||
if (value > 0)
|
||||
buffer |= 0x80u;
|
||||
|
||||
Add(8, buffer);
|
||||
}
|
||||
|
||||
while (value > 0);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public uint ReadUInt() {
|
||||
uint buffer = 0x0u;
|
||||
uint value = 0x0u;
|
||||
int shift = 0;
|
||||
|
||||
do {
|
||||
buffer = Read(8);
|
||||
|
||||
value |= (buffer & 0x7Fu) << shift;
|
||||
shift += 7;
|
||||
}
|
||||
|
||||
while ((buffer & 0x80u) > 0);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public uint PeekUInt() {
|
||||
int tempPosition = readPosition;
|
||||
uint value = ReadUInt();
|
||||
|
||||
readPosition = tempPosition;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public BitBuffer AddLong(long value) {
|
||||
AddInt((int)(value & uint.MaxValue));
|
||||
AddInt((int)(value >> 32));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public long ReadLong() {
|
||||
int low = ReadInt();
|
||||
int high = ReadInt();
|
||||
long value = high;
|
||||
|
||||
return value << 32 | (uint)low;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public long PeekLong() {
|
||||
int tempPosition = readPosition;
|
||||
long value = ReadLong();
|
||||
|
||||
readPosition = tempPosition;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public BitBuffer AddULong(ulong value) {
|
||||
AddUInt((uint)(value & uint.MaxValue));
|
||||
AddUInt((uint)(value >> 32));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public ulong ReadULong() {
|
||||
uint low = ReadUInt();
|
||||
uint high = ReadUInt();
|
||||
|
||||
return (ulong)high << 32 | low;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public ulong PeekULong() {
|
||||
int tempPosition = readPosition;
|
||||
ulong value = ReadULong();
|
||||
|
||||
readPosition = tempPosition;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public BitBuffer AddString(string value) {
|
||||
if (value == null)
|
||||
throw new ArgumentNullException("value");
|
||||
|
||||
uint length = (uint)value.Length;
|
||||
|
||||
if (length > stringLengthMax) {
|
||||
length = (uint)stringLengthMax;
|
||||
|
||||
throw new ArgumentOutOfRangeException("value length exceeded");
|
||||
}
|
||||
|
||||
Add(stringLengthBits, length);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
Add(bitsASCII, ToASCII(value[i]));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public string ReadString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
uint length = Read(stringLengthBits);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
builder.Append((char)Read(bitsASCII));
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
for (int i = chunks.Length - 1; i >= 0; i--) {
|
||||
builder.Append(Convert.ToString(chunks[i], 2).PadLeft(32, '0'));
|
||||
}
|
||||
|
||||
StringBuilder spaced = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < builder.Length; i++) {
|
||||
spaced.Append(builder[i]);
|
||||
|
||||
if (((i + 1) % 8) == 0)
|
||||
spaced.Append(" ");
|
||||
}
|
||||
|
||||
return spaced.ToString();
|
||||
}
|
||||
|
||||
private void ExpandArray() {
|
||||
int newCapacity = (chunks.Length * growFactor) + minGrow;
|
||||
uint[] newChunks = new uint[newCapacity];
|
||||
|
||||
Array.Copy(chunks, newChunks, chunks.Length);
|
||||
chunks = newChunks;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
private static int FindHighestBitPosition(byte data) {
|
||||
int shiftCount = 0;
|
||||
|
||||
while (data > 0) {
|
||||
data >>= 1;
|
||||
shiftCount++;
|
||||
}
|
||||
|
||||
return shiftCount;
|
||||
}
|
||||
|
||||
private static byte ToASCII(char character) {
|
||||
byte value = 0;
|
||||
|
||||
try {
|
||||
value = Convert.ToByte(character);
|
||||
}
|
||||
|
||||
catch (OverflowException) {
|
||||
throw new Exception("Cannot convert to ASCII: " + character);
|
||||
}
|
||||
|
||||
if (value > 127)
|
||||
throw new Exception("Cannot convert to ASCII: " + character);
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace NetStack.Threading {
|
||||
[StructLayout(LayoutKind.Explicit, Size = 192)]
|
||||
public sealed class ArrayQueue {
|
||||
[FieldOffset(0)]
|
||||
private readonly Entry[] _array;
|
||||
[FieldOffset(8)]
|
||||
private readonly int _arrayMask;
|
||||
[FieldOffset(64)]
|
||||
private int _enqueuePosition;
|
||||
[FieldOffset(128)]
|
||||
private int _dequeuePosition;
|
||||
|
||||
public int Count {
|
||||
get {
|
||||
return _enqueuePosition - _dequeuePosition;
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayQueue(int capacity) {
|
||||
if (capacity < 2)
|
||||
throw new ArgumentException("Queue size should be greater than or equal to two");
|
||||
|
||||
if ((capacity & (capacity - 1)) != 0)
|
||||
throw new ArgumentException("Queue size should be a power of two");
|
||||
|
||||
_arrayMask = capacity - 1;
|
||||
_array = new Entry[capacity];
|
||||
_enqueuePosition = 0;
|
||||
_dequeuePosition = 0;
|
||||
}
|
||||
|
||||
public void Enqueue(object item) {
|
||||
while (true) {
|
||||
if (TryEnqueue(item))
|
||||
break;
|
||||
|
||||
Thread.SpinWait(1);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryEnqueue(object item) {
|
||||
var array = _array;
|
||||
var position = _enqueuePosition;
|
||||
var index = position & _arrayMask;
|
||||
|
||||
if (array[index].IsSet != 0)
|
||||
return false;
|
||||
|
||||
array[index].element = item;
|
||||
array[index].IsSet = 1;
|
||||
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
Volatile.Write(ref _enqueuePosition, position + 1);
|
||||
#else
|
||||
Thread.MemoryBarrier();
|
||||
_enqueuePosition = position + 1;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public object Dequeue() {
|
||||
while (true) {
|
||||
object element;
|
||||
|
||||
if (TryDequeue(out element))
|
||||
return element;
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryDequeue(out object result) {
|
||||
var array = _array;
|
||||
var position = _dequeuePosition;
|
||||
var index = position & _arrayMask;
|
||||
|
||||
if (array[index].IsSet == 0) {
|
||||
result = default(object);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
result = array[index].element;
|
||||
array[index].element = default(object);
|
||||
array[index].IsSet = 0;
|
||||
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
Volatile.Write(ref _dequeuePosition, position + 1);
|
||||
#else
|
||||
Thread.MemoryBarrier();
|
||||
_dequeuePosition = position + 1;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16)]
|
||||
private struct Entry {
|
||||
[FieldOffset(0)]
|
||||
private int isSet;
|
||||
[FieldOffset(8)]
|
||||
internal object element;
|
||||
|
||||
internal int IsSet {
|
||||
get {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
return Volatile.Read(ref isSet);
|
||||
#else
|
||||
Thread.MemoryBarrier();
|
||||
return isSet;
|
||||
#endif
|
||||
}
|
||||
|
||||
set {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
Volatile.Write(ref isSet, value);
|
||||
#else
|
||||
Thread.MemoryBarrier();
|
||||
isSet = value;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Alexander Nikitin, Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace NetStack.Threading {
|
||||
[StructLayout(LayoutKind.Explicit, Size = 192)]
|
||||
public sealed class ConcurrentBuffer {
|
||||
[FieldOffset(0)]
|
||||
private readonly Cell[] _buffer;
|
||||
[FieldOffset(8)]
|
||||
private readonly int _bufferMask;
|
||||
[FieldOffset(64)]
|
||||
private int _enqueuePosition;
|
||||
[FieldOffset(128)]
|
||||
private int _dequeuePosition;
|
||||
|
||||
public int Count {
|
||||
get {
|
||||
return _enqueuePosition - _dequeuePosition;
|
||||
}
|
||||
}
|
||||
|
||||
public ConcurrentBuffer(int bufferSize) {
|
||||
if (bufferSize < 2)
|
||||
throw new ArgumentException("Buffer size should be greater than or equal to two");
|
||||
|
||||
if ((bufferSize & (bufferSize - 1)) != 0)
|
||||
throw new ArgumentException("Buffer size should be a power of two");
|
||||
|
||||
_bufferMask = bufferSize - 1;
|
||||
_buffer = new Cell[bufferSize];
|
||||
|
||||
for (var i = 0; i < bufferSize; i++) {
|
||||
_buffer[i] = new Cell(i, null);
|
||||
}
|
||||
|
||||
_enqueuePosition = 0;
|
||||
_dequeuePosition = 0;
|
||||
}
|
||||
|
||||
public void Enqueue(object item) {
|
||||
while (true) {
|
||||
if (TryEnqueue(item))
|
||||
break;
|
||||
|
||||
Thread.SpinWait(1);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryEnqueue(object item) {
|
||||
do {
|
||||
var buffer = _buffer;
|
||||
var position = _enqueuePosition;
|
||||
var index = position & _bufferMask;
|
||||
var cell = buffer[index];
|
||||
|
||||
if (cell.Sequence == position && Interlocked.CompareExchange(ref _enqueuePosition, position + 1, position) == position) {
|
||||
buffer[index].Element = item;
|
||||
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
Volatile.Write(ref buffer[index].Sequence, position + 1);
|
||||
#else
|
||||
Thread.MemoryBarrier();
|
||||
buffer[index].Sequence = position + 1;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (cell.Sequence < position)
|
||||
return false;
|
||||
}
|
||||
|
||||
while (true);
|
||||
}
|
||||
|
||||
public object Dequeue() {
|
||||
while (true) {
|
||||
object element;
|
||||
|
||||
if (TryDequeue(out element))
|
||||
return element;
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryDequeue(out object result) {
|
||||
do {
|
||||
var buffer = _buffer;
|
||||
var bufferMask = _bufferMask;
|
||||
var position = _dequeuePosition;
|
||||
var index = position & bufferMask;
|
||||
var cell = buffer[index];
|
||||
|
||||
if (cell.Sequence == position + 1 && Interlocked.CompareExchange(ref _dequeuePosition, position + 1, position) == position) {
|
||||
result = cell.Element;
|
||||
buffer[index].Element = null;
|
||||
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
Volatile.Write(ref buffer[index].Sequence, position + bufferMask + 1);
|
||||
#else
|
||||
Thread.MemoryBarrier();
|
||||
buffer[index].Sequence = position + bufferMask + 1;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (cell.Sequence < position + 1) {
|
||||
result = default(object);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
while (true);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16)]
|
||||
private struct Cell {
|
||||
[FieldOffset(0)]
|
||||
public int Sequence;
|
||||
[FieldOffset(8)]
|
||||
public object Element;
|
||||
|
||||
public Cell(int sequence, object element) {
|
||||
Sequence = sequence;
|
||||
Element = element;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Virgile Bello, Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace NetStack.Threading {
|
||||
public sealed class ConcurrentPool<T> where T : class {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
private SpinLock _lock;
|
||||
#else
|
||||
private object _lock;
|
||||
#endif
|
||||
private readonly Func<T> _factory;
|
||||
private Segment _head;
|
||||
private Segment _tail;
|
||||
|
||||
public ConcurrentPool(int capacity, Func<T> factory) {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
_lock = new SpinLock();
|
||||
#else
|
||||
_lock = new Object();
|
||||
#endif
|
||||
_head = _tail = new Segment(capacity);
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
public T Acquire() {
|
||||
while (true) {
|
||||
var localHead = _head;
|
||||
var count = localHead.Count;
|
||||
|
||||
if (count == 0) {
|
||||
if (localHead.Next != null) {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
bool lockTaken = false;
|
||||
|
||||
try {
|
||||
_lock.Enter(ref lockTaken);
|
||||
|
||||
if (_head.Next != null && _head.Count == 0)
|
||||
_head = _head.Next;
|
||||
}
|
||||
|
||||
finally {
|
||||
if (lockTaken)
|
||||
_lock.Exit(false);
|
||||
}
|
||||
#else
|
||||
try {
|
||||
Monitor.Enter(_lock);
|
||||
|
||||
if (_head.Next != null && _head.Count == 0)
|
||||
_head = _head.Next;
|
||||
}
|
||||
|
||||
finally {
|
||||
Monitor.Exit(_lock);
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
return _factory();
|
||||
}
|
||||
} else if (Interlocked.CompareExchange(ref localHead.Count, count - 1, count) == count) {
|
||||
var localLow = Interlocked.Increment(ref localHead.Low) - 1;
|
||||
var index = localLow & localHead.Mask;
|
||||
T item;
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
var spinWait = new SpinWait();
|
||||
#endif
|
||||
|
||||
while ((item = Interlocked.Exchange(ref localHead.Items[index], null)) == null) {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
spinWait.SpinOnce();
|
||||
#else
|
||||
Thread.SpinWait(1);
|
||||
#endif
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Release(T item) {
|
||||
while (true) {
|
||||
var localTail = _tail;
|
||||
var count = localTail.Count;
|
||||
|
||||
if (count == localTail.Items.Length) {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
bool lockTaken = false;
|
||||
|
||||
try {
|
||||
_lock.Enter(ref lockTaken);
|
||||
|
||||
if (_tail.Next == null && count == _tail.Items.Length)
|
||||
_tail = _tail.Next = new Segment(_tail.Items.Length << 1);
|
||||
}
|
||||
|
||||
finally {
|
||||
if (lockTaken)
|
||||
_lock.Exit(false);
|
||||
}
|
||||
#else
|
||||
try {
|
||||
Monitor.Enter(_lock);
|
||||
|
||||
if (_tail.Next == null && count == _tail.Items.Length)
|
||||
_tail = _tail.Next = new Segment(_tail.Items.Length << 1);
|
||||
}
|
||||
|
||||
finally {
|
||||
Monitor.Exit(_lock);
|
||||
}
|
||||
#endif
|
||||
} else if (Interlocked.CompareExchange(ref localTail.Count, count + 1, count) == count) {
|
||||
var localHigh = Interlocked.Increment(ref localTail.High) - 1;
|
||||
var index = localHigh & localTail.Mask;
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
var spinWait = new SpinWait();
|
||||
#endif
|
||||
|
||||
while (Interlocked.CompareExchange(ref localTail.Items[index], item, null) != null) {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
spinWait.SpinOnce();
|
||||
#else
|
||||
Thread.SpinWait(1);
|
||||
#endif
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class Segment {
|
||||
public readonly T[] Items;
|
||||
public readonly int Mask;
|
||||
public int Low;
|
||||
public int High;
|
||||
public int Count;
|
||||
public Segment Next;
|
||||
|
||||
public Segment(int size) {
|
||||
if (size <= 0 || ((size & (size - 1)) != 0))
|
||||
throw new ArgumentOutOfRangeException("Segment size must be power of two");
|
||||
|
||||
Items = new T[size];
|
||||
Mask = size - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
-63
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace NetStack.Unsafe {
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
public static class Memory {
|
||||
[MethodImpl(256)]
|
||||
public static unsafe void Copy(IntPtr source, int sourceOffset, byte[] destination, int destinationOffset, int length) {
|
||||
if (length > 0) {
|
||||
fixed (byte* destinationPointer = &destination[destinationOffset]) {
|
||||
byte* sourcePointer = (byte*)source + sourceOffset;
|
||||
|
||||
Buffer.MemoryCopy(sourcePointer, destinationPointer, length, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public static unsafe void Copy(byte[] source, int sourceOffset, IntPtr destination, int destinationOffset, int length) {
|
||||
if (length > 0) {
|
||||
fixed (byte* sourcePointer = &source[sourceOffset]) {
|
||||
byte* destinationPointer = (byte*)destination + destinationOffset;
|
||||
|
||||
Buffer.MemoryCopy(sourcePointer, destinationPointer, length, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public static unsafe void Copy(byte[] source, int sourceOffset, byte[] destination, int destinationOffset, int length) {
|
||||
if (length > 0) {
|
||||
fixed (byte* sourcePointer = &source[sourceOffset]) {
|
||||
fixed (byte* destinationPointer = &destination[destinationOffset]) {
|
||||
Buffer.MemoryCopy(sourcePointer, destinationPointer, length, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
using NetStack.Serialization;
|
||||
|
||||
namespace Ragon.Common
|
||||
{
|
||||
public interface IRagonSerializable
|
||||
{
|
||||
public void Serialize(BitBuffer buffer);
|
||||
public void Deserialize(BitBuffer buffer);
|
||||
}
|
||||
}
|
||||
@@ -1,200 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
|
||||
namespace Ragon.Common
|
||||
{
|
||||
|
||||
public class RagonSerializer
|
||||
{
|
||||
private byte[] _data;
|
||||
private int _offset;
|
||||
private int _size;
|
||||
public int Lenght => _offset;
|
||||
public int Size => _size - _offset;
|
||||
|
||||
public RagonSerializer(int capacity = 256)
|
||||
{
|
||||
_data = new byte[capacity];
|
||||
_offset = 0;
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteByte(byte value)
|
||||
{
|
||||
ResizeIfNeed(1);
|
||||
|
||||
_data[_offset] = value;
|
||||
_offset += 1;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public byte ReadByte()
|
||||
{
|
||||
var value = _data[_offset];
|
||||
_offset += 1;
|
||||
return value;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteInt(int value)
|
||||
{
|
||||
ResizeIfNeed(4);
|
||||
|
||||
_data[_offset] = (byte) (value & 0x00FF);
|
||||
_data[_offset + 1] = (byte) ((value & 0xFF00) >> 8);
|
||||
_data[_offset + 2] = (byte) ((value & 0xFF00) >> 16);
|
||||
_data[_offset + 3] = (byte) ((value & 0xFF00) >> 24);
|
||||
_offset += 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int ReadInt()
|
||||
{
|
||||
var value = _data[_offset] + (_data[_offset + 1] << 8) + (_data[_offset + 2] << 16) + (_data[_offset + 3] << 24);
|
||||
_offset += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
[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)]
|
||||
public string ReadString()
|
||||
{
|
||||
var lenght = ReadUShort();
|
||||
var stringRaw = _data.AsSpan().Slice(_offset, lenght);
|
||||
var str = Encoding.UTF8.GetString(stringRaw);
|
||||
_offset += lenght;
|
||||
return str;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ReadOnlySpan<byte> ReadData(int lenght)
|
||||
{
|
||||
var data = _data.AsSpan();
|
||||
var payloadData = data.Slice(_offset, lenght);
|
||||
|
||||
_offset += payloadData.Length;
|
||||
return payloadData;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteData(ref ReadOnlySpan<byte> payload)
|
||||
{
|
||||
ResizeIfNeed(payload.Length);
|
||||
|
||||
var data = _data.AsSpan();
|
||||
var payloadData = data.Slice(_offset, payload.Length);
|
||||
|
||||
payload.CopyTo(payloadData);
|
||||
_offset += payload.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Span<byte> GetWritableData(int lenght)
|
||||
{
|
||||
ResizeIfNeed(lenght);
|
||||
|
||||
var data = _data.AsSpan();
|
||||
var payloadData = data.Slice(_offset, lenght);
|
||||
|
||||
_offset += lenght;
|
||||
return payloadData;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteOperation(RagonOperation ragonOperation)
|
||||
{
|
||||
ResizeIfNeed(1);
|
||||
|
||||
_data[_offset] = (byte) ragonOperation;
|
||||
_offset += 1;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public RagonOperation ReadOperation()
|
||||
{
|
||||
var op = (RagonOperation) _data[_offset];
|
||||
_offset += 1;
|
||||
return op;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteUShort(ushort value)
|
||||
{
|
||||
ResizeIfNeed(2);
|
||||
|
||||
_data[_offset] = (byte) (value & 0x00FF);
|
||||
_data[_offset + 1] = (byte) ((value & 0xFF00) >> 8);
|
||||
_offset += 2;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ushort ReadUShort()
|
||||
{
|
||||
var value = (ushort) (_data[_offset] + (_data[_offset + 1] << 8));
|
||||
_offset += 2;
|
||||
return value;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_offset = 0;
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
public void ToSpan(ref Span<byte> data)
|
||||
{
|
||||
var span = _data.AsSpan();
|
||||
var dataSpan = span.Slice(0, _offset);
|
||||
dataSpan.CopyTo(data);
|
||||
}
|
||||
|
||||
public void FromSpan(ref ReadOnlySpan<byte> data)
|
||||
{
|
||||
Clear();
|
||||
ResizeIfNeed(data.Length);
|
||||
var dataSpan = _data.AsSpan();
|
||||
data.CopyTo(dataSpan);
|
||||
_size = data.Length;
|
||||
}
|
||||
|
||||
public void FromArray(byte[] data)
|
||||
{
|
||||
Clear();
|
||||
ResizeIfNeed(data.Length);
|
||||
Buffer.BlockCopy(data, 0, _data, 0, _offset);
|
||||
_size = data.Length;
|
||||
}
|
||||
|
||||
|
||||
public byte[] ToArray()
|
||||
{
|
||||
var bytes = new byte[_offset];
|
||||
Buffer.BlockCopy(_data, 0, bytes, 0, _offset);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private void ResizeIfNeed(int lenght)
|
||||
{
|
||||
if (_offset + lenght < _data.Length)
|
||||
return;
|
||||
|
||||
var newData = new byte[_data.Length * 4 + lenght];
|
||||
Buffer.BlockCopy(_data, 0, newData, 0, _data.Length);
|
||||
_data = newData;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Ragon.Core.Time;
|
||||
|
||||
public interface IAction
|
||||
{
|
||||
public void Tick();
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace Ragon.Core.Time;
|
||||
|
||||
public class Loop
|
||||
{
|
||||
private List<IAction> _tasks;
|
||||
|
||||
public Loop()
|
||||
{
|
||||
|
||||
_tasks = new List<IAction>(35);
|
||||
}
|
||||
|
||||
public void Run(IAction task)
|
||||
{
|
||||
_tasks.Add(task);
|
||||
}
|
||||
|
||||
public void Stop(IAction task)
|
||||
{
|
||||
_tasks.Remove(task);
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
foreach (var task in _tasks)
|
||||
task.Tick();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
using System.Diagnostics;
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Lobby;
|
||||
using Ragon.Core.Server;
|
||||
using Ragon.Core.Time;
|
||||
using Ragon.Server;
|
||||
using Ragon.Server.ENet;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public class Application : INetworkListener
|
||||
{
|
||||
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private readonly INetworkServer _server;
|
||||
private readonly Thread _dedicatedThread;
|
||||
private readonly Executor _executor;
|
||||
private readonly Configuration _configuration;
|
||||
private readonly HandlerRegistry _handlerRegistry;
|
||||
private readonly ILobby _lobby;
|
||||
private readonly Loop _loop;
|
||||
private readonly Dictionary<ushort, PlayerContext> _contexts;
|
||||
|
||||
public Application(Configuration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_executor = new Executor();
|
||||
_dedicatedThread = new Thread(Execute);
|
||||
_dedicatedThread.IsBackground = true;
|
||||
_contexts = new Dictionary<ushort, PlayerContext>();
|
||||
_handlerRegistry = new HandlerRegistry();
|
||||
_lobby = new LobbyInMemory();
|
||||
_loop = new Loop();
|
||||
|
||||
if (configuration.ServerType == "enet")
|
||||
_server = new ENetServer();
|
||||
|
||||
if (configuration.ServerType == "websocket")
|
||||
_server = new NativeWebSocketServer(_executor);
|
||||
|
||||
Debug.Assert(_server != null, $"Socket type not supported: {configuration.ServerType}. Supported: [enet, websocket]");
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
_executor.Execute();
|
||||
_loop.Tick();
|
||||
_server.Poll();
|
||||
|
||||
Thread.Sleep((int)1000.0f / _configuration.ServerTickRate);
|
||||
}
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
var networkConfiguration = new NetworkConfiguration()
|
||||
{
|
||||
LimitConnections = _configuration.LimitConnections,
|
||||
Protocol = RagonVersion.Parse(_configuration.GameProtocol),
|
||||
Address = "0.0.0.0",
|
||||
Port = _configuration.Port,
|
||||
};
|
||||
|
||||
_server.Start(this, networkConfiguration);
|
||||
_dedicatedThread.Start();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_server.Stop();
|
||||
_dedicatedThread.Interrupt();
|
||||
}
|
||||
|
||||
public void OnConnected(INetworkConnection connection)
|
||||
{
|
||||
var context = new PlayerContext(connection, new LobbyPlayer(connection));
|
||||
context.Lobby = _lobby;
|
||||
context.Loop = _loop;
|
||||
|
||||
_logger.Trace($"Connected {connection.Id}");
|
||||
_contexts.Add(connection.Id, context);
|
||||
}
|
||||
|
||||
public void OnDisconnected(INetworkConnection connection)
|
||||
{
|
||||
_logger.Trace($"Disconnected {connection.Id}");
|
||||
|
||||
if (_contexts.Remove(connection.Id, out var context))
|
||||
{
|
||||
var room = context.Room;
|
||||
if (room != null)
|
||||
{
|
||||
room.RemovePlayer(context.RoomPlayer);
|
||||
|
||||
_lobby.RemoveIfEmpty(room);
|
||||
}
|
||||
|
||||
context.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnTimeout(INetworkConnection connection)
|
||||
{
|
||||
if (_contexts.Remove(connection.Id, out var context))
|
||||
context.Dispose();
|
||||
}
|
||||
|
||||
public void OnData(INetworkConnection connection, byte[] data)
|
||||
{
|
||||
if (_contexts.TryGetValue(connection.Id, out var context))
|
||||
_handlerRegistry.Handle(context, data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
[Serializable]
|
||||
public struct Configuration
|
||||
{
|
||||
public string ServerKey;
|
||||
public string ServerType;
|
||||
public ushort ServerTickRate;
|
||||
public string GameProtocol;
|
||||
public ushort Port;
|
||||
public int LimitConnections;
|
||||
public int LimitPlayersPerRoom;
|
||||
public int LimitRooms;
|
||||
|
||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||
private static readonly string ServerVersion = "1.0.31-rc";
|
||||
|
||||
private static void CopyrightInfo()
|
||||
{
|
||||
Logger.Info($"Server Version: {ServerVersion}");
|
||||
Logger.Info($"Machine Name: {Environment.MachineName}");
|
||||
Logger.Info($"OS: {Environment.OSVersion}");
|
||||
Logger.Info($"Processors: {Environment.ProcessorCount}");
|
||||
Logger.Info($"Runtime Version: {Environment.Version}");
|
||||
Logger.Info("==================================");
|
||||
Logger.Info("| |");
|
||||
Logger.Info("| Ragon |");
|
||||
Logger.Info("| |");
|
||||
Logger.Info("==================================");
|
||||
}
|
||||
|
||||
public static Configuration Load(string filePath)
|
||||
{
|
||||
CopyrightInfo();
|
||||
|
||||
var data = File.ReadAllText(filePath);
|
||||
var configuration = JsonConvert.DeserializeObject<Configuration>(data);
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class Entity
|
||||
{
|
||||
private static ushort _idGenerator = 0;
|
||||
public ushort Id { get; private set; }
|
||||
public ushort Type { get; private set; }
|
||||
public ushort StaticId { get; private set; }
|
||||
public RoomPlayer Owner { get; private set; }
|
||||
public RagonAuthority Authority { get; private set; }
|
||||
public EntityState State { get; private set; }
|
||||
public byte[] Payload { get; private set; }
|
||||
|
||||
private readonly List<EntityEvent> _bufferedEvents;
|
||||
|
||||
public Entity(RoomPlayer owner, ushort type, ushort staticId, RagonAuthority eventAuthority)
|
||||
{
|
||||
Owner = owner;
|
||||
StaticId = staticId;
|
||||
Type = type;
|
||||
Id = _idGenerator++;
|
||||
Payload = Array.Empty<byte>();
|
||||
Authority = eventAuthority;
|
||||
State = new EntityState(this);
|
||||
|
||||
_bufferedEvents = new List<EntityEvent>();
|
||||
}
|
||||
|
||||
public void SetPayload(byte[] payload)
|
||||
{
|
||||
Payload = payload;
|
||||
}
|
||||
|
||||
public void SetOwner(RoomPlayer owner)
|
||||
{
|
||||
Owner = owner;
|
||||
}
|
||||
|
||||
public void RestoreBufferedEvents(RoomPlayer roomPlayer, RagonSerializer writer)
|
||||
{
|
||||
foreach (var bufferedEvent in _bufferedEvents)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
writer.WriteUShort(bufferedEvent.EventId);
|
||||
writer.WriteUShort(bufferedEvent.Invoker.Connection.Id);
|
||||
writer.WriteByte((byte)RagonReplicationMode.Server);
|
||||
writer.WriteUShort(Id);
|
||||
|
||||
ReadOnlySpan<byte> data = bufferedEvent.EventData.AsSpan();
|
||||
writer.WriteData(ref data);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
roomPlayer.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
}
|
||||
|
||||
public void Create()
|
||||
{
|
||||
var room = Owner.Room;
|
||||
var serializer = room.Writer;
|
||||
|
||||
serializer.Clear();
|
||||
serializer.WriteOperation(RagonOperation.CREATE_ENTITY);
|
||||
serializer.WriteUShort(Type);
|
||||
serializer.WriteUShort(Id);
|
||||
serializer.WriteUShort(Owner.Connection.Id);
|
||||
|
||||
ReadOnlySpan<byte> entityPayload = Payload.AsSpan();
|
||||
serializer.WriteUShort((ushort)entityPayload.Length);
|
||||
serializer.WriteData(ref entityPayload);
|
||||
|
||||
var sendData = serializer.ToArray();
|
||||
foreach (var player in room.ReadyPlayersList)
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void Destroy(byte[] payload)
|
||||
{
|
||||
var room = Owner.Room;
|
||||
var serializer = room.Writer;
|
||||
|
||||
serializer.Clear();
|
||||
serializer.WriteOperation(RagonOperation.DESTROY_ENTITY);
|
||||
serializer.WriteInt(Id);
|
||||
serializer.WriteUShort(0);
|
||||
// serializer.WriteData(ref Payload);
|
||||
|
||||
var sendData = serializer.ToArray();
|
||||
foreach (var player in room.ReadyPlayersList)
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void ReplicateEvent(
|
||||
RoomPlayer caller,
|
||||
ushort eventId,
|
||||
ReadOnlySpan<byte> payload,
|
||||
RagonReplicationMode eventMode,
|
||||
RoomPlayer targetPlayer
|
||||
)
|
||||
{
|
||||
var room = Owner.Room;
|
||||
var serializer = room.Writer;
|
||||
|
||||
serializer.Clear();
|
||||
serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
serializer.WriteUShort(eventId);
|
||||
serializer.WriteUShort(caller.Connection.Id);
|
||||
serializer.WriteByte((byte)eventMode);
|
||||
serializer.WriteUShort(Id);
|
||||
serializer.WriteData(ref payload);
|
||||
|
||||
var sendData = serializer.ToArray();
|
||||
targetPlayer.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void ReplicateEvent(
|
||||
RoomPlayer caller,
|
||||
ushort eventId,
|
||||
ReadOnlySpan<byte> payload,
|
||||
RagonReplicationMode eventMode,
|
||||
RagonTarget targetMode
|
||||
)
|
||||
{
|
||||
if (Authority == RagonAuthority.OwnerOnly &&
|
||||
Owner.Connection.Id != caller.Connection.Id)
|
||||
{
|
||||
Console.WriteLine($"Player have not enought authority for event with Id {eventId}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner)
|
||||
{
|
||||
var bufferedEvent = new EntityEvent(caller, eventId, payload.ToArray(), targetMode);
|
||||
_bufferedEvents.Add(bufferedEvent);
|
||||
}
|
||||
|
||||
var room = Owner.Room;
|
||||
var serializer = room.Writer;
|
||||
|
||||
serializer.Clear();
|
||||
serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
serializer.WriteUShort(eventId);
|
||||
serializer.WriteUShort(caller.Connection.Id);
|
||||
serializer.WriteByte((byte)eventMode);
|
||||
serializer.WriteUShort(Id);
|
||||
serializer.WriteData(ref payload);
|
||||
|
||||
var sendData = serializer.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 != caller.Connection.Id)
|
||||
roomPlayer.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonTarget.All:
|
||||
{
|
||||
foreach (var roomPlayer in room.ReadyPlayersList)
|
||||
roomPlayer.Connection.Reliable.Send(sendData);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityEvent
|
||||
{
|
||||
public RoomPlayer Invoker { get; private set; }
|
||||
public ushort EventId { get; private set; }
|
||||
public byte[] EventData { get; private set; }
|
||||
public RagonTarget Target { set; private get; }
|
||||
|
||||
public EntityEvent(
|
||||
RoomPlayer invoker,
|
||||
ushort eventId,
|
||||
byte[] payload,
|
||||
RagonTarget target
|
||||
)
|
||||
{
|
||||
Invoker = invoker;
|
||||
EventId = eventId;
|
||||
EventData = payload;
|
||||
Target = target;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityList
|
||||
{
|
||||
private readonly List<Entity> _dynamicEntitiesList = new List<Entity>();
|
||||
private readonly List<Entity> _staticEntitiesList = new List<Entity>();
|
||||
private readonly Dictionary<ushort, Entity> _entitiesMap = new Dictionary<ushort, Entity>();
|
||||
|
||||
public IReadOnlyList<Entity> StaticList => _staticEntitiesList;
|
||||
public IReadOnlyList<Entity> DynamicList => _dynamicEntitiesList;
|
||||
public IReadOnlyDictionary<ushort, Entity> Map => _entitiesMap;
|
||||
|
||||
public void Add(Entity entity)
|
||||
{
|
||||
if (entity.StaticId != 0)
|
||||
_staticEntitiesList.Add(entity);
|
||||
else
|
||||
_dynamicEntitiesList.Add(entity);
|
||||
|
||||
_entitiesMap.Add(entity.Id, entity);
|
||||
}
|
||||
|
||||
public bool Remove(Entity entity)
|
||||
{
|
||||
if (_entitiesMap.Remove(entity.Id, out var existEntity))
|
||||
{
|
||||
_staticEntitiesList.Remove(entity);
|
||||
_dynamicEntitiesList.Remove(entity);
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityState
|
||||
{
|
||||
private List<EntityStateProperty> _properties;
|
||||
private Entity _entity;
|
||||
|
||||
public EntityState(Entity entity, int capacity = 10)
|
||||
{
|
||||
_entity = entity;
|
||||
_properties = new List<EntityStateProperty>(10);
|
||||
}
|
||||
|
||||
public void AddProperty(EntityStateProperty property)
|
||||
{
|
||||
_properties.Add(property);
|
||||
}
|
||||
|
||||
public void Write(RagonSerializer serializer)
|
||||
{
|
||||
serializer.WriteUShort(_entity.Id);
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < _properties.Count; propertyIndex++)
|
||||
{
|
||||
var property = _properties[propertyIndex];
|
||||
if (property.IsDirty)
|
||||
{
|
||||
serializer.WriteBool(true);
|
||||
var span = serializer.GetWritableData(property.Size);
|
||||
var data = property.Read();
|
||||
data.CopyTo(span);
|
||||
property.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
serializer.WriteBool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Read(RagonSerializer serializer)
|
||||
{
|
||||
for (var i = 0; i < _properties.Count; i++)
|
||||
{
|
||||
if (serializer.ReadBool())
|
||||
{
|
||||
var property = _properties[i];
|
||||
var size = property.Size;
|
||||
if (!property.IsFixed)
|
||||
size = serializer.ReadUShort();
|
||||
|
||||
if (size > property.Capacity)
|
||||
{
|
||||
Console.WriteLine($"Property {i} payload too large, size: {size}");
|
||||
continue;
|
||||
}
|
||||
|
||||
var propertyPayload = serializer.ReadData(size);
|
||||
property.Write(ref propertyPayload);
|
||||
property.Size = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Snapshot(RagonSerializer serializer)
|
||||
{
|
||||
ReadOnlySpan<byte> payload = _entity.Payload.AsSpan();
|
||||
|
||||
serializer.WriteUShort(_entity.Type);
|
||||
serializer.WriteUShort(_entity.Id);
|
||||
|
||||
if (_entity.StaticId != 0)
|
||||
serializer.WriteUShort(_entity.StaticId);
|
||||
|
||||
serializer.WriteUShort(_entity.Owner.Connection.Id);
|
||||
serializer.WriteUShort((ushort) payload.Length);
|
||||
serializer.WriteData(ref payload);
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < _properties.Count; propertyIndex++)
|
||||
{
|
||||
var property = _properties[propertyIndex];
|
||||
var hasPayload = property.IsFixed || property.Size > 0 && !property.IsFixed;
|
||||
if (hasPayload)
|
||||
{
|
||||
serializer.WriteBool(true);
|
||||
var span = serializer.GetWritableData(property.Size);
|
||||
var data = property.Read();
|
||||
data.CopyTo(span);
|
||||
}
|
||||
else
|
||||
{
|
||||
serializer.WriteBool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityStateProperty
|
||||
{
|
||||
public int Size { get; set; }
|
||||
public int Capacity { get; set; }
|
||||
public bool IsDirty { get; private set; }
|
||||
public bool IsFixed { get; private set; }
|
||||
private byte[] _data;
|
||||
|
||||
public EntityStateProperty(int size, bool isFixed)
|
||||
{
|
||||
Capacity = 512;
|
||||
Size = size;
|
||||
IsFixed = isFixed;
|
||||
IsDirty = true;
|
||||
|
||||
_data = new byte[Capacity];
|
||||
}
|
||||
|
||||
public ReadOnlySpan<byte> Read()
|
||||
{
|
||||
var dataSpan = _data.AsSpan();
|
||||
var src = dataSpan.Slice(0, Size);
|
||||
return src;
|
||||
}
|
||||
|
||||
public void Write(ref ReadOnlySpan<byte> src)
|
||||
{
|
||||
src.CopyTo(_data);
|
||||
IsDirty = true;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
IsDirty = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Time;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class Room: IAction
|
||||
{
|
||||
public string Id { get; private set; }
|
||||
public RoomInformation Info { get; private set; }
|
||||
public RoomPlayer Owner { get; private set; }
|
||||
public RagonSerializer Writer { get; }
|
||||
public Dictionary<ushort, RoomPlayer> Players { get; private set; }
|
||||
public List<RoomPlayer> WaitPlayersList { get; private set; }
|
||||
public List<RoomPlayer> ReadyPlayersList { get; private set; }
|
||||
public List<RoomPlayer> PlayerList { get; private set; }
|
||||
|
||||
public Dictionary<ushort, Entity> Entities { get; private set; }
|
||||
public List<Entity> DynamicEntitiesList { get; private set; }
|
||||
public List<Entity> StaticEntitiesList { get; private set; }
|
||||
public List<Entity> EntityList { get; private set; }
|
||||
|
||||
private readonly HashSet<Entity> _entitiesDirtySet;
|
||||
|
||||
public Room(string roomId, RoomInformation info)
|
||||
{
|
||||
Id = roomId;
|
||||
Info = info;
|
||||
|
||||
Players = new Dictionary<ushort, RoomPlayer>(info.Max);
|
||||
WaitPlayersList = new List<RoomPlayer>(info.Max);
|
||||
ReadyPlayersList = new List<RoomPlayer>(info.Max);
|
||||
PlayerList = new List<RoomPlayer>(info.Max);
|
||||
|
||||
Entities = new Dictionary<ushort, Entity>();
|
||||
DynamicEntitiesList = new List<Entity>();
|
||||
StaticEntitiesList = new List<Entity>();
|
||||
EntityList = new List<Entity>();
|
||||
|
||||
_entitiesDirtySet = new HashSet<Entity>();
|
||||
Writer = new RagonSerializer(512);
|
||||
}
|
||||
|
||||
public void AttachEntity(RoomPlayer newOwner, Entity entity)
|
||||
{
|
||||
Entities.Add(entity.Id, entity);
|
||||
EntityList.Add(entity);
|
||||
|
||||
if (entity.StaticId == 0)
|
||||
DynamicEntitiesList.Add(entity);
|
||||
else
|
||||
StaticEntitiesList.Add(entity);
|
||||
|
||||
entity.Create();
|
||||
|
||||
newOwner.Entities.Add(entity);
|
||||
}
|
||||
|
||||
public void DetachEntity(RoomPlayer currentOwner, Entity entity, byte[] payload)
|
||||
{
|
||||
Entities.Remove(entity.Id);
|
||||
EntityList.Remove(entity);
|
||||
StaticEntitiesList.Remove(entity);
|
||||
DynamicEntitiesList.Remove(entity);
|
||||
_entitiesDirtySet.Remove(entity);
|
||||
|
||||
entity.Destroy(payload);
|
||||
currentOwner.Entities.Remove(entity);
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
var entities = (ushort) _entitiesDirtySet.Count;
|
||||
if (entities > 0)
|
||||
{
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
Writer.WriteUShort(entities);
|
||||
|
||||
foreach (var entity in _entitiesDirtySet)
|
||||
entity.State.Write(Writer);
|
||||
|
||||
_entitiesDirtySet.Clear();
|
||||
|
||||
var sendData = Writer.ToArray();
|
||||
foreach (var roomPlayer in ReadyPlayersList)
|
||||
roomPlayer.Connection.Unreliable.Send(sendData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddPlayer(RoomPlayer player)
|
||||
{
|
||||
if (Players.Count == 0)
|
||||
Owner = player;
|
||||
|
||||
player.Attach(this);
|
||||
|
||||
PlayerList.Add(player);
|
||||
Players.Add(player.Connection.Id, player);
|
||||
}
|
||||
|
||||
public void RemovePlayer(RoomPlayer roomPlayer)
|
||||
{
|
||||
if (Players.Remove(roomPlayer.Connection.Id, out var player))
|
||||
{
|
||||
PlayerList.Remove(player);
|
||||
|
||||
{
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.PLAYER_LEAVED);
|
||||
Writer.WriteString(player.Id);
|
||||
|
||||
var entitiesToDelete = player.Entities.DynamicList;
|
||||
Writer.WriteUShort((ushort) entitiesToDelete.Count);
|
||||
foreach (var entity in entitiesToDelete)
|
||||
{
|
||||
Writer.WriteUShort(entity.Id);
|
||||
EntityList.Remove(entity);
|
||||
}
|
||||
|
||||
var sendData = Writer.ToArray();
|
||||
Broadcast(sendData);
|
||||
}
|
||||
|
||||
if (roomPlayer.Connection.Id == Owner.Connection.Id && PlayerList.Count > 0)
|
||||
{
|
||||
var nextOwner = PlayerList[0];
|
||||
|
||||
Owner = nextOwner;
|
||||
|
||||
var entitiesToUpdate = roomPlayer.Entities.StaticList;
|
||||
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.OWNERSHIP_CHANGED);
|
||||
Writer.WriteString(Owner.Id);
|
||||
Writer.WriteUShort((ushort) entitiesToUpdate.Count);
|
||||
|
||||
foreach (var entity in entitiesToUpdate)
|
||||
{
|
||||
Writer.WriteUShort(entity.Id);
|
||||
|
||||
entity.SetOwner(nextOwner);
|
||||
nextOwner.Entities.Add(entity);
|
||||
}
|
||||
|
||||
var sendData = Writer.ToArray();
|
||||
Broadcast(sendData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateReadyPlayerList()
|
||||
{
|
||||
ReadyPlayersList = PlayerList.Where(p => p.IsLoaded).ToList();
|
||||
}
|
||||
|
||||
public void Track(Entity entity)
|
||||
{
|
||||
_entitiesDirtySet.Add(entity);
|
||||
}
|
||||
|
||||
public void Broadcast(byte[] data)
|
||||
{
|
||||
foreach (var readyPlayer in ReadyPlayersList)
|
||||
readyPlayer.Connection.Reliable.Send(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class RoomInformation
|
||||
{
|
||||
public string Map { get; init; } = "none";
|
||||
public int Min { get; init; }
|
||||
public int Max { get; init; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Map: {Map} Count: {Min}/{Max}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Ragon.Server;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class RoomPlayer
|
||||
{
|
||||
public INetworkConnection Connection { get; }
|
||||
public string Id { get; }
|
||||
public string Name { get; }
|
||||
public bool IsLoaded { get; private set; }
|
||||
public Room Room { get; private set; }
|
||||
public EntityList Entities { get; private set; }
|
||||
|
||||
public RoomPlayer(INetworkConnection connection, string id, string name)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
Connection = connection;
|
||||
Entities = new EntityList();
|
||||
}
|
||||
|
||||
public void Attach(Room room)
|
||||
{
|
||||
Room = room;
|
||||
}
|
||||
|
||||
public void Detach()
|
||||
{
|
||||
Room = null!;
|
||||
}
|
||||
|
||||
public void SetReady()
|
||||
{
|
||||
IsLoaded = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Handlers;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public sealed class HandlerRegistry
|
||||
{
|
||||
private IHandler _entityEventHandler;
|
||||
private IHandler _entityCreateHandler;
|
||||
private IHandler _entityDestroyHandler;
|
||||
private IHandler _entityStateHandler;
|
||||
private IHandler _sceneLoadedHandler;
|
||||
|
||||
private IHandler _authorizationHandler;
|
||||
private IHandler _joinOrCreateHandler;
|
||||
private IHandler _createHandler;
|
||||
private IHandler _joinHandler;
|
||||
private IHandler _leaveHandler;
|
||||
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private RagonSerializer _reader;
|
||||
private RagonSerializer _writer;
|
||||
|
||||
public HandlerRegistry()
|
||||
{
|
||||
_reader = new RagonSerializer(2048);
|
||||
_writer = new RagonSerializer(2048);
|
||||
|
||||
_authorizationHandler = new AuthorizationHandler();
|
||||
_joinOrCreateHandler = new JoinOrCreateHandler();
|
||||
_sceneLoadedHandler = new SceneLoadedHandler();
|
||||
_createHandler = new CreateHandler();
|
||||
_joinHandler = new JoinHandler();
|
||||
_leaveHandler = new LeaveHandler();
|
||||
|
||||
_entityEventHandler = new EntityEventHandler();
|
||||
_entityCreateHandler = new EntityCreateHandler();
|
||||
_entityDestroyHandler = new EntityDestroyHandler();
|
||||
_entityStateHandler = new EntityStateHandler();
|
||||
}
|
||||
|
||||
public void Handle(PlayerContext context, byte[] data)
|
||||
{
|
||||
_writer.Clear();
|
||||
_reader.Clear();
|
||||
_reader.FromArray(data);
|
||||
|
||||
var operation = _reader.ReadOperation();
|
||||
switch (operation)
|
||||
{
|
||||
case RagonOperation.REPLICATE_ENTITY_EVENT:
|
||||
{
|
||||
if (context.RoomPlayer != null)
|
||||
_entityEventHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.REPLICATE_ENTITY_STATE:
|
||||
{
|
||||
if (context.RoomPlayer != null)
|
||||
_entityStateHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.CREATE_ENTITY:
|
||||
{
|
||||
if (context.RoomPlayer != null)
|
||||
_entityCreateHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.DESTROY_ENTITY:
|
||||
{
|
||||
if (context.RoomPlayer != null)
|
||||
_entityDestroyHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.SCENE_LOADED:
|
||||
{
|
||||
if (context.RoomPlayer != null)
|
||||
_sceneLoadedHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.JOIN_OR_CREATE_ROOM:
|
||||
{
|
||||
_joinOrCreateHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.CREATE_ROOM:
|
||||
{
|
||||
_createHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.JOIN_ROOM:
|
||||
{
|
||||
_joinHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.LEAVE_ROOM:
|
||||
{
|
||||
_leaveHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.AUTHORIZE:
|
||||
{
|
||||
_authorizationHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public interface IHandler
|
||||
{
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Lobby;
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class AuthorizationHandler: IHandler
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Authorized)
|
||||
{
|
||||
_logger.Warn("Player already authorized");
|
||||
return;
|
||||
}
|
||||
|
||||
var key = reader.ReadString();
|
||||
var playerName = reader.ReadString();
|
||||
var additionalData = reader.ReadData(reader.Size);
|
||||
|
||||
context.LobbyPlayer.Name = playerName;
|
||||
context.LobbyPlayer.AdditionalData = additionalData.ToArray();
|
||||
context.LobbyPlayer.Status = LobbyPlayerStatus.Authorized;
|
||||
|
||||
var playerId = context.LobbyPlayer.Id;
|
||||
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
|
||||
writer.WriteString(playerId);
|
||||
writer.WriteString(playerName);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
context.Connection.Reliable.Send(sendData);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} authorized");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Game;
|
||||
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class EntityCreateHandler: IHandler
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
var entityType = reader.ReadUShort();
|
||||
var eventAuthority = (RagonAuthority) reader.ReadByte();
|
||||
var propertiesCount = reader.ReadUShort();
|
||||
|
||||
var entity = new Entity(context.RoomPlayer, entityType, 0, eventAuthority);
|
||||
for (var i = 0; i < propertiesCount; i++)
|
||||
{
|
||||
var propertyType = reader.ReadBool();
|
||||
var propertySize = reader.ReadUShort();
|
||||
entity.State.AddProperty(new EntityStateProperty(propertySize, propertyType));
|
||||
}
|
||||
|
||||
var entityPayload = reader.ReadData(reader.Size);
|
||||
entity.SetPayload(entityPayload.ToArray());
|
||||
|
||||
context.Room.AttachEntity(context.RoomPlayer, entity);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} created entity {entity.Id}:{entity.Type}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class EntityDestroyHandler: IHandler
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
var entityId = reader.ReadUShort();
|
||||
if (context.Room.Entities.TryGetValue(entityId, out var entity))
|
||||
{
|
||||
var player = context.RoomPlayer;
|
||||
var payload = reader.ReadData(reader.Size);
|
||||
|
||||
context.Room.DetachEntity(player, entity, Array.Empty<byte>());
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} destoyed entity {entity.Id}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class EntityEventHandler: IHandler
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
var player = context.RoomPlayer;
|
||||
var room = context.Room;
|
||||
var entityId = reader.ReadUShort();
|
||||
|
||||
if (!room.Entities.TryGetValue(entityId, out var ent))
|
||||
{
|
||||
_logger.Warn($"Entity not found for event with Id {entityId}");
|
||||
return;
|
||||
}
|
||||
|
||||
var eventId = reader.ReadUShort();
|
||||
var eventMode = (RagonReplicationMode) reader.ReadByte();
|
||||
var targetMode = (RagonTarget) reader.ReadByte();
|
||||
var payloadData = reader.ReadData(reader.Size);
|
||||
var targetPlayerPeerId = reader.ReadUShort();
|
||||
|
||||
if (targetMode == RagonTarget.Player && context.Room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer))
|
||||
{
|
||||
Span<byte> payloadRaw = stackalloc byte[payloadData.Length];
|
||||
ReadOnlySpan<byte> payload = payloadRaw;
|
||||
payloadData.CopyTo(payloadRaw);
|
||||
|
||||
_logger.Trace($"Event {eventId} Payload: {payloadData.Length} to {targetMode}");
|
||||
ent.ReplicateEvent(player, eventId, payload, eventMode, targetPlayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
Span<byte> payloadRaw = stackalloc byte[payloadData.Length];
|
||||
ReadOnlySpan<byte> payload = payloadRaw;
|
||||
payloadData.CopyTo(payloadRaw);
|
||||
|
||||
_logger.Trace($"Event {eventId} Payload: {payloadData.Length} to {targetMode}");
|
||||
ent.ReplicateEvent(player, eventId, payload, eventMode, targetMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class EntityStateHandler: IHandler
|
||||
{
|
||||
private ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
var room = context.Room;
|
||||
var entitiesCount = reader.ReadUShort();
|
||||
for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++)
|
||||
{
|
||||
var entityId = reader.ReadUShort();
|
||||
|
||||
if (room.Entities.TryGetValue(entityId, out var entity))
|
||||
{
|
||||
entity.State.Read(reader);
|
||||
room.Track(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Error($"Entity with Id {entityId} not found, replication interrupted");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Game;
|
||||
using Ragon.Core.Lobby;
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class CreateHandler: IHandler
|
||||
{
|
||||
private RagonRoomParameters _roomParameters = new();
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized)
|
||||
{
|
||||
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
|
||||
return;
|
||||
}
|
||||
|
||||
var custom = reader.ReadBool();
|
||||
var roomId = Guid.NewGuid().ToString();
|
||||
|
||||
if (custom)
|
||||
{
|
||||
roomId = reader.ReadString();
|
||||
if (context.Lobby.FindRoomById(roomId, out _))
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_FAILED);
|
||||
writer.WriteString($"Room with id {roomId} already exists");
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
context.Connection.Reliable.Send(sendData);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} join failed to room {roomId}, room already exist");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_roomParameters.Deserialize(reader);
|
||||
|
||||
var information = new RoomInformation()
|
||||
{
|
||||
Map = _roomParameters.Map,
|
||||
Max = _roomParameters.Max,
|
||||
Min = _roomParameters.Min,
|
||||
};
|
||||
|
||||
var lobbyPlayer = context.LobbyPlayer;
|
||||
var roomPlayer = new RoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
|
||||
var room = new Room(roomId, information);
|
||||
room.AddPlayer(roomPlayer);
|
||||
|
||||
context.Room?.RemovePlayer(context.RoomPlayer);
|
||||
context.Room = room;
|
||||
context.RoomPlayer = roomPlayer;
|
||||
context.Lobby.Persist(room);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} {information}");
|
||||
|
||||
JoinSuccess(roomPlayer, room, writer);
|
||||
|
||||
context.Loop.Run(room);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}");
|
||||
}
|
||||
|
||||
private void JoinSuccess(RoomPlayer player, Room room, RagonSerializer writer)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
||||
writer.WriteString(room.Id);
|
||||
writer.WriteString(player.Id);
|
||||
writer.WriteString(room.Owner.Id);
|
||||
writer.WriteUShort((ushort) room.Info.Min);
|
||||
writer.WriteUShort((ushort) room.Info.Max);
|
||||
writer.WriteString(room.Info.Map);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Game;
|
||||
using Ragon.Core.Lobby;
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class JoinHandler : IHandler
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
var roomId = reader.ReadString();
|
||||
var lobbyPlayer = context.LobbyPlayer;
|
||||
|
||||
if (!context.Lobby.FindRoomById(roomId, out var existsRoom))
|
||||
{
|
||||
JoinFailed(lobbyPlayer, writer);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} failed to join room {roomId}");
|
||||
return;
|
||||
}
|
||||
|
||||
var roomPlayer = new RoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
|
||||
context.Room?.RemovePlayer(context.RoomPlayer);
|
||||
context.Room = existsRoom;
|
||||
context.RoomPlayer = roomPlayer;
|
||||
|
||||
existsRoom.AddPlayer(roomPlayer);
|
||||
|
||||
JoinSuccess(roomPlayer, existsRoom, writer);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to {existsRoom.Id}");
|
||||
}
|
||||
|
||||
private void JoinSuccess(RoomPlayer player, Room room, RagonSerializer writer)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
||||
writer.WriteString(room.Id);
|
||||
writer.WriteString(player.Id);
|
||||
writer.WriteString(room.Owner.Id);
|
||||
writer.WriteUShort((ushort) room.Info.Min);
|
||||
writer.WriteUShort((ushort) room.Info.Max);
|
||||
writer.WriteString(room.Info.Map);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
private void JoinFailed(LobbyPlayer player, RagonSerializer writer)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_FAILED);
|
||||
writer.WriteString($"Room not exists");
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Game;
|
||||
using Ragon.Core.Lobby;
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class JoinOrCreateHandler : IHandler
|
||||
{
|
||||
private RagonRoomParameters _roomParameters = new();
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized)
|
||||
{
|
||||
_logger.Warn("Player not authorized for this request");
|
||||
return;
|
||||
}
|
||||
|
||||
var roomId = Guid.NewGuid().ToString();
|
||||
var lobbyPlayer = context.LobbyPlayer;
|
||||
|
||||
_roomParameters.Deserialize(reader);
|
||||
|
||||
if (context.Lobby.FindRoomByMap(_roomParameters.Map, out var existsRoom))
|
||||
{
|
||||
var roomPlayer = new RoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
|
||||
context.Room?.RemovePlayer(context.RoomPlayer);
|
||||
context.Room = existsRoom;
|
||||
context.RoomPlayer = roomPlayer;
|
||||
|
||||
existsRoom.AddPlayer(roomPlayer);
|
||||
|
||||
JoinSuccess(roomPlayer, existsRoom, writer);
|
||||
}
|
||||
else
|
||||
{
|
||||
var information = new RoomInformation()
|
||||
{
|
||||
Map = _roomParameters.Map,
|
||||
Max = _roomParameters.Max,
|
||||
Min = _roomParameters.Min,
|
||||
};
|
||||
|
||||
var room = new Room(roomId, information);
|
||||
context.Lobby.Persist(room);
|
||||
|
||||
var roomPlayer = new RoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
room.AddPlayer(roomPlayer);
|
||||
|
||||
context.Room?.RemovePlayer(context.RoomPlayer);
|
||||
context.Room = room;
|
||||
context.RoomPlayer = roomPlayer;
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} {information}");
|
||||
|
||||
JoinSuccess(roomPlayer, room, writer);
|
||||
|
||||
context.Loop.Run(room);
|
||||
}
|
||||
}
|
||||
|
||||
private void JoinSuccess(RoomPlayer player, Room room, RagonSerializer writer)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
||||
writer.WriteString(room.Id);
|
||||
writer.WriteString(player.Id);
|
||||
writer.WriteString(room.Owner.Id);
|
||||
writer.WriteUShort((ushort) room.Info.Min);
|
||||
writer.WriteUShort((ushort) room.Info.Max);
|
||||
writer.WriteString(room.Info.Map);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
|
||||
_logger.Trace($"{player.Connection.Id}|{player.Name} joined to room {room.Id}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class LeaveHandler: IHandler
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
var room = context.Room;
|
||||
var roomPlayer = context.RoomPlayer;
|
||||
if (room != null)
|
||||
{
|
||||
context.Room?.RemovePlayer(roomPlayer);
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} leaved from {room.Id}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Game;
|
||||
using Ragon.Core.Lobby;
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class SceneLoadedHandler : IHandler
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized)
|
||||
return;
|
||||
|
||||
var owner = context.Room.Owner;
|
||||
var player = context.RoomPlayer;
|
||||
var room = context.Room;
|
||||
|
||||
if (player == owner)
|
||||
{
|
||||
var statics = reader.ReadUShort();
|
||||
for (var staticIndex = 0; staticIndex < statics; staticIndex++)
|
||||
{
|
||||
var entityType = reader.ReadUShort();
|
||||
var eventAuthority = (RagonAuthority) reader.ReadByte();
|
||||
var staticId = reader.ReadUShort();
|
||||
var propertiesCount = reader.ReadUShort();
|
||||
|
||||
var entity = new Entity(player, entityType, staticId, eventAuthority);
|
||||
for (var propertyIndex = 0; propertyIndex < propertiesCount; propertyIndex++)
|
||||
{
|
||||
var propertyType = reader.ReadBool();
|
||||
var propertySize = reader.ReadUShort();
|
||||
entity.State.AddProperty(new EntityStateProperty(propertySize, propertyType));
|
||||
}
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} created entity {entity.Id}:{entity.Type}");
|
||||
room.AttachEntity(player, entity);
|
||||
}
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} loaded");
|
||||
|
||||
room.WaitPlayersList.Add(player);
|
||||
|
||||
foreach (var roomPlayer in room.WaitPlayersList)
|
||||
{
|
||||
DispatchPlayerJoinExcludePlayer(room, roomPlayer, writer);
|
||||
|
||||
roomPlayer.SetReady();
|
||||
}
|
||||
|
||||
room.UpdateReadyPlayerList();
|
||||
|
||||
DispatchSnapshot(room, room.WaitPlayersList, writer);
|
||||
|
||||
room.WaitPlayersList.Clear();
|
||||
}
|
||||
else if (owner.IsLoaded)
|
||||
{
|
||||
player.SetReady();
|
||||
|
||||
DispatchPlayerJoinExcludePlayer(room, player, writer);
|
||||
|
||||
room.UpdateReadyPlayerList();
|
||||
|
||||
DispatchSnapshot(room, new List<RoomPlayer>() { player }, writer);
|
||||
|
||||
foreach (var entity in room.EntityList)
|
||||
entity.RestoreBufferedEvents(player, writer);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Trace($"Player {player.Connection.Id}|{context.LobbyPlayer.Name} waiting owner of room");
|
||||
room.WaitPlayersList.Add(player);
|
||||
}
|
||||
}
|
||||
|
||||
private void DispatchPlayerJoinExcludePlayer(Room room, RoomPlayer roomPlayer, RagonSerializer writer)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.PLAYER_JOINED);
|
||||
writer.WriteUShort(roomPlayer.Connection.Id);
|
||||
writer.WriteString(roomPlayer.Id);
|
||||
writer.WriteString(roomPlayer.Name);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
foreach (var awaiter in room.ReadyPlayersList)
|
||||
{
|
||||
if (awaiter != roomPlayer)
|
||||
awaiter.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
}
|
||||
|
||||
private void DispatchSnapshot(Room room, List<RoomPlayer> receviersList, RagonSerializer writer)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.SNAPSHOT);
|
||||
writer.WriteUShort((ushort) room.ReadyPlayersList.Count);
|
||||
foreach (var roomPlayer in room.ReadyPlayersList)
|
||||
{
|
||||
writer.WriteUShort(roomPlayer.Connection.Id);
|
||||
writer.WriteString(roomPlayer.Id);
|
||||
writer.WriteString(roomPlayer.Name);
|
||||
}
|
||||
|
||||
var dynamicEntities = room.DynamicEntitiesList;
|
||||
var dynamicEntitiesCount = (ushort) dynamicEntities.Count;
|
||||
writer.WriteUShort(dynamicEntitiesCount);
|
||||
foreach (var entity in dynamicEntities)
|
||||
entity.State.Snapshot(writer);
|
||||
|
||||
var staticEntities = room.StaticEntitiesList;
|
||||
var staticEntitiesCount = (ushort) staticEntities.Count;
|
||||
writer.WriteUShort(staticEntitiesCount);
|
||||
foreach (var entity in staticEntities)
|
||||
entity.State.Snapshot(writer);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
foreach (var player in receviersList)
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Ragon.Core.Game;
|
||||
|
||||
namespace Ragon.Core.Lobby;
|
||||
|
||||
public interface ILobby
|
||||
{
|
||||
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out Room room);
|
||||
public bool FindRoomByMap(string map, [MaybeNullWhen(false)] out Room room);
|
||||
public void Persist(Room room);
|
||||
public void RemoveIfEmpty(Room room);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using NLog;
|
||||
using Ragon.Core.Game;
|
||||
|
||||
namespace Ragon.Core.Lobby;
|
||||
|
||||
public class LobbyInMemory : ILobby
|
||||
{
|
||||
private readonly List<Room> _rooms = new();
|
||||
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out Room room)
|
||||
{
|
||||
foreach (var existRoom in _rooms)
|
||||
{
|
||||
var info = existRoom.Info;
|
||||
if (existRoom.Id == roomId && info.Min < info.Max)
|
||||
{
|
||||
room = existRoom;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
room = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool FindRoomByMap(string map, [MaybeNullWhen(false)] out Room room)
|
||||
{
|
||||
foreach (var existRoom in _rooms)
|
||||
{
|
||||
var info = existRoom.Info;
|
||||
if (info.Map == map && existRoom.Players.Count < info.Max)
|
||||
{
|
||||
room = existRoom;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
room = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Persist(Room room)
|
||||
{
|
||||
_rooms.Add(room);
|
||||
|
||||
foreach (var r in _rooms)
|
||||
_logger.Trace($"Room: {r.Id} {r.Info} Players: {r.Players.Count}");
|
||||
}
|
||||
|
||||
public void RemoveIfEmpty(Room room)
|
||||
{
|
||||
if (room.Players.Count == 0)
|
||||
_rooms.Remove(room);
|
||||
|
||||
foreach (var r in _rooms)
|
||||
_logger.Trace($"Room: {r.Id} {r.Info} Players: {r.Players.Count}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Ragon.Server;
|
||||
|
||||
namespace Ragon.Core.Lobby;
|
||||
|
||||
public enum LobbyPlayerStatus
|
||||
{
|
||||
Unauthorized,
|
||||
Authorized,
|
||||
}
|
||||
|
||||
public class LobbyPlayer
|
||||
{
|
||||
public string Id { get; private set; }
|
||||
public string Name { get; set; }
|
||||
public byte[] AdditionalData { get; set; }
|
||||
public LobbyPlayerStatus Status { get; set; }
|
||||
public INetworkConnection Connection { get; private set; }
|
||||
|
||||
public LobbyPlayer(INetworkConnection connection)
|
||||
{
|
||||
Id = Guid.NewGuid().ToString();
|
||||
Connection = connection;
|
||||
Status = LobbyPlayerStatus.Unauthorized;
|
||||
Name = "None";
|
||||
AdditionalData = Array.Empty<byte>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using NLog;
|
||||
using Ragon.Core.Game;
|
||||
using Ragon.Core.Lobby;
|
||||
using Ragon.Core.Time;
|
||||
using Ragon.Server;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public class PlayerContext: IDisposable
|
||||
{
|
||||
public INetworkConnection Connection { get; }
|
||||
public Loop Loop;
|
||||
public ILobby Lobby { get; set; }
|
||||
public LobbyPlayer LobbyPlayer { private set; get; }
|
||||
public Room? Room { get; set; }
|
||||
public RoomPlayer? RoomPlayer { get; set; }
|
||||
|
||||
public PlayerContext(INetworkConnection conn, LobbyPlayer player)
|
||||
{
|
||||
Connection = conn;
|
||||
LobbyPlayer = player;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
RoomPlayer?.Room.RemovePlayer(RoomPlayer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta2" />
|
||||
<PackageReference Include="NLog" Version="5.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ragon.Protocol\Ragon.Protocol.csproj" />
|
||||
<ProjectReference Include="..\Ragon.Server.ENet\Ragon.Server.ENet.csproj" />
|
||||
<ProjectReference Include="..\Ragon.Server.NativeWebSockets\Ragon.Server.NativeWebSockets.csproj" />
|
||||
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -5,6 +5,7 @@
|
||||
<Nullable>disable</Nullable>
|
||||
<LangVersion>8</LangVersion>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<RootNamespace>Ragon.Common</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
@@ -18,8 +19,4 @@
|
||||
<OutputPath></OutputPath>
|
||||
<DefineConstants>TRACE;NETSTACK_SPAN</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="External" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -2,7 +2,8 @@ namespace Ragon.Common
|
||||
{
|
||||
public enum RagonAuthority: byte
|
||||
{
|
||||
OWNER_ONLY,
|
||||
ALL,
|
||||
None,
|
||||
OwnerOnly,
|
||||
All,
|
||||
}
|
||||
}
|
||||
@@ -5,28 +5,21 @@ namespace Ragon.Common
|
||||
AUTHORIZE,
|
||||
AUTHORIZED_SUCCESS,
|
||||
AUTHORIZED_FAILED,
|
||||
|
||||
JOIN_OR_CREATE_ROOM,
|
||||
CREATE_ROOM,
|
||||
JOIN_ROOM,
|
||||
LEAVE_ROOM,
|
||||
OWNERSHIP_CHANGED,
|
||||
JOIN_SUCCESS,
|
||||
JOIN_FAILED,
|
||||
|
||||
LOAD_SCENE,
|
||||
SCENE_IS_LOADED,
|
||||
|
||||
SCENE_LOADED,
|
||||
PLAYER_JOINED,
|
||||
PLAYER_LEAVED,
|
||||
|
||||
CREATE_ENTITY,
|
||||
CREATE_STATIC_ENTITY,
|
||||
DESTROY_ENTITY,
|
||||
|
||||
SNAPSHOT,
|
||||
|
||||
REPLICATE_ENTITY_STATE,
|
||||
REPLICATE_ENTITY_EVENT,
|
||||
REPLICATE_EVENT,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Ragon.Common
|
||||
{
|
||||
public enum RagonReplicationMode: byte
|
||||
{
|
||||
Local,
|
||||
Server,
|
||||
LocalAndServer,
|
||||
Buffered,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
namespace Ragon.Common
|
||||
{
|
||||
public class RagonRoomParameters: IRagonSerializable
|
||||
{
|
||||
public string Map { get; set; }
|
||||
public int Min { get; set; }
|
||||
public int Max { get; set; }
|
||||
|
||||
public void Serialize(RagonSerializer buffer)
|
||||
{
|
||||
buffer.WriteString(Map);
|
||||
buffer.WriteInt(Min);
|
||||
buffer.WriteInt(Max);
|
||||
}
|
||||
|
||||
public void Deserialize(RagonSerializer buffer)
|
||||
{
|
||||
Map = buffer.ReadString();
|
||||
Min = buffer.ReadInt();
|
||||
Max = buffer.ReadInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Ragon.Common
|
||||
{
|
||||
public interface IRagonSerializable
|
||||
{
|
||||
public void Serialize(RagonSerializer serializer);
|
||||
public void Deserialize(RagonSerializer serializer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
|
||||
namespace Ragon.Common
|
||||
{
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
internal struct ValueConverter
|
||||
{
|
||||
[FieldOffset(0)] public int Int;
|
||||
[FieldOffset(0)] public float Float;
|
||||
[FieldOffset(0)] public long Long;
|
||||
[FieldOffset(0)] public byte Byte0;
|
||||
[FieldOffset(1)] public byte Byte1;
|
||||
[FieldOffset(2)] public byte Byte2;
|
||||
[FieldOffset(3)] public byte Byte3;
|
||||
[FieldOffset(4)] public byte Byte4;
|
||||
[FieldOffset(5)] public byte Byte5;
|
||||
[FieldOffset(6)] public byte Byte6;
|
||||
[FieldOffset(7)] public byte Byte7;
|
||||
}
|
||||
|
||||
public class RagonSerializer
|
||||
{
|
||||
private byte[] _data;
|
||||
private int _offset;
|
||||
private int _size;
|
||||
|
||||
public int Lenght => _offset;
|
||||
public int Size => _size - _offset;
|
||||
|
||||
public RagonSerializer(int capacity = 256)
|
||||
{
|
||||
_data = new byte[capacity];
|
||||
_offset = 0;
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Reset()
|
||||
{
|
||||
_size = _offset;
|
||||
_offset = 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AddOffset(int offset)
|
||||
{
|
||||
_offset += offset;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int WriteByte(byte value)
|
||||
{
|
||||
ResizeIfNeed(1);
|
||||
_data[_offset] = value;
|
||||
_offset += 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public byte ReadByte()
|
||||
{
|
||||
var value = _data[_offset];
|
||||
_offset += 1;
|
||||
return value;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int WriteBool(bool value)
|
||||
{
|
||||
ResizeIfNeed(1);
|
||||
_data[_offset] = value ? (byte) 1 : (byte) 0;
|
||||
_offset += 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool ReadBool()
|
||||
{
|
||||
var value = _data[_offset];
|
||||
_offset += 1;
|
||||
return value == 1;
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int WriteInt(int value)
|
||||
{
|
||||
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;
|
||||
_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)]
|
||||
public int ReadInt()
|
||||
{
|
||||
var converter = new ValueConverter {Byte0 = _data[_offset], Byte1 = _data[_offset + 1], Byte2 = _data[_offset + 2], Byte3 = _data[_offset + 3]};
|
||||
_offset += 4;
|
||||
return converter.Int;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteLong(long value)
|
||||
{
|
||||
ResizeIfNeed(8);
|
||||
WriteLong(value, _offset);
|
||||
_offset += 8;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int WriteLong(long value, int offset)
|
||||
{
|
||||
var converter = new ValueConverter() {Long = value};
|
||||
_data[offset] = converter.Byte0;
|
||||
_data[offset + 1] = converter.Byte1;
|
||||
_data[offset + 2] = converter.Byte2;
|
||||
_data[offset + 3] = converter.Byte3;
|
||||
_data[offset + 4] = converter.Byte4;
|
||||
_data[offset + 5] = converter.Byte5;
|
||||
_data[offset + 6] = converter.Byte6;
|
||||
_data[offset + 7] = converter.Byte7;
|
||||
return 8;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public long ReadLong()
|
||||
{
|
||||
var converter = new ValueConverter
|
||||
{
|
||||
Byte0 = _data[_offset],
|
||||
Byte1 = _data[_offset + 1],
|
||||
Byte2 = _data[_offset + 2],
|
||||
Byte3 = _data[_offset + 3],
|
||||
Byte4 = _data[_offset + 4],
|
||||
Byte5 = _data[_offset + 5],
|
||||
Byte6 = _data[_offset + 6],
|
||||
Byte7 = _data[_offset + 7],
|
||||
};
|
||||
_offset += 8;
|
||||
return converter.Long;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int WriteFloat(float value)
|
||||
{
|
||||
var converter = new ValueConverter() {Float = value};
|
||||
WriteInt(converter.Int);
|
||||
return 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public float ReadFloat()
|
||||
{
|
||||
var rawValue = ReadInt();
|
||||
var converter = new ValueConverter() {Int = rawValue};
|
||||
var value = converter.Float;
|
||||
return value;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int WriteString(string value)
|
||||
{
|
||||
var rawData = Encoding.UTF8.GetBytes(value).AsSpan();
|
||||
ResizeIfNeed(2 + rawData.Length);
|
||||
WriteUShort((ushort) rawData.Length);
|
||||
var data = _data.AsSpan().Slice(_offset, rawData.Length);
|
||||
rawData.CopyTo(data);
|
||||
_offset += rawData.Length;
|
||||
|
||||
return rawData.Length + 2;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadString()
|
||||
{
|
||||
var lenght = ReadUShort();
|
||||
var stringRaw = _data.AsSpan().Slice(_offset, lenght);
|
||||
var str = Encoding.UTF8.GetString(stringRaw);
|
||||
_offset += lenght;
|
||||
return str;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ReadOnlySpan<byte> ReadData(int lenght)
|
||||
{
|
||||
var data = _data.AsSpan();
|
||||
var payloadData = data.Slice(_offset, lenght);
|
||||
|
||||
_offset += payloadData.Length;
|
||||
return payloadData;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int WriteData(ref ReadOnlySpan<byte> payload)
|
||||
{
|
||||
ResizeIfNeed(payload.Length);
|
||||
|
||||
var data = _data.AsSpan();
|
||||
var payloadData = data.Slice(_offset, payload.Length);
|
||||
|
||||
payload.CopyTo(payloadData);
|
||||
_offset += payload.Length;
|
||||
return payload.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Span<byte> GetWritableData(int lenght)
|
||||
{
|
||||
ResizeIfNeed(lenght);
|
||||
|
||||
var data = _data.AsSpan();
|
||||
var payloadData = data.Slice(_offset, lenght);
|
||||
|
||||
_offset += lenght;
|
||||
return payloadData;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int WriteOperation(RagonOperation ragonOperation)
|
||||
{
|
||||
ResizeIfNeed(1);
|
||||
|
||||
_data[_offset] = (byte) ragonOperation;
|
||||
_offset += 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public RagonOperation ReadOperation()
|
||||
{
|
||||
var op = (RagonOperation) _data[_offset];
|
||||
_offset += 1;
|
||||
return op;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int WriteUShort(ushort value)
|
||||
{
|
||||
ResizeIfNeed(2);
|
||||
|
||||
_data[_offset] = (byte) (value & 0x00FF);
|
||||
_data[_offset + 1] = (byte) ((value & 0xFF00) >> 8);
|
||||
_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)]
|
||||
public ushort ReadUShort()
|
||||
{
|
||||
var value = (ushort) (_data[_offset] + (_data[_offset + 1] << 8));
|
||||
_offset += 2;
|
||||
return value;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_offset = 0;
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
public void ToSpan(ref Span<byte> data)
|
||||
{
|
||||
var span = _data.AsSpan();
|
||||
var dataSpan = span.Slice(0, _offset);
|
||||
dataSpan.CopyTo(data);
|
||||
}
|
||||
|
||||
public void FromSpan(ref ReadOnlySpan<byte> data)
|
||||
{
|
||||
Clear();
|
||||
ResizeIfNeed(data.Length);
|
||||
var dataSpan = _data.AsSpan();
|
||||
data.CopyTo(dataSpan);
|
||||
_size = data.Length;
|
||||
}
|
||||
|
||||
public void FromArray(byte[] data)
|
||||
{
|
||||
Clear();
|
||||
ResizeIfNeed(data.Length);
|
||||
Buffer.BlockCopy(data, 0, _data, 0, data.Length);
|
||||
_size = data.Length;
|
||||
}
|
||||
|
||||
|
||||
public byte[] ToArray()
|
||||
{
|
||||
var bytes = new byte[_offset];
|
||||
Buffer.BlockCopy(_data, 0, bytes, 0, _offset);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private void ResizeIfNeed(int lenght)
|
||||
{
|
||||
if (_offset + lenght < _data.Length)
|
||||
return;
|
||||
|
||||
var newData = new byte[_data.Length * 4 + lenght];
|
||||
Buffer.BlockCopy(_data, 0, newData, 0, _data.Length);
|
||||
_data = newData;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Ragon.Common
|
||||
{
|
||||
public enum RagonTarget: byte
|
||||
{
|
||||
Owner,
|
||||
ExceptOwner,
|
||||
ExceptInvoker,
|
||||
All,
|
||||
Player,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace Ragon.Common
|
||||
{
|
||||
public static class RagonVersion
|
||||
{
|
||||
public static uint Parse(string version)
|
||||
{
|
||||
var strings = version.Split(".");
|
||||
if (strings.Length < 3)
|
||||
return 0;
|
||||
|
||||
var parts = new uint[] {0, 0, 0};
|
||||
for (int i = 0; i < parts.Length; i++)
|
||||
{
|
||||
if (!uint.TryParse(strings[i], out var v))
|
||||
return 0;
|
||||
|
||||
parts[i] = v;
|
||||
}
|
||||
|
||||
return (parts[0] << 16) | (parts[1] << 8) | parts[2];
|
||||
}
|
||||
|
||||
public static string Parse(uint version)
|
||||
{
|
||||
return (version >> 16 & 0xFF) + "." + (version >> 8 & 0xFF) + "." + (version & 0xFF);
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using NLog;
|
||||
using Ragon.Core;
|
||||
|
||||
namespace Ragon.Relay
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var logger = LogManager.GetLogger("Ragon.Relay");
|
||||
|
||||
logger.Info("Relay Application");
|
||||
|
||||
var configuration = Configuration.Load("relay.config.json");
|
||||
var relay = new Application(configuration);
|
||||
|
||||
logger.Info("Started");
|
||||
relay.Start();
|
||||
|
||||
Console.ReadKey();
|
||||
|
||||
relay.Stop();
|
||||
logger.Info("Stopped");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,13 +14,13 @@
|
||||
<None Update="NLog.config">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="config.json">
|
||||
<None Update="relay.config.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ragon\Ragon.csproj" />
|
||||
<ProjectReference Include="..\Ragon.Core\Ragon.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"serverKey": "defaultkey",
|
||||
"serverType": "enet",
|
||||
"serverTickRate": 20,
|
||||
"gameProtocol": "1.0.0",
|
||||
"port": 5000,
|
||||
"limitConnections": 4095,
|
||||
"limitPlayersPerRoom": 20,
|
||||
"limitRooms": 200
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using ENet;
|
||||
|
||||
namespace Ragon.Server.ENet;
|
||||
|
||||
public sealed class ENetConnection: INetworkConnection
|
||||
{
|
||||
public ushort Id { get; }
|
||||
public INetworkChannel Reliable { get; private set; }
|
||||
public INetworkChannel Unreliable { get; private set; }
|
||||
|
||||
public ENetConnection(Peer peer)
|
||||
{
|
||||
Id = (ushort) peer.ID;
|
||||
Reliable = new ENetReliableChannel(peer, 0);
|
||||
Unreliable = new ENetUnreliableChannel(peer, 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using ENet;
|
||||
|
||||
namespace Ragon.Server.ENet;
|
||||
|
||||
public sealed class ENetReliableChannel: INetworkChannel
|
||||
{
|
||||
private Peer _peer;
|
||||
private byte _channelId;
|
||||
|
||||
public ENetReliableChannel(Peer peer, int channelId)
|
||||
{
|
||||
_peer = peer;
|
||||
_channelId = (byte) channelId;
|
||||
}
|
||||
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
var newPacket = new Packet();
|
||||
newPacket.Create(data, data.Length, PacketFlags.Reliable);
|
||||
|
||||
_peer.Send(_channelId, ref newPacket);
|
||||
}
|
||||
}
|
||||
Executable
+116
@@ -0,0 +1,116 @@
|
||||
using System.Diagnostics;
|
||||
using ENet;
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
|
||||
namespace Ragon.Server.ENet
|
||||
{
|
||||
public sealed class ENetServer: INetworkServer
|
||||
{
|
||||
private readonly Host _host;
|
||||
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
private ENetConnection[] _connections;
|
||||
private uint _protocol;
|
||||
private INetworkListener _listener;
|
||||
private Event _event;
|
||||
|
||||
public ENetServer()
|
||||
{
|
||||
_host = new Host();
|
||||
_connections = Array.Empty<ENetConnection>();
|
||||
}
|
||||
|
||||
public void Start(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 127.0.0.1:{configuration.Port}");
|
||||
_logger.Info($"Protocol: {protocolDecoded}");
|
||||
}
|
||||
|
||||
public void Poll()
|
||||
{
|
||||
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.Warn($"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, dataRaw);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_host?.Dispose();
|
||||
|
||||
Library.Deinitialize();
|
||||
}
|
||||
|
||||
private bool IsValidProtocol(uint protocol)
|
||||
{
|
||||
return protocol == _protocol;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using ENet;
|
||||
|
||||
namespace Ragon.Server.ENet;
|
||||
|
||||
public sealed class ENetUnreliableChannel: INetworkChannel
|
||||
{
|
||||
private Peer _peer;
|
||||
private byte _channelId;
|
||||
|
||||
public ENetUnreliableChannel(Peer peer, int channelId)
|
||||
{
|
||||
_peer = peer;
|
||||
_channelId = (byte) channelId;
|
||||
}
|
||||
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
var newPacket = new Packet();
|
||||
newPacket.Create(data, data.Length, PacketFlags.None);
|
||||
|
||||
_peer.Send(_channelId, ref newPacket);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>StressTest</RootNamespace>
|
||||
<RootNamespace>Ragon.ENet</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ragon.Common\Ragon.Common.csproj" />
|
||||
<PackageReference Include="ENet-CSharp" Version="2.4.8" />
|
||||
<PackageReference Include="NLog" Version="5.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ENet-CSharp" Version="2.4.8" />
|
||||
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>Ragon.WebSockets</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NLog" Version="5.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Net.WebSockets;
|
||||
using NLog;
|
||||
|
||||
namespace Ragon.Server.NativeWebSockets;
|
||||
|
||||
public sealed class WebSocketConnection : INetworkConnection
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
public ushort Id { get; }
|
||||
public INetworkChannel Reliable { get; private set; }
|
||||
public INetworkChannel Unreliable { get; private set; }
|
||||
|
||||
public WebSocket Socket { get; private set; }
|
||||
private WebSocketReliableChannel[] _channels;
|
||||
|
||||
public WebSocketConnection(WebSocket webSocket, ushort peerId)
|
||||
{
|
||||
Id = peerId;
|
||||
Socket = webSocket;
|
||||
|
||||
var reliableChannel = new WebSocketReliableChannel(webSocket);
|
||||
var unreliableChannel = new WebSocketReliableChannel(webSocket);
|
||||
|
||||
_channels = new[] { reliableChannel, unreliableChannel };
|
||||
|
||||
Reliable = reliableChannel;
|
||||
Unreliable = unreliableChannel;
|
||||
}
|
||||
|
||||
public async Task Flush()
|
||||
{
|
||||
foreach (var channel in _channels)
|
||||
{
|
||||
try
|
||||
{
|
||||
await channel.Flush();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Net.WebSockets;
|
||||
using Ragon.Server;
|
||||
|
||||
namespace Ragon.Server.NativeWebSockets;
|
||||
|
||||
public class WebSocketReliableChannel : INetworkChannel
|
||||
{
|
||||
private Queue<byte[]> _queue;
|
||||
private WebSocket _socket;
|
||||
|
||||
public WebSocketReliableChannel(WebSocket webSocket)
|
||||
{
|
||||
_socket = webSocket;
|
||||
_queue = new Queue<byte[]>(512);
|
||||
}
|
||||
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
_queue.Enqueue(data);
|
||||
}
|
||||
|
||||
public async Task Flush()
|
||||
{
|
||||
while (_queue.TryDequeue(out var sendData) && _socket.State == WebSocketState.Open)
|
||||
await _socket.SendAsync(sendData, WebSocketMessageType.Binary, WebSocketMessageFlags.EndOfMessage, CancellationToken.None);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.WebSockets;
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Server;
|
||||
using Ragon.Server;
|
||||
using Ragon.Server.NativeWebSockets;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public class NativeWebSocketServer : INetworkServer
|
||||
{
|
||||
private ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||
private INetworkListener _networkListener;
|
||||
private Stack<ushort> _sequencer;
|
||||
private Executor _executor;
|
||||
private HttpListener _httpListener;
|
||||
private WebSocketConnection[] _connections;
|
||||
private List<WebSocketConnection> _activeConnections;
|
||||
private CancellationTokenSource _cancellationTokenSource;
|
||||
|
||||
public NativeWebSocketServer(Executor executor)
|
||||
{
|
||||
_sequencer = new Stack<ushort>();
|
||||
_connections = Array.Empty<WebSocketConnection>();
|
||||
_activeConnections = new List<WebSocketConnection>();
|
||||
_executor = executor;
|
||||
}
|
||||
|
||||
public async void StartAccept(CancellationToken cancellationToken)
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
var context = await _httpListener.GetContextAsync();
|
||||
if (!context.Request.IsWebSocketRequest) continue;
|
||||
|
||||
var webSocketContext = await context.AcceptWebSocketAsync(null);
|
||||
var webSocket = webSocketContext.WebSocket;
|
||||
|
||||
var peerId = _sequencer.Pop();
|
||||
var connection = new WebSocketConnection(webSocket, peerId);
|
||||
|
||||
_connections[peerId] = connection;
|
||||
StartListen(connection, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
async void StartListen(WebSocketConnection connection, CancellationToken cancellationToken)
|
||||
{
|
||||
_activeConnections.Add(connection);
|
||||
_networkListener.OnConnected(connection);
|
||||
|
||||
var webSocket = connection.Socket;
|
||||
var bytes = new byte[2048];
|
||||
var buffer = new Memory<byte>(bytes);
|
||||
while (
|
||||
webSocket.State == WebSocketState.Open ||
|
||||
!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await webSocket.ReceiveAsync(buffer, cancellationToken);
|
||||
var dataRaw = buffer.Slice(0, result.Count);
|
||||
|
||||
_networkListener.OnData(connection, dataRaw.ToArray());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_sequencer.Push(connection.Id);
|
||||
_activeConnections.Remove(connection);
|
||||
_networkListener.OnDisconnected(connection);
|
||||
}
|
||||
|
||||
public void Poll()
|
||||
{
|
||||
Flush();
|
||||
}
|
||||
|
||||
public async void Flush()
|
||||
{
|
||||
foreach (var conn in _activeConnections)
|
||||
await conn.Flush();
|
||||
}
|
||||
|
||||
public void Start(
|
||||
INetworkListener listener,
|
||||
NetworkConfiguration configuration
|
||||
)
|
||||
{
|
||||
_networkListener = listener;
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
|
||||
var limit = (ushort)configuration.LimitConnections;
|
||||
for (ushort i = limit; i != 0; i--)
|
||||
_sequencer.Push(i);
|
||||
|
||||
_sequencer.Push(0);
|
||||
|
||||
_connections = new WebSocketConnection[configuration.LimitConnections];
|
||||
|
||||
_httpListener = new HttpListener();
|
||||
_httpListener.Prefixes.Add($"http://127.0.0.1:{configuration.Port}/");
|
||||
_httpListener.Start();
|
||||
|
||||
_executor.Run(() => StartAccept(_cancellationTokenSource.Token));
|
||||
|
||||
var protocolDecoded = RagonVersion.Parse(configuration.Protocol);
|
||||
_logger.Info($"Listen at http://*:{configuration.Port}/");
|
||||
_logger.Info($"Protocol: {protocolDecoded}");
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_cancellationTokenSource.Cancel();
|
||||
_httpListener.Stop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Ragon.Server;
|
||||
|
||||
public interface INetworkChannel
|
||||
{
|
||||
void Send(byte[] data);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Ragon.Server;
|
||||
|
||||
public interface INetworkConnection
|
||||
{
|
||||
public ushort Id { get; }
|
||||
public INetworkChannel Reliable { get; }
|
||||
public INetworkChannel Unreliable { get; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Ragon.Server;
|
||||
|
||||
public interface INetworkListener
|
||||
{
|
||||
void OnConnected(INetworkConnection connection);
|
||||
void OnDisconnected(INetworkConnection connection);
|
||||
void OnTimeout(INetworkConnection connection);
|
||||
void OnData(INetworkConnection connection, byte[] data);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Ragon.Server;
|
||||
|
||||
public interface INetworkServer
|
||||
{
|
||||
public void Stop();
|
||||
public void Poll();
|
||||
public void Start(INetworkListener listener, NetworkConfiguration configuration);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System.Threading.Channels;
|
||||
|
||||
namespace Ragon.Core.Server;
|
||||
|
||||
public class Executor: TaskScheduler
|
||||
{
|
||||
private ChannelReader<Task> _reader;
|
||||
private ChannelWriter<Task> _writer;
|
||||
private Queue<Task> _pendingTasks;
|
||||
private TaskFactory _taskFactory;
|
||||
|
||||
public void Run(Action action)
|
||||
{
|
||||
_taskFactory.StartNew(action);
|
||||
}
|
||||
|
||||
public Executor()
|
||||
{
|
||||
var channel = Channel.CreateUnbounded<Task>();
|
||||
_reader = channel.Reader;
|
||||
_writer = channel.Writer;
|
||||
|
||||
_taskFactory = new TaskFactory(this);
|
||||
_pendingTasks = new Queue<Task>();
|
||||
}
|
||||
|
||||
protected override IEnumerable<Task>? GetScheduledTasks()
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
protected override void QueueTask(Task task)
|
||||
{
|
||||
_writer.TryWrite(task);
|
||||
}
|
||||
|
||||
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
while (_reader.TryRead(out var task))
|
||||
{
|
||||
TryExecuteTask(task);
|
||||
|
||||
if (task.Status == TaskStatus.Running)
|
||||
_pendingTasks.Enqueue(task);
|
||||
}
|
||||
|
||||
while (_pendingTasks.TryDequeue(out var task))
|
||||
_writer.TryWrite(task);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Ragon.Server;
|
||||
|
||||
public struct NetworkConfiguration
|
||||
{
|
||||
public int LimitConnections { get; set; }
|
||||
public int Port { get; set; }
|
||||
public uint Protocol { get; set; }
|
||||
public string Address { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ragon.Protocol\Ragon.Protocol.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,18 +0,0 @@
|
||||
using System;
|
||||
using Game.Source;
|
||||
using Ragon.Core;
|
||||
|
||||
namespace SimpleServer
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var bootstrap = new Bootstrap();
|
||||
var app = bootstrap.Configure(new SimplePluginFactory());
|
||||
app.Start();
|
||||
Console.Read();
|
||||
app.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Ragon.Core;
|
||||
|
||||
namespace Game.Source;
|
||||
|
||||
public class AuthorizationProviderByKey: IAuthorizationProvider
|
||||
{
|
||||
private Configuration _configuration;
|
||||
public AuthorizationProviderByKey(Configuration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public async Task OnAuthorizationRequest(string key, string name, byte protocol, byte[] additionalData, Action<string, string> accept, Action<uint> reject)
|
||||
{
|
||||
if (key == _configuration.Key)
|
||||
{
|
||||
var playerId = Guid.NewGuid().ToString();
|
||||
var playerName = name;
|
||||
|
||||
accept(playerId, playerName);
|
||||
}
|
||||
else
|
||||
{
|
||||
reject(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
using Ragon.Core;
|
||||
|
||||
namespace Game.Source
|
||||
{
|
||||
public class SimplePlugin: PluginBase
|
||||
{
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
// _logger.Info("Plugin started");
|
||||
}
|
||||
|
||||
public override void OnStop()
|
||||
{
|
||||
// _logger.Info("Plugin stopped");
|
||||
}
|
||||
|
||||
public override void OnPlayerJoined(Player player)
|
||||
{
|
||||
// _logger.Info($"Player({player.PlayerName}) joined to Room({GameRoom.Id})");
|
||||
}
|
||||
|
||||
public override void OnPlayerLeaved(Player player)
|
||||
{
|
||||
// _logger.Info($"Player({player.PlayerName}) left from Room({GameRoom.Id})");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Ragon.Core;
|
||||
|
||||
namespace Game.Source
|
||||
{
|
||||
public class SimplePluginFactory : PluginFactory
|
||||
{
|
||||
public PluginBase CreatePlugin(string map)
|
||||
{
|
||||
return new SimplePlugin();
|
||||
}
|
||||
|
||||
public IAuthorizationProvider CreateAuthorizationProvider(Configuration configuration)
|
||||
{
|
||||
return new AuthorizationProviderByKey(configuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
{
|
||||
"key": "defaultkey",
|
||||
"statisticsInterval": 5,
|
||||
"sendRate": 30,
|
||||
"port": 4444,
|
||||
"skipTimeout": 60,
|
||||
"reconnectTimeout": 300,
|
||||
"maxConnections": 4095,
|
||||
"maxPlayersPerRoom": 20,
|
||||
"maxRooms": 200
|
||||
}
|
||||
@@ -1,206 +0,0 @@
|
||||
using System;
|
||||
using ENet;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Stress
|
||||
{
|
||||
class SimulationClient
|
||||
{
|
||||
public Host Host;
|
||||
public Peer Peer;
|
||||
public bool InRoom;
|
||||
public List<int> Entities = new List<int>();
|
||||
}
|
||||
|
||||
class SimulationThread
|
||||
{
|
||||
private List<SimulationClient> _clients = new List<SimulationClient>();
|
||||
|
||||
public void Start(string url, ushort port, int numClients)
|
||||
{
|
||||
for (var i = 0; i < numClients; i++)
|
||||
{
|
||||
var client = CreateClient(url, port);
|
||||
_clients.Add(client);
|
||||
}
|
||||
|
||||
var thread = new Thread(Execute);
|
||||
thread.IsBackground = true;
|
||||
thread.Start();
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
var ragonSerializer = new RagonSerializer();
|
||||
|
||||
while (true)
|
||||
{
|
||||
foreach (SimulationClient simulationClient in _clients)
|
||||
{
|
||||
bool polled = false;
|
||||
Event netEvent;
|
||||
|
||||
while (!polled)
|
||||
{
|
||||
if (simulationClient.Host.CheckEvents(out netEvent) <= 0)
|
||||
{
|
||||
if (simulationClient.Host.Service(0, out netEvent) <= 0)
|
||||
break;
|
||||
|
||||
polled = true;
|
||||
}
|
||||
|
||||
switch (netEvent.Type)
|
||||
{
|
||||
case EventType.None:
|
||||
break;
|
||||
case EventType.Connect:
|
||||
{
|
||||
ragonSerializer.Clear();
|
||||
ragonSerializer.WriteOperation(RagonOperation.AUTHORIZE);
|
||||
ragonSerializer.WriteString("defaultkey");
|
||||
ragonSerializer.WriteString("Player " + DateTime.Now.Ticks);
|
||||
ragonSerializer.WriteByte(0);
|
||||
|
||||
var sendData = ragonSerializer.ToArray();
|
||||
var packet = new Packet();
|
||||
packet.Create(sendData, PacketFlags.Reliable);
|
||||
simulationClient.Peer.Send(0, ref packet);
|
||||
Console.WriteLine("Client connected to server");
|
||||
break;
|
||||
}
|
||||
case EventType.Disconnect:
|
||||
Console.WriteLine("Client disconnected from server");
|
||||
break;
|
||||
case EventType.Timeout:
|
||||
Console.WriteLine("Client connection timeout");
|
||||
break;
|
||||
case EventType.Receive:
|
||||
var data = new byte[netEvent.Packet.Length];
|
||||
netEvent.Packet.CopyTo(data);
|
||||
|
||||
var op = (RagonOperation) data[0];
|
||||
switch (op)
|
||||
{
|
||||
case RagonOperation.AUTHORIZED_SUCCESS:
|
||||
{
|
||||
ragonSerializer.Clear();
|
||||
ragonSerializer.WriteOperation(RagonOperation.JOIN_OR_CREATE_ROOM);
|
||||
ragonSerializer.WriteString("map");
|
||||
ragonSerializer.WriteInt(1);
|
||||
ragonSerializer.WriteInt(5);
|
||||
|
||||
var sendData = ragonSerializer.ToArray();
|
||||
var packet = new Packet();
|
||||
packet.Create(sendData, PacketFlags.Reliable);
|
||||
simulationClient.Peer.Send(0, ref packet);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.JOIN_SUCCESS:
|
||||
{
|
||||
simulationClient.InRoom = true;
|
||||
|
||||
ragonSerializer.Clear();
|
||||
ragonSerializer.WriteOperation(RagonOperation.SCENE_IS_LOADED);
|
||||
|
||||
var sendData = ragonSerializer.ToArray();
|
||||
var packet = new Packet();
|
||||
packet.Create(sendData, PacketFlags.Reliable);
|
||||
simulationClient.Peer.Send(0, ref packet);
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonOperation.SNAPSHOT:
|
||||
{
|
||||
ragonSerializer.Clear();
|
||||
ragonSerializer.WriteOperation(RagonOperation.CREATE_ENTITY);
|
||||
ragonSerializer.WriteUShort(0);
|
||||
ragonSerializer.WriteUShort(0);
|
||||
ragonSerializer.WriteUShort(0);
|
||||
|
||||
var sendData = ragonSerializer.ToArray();
|
||||
var packet = new Packet();
|
||||
packet.Create(sendData, PacketFlags.Reliable);
|
||||
simulationClient.Peer.Send(0, ref packet);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.CREATE_ENTITY:
|
||||
{
|
||||
ReadOnlySpan<byte> payload = data.AsSpan().Slice(1, data.Length - 1);
|
||||
ragonSerializer.Clear();
|
||||
ragonSerializer.FromSpan(ref payload);
|
||||
|
||||
var entityType = ragonSerializer.ReadUShort();
|
||||
var state = ragonSerializer.ReadByte();
|
||||
var ennt = ragonSerializer.ReadByte();
|
||||
var entityId = ragonSerializer.ReadInt();
|
||||
|
||||
simulationClient.Entities.Add(entityId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Console.WriteLine(op);
|
||||
// Console.WriteLine("Packet received from server - Channel ID: " + netEvent.ChannelID + ", Data length: " + netEvent.Packet.Length);
|
||||
netEvent.Packet.Dispose();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (simulationClient.InRoom)
|
||||
{
|
||||
foreach (var entity in simulationClient.Entities)
|
||||
{
|
||||
ragonSerializer.Clear();
|
||||
ragonSerializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
ragonSerializer.WriteInt(entity);
|
||||
ragonSerializer.WriteInt(100);
|
||||
ragonSerializer.WriteInt(200);
|
||||
ragonSerializer.WriteInt(300);
|
||||
|
||||
var sendData = ragonSerializer.ToArray();
|
||||
var packet = new Packet();
|
||||
packet.Create(sendData, PacketFlags.Instant);
|
||||
simulationClient.Peer.Send(1, ref packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Thread.Sleep(33);
|
||||
}
|
||||
}
|
||||
|
||||
SimulationClient CreateClient(string url, ushort port)
|
||||
{
|
||||
Host client = new Host();
|
||||
Address address = new Address();
|
||||
|
||||
address.SetHost(url);
|
||||
address.Port = port;
|
||||
|
||||
client.Create();
|
||||
Console.WriteLine("Created client");
|
||||
|
||||
Peer peer = client.Connect(address);
|
||||
|
||||
return new SimulationClient() {Host = client, Peer = peer};
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Library.Initialize();
|
||||
|
||||
for (var i = 0; i < 80; i ++)
|
||||
{
|
||||
var thread = new SimulationThread();
|
||||
thread.Start("49.12.70.233", 4444, 50);
|
||||
Thread.Sleep(300);
|
||||
}
|
||||
|
||||
Console.ReadKey();
|
||||
Library.Deinitialize();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,16 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon", "Ragon\Ragon.csproj", "{BABA1AF0-CF91-43F2-9577-53800068ACCF}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Relay", "Ragon.Relay\Ragon.Relay.csproj", "{C2B87ADE-A122-4366-8EB7-24DAE11EBAF0}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.SimpleServer", "Ragon.SimpleServer\Ragon.SimpleServer.csproj", "{C2B87ADE-A122-4366-8EB7-24DAE11EBAF0}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Protocol", "Ragon.Protocol\Ragon.Protocol.csproj", "{F478B2A2-36F4-43B9-9BB7-382A57C449B2}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Common", "Ragon.Common\Ragon.Common.csproj", "{F478B2A2-36F4-43B9-9BB7-382A57C449B2}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Server", "Ragon.Server\Ragon.Server.csproj", "{45D3B686-8960-4656-91B2-6F8BFCCAA225}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Stress", "Ragon.Stress\Ragon.Stress.csproj", "{45E4C6A4-6AB5-4BEA-82DD-1F75C1648EC4}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Core", "Ragon.Core\Ragon.Core.csproj", "{F4AA86B9-2486-4B53-BA77-43D958A2FDC3}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Server.NativeWebSockets", "Ragon.Server.NativeWebSockets\Ragon.Server.NativeWebSockets.csproj", "{81050343-A9B8-487B-86C8-7A5B7DD9C39B}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Server.ENet", "Ragon.Server.ENet\Ragon.Server.ENet.csproj", "{DD79AC4F-9E5C-4938-850E-805D537E68D0}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -14,10 +18,6 @@ Global
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{BABA1AF0-CF91-43F2-9577-53800068ACCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{BABA1AF0-CF91-43F2-9577-53800068ACCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{BABA1AF0-CF91-43F2-9577-53800068ACCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{BABA1AF0-CF91-43F2-9577-53800068ACCF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{C2B87ADE-A122-4366-8EB7-24DAE11EBAF0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C2B87ADE-A122-4366-8EB7-24DAE11EBAF0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C2B87ADE-A122-4366-8EB7-24DAE11EBAF0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
@@ -26,9 +26,21 @@ Global
|
||||
{F478B2A2-36F4-43B9-9BB7-382A57C449B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F478B2A2-36F4-43B9-9BB7-382A57C449B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F478B2A2-36F4-43B9-9BB7-382A57C449B2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{45E4C6A4-6AB5-4BEA-82DD-1F75C1648EC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{45E4C6A4-6AB5-4BEA-82DD-1F75C1648EC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{45E4C6A4-6AB5-4BEA-82DD-1F75C1648EC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{45E4C6A4-6AB5-4BEA-82DD-1F75C1648EC4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{45D3B686-8960-4656-91B2-6F8BFCCAA225}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{45D3B686-8960-4656-91B2-6F8BFCCAA225}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{45D3B686-8960-4656-91B2-6F8BFCCAA225}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{45D3B686-8960-4656-91B2-6F8BFCCAA225}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F4AA86B9-2486-4B53-BA77-43D958A2FDC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F4AA86B9-2486-4B53-BA77-43D958A2FDC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F4AA86B9-2486-4B53-BA77-43D958A2FDC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F4AA86B9-2486-4B53-BA77-43D958A2FDC3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{81050343-A9B8-487B-86C8-7A5B7DD9C39B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{81050343-A9B8-487B-86C8-7A5B7DD9C39B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{81050343-A9B8-487B-86C8-7A5B7DD9C39B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{81050343-A9B8-487B-86C8-7A5B7DD9C39B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{DD79AC4F-9E5C-4938-850E-805D537E68D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{DD79AC4F-9E5C-4938-850E-805D537E68D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DD79AC4F-9E5C-4938-850E-805D537E68D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DD79AC4F-9E5C-4938-850E-805D537E68D0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<LangVersion>10</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<DefineConstants>TRACE;NETSTACK_SPAN</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<DefineConstants>TRACE;NETSTACK_SPAN</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ENet-CSharp" Version="2.4.8" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta1" />
|
||||
<PackageReference Include="NLog" Version="5.0.0-rc2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ragon.Common\Ragon.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,35 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using ENet;
|
||||
using NLog;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public class Application
|
||||
{
|
||||
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private readonly GameThread _gameThread;
|
||||
|
||||
public Application(PluginFactory factory, Configuration configuration)
|
||||
{
|
||||
_gameThread = new GameThread(factory, configuration);
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Library.Initialize();
|
||||
_gameThread.Start();
|
||||
_logger.Info("Started");
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_gameThread.Stop();
|
||||
Library.Deinitialize();
|
||||
_logger.Info("Stopped");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using NLog.Targets;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public class AuthorizationManager : IAuthorizationManager
|
||||
{
|
||||
private IAuthorizationProvider _provider;
|
||||
private IGameThread _gameThread;
|
||||
private Lobby _lobby;
|
||||
private RagonSerializer _serializer;
|
||||
private readonly Dictionary<uint, Player> _playersByPeers;
|
||||
private readonly Dictionary<string, Player> _playersByIds;
|
||||
|
||||
public AuthorizationManager(IAuthorizationProvider provider, IGameThread gameThread, Lobby lobby, RagonSerializer serializer)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_lobby = lobby;
|
||||
_provider = provider;
|
||||
_gameThread = gameThread;
|
||||
_playersByIds = new Dictionary<string, Player>();
|
||||
_playersByPeers = new Dictionary<uint, Player>();
|
||||
}
|
||||
|
||||
public void OnAuthorization(uint peerId, string key, string name, byte protocol)
|
||||
{
|
||||
var dispatcher = _gameThread.ThreadDispatcher;
|
||||
|
||||
_provider.OnAuthorizationRequest(key, name, protocol, Array.Empty<byte>(),
|
||||
(playerId, playerName) => { dispatcher.Dispatch(() => Accepted(peerId, playerId, playerName)); },
|
||||
(errorCode) => { dispatcher.Dispatch(() => Rejected(peerId, errorCode)); });
|
||||
}
|
||||
|
||||
public void Accepted(uint peerId, string playerId, string playerName)
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
|
||||
_serializer.WriteString(playerId);
|
||||
_serializer.WriteString(playerName);
|
||||
|
||||
var player = new Player()
|
||||
{
|
||||
Id = playerId,
|
||||
PlayerName = playerName,
|
||||
PeerId = peerId,
|
||||
IsLoaded = false,
|
||||
Entities = new List<Entity>(),
|
||||
EntitiesIds = new List<int>(),
|
||||
};
|
||||
|
||||
_playersByIds.Add(playerId, player);
|
||||
_playersByPeers.Add(peerId, player);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
_gameThread.Server.Send(peerId, sendData, DeliveryType.Reliable);
|
||||
}
|
||||
|
||||
public void Rejected(uint peerId, uint code)
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.AUTHORIZED_FAILED);
|
||||
_serializer.WriteInt((int) code);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
_gameThread.Server.Send(peerId, sendData, DeliveryType.Reliable);
|
||||
_gameThread.Server.Disconnect(peerId, 0);
|
||||
}
|
||||
|
||||
public void Cleanup(uint peerId)
|
||||
{
|
||||
if (_playersByPeers.Remove(peerId, out var player))
|
||||
_playersByIds.Remove(player.Id);
|
||||
}
|
||||
|
||||
public Player? GetPlayer(uint peerId)
|
||||
{
|
||||
if (_playersByPeers.TryGetValue(peerId, out var player))
|
||||
return player;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Player GetPlayer(string playerId)
|
||||
{
|
||||
return _playersByIds[playerId];
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Ragon.Core;
|
||||
|
||||
public interface IAuthorizationManager
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public interface IAuthorizationProvider
|
||||
{
|
||||
Task OnAuthorizationRequest(string key, string playerName, byte protocol, byte[] additionalData, Action<string, string> Accept, Action<uint> Reject);
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using NLog;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public class Bootstrap
|
||||
{
|
||||
private ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public Application Configure(PluginFactory factory)
|
||||
{
|
||||
_logger.Info("Configure application...");
|
||||
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json");
|
||||
var configuration = ConfigurationLoader.Load(filePath);
|
||||
var app = new Application(factory, configuration);
|
||||
return app;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
[Serializable]
|
||||
public struct Configuration
|
||||
{
|
||||
public string Key;
|
||||
public int StatisticsInterval;
|
||||
public ushort SendRate;
|
||||
public ushort Port;
|
||||
public int SkipTimeout;
|
||||
public int ReconnectTimeout;
|
||||
public int MaxConnections;
|
||||
public int MaxPlayersPerRoom;
|
||||
public int MaxRooms;
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Logger = NLog.Logger;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public static class ConfigurationLoader
|
||||
{
|
||||
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private static readonly string _serverVersion = "1.0.9-rc";
|
||||
|
||||
private static void CopyrightInfo()
|
||||
{
|
||||
_logger.Info($"Server Version: {_serverVersion}");
|
||||
_logger.Info($"Machine Name: {Environment.MachineName}");
|
||||
_logger.Info($"OS: {Environment.OSVersion}");
|
||||
_logger.Info($"Processors: {Environment.ProcessorCount}");
|
||||
_logger.Info($"Runtime Version: {Environment.Version}");
|
||||
|
||||
_logger.Info("==================================");
|
||||
_logger.Info("= =");
|
||||
_logger.Info($"={"Ragon".PadBoth(32)}=");
|
||||
_logger.Info("= =");
|
||||
_logger.Info("==================================");
|
||||
}
|
||||
|
||||
public static Configuration Load(string filePath)
|
||||
{
|
||||
CopyrightInfo();
|
||||
|
||||
var data = File.ReadAllText(filePath);
|
||||
var configuration = JsonConvert.DeserializeObject<Configuration>(data);
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public class Entity
|
||||
{
|
||||
private static int _idGenerator = 0;
|
||||
public int EntityId { get; private set; }
|
||||
public int StaticId { get; private set; }
|
||||
public uint OwnerId { get; private set; }
|
||||
public ushort EntityType { get; private set; }
|
||||
public RagonAuthority Authority { get; private set; }
|
||||
public EntityState State { get; private set; }
|
||||
public EntityState Payload { get; private set; }
|
||||
|
||||
public Entity(uint ownerId, ushort entityType, int staticId, RagonAuthority stateAuthority, RagonAuthority eventAuthority)
|
||||
{
|
||||
OwnerId = ownerId;
|
||||
StaticId = staticId;
|
||||
EntityType = entityType;
|
||||
EntityId = _idGenerator++;
|
||||
State = new EntityState(stateAuthority);
|
||||
Payload = new EntityState(stateAuthority);
|
||||
Authority = eventAuthority;
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public class EntityState
|
||||
{
|
||||
public bool isDirty { get; private set; }
|
||||
public RagonAuthority Authority { get; private set; }
|
||||
public int Size => _size;
|
||||
|
||||
private int _size = 0;
|
||||
private byte[] _data = new byte[2048];
|
||||
|
||||
public EntityState(RagonAuthority ragonAuthority)
|
||||
{
|
||||
Authority = ragonAuthority;
|
||||
isDirty = false;
|
||||
}
|
||||
|
||||
public ReadOnlySpan<byte> Read()
|
||||
{
|
||||
return _data.AsSpan().Slice(0, _size);
|
||||
}
|
||||
|
||||
public void Write(ref ReadOnlySpan<byte> src)
|
||||
{
|
||||
src.CopyTo(_data);
|
||||
_size = src.Length;
|
||||
isDirty = true;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
isDirty = false;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static string PadBoth(this string str, int length)
|
||||
{
|
||||
int spaces = length - str.Length;
|
||||
int padLeft = spaces / 2 + str.Length;
|
||||
return str.PadLeft(padLeft).PadRight(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public static class ValueExtensions
|
||||
{
|
||||
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
|
||||
{
|
||||
if (val.CompareTo(min) < 0) return min;
|
||||
else if (val.CompareTo(max) > 0) return max;
|
||||
else return val;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,457 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public class GameRoom : IGameRoom
|
||||
{
|
||||
public int PlayersMin { get; private set; }
|
||||
public int PlayersMax { get; private set; }
|
||||
public int PlayersCount => _players.Count;
|
||||
public string Id { get; private set; }
|
||||
public string Map { get; private set; }
|
||||
|
||||
private ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||
private Dictionary<uint, Player> _players = new();
|
||||
private Dictionary<int, Entity> _entities = new();
|
||||
private uint _owner;
|
||||
|
||||
private readonly IScheduler _scheduler;
|
||||
private readonly IGameThread _gameThread;
|
||||
private readonly PluginBase _plugin;
|
||||
private readonly RagonSerializer _serializer = new(512);
|
||||
|
||||
// Cache
|
||||
private uint[] _readyPlayers = Array.Empty<uint>();
|
||||
private uint[] _allPlayers = Array.Empty<uint>();
|
||||
private Entity[] _entitiesAll = Array.Empty<Entity>();
|
||||
|
||||
public GameRoom(IGameThread gameThread, PluginBase pluginBase, string map, int min, int max)
|
||||
{
|
||||
_gameThread = gameThread;
|
||||
_plugin = pluginBase;
|
||||
_scheduler = new Scheduler();
|
||||
|
||||
Map = map;
|
||||
PlayersMin = min;
|
||||
PlayersMax = max;
|
||||
Id = Guid.NewGuid().ToString();
|
||||
|
||||
_plugin.Attach(this);
|
||||
}
|
||||
|
||||
public void Joined(Player player, ReadOnlySpan<byte> payload)
|
||||
{
|
||||
if (_players.Count == 0)
|
||||
{
|
||||
_owner = player.PeerId;
|
||||
}
|
||||
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.PLAYER_JOINED);
|
||||
_serializer.WriteUShort((ushort) player.PeerId);
|
||||
_serializer.WriteString(player.Id);
|
||||
_serializer.WriteString(player.PlayerName);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
}
|
||||
|
||||
_players.Add(player.PeerId, player);
|
||||
_allPlayers = _players.Select(p => p.Key).ToArray();
|
||||
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
||||
_serializer.WriteString(Id);
|
||||
_serializer.WriteString(player.Id);
|
||||
_serializer.WriteString(GetOwner().Id);
|
||||
_serializer.WriteUShort((ushort) PlayersMin);
|
||||
_serializer.WriteUShort((ushort) PlayersMax);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Send(player.PeerId, sendData, DeliveryType.Reliable);
|
||||
}
|
||||
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.LOAD_SCENE);
|
||||
_serializer.WriteString(Map);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Send(player.PeerId, sendData, DeliveryType.Reliable);
|
||||
}
|
||||
}
|
||||
|
||||
public void Leave(uint peerId)
|
||||
{
|
||||
if (_players.Remove(peerId, out var player))
|
||||
{
|
||||
_allPlayers = _players.Select(p => p.Key).ToArray();
|
||||
_readyPlayers = _players.Where(p => p.Value.IsLoaded).Select(p => p.Key).ToArray();
|
||||
|
||||
var isOwnershipChange = player.PeerId == _owner;
|
||||
|
||||
{
|
||||
_plugin.OnPlayerLeaved(player);
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.PLAYER_LEAVED);
|
||||
_serializer.WriteString(player.Id);
|
||||
|
||||
_serializer.WriteUShort((ushort) player.EntitiesIds.Count);
|
||||
foreach (var entityId in player.EntitiesIds)
|
||||
{
|
||||
_serializer.WriteInt(entityId);
|
||||
_entities.Remove(entityId);
|
||||
}
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_readyPlayers, sendData);
|
||||
}
|
||||
|
||||
if (_allPlayers.Length > 0 && isOwnershipChange)
|
||||
{
|
||||
var newRoomOwnerId = _allPlayers[0];
|
||||
var newRoomOwner = _players[newRoomOwnerId];
|
||||
|
||||
_owner = newRoomOwnerId;
|
||||
|
||||
{
|
||||
_plugin.OnOwnershipChanged(newRoomOwner);
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.OWNERSHIP_CHANGED);
|
||||
_serializer.WriteString(newRoomOwner.Id);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_readyPlayers, sendData);
|
||||
}
|
||||
}
|
||||
|
||||
_entitiesAll = _entities.Values.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public void ProcessEvent(uint peerId, ReadOnlySpan<byte> rawData)
|
||||
{
|
||||
var operation = (RagonOperation) rawData[0];
|
||||
var payloadRawData = rawData.Slice(1, rawData.Length - 1);
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.FromSpan(ref payloadRawData);
|
||||
|
||||
switch (operation)
|
||||
{
|
||||
case RagonOperation.REPLICATE_ENTITY_STATE:
|
||||
{
|
||||
var entityId = _serializer.ReadInt();
|
||||
if (_entities.TryGetValue(entityId, out var ent))
|
||||
{
|
||||
if (ent.State.Authority == RagonAuthority.OWNER_ONLY && ent.OwnerId != peerId)
|
||||
return;
|
||||
|
||||
var entityStateData = _serializer.ReadData(_serializer.Size);
|
||||
ent.State.Write(ref entityStateData);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonOperation.REPLICATE_ENTITY_EVENT:
|
||||
{
|
||||
var evntId = _serializer.ReadUShort();
|
||||
var evntMode = _serializer.ReadByte();
|
||||
var entityId = _serializer.ReadInt();
|
||||
|
||||
if (!_entities.TryGetValue(entityId, out var ent))
|
||||
return;
|
||||
|
||||
if (ent.Authority == RagonAuthority.OWNER_ONLY && ent.OwnerId != peerId)
|
||||
return;
|
||||
|
||||
Span<byte> payloadRaw = stackalloc byte[_serializer.Size];
|
||||
var payloadData = _serializer.ReadData(_serializer.Size);
|
||||
payloadData.CopyTo(payloadRaw);
|
||||
|
||||
ReadOnlySpan<byte> payload = payloadRaw;
|
||||
if (_plugin.InternalHandle(peerId, entityId, evntId, ref payload))
|
||||
return;
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
_serializer.WriteUShort(evntId);
|
||||
_serializer.WriteUShort((ushort) peerId);
|
||||
_serializer.WriteByte(evntMode);
|
||||
_serializer.WriteInt(entityId);
|
||||
_serializer.WriteData(ref payload);
|
||||
var sendData = _serializer.ToArray();
|
||||
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.REPLICATE_EVENT:
|
||||
{
|
||||
var evntId = _serializer.ReadUShort();
|
||||
var evntMode = _serializer.ReadByte();
|
||||
Span<byte> payloadRaw = stackalloc byte[_serializer.Size];
|
||||
var payloadData = _serializer.ReadData(_serializer.Size);
|
||||
payloadData.CopyTo(payloadRaw);
|
||||
|
||||
ReadOnlySpan<byte> payload = payloadRaw;
|
||||
if (_plugin.InternalHandle(peerId, evntId, ref payload))
|
||||
return;
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.REPLICATE_EVENT);
|
||||
_serializer.WriteUShort((ushort) peerId);
|
||||
_serializer.WriteByte(evntMode);
|
||||
_serializer.WriteUShort(evntId);
|
||||
_serializer.WriteData(ref payload);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.CREATE_STATIC_ENTITY:
|
||||
{
|
||||
var entityType = _serializer.ReadUShort();
|
||||
var staticId = _serializer.ReadUShort();
|
||||
var stateAuthority = (RagonAuthority) _serializer.ReadByte();
|
||||
var eventAuthority = (RagonAuthority) _serializer.ReadByte();
|
||||
var entity = new Entity(peerId, entityType, staticId, stateAuthority, eventAuthority);
|
||||
|
||||
{
|
||||
var entityPayload = _serializer.ReadData(_serializer.Size);
|
||||
entity.Payload.Write(ref entityPayload);
|
||||
}
|
||||
|
||||
var player = _players[peerId];
|
||||
player.Entities.Add(entity);
|
||||
player.EntitiesIds.Add(entity.EntityId);
|
||||
|
||||
var ownerId = (ushort) peerId;
|
||||
|
||||
_entities.Add(entity.EntityId, entity);
|
||||
_entitiesAll = _entities.Values.ToArray();
|
||||
|
||||
_plugin.OnEntityCreated(player, entity);
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.CREATE_STATIC_ENTITY);
|
||||
_serializer.WriteUShort(entityType);
|
||||
_serializer.WriteUShort(staticId);
|
||||
_serializer.WriteByte((byte) stateAuthority);
|
||||
_serializer.WriteByte((byte) eventAuthority);
|
||||
_serializer.WriteInt(entity.EntityId);
|
||||
_serializer.WriteUShort(ownerId);
|
||||
|
||||
{
|
||||
var entityPayload = entity.Payload.Read();
|
||||
_serializer.WriteData(ref entityPayload);
|
||||
}
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.CREATE_ENTITY:
|
||||
{
|
||||
var entityType = _serializer.ReadUShort();
|
||||
var stateAuthority = (RagonAuthority) _serializer.ReadByte();
|
||||
var eventAuthority = (RagonAuthority) _serializer.ReadByte();
|
||||
var entity = new Entity(peerId, entityType, -1, stateAuthority, eventAuthority);
|
||||
|
||||
{
|
||||
var entityPayload = _serializer.ReadData(_serializer.Size);
|
||||
entity.Payload.Write(ref entityPayload);
|
||||
}
|
||||
|
||||
var player = _players[peerId];
|
||||
player.Entities.Add(entity);
|
||||
player.EntitiesIds.Add(entity.EntityId);
|
||||
|
||||
var ownerId = (ushort) peerId;
|
||||
|
||||
_entities.Add(entity.EntityId, entity);
|
||||
_entitiesAll = _entities.Values.ToArray();
|
||||
|
||||
_plugin.OnEntityCreated(player, entity);
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.CREATE_ENTITY);
|
||||
_serializer.WriteUShort(entityType);
|
||||
_serializer.WriteByte((byte) stateAuthority);
|
||||
_serializer.WriteByte((byte) eventAuthority);
|
||||
_serializer.WriteInt(entity.EntityId);
|
||||
_serializer.WriteUShort(ownerId);
|
||||
|
||||
{
|
||||
var entityPayload = entity.Payload.Read();
|
||||
_serializer.WriteData(ref entityPayload);
|
||||
}
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.DESTROY_ENTITY:
|
||||
{
|
||||
var entityId = _serializer.ReadInt();
|
||||
if (_entities.TryGetValue(entityId, out var entity))
|
||||
{
|
||||
if (entity.Authority == RagonAuthority.OWNER_ONLY && entity.OwnerId != peerId)
|
||||
return;
|
||||
|
||||
var player = _players[peerId];
|
||||
var destroyPayload = _serializer.ReadData(_serializer.Size);
|
||||
|
||||
player.Entities.Remove(entity);
|
||||
player.EntitiesIds.Remove(entity.EntityId);
|
||||
|
||||
_entities.Remove(entityId);
|
||||
_entitiesAll = _entities.Values.ToArray();
|
||||
|
||||
_plugin.OnEntityDestroyed(player, entity);
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.DESTROY_ENTITY);
|
||||
_serializer.WriteInt(entityId);
|
||||
_serializer.WriteData(ref destroyPayload);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonOperation.SCENE_IS_LOADED:
|
||||
{
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.SNAPSHOT);
|
||||
|
||||
_serializer.WriteInt(_allPlayers.Length);
|
||||
foreach (var playerPeerId in _allPlayers)
|
||||
{
|
||||
_serializer.WriteString(_players[playerPeerId].Id);
|
||||
_serializer.WriteUShort((ushort) playerPeerId);
|
||||
_serializer.WriteString(_players[playerPeerId].PlayerName);
|
||||
}
|
||||
|
||||
var dynamicCount = _entitiesAll.Where(e => e.StaticId == -1).ToArray();
|
||||
_serializer.WriteInt(dynamicCount.Length);
|
||||
foreach (var entity in dynamicCount)
|
||||
{
|
||||
if (entity.StaticId != -1) continue;
|
||||
|
||||
var payload = entity.Payload.Read();
|
||||
var state = entity.State.Read();
|
||||
|
||||
_serializer.WriteInt(entity.EntityId);
|
||||
_serializer.WriteByte((byte) entity.State.Authority);
|
||||
_serializer.WriteByte((byte) entity.Authority);
|
||||
_serializer.WriteUShort(entity.EntityType);
|
||||
_serializer.WriteUShort((ushort) entity.OwnerId);
|
||||
_serializer.WriteUShort((ushort) payload.Length);
|
||||
_serializer.WriteData(ref payload);
|
||||
_serializer.WriteUShort((ushort) state.Length);
|
||||
_serializer.WriteData(ref state);
|
||||
}
|
||||
|
||||
var staticCount = _entitiesAll.Where(e => e.StaticId != -1).ToArray();
|
||||
_serializer.WriteInt(staticCount.Length);
|
||||
foreach (var entity in staticCount)
|
||||
{
|
||||
var payload = entity.Payload.Read();
|
||||
var state = entity.State.Read();
|
||||
|
||||
_serializer.WriteInt(entity.EntityId);
|
||||
_serializer.WriteUShort((ushort) entity.StaticId);
|
||||
_serializer.WriteByte((byte) entity.State.Authority);
|
||||
_serializer.WriteByte((byte) entity.Authority);
|
||||
_serializer.WriteUShort(entity.EntityType);
|
||||
_serializer.WriteUShort((ushort) entity.OwnerId);
|
||||
_serializer.WriteUShort((ushort) payload.Length);
|
||||
_serializer.WriteData(ref payload);
|
||||
_serializer.WriteUShort((ushort) state.Length);
|
||||
_serializer.WriteData(ref state);
|
||||
}
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Send(peerId, sendData, DeliveryType.Reliable);
|
||||
|
||||
_players[peerId].IsLoaded = true;
|
||||
_readyPlayers = _players.Where(p => p.Value.IsLoaded).Select(p => p.Key).ToArray();
|
||||
|
||||
_plugin.OnPlayerJoined(_players[peerId]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Tick(float deltaTime)
|
||||
{
|
||||
_scheduler.Tick(deltaTime);
|
||||
|
||||
foreach (var entity in _entitiesAll)
|
||||
{
|
||||
if (entity.State.isDirty)
|
||||
{
|
||||
var state = entity.State.Read();
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
_serializer.WriteInt(entity.EntityId);
|
||||
_serializer.WriteData(ref state);
|
||||
|
||||
var sendData = _serializer.ToArray();
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Unreliable);
|
||||
|
||||
entity.State.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
_plugin.OnStart();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
foreach (var peerId in _allPlayers)
|
||||
_gameThread.Server.Disconnect(peerId, 0);
|
||||
|
||||
_plugin.OnStop();
|
||||
_plugin.Detach();
|
||||
}
|
||||
|
||||
public Player GetPlayerById(uint peerId) => _players[peerId];
|
||||
|
||||
public Entity GetEntityById(int entityId) => _entities[entityId];
|
||||
|
||||
public Player GetOwner() => _players[_owner];
|
||||
|
||||
public IDispatcher GetThreadDispatcher() => _gameThread.ThreadDispatcher;
|
||||
|
||||
public IScheduler GetScheduler() => _scheduler;
|
||||
|
||||
public void Send(uint peerId, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable)
|
||||
{
|
||||
_gameThread.Server.Send(peerId, rawData, deliveryType);
|
||||
}
|
||||
|
||||
public void Broadcast(uint[] peersIds, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable)
|
||||
{
|
||||
_gameThread.Server.Broadcast(peersIds, rawData, deliveryType);
|
||||
}
|
||||
|
||||
public void Broadcast(byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable)
|
||||
{
|
||||
_gameThread.Server.Broadcast(_allPlayers, rawData, deliveryType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using ENet;
|
||||
using NLog;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public class GameThread : IGameThread, IHandler
|
||||
{
|
||||
private readonly RoomManager _roomManager;
|
||||
private readonly Thread _thread;
|
||||
private readonly Stopwatch _gameLoopTimer;
|
||||
private readonly Lobby _lobby;
|
||||
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||
private readonly float _deltaTime = 0.0f;
|
||||
private readonly Stopwatch _statisticsTimer;
|
||||
private readonly Configuration _configuration;
|
||||
private readonly IDispatcherInternal _dispatcherInternal;
|
||||
|
||||
public IDispatcher ThreadDispatcher { get; private set; }
|
||||
public ISocketServer Server { get; private set; }
|
||||
|
||||
public GameThread(PluginFactory factory, Configuration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
|
||||
var authorizationProvider = factory.CreateAuthorizationProvider(configuration);
|
||||
var dispatcher = new Dispatcher();
|
||||
_dispatcherInternal = dispatcher;
|
||||
|
||||
ThreadDispatcher = dispatcher;
|
||||
Server = new ENetServer(this);
|
||||
|
||||
_deltaTime = 1000.0f / configuration.SendRate;
|
||||
|
||||
_roomManager = new RoomManager(factory, this);
|
||||
_lobby = new Lobby(authorizationProvider, _roomManager, this);
|
||||
|
||||
_gameLoopTimer = new Stopwatch();
|
||||
_statisticsTimer = new Stopwatch();
|
||||
|
||||
_thread = new Thread(Execute);
|
||||
_thread.Name = "Game Thread";
|
||||
_thread.IsBackground = true;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Server.Start(_configuration.Port, _configuration.MaxConnections);
|
||||
|
||||
_gameLoopTimer.Start();
|
||||
_statisticsTimer.Start();
|
||||
_thread.Start();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
Server.Stop();
|
||||
|
||||
_gameLoopTimer.Stop();
|
||||
_statisticsTimer.Stop();
|
||||
_thread.Interrupt();
|
||||
}
|
||||
|
||||
private void Execute()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Server.Process();
|
||||
|
||||
_dispatcherInternal.Process();
|
||||
|
||||
var elapsedMilliseconds = _gameLoopTimer.ElapsedMilliseconds;
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void OnEvent(Event evnt)
|
||||
{
|
||||
if (evnt.Type == EventType.Timeout || evnt.Type == EventType.Disconnect)
|
||||
{
|
||||
var player = _lobby.AuthorizationManager.GetPlayer(evnt.Peer.ID);
|
||||
if (player != null)
|
||||
_roomManager.Left(player, Array.Empty<byte>());
|
||||
|
||||
_lobby.OnDisconnected(evnt.Peer.ID);
|
||||
}
|
||||
|
||||
if (evnt.Type == EventType.Receive)
|
||||
{
|
||||
try
|
||||
{
|
||||
var peerId = evnt.Peer.ID;
|
||||
var dataRaw = new byte[evnt.Packet.Length];
|
||||
evnt.Packet.CopyTo(dataRaw);
|
||||
|
||||
var data = new ReadOnlySpan<byte>(dataRaw);
|
||||
if (_roomManager.RoomsBySocket.TryGetValue(peerId, out var room))
|
||||
{
|
||||
room.ProcessEvent(peerId, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
_lobby.ProcessEvent(peerId, data);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
_logger.Error(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
namespace Ragon.Core;
|
||||
|
||||
public interface IGameRoom
|
||||
{
|
||||
public string Id { get; }
|
||||
public string Map { get; }
|
||||
public int PlayersMin { get; }
|
||||
public int PlayersMax { get; }
|
||||
public int PlayersCount { get; }
|
||||
|
||||
public Player GetPlayerById(uint peerId);
|
||||
public Entity GetEntityById(int entityId);
|
||||
public Player GetOwner();
|
||||
public IDispatcher GetThreadDispatcher();
|
||||
public IScheduler GetScheduler();
|
||||
|
||||
public void Send(uint peerId, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable);
|
||||
public void Broadcast(uint[] peersIds, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable);
|
||||
public void Broadcast(byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public interface IGameThread
|
||||
{
|
||||
public IDispatcher ThreadDispatcher { get; }
|
||||
public ISocketServer Server { get; }
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Ragon.Core;
|
||||
|
||||
public interface ILobby
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public class Lobby : ILobby
|
||||
{
|
||||
private readonly RagonSerializer _serializer;
|
||||
private readonly RoomManager _roomManager;
|
||||
private readonly AuthorizationManager _authorizationManager;
|
||||
|
||||
public AuthorizationManager AuthorizationManager => _authorizationManager;
|
||||
|
||||
public Lobby(IAuthorizationProvider provider, RoomManager manager, IGameThread gameThread)
|
||||
{
|
||||
_roomManager = manager;
|
||||
_serializer = new RagonSerializer();
|
||||
_authorizationManager = new AuthorizationManager(provider, gameThread, this, _serializer);
|
||||
}
|
||||
|
||||
public void ProcessEvent(uint peerId, ReadOnlySpan<byte> data)
|
||||
{
|
||||
var op = (RagonOperation) data[0];
|
||||
var payload = data.Slice(1, data.Length - 1);
|
||||
|
||||
_serializer.Clear();
|
||||
_serializer.FromSpan(ref payload);
|
||||
|
||||
switch (op)
|
||||
{
|
||||
case RagonOperation.AUTHORIZE:
|
||||
{
|
||||
var key = _serializer.ReadString();
|
||||
var playerName = _serializer.ReadString();
|
||||
var protocol = _serializer.ReadByte();
|
||||
_authorizationManager.OnAuthorization(peerId, key, playerName, protocol);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.JOIN_ROOM:
|
||||
{
|
||||
var roomId = _serializer.ReadString();
|
||||
var player = _authorizationManager.GetPlayer(peerId);
|
||||
if (player != null)
|
||||
_roomManager.Join(player, roomId, Array.Empty<byte>());
|
||||
break;
|
||||
}
|
||||
case RagonOperation.JOIN_OR_CREATE_ROOM:
|
||||
{
|
||||
var min = _serializer.ReadUShort();
|
||||
var max = _serializer.ReadUShort();
|
||||
var map = _serializer.ReadString();
|
||||
var player = _authorizationManager.GetPlayer(peerId);
|
||||
if (player != null)
|
||||
_roomManager.JoinOrCreate(player, map, min, max, Array.Empty<byte>());
|
||||
break;
|
||||
}
|
||||
case RagonOperation.LEAVE_ROOM:
|
||||
{
|
||||
var player = _authorizationManager.GetPlayer(peerId);
|
||||
if (player != null)
|
||||
_roomManager.Left(player, Array.Empty<byte>());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDisconnected(uint peerId)
|
||||
{
|
||||
_authorizationManager.Cleanup(peerId);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ragon.Core
|
||||
{
|
||||
public class Player
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public uint PeerId { get; set; }
|
||||
public string PlayerName { get; set; }
|
||||
public bool IsLoaded { get; set; }
|
||||
|
||||
public List<Entity> Entities;
|
||||
public List<int> EntitiesIds;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user