Compare commits

...

80 Commits

Author SHA1 Message Date
edmand46 b793ca3e68 chore: update default socket typoe 2022-12-25 07:34:22 -08:00
edmand46 3936e5c95b chore: update version 2022-12-25 03:16:59 -08:00
edmand46 e9418f4b22 fixed: websocket server wrong execution thread 2022-12-25 03:13:01 -08:00
edmand46 f34b05e6ff chore: update version libs 2022-12-24 01:47:49 -08:00
edmand46 a5a67963be feat: websockets 2022-12-20 12:20:52 -08:00
edmand46 ab85578ccf wip 2022-12-17 21:16:02 +04:00
edmand46 13044357a5 refactor: remove some warnings 2022-12-17 18:58:37 +04:00
edmand46 a9be230960 Merge branch 'develop'
# Conflicts:
#	Ragon/Sources/Application.cs
#	Ragon/Sources/Authorization.cs
#	Ragon/Sources/Configuration.cs
#	Ragon/Sources/Entity/Entity.cs
#	Ragon/Sources/GameRoom.cs
#	Ragon/Sources/IApplicationHandler.cs
#	Ragon/Sources/RoomManager.cs
#	Ragon/Sources/Server/ENet/ENetServer.cs
#	Ragon/Sources/Server/Http/WebSocketServer.cs
2022-12-17 14:08:28 +04:00
edmand46 5a4bf0c24e refactor: entity structure changes 2022-12-17 14:05:53 +04:00
edmand46 fa6ace4dc8 wip 2022-12-16 23:36:51 +04:00
edmand46 4d8ed1105a wip 2022-12-16 00:05:46 +04:00
edmand46 e2ef761bd7 fixed: windows server 2022-12-15 23:55:59 +04:00
edmand46 828112855f fixed: crash on send in websocket server 2022-12-04 23:01:05 +04:00
edmand46 06ff76fe0b fixed: second migration 2022-12-01 22:24:03 +04:00
edmand46 c92b5a5bc4 chore: update readme 2022-11-30 23:56:17 +04:00
edmand46 f83d3ea0c7 chore: update version 2022-11-13 18:54:23 +04:00
edmand46 3564eb2adc feat: added except invoker target 2022-11-13 18:53:56 +04:00
edmand46 3531432758 fixed: crash on concurrent authorization 2022-11-13 00:20:41 +04:00
edmand46 6bda468607 v1.0.23-rc 2022-10-24 22:19:50 +04:00
edmand46 7ddd52bf9d feat: added websocket support 2022-10-23 18:50:05 +04:00
edmand46 ecf3631163 Merge branch 'retry'
# Conflicts:
#	Ragon/Sources/Game/GameRoom.cs
2022-10-23 18:49:32 +04:00
edmand46 8a149eb3b4 fixed: added catch error websocket 2022-10-23 18:45:37 +04:00
edmand46 14ae5e8189 fixed: temporary scheduler 2022-10-22 22:58:15 +04:00
edmand46 b7e8327ca8 feat: websocket 2022-10-22 21:34:35 +04:00
edmand46 b9e79af9d8 fixed: player joined opcode 2022-10-21 22:19:21 +04:00
edmand46 545ec02ecc chore: update version 2022-10-21 00:05:01 +04:00
edmand46 73feb77169 fixed: wrong peerId in restored events 2022-10-21 00:04:33 +04:00
edmand46 cbcf1773aa wip 2022-10-16 18:27:40 +04:00
edmand46 5519ae7679 wip 2022-10-16 18:00:32 +04:00
edmand46 1558b5eefb wip 2022-10-16 17:14:32 +04:00
edmand46 aaa0e4a317 wip 2022-10-16 16:50:22 +04:00
edmand46 ff712dc094 wip 2022-10-16 16:22:27 +04:00
edmand46 c2c75cb513 fixed: double authorization request 2022-10-15 18:48:59 +04:00
edmand46 9a22566f79 fixed: additional data missed 2022-10-15 18:48:48 +04:00
edmand46 783d2ce922 chore: bump version 2022-10-02 16:13:53 +04:00
edmand46 5771ec738b fixed: ownership changing 2022-10-02 16:00:34 +04:00
edmand46 85081e1da7 fix: replication entity event 2022-09-11 15:05:32 +04:00
edmand46 dbaa5d9d92 chore: renaming enums 2022-09-10 10:25:45 +04:00
edmand46 54786c065d wip 2022-09-06 22:39:52 +04:00
edmand46 0b3a0dd1ac refactor: spawning entities with properties 2022-09-04 14:43:17 +04:00
edmand46 72ff37e94a bump version 2022-08-30 01:54:50 +04:00
edmand46 75dab9d16f fixed: on scene entities load 2022-08-30 01:53:47 +04:00
edmand46 25b0e54d13 fixed: changed flow of joining 2022-08-29 01:45:47 +04:00
edmand46 e4f664b557 refactor: renamed plugin api 2022-08-28 19:31:07 +04:00
edmand46 174a4c4422 bump version 2022-08-28 11:51:14 +04:00
edmand46 a51d7c73cd fixed: mistype 2022-08-28 11:46:30 +04:00
edmand46 957622a170 fix: on player leave logic 2022-08-28 00:18:26 +04:00
edmand46 4a07424293 feat: added checks for size of payload 2022-08-27 11:21:51 +04:00
edmand46 568a3282c3 fix: restoring properties 2022-08-27 10:51:57 +04:00
edmand46 6a71fe5fe0 feat: restore properties 2022-08-25 23:12:33 +04:00
edmand46 082e183989 chore: move copyright on top of license 2022-08-24 00:06:37 +04:00
edmand46 51c0482a40 fix: leave player logic 2022-08-21 00:15:07 +04:00
edmand46 9e16c53cad fix: gameloop optimization 2022-08-20 22:05:43 +04:00
edmand46 6ec29e5dcd 1.0.11-rc 2022-08-20 12:27:04 +04:00
edmand46 65c1d9c6d4 feat: added checks and new ragon serializer methods 2022-08-14 15:39:22 +04:00
edmand46 f2934bc8ee fix: static in snapshot 2022-08-14 12:36:12 +04:00
edmand46 81879d4fe2 fix: event replication 2022-08-14 11:31:56 +04:00
edmand46 2d89d267f5 Merge remote-tracking branch 'origin/main' 2022-08-14 11:17:59 +04:00
edmand46 34107cbd5f feat: added checking of owner 2022-08-14 11:17:22 +04:00
edmand46 ba2ce25aec chore: removed some logs 2022-08-14 11:14:11 +04:00
edmand46 ca17e7de83 Merge pull request #6 from edmand46/dev
feature: State -> Properties
2022-08-13 23:28:55 +04:00
edmand46 dff6dbcf97 feat: support static entity 2022-08-13 23:22:46 +04:00
edmand46 ae485f96d4 wip 2022-08-13 18:54:02 +04:00
edmand46 74904de16b Merge pull request #5 from edmand46/dev
Target of replication event
2022-07-31 19:13:36 +04:00
edmand46 655cf3503d feat: farget for replication 2022-07-31 19:06:56 +04:00
edmand46 8fd8558323 Merge pull request #4 from edmand46/dev
Remove NetStack dependency
2022-07-31 18:20:43 +04:00
edmand46 944cb65733 refactor: remove NetStack dependency 2022-07-31 18:02:17 +04:00
edmand46 877ebdcde2 Merge pull request #3 from edmand46/dev
Replication level
2022-07-31 09:37:35 +04:00
edmand46 e2032f381a refactor: added error message on creation or joining to room 2022-07-31 09:37:05 +04:00
edmand46 85336f998e refactor: allow set roomId from client, added base room parameters 2022-07-30 16:02:53 +04:00
edmand46 16b8d3a062 feat: replication level 2022-07-30 14:07:48 +04:00
edmand46 f72b4c0904 chore: updated readme.md 2022-07-13 22:44:51 +04:00
edmand46 19272609ef chore: updated license 2022-07-13 22:30:55 +04:00
edmand46 5f3b2d7ed8 - Added support entities on scene
- Improved performance
2022-07-13 07:56:44 +04:00
edmand46 773adeefb2 feat: added scheduler, minor fixes 2022-07-02 11:02:09 +04:00
edmand46 e674600308 fixed: removed outdated code 2022-06-25 23:00:00 +04:00
edmand46 4f587fa59c chore: update readme 2022-06-25 13:32:05 +04:00
edmand46 76caa840bd update version 2022-06-25 13:26:39 +04:00
edmand46 1bff47e56b chore: removed prerelease flag 2022-06-25 11:09:36 +04:00
edmand46 1e41b9f2eb refactor 2022-06-25 11:08:50 +04:00
118 changed files with 2627 additions and 4734 deletions
+5 -5
View File
@@ -19,7 +19,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: with:
tag_name: ${{ github.ref }} tag_name: ${{ github.ref }}
release_name: Ragon.SimpleServer-${{ github.ref }} release_name: Ragon.Relay-${{ github.ref }}
prerelease: true prerelease: true
build: build:
@@ -53,10 +53,10 @@ jobs:
- name: Build - name: Build
shell: bash shell: bash
run: | run: |
release_name="Ragon.SimpleServer-${{ env.TAG }}-${{ matrix.target }}" release_name="Ragon.Relay-${{ env.TAG }}-${{ matrix.target }}"
# Build everything # 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 # Pack files
7z a -tzip "${release_name}.zip" "./${release_name}/*" 7z a -tzip "${release_name}.zip" "./${release_name}/*"
@@ -69,6 +69,6 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: with:
upload_url: ${{ needs.release.outputs.upload_url }} upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: Ragon.SimpleServer-${{ env.TAG }}-${{ matrix.target }}.zip asset_path: Ragon.Relay-${{ env.TAG }}-${{ matrix.target }}.zip
asset_name: Ragon.SimpleServer-${{ env.TAG }}-${{ matrix.target }}.zip asset_name: Ragon.Relay-${{ env.TAG }}-${{ matrix.target }}.zip
asset_content_type: application/zip asset_content_type: application/zip
-275
View File
@@ -1,275 +0,0 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
namespace DisruptorUnity3d
{
/// <summary>
/// Implementation of the Disruptor pattern
/// </summary>
/// <typeparam name="T">the type of item to be stored</typeparam>
public class RingBuffer<T>
{
private readonly T[] _entries;
private readonly int _modMask;
private Volatile.PaddedLong _consumerCursor = new Volatile.PaddedLong();
private Volatile.PaddedLong _producerCursor = new Volatile.PaddedLong();
/// <summary>
/// Creates a new RingBuffer with the given capacity
/// </summary>
/// <param name="capacity">The capacity of the buffer</param>
/// <remarks>Only a single thread may attempt to consume at any one time</remarks>
public RingBuffer(int capacity)
{
capacity = NextPowerOfTwo(capacity);
_modMask = capacity - 1;
_entries = new T[capacity];
}
/// <summary>
/// The maximum number of items that can be stored
/// </summary>
public int Capacity
{
get { return _entries.Length; }
}
public T this[long index]
{
get { unchecked { return _entries[index & _modMask]; } }
set { unchecked { _entries[index & _modMask] = value; } }
}
/// <summary>
/// Removes an item from the buffer.
/// </summary>
/// <returns>The next available item</returns>
public T Dequeue()
{
var next = _consumerCursor.ReadAcquireFence() + 1;
while (_producerCursor.ReadAcquireFence() < next) // makes sure we read the data from _entries after we have read the producer cursor
{
Thread.SpinWait(1);
}
var result = this[next];
_consumerCursor.WriteReleaseFence(next); // makes sure we read the data from _entries before we update the consumer cursor
return result;
}
/// <summary>
/// Attempts to remove an items from the queue
/// </summary>
/// <param name="obj">the items</param>
/// <returns>True if successful</returns>
public bool TryDequeue(out T obj)
{
var next = _consumerCursor.ReadAcquireFence() + 1;
if (_producerCursor.ReadAcquireFence() < next)
{
obj = default(T);
return false;
}
obj = Dequeue();
return true;
}
/// <summary>
/// Add an item to the buffer
/// </summary>
/// <param name="item"></param>
public void Enqueue(T item)
{
var next = _producerCursor.ReadAcquireFence() + 1;
long wrapPoint = next - _entries.Length;
long min = _consumerCursor.ReadAcquireFence();
while (wrapPoint > min)
{
min = _consumerCursor.ReadAcquireFence();
Thread.SpinWait(1);
}
this[next] = item;
_producerCursor.WriteReleaseFence(next); // makes sure we write the data in _entries before we update the producer cursor
}
/// <summary>
/// The number of items in the buffer
/// </summary>
/// <remarks>for indicative purposes only, may contain stale data</remarks>
public int Count { get { return (int)(_producerCursor.ReadFullFence() - _consumerCursor.ReadFullFence()); } }
private static int NextPowerOfTwo(int x)
{
var result = 2;
while (result < x)
{
result <<= 1;
}
return result;
}
}
public static class Volatile
{
private const int CacheLineSize = 64;
[StructLayout(LayoutKind.Explicit, Size = CacheLineSize * 2)]
public struct PaddedLong
{
[FieldOffset(CacheLineSize)]
private long _value;
/// <summary>
/// Create a new <see cref="PaddedLong"/> with the given initial value.
/// </summary>
/// <param name="value">Initial value</param>
public PaddedLong(long value)
{
_value = value;
}
/// <summary>
/// Read the value without applying any fence
/// </summary>
/// <returns>The current value</returns>
public long ReadUnfenced()
{
return _value;
}
/// <summary>
/// Read the value applying acquire fence semantic
/// </summary>
/// <returns>The current value</returns>
public long ReadAcquireFence()
{
var value = _value;
Thread.MemoryBarrier();
return value;
}
/// <summary>
/// Read the value applying full fence semantic
/// </summary>
/// <returns>The current value</returns>
public long ReadFullFence()
{
Thread.MemoryBarrier();
return _value;
}
/// <summary>
/// Read the value applying a compiler only fence, no CPU fence is applied
/// </summary>
/// <returns>The current value</returns>
[MethodImpl(MethodImplOptions.NoOptimization)]
public long ReadCompilerOnlyFence()
{
return _value;
}
/// <summary>
/// Write the value applying release fence semantic
/// </summary>
/// <param name="newValue">The new value</param>
public void WriteReleaseFence(long newValue)
{
Thread.MemoryBarrier();
_value = newValue;
}
/// <summary>
/// Write the value applying full fence semantic
/// </summary>
/// <param name="newValue">The new value</param>
public void WriteFullFence(long newValue)
{
Thread.MemoryBarrier();
_value = newValue;
}
/// <summary>
/// Write the value applying a compiler fence only, no CPU fence is applied
/// </summary>
/// <param name="newValue">The new value</param>
[MethodImpl(MethodImplOptions.NoOptimization)]
public void WriteCompilerOnlyFence(long newValue)
{
_value = newValue;
}
/// <summary>
/// Write without applying any fence
/// </summary>
/// <param name="newValue">The new value</param>
public void WriteUnfenced(long newValue)
{
_value = newValue;
}
/// <summary>
/// Atomically set the value to the given updated value if the current value equals the comparand
/// </summary>
/// <param name="newValue">The new value</param>
/// <param name="comparand">The comparand (expected value)</param>
/// <returns></returns>
public bool AtomicCompareExchange(long newValue, long comparand)
{
return Interlocked.CompareExchange(ref _value, newValue, comparand) == comparand;
}
/// <summary>
/// Atomically set the value to the given updated value
/// </summary>
/// <param name="newValue">The new value</param>
/// <returns>The original value</returns>
public long AtomicExchange(long newValue)
{
return Interlocked.Exchange(ref _value, newValue);
}
/// <summary>
/// Atomically add the given value to the current value and return the sum
/// </summary>
/// <param name="delta">The value to be added</param>
/// <returns>The sum of the current value and the given value</returns>
public long AtomicAddAndGet(long delta)
{
return Interlocked.Add(ref _value, delta);
}
/// <summary>
/// Atomically increment the current value and return the new value
/// </summary>
/// <returns>The incremented value.</returns>
public long AtomicIncrementAndGet()
{
return Interlocked.Increment(ref _value);
}
/// <summary>
/// Atomically increment the current value and return the new value
/// </summary>
/// <returns>The decremented value.</returns>
public long AtomicDecrementAndGet()
{
return Interlocked.Decrement(ref _value);
}
/// <summary>
/// Returns the string representation of the current value.
/// </summary>
/// <returns>the string representation of the current value.</returns>
public override string ToString()
{
var value = ReadFullFence();
return value.ToString();
}
}
}
}
-69
View File
@@ -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
}
}
}
}
-84
View File
@@ -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;
}
}
}
-150
View File
@@ -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
View File
@@ -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);
}
}
-200
View File
@@ -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;
}
}
}
+6
View File
@@ -0,0 +1,6 @@
namespace Ragon.Core.Time;
public interface IAction
{
public void Tick();
}
+28
View File
@@ -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();
}
}
+115
View File
@@ -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);
}
}
+43
View File
@@ -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;
}
}
+188
View File
@@ -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;
}
}
}
}
+24
View File
@@ -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;
}
}
+34
View File
@@ -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;
}
}
+99
View File
@@ -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);
}
}
}
}
+41
View File
@@ -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;
}
}
+166
View File
@@ -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);
}
}
+13
View File
@@ -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}";
}
}
+36
View File
@@ -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;
}
}
+109
View File
@@ -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;
}
}
}
}
+8
View File
@@ -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}");
}
}
}
+47
View File
@@ -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);
}
}
}
+29
View File
@@ -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");
}
}
}
}
+84
View File
@@ -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);
}
}
+62
View File
@@ -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}");
}
}
+19
View File
@@ -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}");
}
}
}
+124
View File
@@ -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);
}
}
+12
View File
@@ -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);
}
+61
View File
@@ -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}");
}
}
+27
View File
@@ -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>();
}
}
+28
View File
@@ -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);
}
}
+21
View File
@@ -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> <Nullable>disable</Nullable>
<LangVersion>8</LangVersion> <LangVersion>8</LangVersion>
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>Ragon.Common</RootNamespace>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
@@ -18,8 +19,4 @@
<OutputPath></OutputPath> <OutputPath></OutputPath>
<DefineConstants>TRACE;NETSTACK_SPAN</DefineConstants> <DefineConstants>TRACE;NETSTACK_SPAN</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Folder Include="External" />
</ItemGroup>
</Project> </Project>
@@ -2,7 +2,8 @@ namespace Ragon.Common
{ {
public enum RagonAuthority: byte public enum RagonAuthority: byte
{ {
OWNER_ONLY, None,
ALL, OwnerOnly,
All,
} }
} }
@@ -5,27 +5,21 @@ namespace Ragon.Common
AUTHORIZE, AUTHORIZE,
AUTHORIZED_SUCCESS, AUTHORIZED_SUCCESS,
AUTHORIZED_FAILED, AUTHORIZED_FAILED,
JOIN_OR_CREATE_ROOM, JOIN_OR_CREATE_ROOM,
CREATE_ROOM,
JOIN_ROOM, JOIN_ROOM,
LEAVE_ROOM, LEAVE_ROOM,
OWNERSHIP_CHANGED, OWNERSHIP_CHANGED,
JOIN_SUCCESS, JOIN_SUCCESS,
JOIN_FAILED, JOIN_FAILED,
LOAD_SCENE, LOAD_SCENE,
SCENE_IS_LOADED, SCENE_LOADED,
PLAYER_JOINED, PLAYER_JOINED,
PLAYER_LEAVED, PLAYER_LEAVED,
CREATE_ENTITY, CREATE_ENTITY,
DESTROY_ENTITY, DESTROY_ENTITY,
SNAPSHOT, SNAPSHOT,
REPLICATE_ENTITY_STATE, REPLICATE_ENTITY_STATE,
REPLICATE_ENTITY_EVENT, 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);
}
}
+335
View File
@@ -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;
}
}
}
+11
View File
@@ -0,0 +1,11 @@
namespace Ragon.Common
{
public enum RagonTarget: byte
{
Owner,
ExceptOwner,
ExceptInvoker,
All,
Player,
}
}
+28
View File
@@ -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);
}
}
}
+28
View File
@@ -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"> <None Update="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Update="config.json"> <None Update="relay.config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Ragon\Ragon.csproj" /> <ProjectReference Include="..\Ragon.Core\Ragon.Core.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>
+10
View File
@@ -0,0 +1,10 @@
{
"serverKey": "defaultkey",
"serverType": "enet",
"serverTickRate": 20,
"gameProtocol": "1.0.0",
"port": 5000,
"limitConnections": 4095,
"limitPlayersPerRoom": 20,
"limitRooms": 200
}
+17
View File
@@ -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);
}
}
+23
View File
@@ -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);
}
}
+116
View File
@@ -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"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<RootNamespace>StressTest</RootNamespace> <RootNamespace>Ragon.ENet</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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>
<ItemGroup> <ItemGroup>
<PackageReference Include="ENet-CSharp" Version="2.4.8" /> <ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
</ItemGroup> </ItemGroup>
</Project> </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();
}
}
+6
View File
@@ -0,0 +1,6 @@
namespace Ragon.Server;
public interface INetworkChannel
{
void Send(byte[] data);
}
+8
View File
@@ -0,0 +1,8 @@
namespace Ragon.Server;
public interface INetworkConnection
{
public ushort Id { get; }
public INetworkChannel Reliable { get; }
public INetworkChannel Unreliable { get; }
}
+9
View File
@@ -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);
}
+8
View File
@@ -0,0 +1,8 @@
namespace Ragon.Server;
public interface INetworkServer
{
public void Stop();
public void Poll();
public void Start(INetworkListener listener, NetworkConfiguration configuration);
}
+55
View File
@@ -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);
}
}
+9
View File
@@ -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; }
}
+13
View File
@@ -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>
-18
View File
@@ -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);
}
}
}
-8
View File
@@ -1,8 +0,0 @@
{
"key": "defaultkey",
"tickRate": 30,
"skipTimeout": 60,
"server": {
"port": 4444
}
}
-258
View File
@@ -1,258 +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.WriteInt(2);
ragonSerializer.WriteInt(20);
ragonSerializer.WriteString("map");
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(16);
}
}
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();
{
var thread = new SimulationThread();
thread.Start("127.0.0.1", 4444, 250);
}
Thread.Sleep(3000);
{
var thread = new SimulationThread();
thread.Start("127.0.0.1", 4444, 250);
}
Thread.Sleep(3000);
{
var thread = new SimulationThread();
thread.Start("127.0.0.1", 4444, 250);
}
Thread.Sleep(3000);
{
var thread = new SimulationThread();
thread.Start("127.0.0.1", 4444, 250);
}
Thread.Sleep(3000);
{
var thread = new SimulationThread();
thread.Start("127.0.0.1", 4444, 250);
}
Thread.Sleep(3000);
{
var thread = new SimulationThread();
thread.Start("127.0.0.1", 4444, 250);
}
Thread.Sleep(3000);
{
var thread = new SimulationThread();
thread.Start("127.0.0.1", 4444, 250);
}
Thread.Sleep(3000);
{
var thread = new SimulationThread();
thread.Start("127.0.0.1", 4444, 250);
}
Thread.Sleep(3000);
Console.ReadKey();
Library.Deinitialize();
}
}
}
+24 -12
View File
@@ -1,12 +1,16 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 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 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 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 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -14,10 +18,6 @@ Global
Release|Any CPU = Release|Any CPU Release|Any CPU = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution 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.ActiveCfg = Debug|Any CPU
{C2B87ADE-A122-4366-8EB7-24DAE11EBAF0}.Debug|Any CPU.Build.0 = 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 {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}.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.ActiveCfg = Release|Any CPU
{F478B2A2-36F4-43B9-9BB7-382A57C449B2}.Release|Any CPU.Build.0 = 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 {45D3B686-8960-4656-91B2-6F8BFCCAA225}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45E4C6A4-6AB5-4BEA-82DD-1F75C1648EC4}.Debug|Any CPU.Build.0 = Debug|Any CPU {45D3B686-8960-4656-91B2-6F8BFCCAA225}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45E4C6A4-6AB5-4BEA-82DD-1F75C1648EC4}.Release|Any CPU.ActiveCfg = Release|Any CPU {45D3B686-8960-4656-91B2-6F8BFCCAA225}.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}.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 EndGlobalSection
EndGlobal EndGlobal
-29
View File
@@ -1,29 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>10</LangVersion>
<Nullable>enable</Nullable>
<TargetFrameworks>net6.0;netstandard2.1</TargetFrameworks>
</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>
-35
View File
@@ -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;
private readonly ENetServer _netServer;
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,89 +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.Dispatcher;
_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)
{
return _playersByPeers[peerId];
}
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);
}
-20
View File
@@ -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 Server
{
public ushort Port;
}
[Serializable]
public struct Configuration
{
public string Key;
public ushort TickRate;
public Server Server;
}
}
@@ -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.5-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,6 +0,0 @@
namespace Ragon.Core;
public interface Receiver<T>
{
public bool Receive(out T data);
}
@@ -1,6 +0,0 @@
namespace Ragon.Core;
public interface ISender<T>
{
public void Send(T data);
}
-21
View File
@@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
namespace Ragon.Core;
public class Dispatcher: IDispatcher
{
public Queue<DispatcherTask> _actions = new Queue<DispatcherTask>();
public void Dispatch(Action action)
{
lock (_actions)
_actions.Enqueue(new DispatcherTask() { Action = action });
}
public void Process()
{
lock(_actions)
while(_actions.TryDequeue(out var action))
action.Execute();
}
}
-15
View File
@@ -1,15 +0,0 @@
using System;
using System.Diagnostics;
namespace Ragon.Core;
public class DispatcherTask
{
public Action Action;
public Action Callback;
public void Execute()
{
Action?.Invoke();
}
}
-9
View File
@@ -1,9 +0,0 @@
using System;
namespace Ragon.Core;
public interface IDispatcher
{
public void Dispatch(Action action);
public void Process();
}
@@ -1,130 +0,0 @@
using System.Collections.Generic;
using System.Threading;
namespace Ragon.Core.Core.Utils;
public class SynchronizedCache
{
private ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
private Dictionary<int, string> innerCache = new Dictionary<int, string>();
public int Count
{ get { return innerCache.Count; } }
public string Read(int key)
{
cacheLock.EnterReadLock();
try
{
return innerCache[key];
}
finally
{
cacheLock.ExitReadLock();
}
}
public void Add(int key, string value)
{
cacheLock.EnterWriteLock();
try
{
innerCache.Add(key, value);
}
finally
{
cacheLock.ExitWriteLock();
}
}
public bool AddWithTimeout(int key, string value, int timeout)
{
if (cacheLock.TryEnterWriteLock(timeout))
{
try
{
innerCache.Add(key, value);
}
finally
{
cacheLock.ExitWriteLock();
}
return true;
}
else
{
return false;
}
}
public AddOrUpdateStatus AddOrUpdate(int key, string value)
{
cacheLock.EnterUpgradeableReadLock();
try
{
string result = null;
if (innerCache.TryGetValue(key, out result))
{
if (result == value)
{
return AddOrUpdateStatus.Unchanged;
}
else
{
cacheLock.EnterWriteLock();
try
{
innerCache[key] = value;
}
finally
{
cacheLock.ExitWriteLock();
}
return AddOrUpdateStatus.Updated;
}
}
else
{
cacheLock.EnterWriteLock();
try
{
innerCache.Add(key, value);
}
finally
{
cacheLock.ExitWriteLock();
}
return AddOrUpdateStatus.Added;
}
}
finally
{
cacheLock.ExitUpgradeableReadLock();
}
}
public void Delete(int key)
{
cacheLock.EnterWriteLock();
try
{
innerCache.Remove(key);
}
finally
{
cacheLock.ExitWriteLock();
}
}
public enum AddOrUpdateStatus
{
Added,
Updated,
Unchanged
};
~SynchronizedCache()
{
if (cacheLock != null) cacheLock.Dispose();
}
}
-24
View File
@@ -1,24 +0,0 @@
using Ragon.Common;
namespace Ragon.Core;
public class Entity
{
private static int _idGenerator = 0;
public int EntityId { 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, RagonAuthority stateAuthority, RagonAuthority eventAuthority)
{
OwnerId = ownerId;
EntityType = entityType;
EntityId = _idGenerator++;
State = new EntityState(stateAuthority);
Payload = new EntityState(stateAuthority);
Authority = eventAuthority;
}
}
-38
View File
@@ -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;
}
}
-7
View File
@@ -1,7 +0,0 @@
namespace Ragon.Core;
public enum DeliveryType
{
Unreliable,
Reliable
}
-12
View File
@@ -1,12 +0,0 @@
using System;
namespace Ragon.Core
{
public struct SocketEvent
{
public EventType Type;
public DeliveryType Delivery;
public byte[] Data;
public uint PeerId;
}
}

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