Compare commits

..

94 Commits

Author SHA1 Message Date
edmand46 b39bd8bd0a fix: updated workflows 2024-04-14 19:23:08 +03:00
edmand46 12b80c546c fix: updated workflows 2024-04-14 19:14:57 +03:00
edmand46 5c20fbafc1 Merge pull request #15 from edmand46/feat/rooms
feat: Room List
2024-04-13 18:27:28 +03:00
edmand46 bd0c6df5ea fix: timer, providing api for listeners 2024-04-13 18:25:39 +03:00
edmand46 c4811ef052 feat(wip): list rooms 2024-04-13 17:43:23 +03:00
edmand46 d82964526c feat(wip): room list support 2024-04-13 16:17:31 +03:00
edmand46 acedaef270 ♻️ unified event api for room and entities 2024-04-07 17:15:52 +03:00
edmand46 0c4bdb83c9 added new method for RagonBuffer 2024-04-07 17:11:45 +03:00
edmand46 5a79502848 RagonStream 2024-04-06 19:11:16 +03:00
edmand46 b8fe2bb13f 🐛 wrong condition in lobby in memory on finding room by id 2024-02-04 20:06:07 +03:00
edmand46 89d130a193 🐛 fixed double call on invoke local flag in ragon property 2024-01-21 12:20:47 +03:00
edmand46 0cd912a1aa 🎨 added null checks 2024-01-13 15:15:29 +03:00
edmand46 c64cc61c78 🐛 prediction spawning for wrong connection, payload capacity 2024-01-04 23:29:35 +03:00
edmand46 2dcb047014 chore: added logging 2023-11-05 22:19:55 +03:00
edmand46 33f8bba2ed fixed: remove exception on non exists player 2023-11-05 22:08:41 +03:00
edmand46 5aa159ed2f fixed: custom property wrong size 2023-11-05 21:53:57 +03:00
edmand46 892558ab16 chore: update version 2023-10-22 21:10:31 +03:00
edmand46 f55634877a 📝 update readme.md 2023-10-21 23:10:41 +03:00
edmand46 8b3a29a750 📝 update readme.md 2023-10-21 23:09:49 +03:00
edmand46 6a50bffe1e 📝 update readme.md 2023-10-21 23:03:55 +03:00
edmand46 9144ad58ef 📝 update readme.md 2023-10-21 23:03:37 +03:00
edmand46 ee9f3fbe3a 🐛 crash on abnormal disconnecting from websocket server 2023-10-19 20:20:40 +03:00
edmand46 893c73512a feat: allow control replication via code, fix null owner on OnEntityCreated 2023-10-16 23:37:35 +03:00
edmand46 e7dd693147 🐛 Event Payload 2023-10-14 23:50:25 +03:00
edmand46 fef1007c8c 🔖 update version 2023-10-14 21:48:33 +03:00
edmand46 e33c442a18 🐛 WebSocket buffer size 2023-10-14 21:35:51 +03:00
edmand46 09b185a3ad 🐛 checking size event 2023-10-13 12:10:46 +03:00
edmand46 7d154ea4d4 🎨 cleanup 2023-10-13 11:02:56 +03:00
edmand46 d2577e5d1f remove allocation on raw data replication 2023-10-13 10:58:15 +03:00
edmand46 e25f42f9ff Merge pull request #14 from edmand46/v1.3.0
Ragon v1.3.0
2023-10-12 17:41:30 +03:00
edmand46 28cc41c3ad 🎨 update project 2023-10-12 17:40:57 +03:00
edmand46 fc483c0854 🚑 crash on timeout disconnect 2023-10-12 16:09:24 +03:00
edmand46 bcc45f7db8 🐛 lost piece of data 2023-10-12 15:38:34 +03:00
edmand46 689a240e5b 🐛 lost piece of data 2023-10-12 15:35:40 +03:00
edmand46 85b75766a9 🎨 remove $ from logs 2023-10-12 11:21:21 +03:00
edmand46 b90ed974e5 🐛 checking authority on destroy object 2023-10-12 11:18:04 +03:00
edmand46 3da57e086e 🐛 channels more then capacity 2023-10-11 21:21:58 +03:00
edmand46 745d196a8b wip 2023-10-11 20:49:00 +03:00
edmand46 860051777e wip 2023-10-11 19:38:26 +03:00
edmand46 6422db783a wip 2023-10-11 19:37:50 +03:00
edmand46 5d812d7acc 🚧 clean up listeners 2023-10-09 09:17:43 +03:00
edmand46 c214b6ca7f wip 2023-10-08 21:13:31 +03:00
edmand46 64842886d7 multiple subscribers, and unsubscribe 2023-10-07 20:20:02 +03:00
edmand46 c892c2b67a 🐛 operation code for room 2023-10-07 19:33:55 +03:00
edmand46 e1a3ea45e2 🚧 pass-through raw data, refactoring 2023-10-07 19:30:52 +03:00
edmand46 8788cb0fcf wip 2023-10-04 14:42:59 +03:00
edmand46 27db256902 🐛 scene entities not execute buffered events 2023-09-03 13:29:37 +03:00
edmand46 8705e93929 🐛 event size 2023-08-02 22:13:51 +03:00
edmand46 08e931d1bd 🚑 remove static entity spawn 2023-08-01 22:21:21 +03:00
edmand46 fb58dedfaf changed scene loading flow 2023-07-30 21:46:42 +03:00
edmand46 92062cd708 🎨 update naming of fields 2023-07-30 21:19:39 +03:00
edmand46 5fc55eaddc wip 2023-07-30 21:14:14 +03:00
edmand46 4a8aae11e3 🚑 fixed empty state values 2023-07-30 17:44:51 +03:00
edmand46 cd9304e63a Merge pull request #13 from edmand46/fix/payload
Fix/payload
2023-07-30 17:01:08 +03:00
edmand46 5199b5271b 🐛 Remove listener inside callback 2023-07-30 16:56:11 +03:00
edmand46 c01b748031 wip 2023-07-29 10:58:06 +03:00
edmand46 0a8d761cc1 🐛 empty payload 2023-07-23 15:56:08 +03:00
edmand46 f38c7e98de ⬆️ deps 2023-07-23 11:23:38 +03:00
edmand46 0479a21980 feat: added safe get entity by id 2023-07-09 07:40:06 +03:00
edmand46 1406b17d62 🚑 independent buffers in properties 2023-07-01 08:12:40 +03:00
edmand46 105457ffa0 added transfer ownership, limit buffered events 2023-07-01 07:47:57 +03:00
edmand46 20662ae24d wip 2023-06-27 23:41:30 +03:00
edmand46 6c441d9dee feat: Entity.OnEvent now support resubscribing 2023-05-25 11:28:18 +03:00
edmand46 907bd2611e added reason of disconnect at callback 2023-05-08 00:16:43 +03:00
edmand46 91d8516ac9 independent buffers for ragon properties, extended buffer functionality 2023-05-07 23:54:07 +03:00
edmand46 ecdafeab00 ♻️ naming changing 2023-05-07 18:16:46 +03:00
edmand46 88baff9fee compressor extensions for write/read data and reduce boilerplate 2023-05-07 18:07:56 +03:00
edmand46 fdb41649b2 ♻️ added checks in client sdk 2023-05-07 12:46:39 +03:00
edmand46 aa607a7eb9 Update README.md 2023-05-06 15:30:16 +04:00
edmand46 efebf4ceda 🐛 scene entities 2023-04-14 17:42:27 +04:00
edmand46 17d1b7307d ♻️ plugin api 2023-04-14 14:32:24 +04:00
edmand46 fc28f512ba ♻️ plugin api 2023-04-14 14:32:04 +04:00
edmand46 6c4a51534a Merge pull request #12 from edmand46/develop
Plugins
2023-04-13 20:49:31 +04:00
edmand46 c91551ae08 📝 update headers 2023-04-13 20:49:00 +04:00
edmand46 e1a9ad476c http-commands 2023-04-13 20:42:05 +04:00
edmand46 24c9aa2043 🎨 namespaces 2023-04-09 11:06:52 +04:00
edmand46 bfd6c1b54b 🚧 plugin system, webhook system 2023-04-09 10:52:18 +04:00
edmand46 f2edc94958 chore: move sources to Sources folder 2023-04-05 18:53:21 +04:00
edmand46 b8dfc4cf41 chore: removed unused project 2023-04-05 18:48:37 +04:00
edmand46 bd7713bfcb fixed: cache sending on disconnecting 2023-03-31 12:56:23 +04:00
edmand46 043523d712 fixed: checking owner 2023-03-25 20:52:36 +04:00
edmand46 0dc5307b92 fixed: tickrate 2023-03-23 19:17:54 +04:00
edmand46 7b581b9afe chore: added tickrate logging 2023-03-23 18:07:48 +04:00
edmand46 8c5e063ef0 fixed: initial dirty property not tracked 2023-03-23 14:05:03 +04:00
edmand46 1a5f72a815 fixed: property update 2023-03-23 05:28:47 +04:00
edmand46 828da0d3da fix: wrong initial capacity 2023-03-22 11:04:14 +04:00
edmand46 951174e491 feat: split interface IRagonListener 2023-03-07 13:12:48 +04:00
edmand46 10b85867af feat: split interface IRagonListener 2023-03-07 00:57:13 +04:00
edmand46 252a46a713 chore: naming 2023-03-06 10:46:02 +04:00
edmand46 273c167c87 chore: naming 2023-03-06 10:31:42 +04:00
edmand46 192fb9e8eb chore: grammarly mistake 2023-03-06 10:29:22 +04:00
edmand46 a8ddc40268 Merge pull request #11 from edmand46/next
Update readme
2023-03-06 10:23:13 +04:00
edmand46 1ae545b353 chore: update readme.md 2023-03-06 10:22:27 +04:00
edmand46 b2058d21ce Merge pull request #10 from edmand46/next
Major update
2023-03-06 10:11:32 +04:00
163 changed files with 4746 additions and 1305 deletions
+25
View File
@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
+1 -1
View File
@@ -49,7 +49,7 @@ jobs:
- name: Setup dotnet - name: Setup dotnet
uses: actions/setup-dotnet@v1 uses: actions/setup-dotnet@v1
with: with:
dotnet-version: 7.0.x dotnet-version: 8.0.x
- name: Build - name: Build
shell: bash shell: bash
run: | run: |
+4 -1
View File
@@ -1,6 +1,9 @@
.DS_Store .DS_Store
.idea .idea
.vs .vs
.vscode
obj obj
bin bin
*.user *.user
*.dylib
*.dll
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

+2 -5
View File
@@ -1,12 +1,12 @@
<p align="center"> <p align="center">
<img src="Images/ragon-logo.png" width="200" > <img src="Images/logo.png">
</p> </p>
## Ragon Server ## Ragon Server
Ragon is fully free, small and high perfomance room based game server with plugin based architecture. Ragon is fully free, small and high perfomance room based game server with plugin based architecture.
<a href="https://www.ragon-server.com/docs/installation">Documentation</a> <a href="https://www.ragon-server.com/docs/overview">Documentation</a>
### Features: ### Features:
- Effective - Effective
@@ -26,9 +26,6 @@ Ragon is fully free, small and high perfomance room based game server with plugi
### Dependencies ### Dependencies
* ENet-Sharp [v2.4.8] * ENet-Sharp [v2.4.8]
### License
MIT
### Tips ### Tips
\* Limited to 4095 CCU by library ENet-Sharp (1) \* Limited to 4095 CCU by library ENet-Sharp (1)
\* Non finally (2) \* Non finally (2)
+1 -1
View File
@@ -17,7 +17,7 @@
using Ragon.Protocol; using Ragon.Protocol;
namespace Ragon.Client.Simulation namespace Ragon.Client.Property
{ {
[Serializable] [Serializable]
public class RagonBool : RagonProperty public class RagonBool : RagonProperty
+1 -1
View File
@@ -17,7 +17,7 @@
using Ragon.Protocol; using Ragon.Protocol;
namespace Ragon.Client.Simulation namespace Ragon.Client.Property
{ {
[Serializable] [Serializable]
public class RagonInt : RagonProperty public class RagonInt : RagonProperty
@@ -0,0 +1,76 @@
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Numerics;
using Ragon.Client.Compressor;
using Ragon.Protocol;
namespace Ragon.Client.Property;
public class RagonQuaternion : RagonProperty
{
private Quaternion _value;
public Quaternion Value
{
get => _value;
set
{
_value = value;
MarkAsChanged();
}
}
private readonly FloatCompressor _compressor;
public RagonQuaternion(bool invokeLocal = false, int priority = 0) : base(priority, invokeLocal)
{
_compressor = new FloatCompressor(-1.0f, 1f, 0.01f);
SetFixedSize(_compressor.RequiredBits * 4);
}
public override void Serialize(RagonBuffer buffer)
{
var compressedX = _compressor.Compress(_value.X);
var compressedY = _compressor.Compress(_value.Y);
var compressedZ = _compressor.Compress(_value.Z);
var compressedW = _compressor.Compress(_value.W);
buffer.Write(compressedX, _compressor.RequiredBits);
buffer.Write(compressedY, _compressor.RequiredBits);
buffer.Write(compressedZ, _compressor.RequiredBits);
buffer.Write(compressedW, _compressor.RequiredBits);
}
public override void Deserialize(RagonBuffer buffer)
{
var compressedX = buffer.Read(_compressor.RequiredBits);
var compressedY = buffer.Read(_compressor.RequiredBits);
var compressedZ = buffer.Read(_compressor.RequiredBits);
var compressedW = buffer.Read(_compressor.RequiredBits);
var x = _compressor.Decompress(compressedX);
var y = _compressor.Decompress(compressedY);
var z = _compressor.Decompress(compressedZ);
var w = _compressor.Decompress(compressedW);
_value = new Quaternion(x, y, z, w);
InvokeChanged();
}
}
+1 -1
View File
@@ -18,7 +18,7 @@
using System.Text; using System.Text;
using Ragon.Protocol; using Ragon.Protocol;
namespace Ragon.Client.Simulation namespace Ragon.Client.Property
{ {
[Serializable] [Serializable]
public class RagonString : RagonProperty public class RagonString : RagonProperty
@@ -0,0 +1,330 @@
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Numerics;
using Ragon.Client.Compressor;
using Ragon.Protocol;
namespace Ragon.Client.Property
{
[Serializable]
public enum RagonAxis
{
XYZ,
XY,
YZ,
XZ,
X,
Y,
Z
}
[Serializable]
public class RagonVector3 : RagonProperty
{
private Vector3 _value;
public Vector3 Value
{
get => _value;
set
{
_value = value;
MarkAsChanged();
}
}
private RagonAxis _axis;
private FloatCompressor _compressorX;
private FloatCompressor _compressorY;
private FloatCompressor _compressorZ;
public RagonVector3(
RagonAxis axis = RagonAxis.XYZ,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_axis = axis;
var defaultCompressor = new FloatCompressor(-1024.0f, 1024f, 0.01f);
_compressorX = defaultCompressor;
_compressorY = defaultCompressor;
_compressorZ = defaultCompressor;
switch (_axis)
{
case RagonAxis.XYZ:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.XY:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits);
break;
case RagonAxis.XZ:
SetFixedSize(_compressorX.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.YZ:
SetFixedSize(_compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.X:
SetFixedSize(_compressorX.RequiredBits);
break;
case RagonAxis.Y:
SetFixedSize(_compressorY.RequiredBits);
break;
case RagonAxis.Z:
SetFixedSize(_compressorZ.RequiredBits);
break;
}
}
public RagonVector3(
Vector3 initialValue,
RagonAxis axis = RagonAxis.XYZ,
float min = -1024.0f,
float max = 1024.0f,
float precision = 0.1f,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_value = initialValue;
_axis = axis;
var defaultCompressor = new FloatCompressor(min, max, precision);
_compressorX = defaultCompressor;
_compressorY = defaultCompressor;
_compressorZ = defaultCompressor;
switch (_axis)
{
case RagonAxis.XYZ:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.XY:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits);
break;
case RagonAxis.XZ:
SetFixedSize(_compressorX.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.YZ:
SetFixedSize(_compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.X:
SetFixedSize(_compressorX.RequiredBits);
break;
case RagonAxis.Y:
SetFixedSize(_compressorY.RequiredBits);
break;
case RagonAxis.Z:
SetFixedSize(_compressorZ.RequiredBits);
break;
}
}
public RagonVector3(
RagonAxis axis = RagonAxis.XYZ,
FloatCompressor compressorX = null,
FloatCompressor compressorY = null,
FloatCompressor compressorZ = null,
bool invokeLocal = true,
int priority = 0
) : base(priority, invokeLocal)
{
_axis = axis;
var defaultCompressor = new FloatCompressor(-1024.0f, 1024f, 0.01f);
_compressorX = defaultCompressor;
_compressorY = defaultCompressor;
_compressorZ = defaultCompressor;
if (compressorX != null)
_compressorX = compressorX;
if (compressorY != null)
_compressorY = compressorY;
if (compressorZ != null)
_compressorZ = compressorZ;
switch (_axis)
{
case RagonAxis.XYZ:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.XY:
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits);
break;
case RagonAxis.XZ:
SetFixedSize(_compressorX.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.YZ:
SetFixedSize(_compressorY.RequiredBits + _compressorZ.RequiredBits);
break;
case RagonAxis.X:
SetFixedSize(_compressorX.RequiredBits);
break;
case RagonAxis.Y:
SetFixedSize(_compressorY.RequiredBits);
break;
case RagonAxis.Z:
SetFixedSize(_compressorZ.RequiredBits);
break;
}
}
public override void Serialize(RagonBuffer buffer)
{
switch (_axis)
{
case RagonAxis.XYZ:
{
var compressedX = _compressorX.Compress(_value.X);
var compressedY = _compressorY.Compress(_value.Y);
var compressedZ = _compressorZ.Compress(_value.Z);
buffer.Write(compressedX, _compressorX.RequiredBits);
buffer.Write(compressedY, _compressorY.RequiredBits);
buffer.Write(compressedZ, _compressorZ.RequiredBits);
}
break;
case RagonAxis.XY:
{
var compressedX = _compressorX.Compress(_value.X);
var compressedY = _compressorY.Compress(_value.Y);
buffer.Write(compressedX, _compressorX.RequiredBits);
buffer.Write(compressedY, _compressorY.RequiredBits);
}
break;
case RagonAxis.XZ:
{
var compressedX = _compressorX.Compress(_value.X);
var compressedZ = _compressorZ.Compress(_value.Z);
buffer.Write(compressedX, _compressorX.RequiredBits);
buffer.Write(compressedZ, _compressorZ.RequiredBits);
break;
}
case RagonAxis.YZ:
{
var compressedY = _compressorY.Compress(_value.Y);
var compressedZ = _compressorZ.Compress(_value.Z);
buffer.Write(compressedY, _compressorY.RequiredBits);
buffer.Write(compressedZ, _compressorZ.RequiredBits);
break;
}
case RagonAxis.X:
{
var compressedX = _compressorX.Compress(_value.X);
buffer.Write(compressedX, _compressorX.RequiredBits);
break;
}
case RagonAxis.Y:
{
var compressedY = _compressorY.Compress(_value.Y);
buffer.Write(compressedY, _compressorY.RequiredBits);
break;
}
case RagonAxis.Z:
{
var compressedZ = _compressorZ.Compress(_value.Z);
buffer.Write(compressedZ, _compressorZ.RequiredBits);
break;
}
}
}
public override void Deserialize(RagonBuffer buffer)
{
switch (_axis)
{
case RagonAxis.XYZ:
{
var compressedX = buffer.Read(_compressorX.RequiredBits);
var compressedY = buffer.Read(_compressorY.RequiredBits);
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
_value.X = _compressorX.Decompress(compressedX);
_value.Y = _compressorY.Decompress(compressedY);
_value.Z = _compressorZ.Decompress(compressedZ);
break;
}
case RagonAxis.XY:
{
var compressedX = buffer.Read(_compressorX.RequiredBits);
var compressedY = buffer.Read(_compressorY.RequiredBits);
_value.X = _compressorX.Decompress(compressedX);
_value.Z = _compressorY.Decompress(compressedY);
break;
}
case RagonAxis.XZ:
{
var compressedX = buffer.Read(_compressorX.RequiredBits);
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
_value.X = _compressorX.Decompress(compressedX);
_value.Z = _compressorZ.Decompress(compressedZ);
break;
}
case RagonAxis.YZ:
{
var compressedY = buffer.Read(_compressorY.RequiredBits);
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
_value.Y = _compressorY.Decompress(compressedY);
_value.Z = _compressorZ.Decompress(compressedZ);
break;
}
case RagonAxis.X:
{
var compressedX = buffer.Read(_compressorX.RequiredBits);
_value.X = _compressorX.Decompress(compressedX);
break;
}
case RagonAxis.Y:
{
var compressedY = buffer.Read(_compressorY.RequiredBits);
_value.Y = _compressorY.Decompress(compressedY);
break;
}
case RagonAxis.Z:
{
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
_value.Z = _compressorZ.Decompress(compressedZ);
break;
}
}
InvokeChanged();
}
}
}
+3 -3
View File
@@ -1,22 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<LangVersion>10</LangVersion> <LangVersion>10</LangVersion>
<RootNamespace>Ragon.Client.Simulation</RootNamespace> <RootNamespace>Ragon.Client.Simulation</RootNamespace>
<Authors>Eduard Kargin (Edmand46)</Authors> <Authors>Eduard Kargin (Edmand46)</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>none</DebugType> <DebugType>none</DebugType>
<OutputPath>/Users/edmand46/RagonProjects/ragon-unity-sdk/Assets/Ragon/Runtime/Plugins</OutputPath> <OutputPath></OutputPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>/Users/edmand46/RagonProjects/ragon-unity-sdk/Assets/Ragon/Runtime/Plugins</OutputPath> <OutputPath></OutputPath>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
+136 -50
View File
@@ -19,19 +19,49 @@ using Ragon.Protocol;
namespace Ragon.Client namespace Ragon.Client
{ {
public sealed class RagonEntity public sealed class RagonEntity : IDisposable
{ {
private class EventSubscription : IDisposable
{
private List<Action<RagonPlayer, IRagonEvent>> _callbacks;
private List<Action<RagonPlayer, IRagonEvent>> _localCallbacks;
private Action<RagonPlayer, IRagonEvent> _callback;
public EventSubscription(
List<Action<RagonPlayer, IRagonEvent>> callbacks,
List<Action<RagonPlayer, IRagonEvent>> localCallbacks,
Action<RagonPlayer, IRagonEvent> callback)
{
_callbacks = callbacks;
_localCallbacks = localCallbacks;
_callback = callback;
}
public void Dispose()
{
_callbacks?.Remove(_callback);
_localCallbacks?.Remove(_callback);
_callbacks = null!;
_localCallbacks = null!;
_callback = null!;
}
}
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer); private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
private RagonClient _client; private RagonClient _client;
public ushort Id { get; private set; } public ushort Id { get; private set; }
public ushort Type { get; private set; } public ushort Type { get; private set; }
public bool Replication { get; private set; }
public RagonAuthority Authority { get; private set; } public RagonAuthority Authority { get; private set; }
public RagonPlayer Owner { get; private set; } public RagonPlayer Owner { get; private set; }
public RagonEntityState State { get; private set; } public RagonEntityState State { get; private set; }
public bool IsStatic => SceneId > 0;
public bool IsReplicated { get; private set; }
public bool IsAttached { get; private set; } public bool IsAttached { get; private set; }
public bool HasAuthority { get; private set; } public bool HasAuthority { get; private set; }
@@ -41,39 +71,40 @@ namespace Ragon.Client
internal bool PropertiesChanged => _propertiesChanged; internal bool PropertiesChanged => _propertiesChanged;
internal ushort SceneId => _sceneId; internal ushort SceneId => _sceneId;
private ushort _sceneId; private ushort _sceneId;
private bool _propertiesChanged; private bool _propertiesChanged;
private RagonPayload _spawnPayload; private RagonPayload _spawnPayload;
private RagonPayload _destroyPayload; private RagonPayload _destroyPayload;
private Dictionary<int, OnEventDelegate> _events = new(); private readonly Dictionary<int, OnEventDelegate> _events = new Dictionary<int, OnEventDelegate>();
private Dictionary<int, Action<RagonPlayer, IRagonEvent>> _localEvents = new(); private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
public RagonEntity(ushort type = 0, ushort sceneId = 0) public RagonEntity(ushort type = 0, ushort sceneId = 0, bool replicated = true)
{ {
State = new RagonEntityState(this); State = new RagonEntityState(this);
Type = type; Type = type;
IsReplicated = replicated;
_spawnPayload = new RagonPayload(0);
_destroyPayload = new RagonPayload(0);
_sceneId = sceneId; _sceneId = sceneId;
} }
internal void Attach(RagonClient client, ushort entityId, ushort entityType, bool hasAuthority, RagonPlayer owner, RagonPayload payload) internal void Attach()
{ {
Type = entityType;
Id = entityId;
Owner = owner;
IsAttached = true; IsAttached = true;
Replication = true;
HasAuthority = hasAuthority;
_client = client;
_spawnPayload = payload;
Attached?.Invoke(this); Attached?.Invoke(this);
} }
public void SetReplication(bool enabled)
{
IsReplicated = enabled;
}
internal void Detach(RagonPayload payload) internal void Detach(RagonPayload payload)
{ {
_destroyPayload = payload; _destroyPayload = payload;
@@ -83,21 +114,36 @@ namespace Ragon.Client
internal T GetPayload<T>(RagonPayload data) where T : IRagonPayload, new() internal T GetPayload<T>(RagonPayload data) where T : IRagonPayload, new()
{ {
var payload = new T();
if (data.Size <= 0) return payload;
var buffer = new RagonBuffer(); var buffer = new RagonBuffer();
data.Write(buffer); data.Write(buffer);
var payload = new T();
payload.Deserialize(buffer); payload.Deserialize(buffer);
return payload; return payload;
} }
public T GetSpawnPayload<T>() where T : IRagonPayload, new() public void Prepare(RagonClient client, ushort entityId, ushort entityType, bool hasAuthority, RagonPlayer player, RagonPayload payload)
{
Type = entityType;
Id = entityId;
HasAuthority = hasAuthority;
_client = client;
_spawnPayload = payload;
Owner = player;
}
public T GetAttachPayload<T>() where T : IRagonPayload, new()
{ {
return GetPayload<T>(_spawnPayload); return GetPayload<T>(_spawnPayload);
} }
public T GetDestroyPayload<T>() where T : IRagonPayload, new() public T GetDetachPayload<T>() where T : IRagonPayload, new()
{ {
return GetPayload<T>(_destroyPayload); return GetPayload<T>(_destroyPayload);
} }
@@ -105,6 +151,12 @@ namespace Ragon.Client
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode) public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
where TEvent : IRagonEvent, new() where TEvent : IRagonEvent, new()
{ {
if (!IsAttached)
{
RagonLog.Error("Entity not attached");
return;
}
var evntId = _client.Event.GetEventCode(evnt); var evntId = _client.Event.GetEventCode(evnt);
var buffer = _client.Buffer; var buffer = _client.Buffer;
@@ -112,12 +164,12 @@ namespace Ragon.Client
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT); buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
buffer.WriteUShort(Id); buffer.WriteUShort(Id);
buffer.WriteUShort(evntId); buffer.WriteUShort(evntId);
buffer.WriteByte((byte) replicationMode); buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte) RagonTarget.Player); buffer.WriteByte((byte)RagonTarget.Player);
buffer.WriteUShort((ushort) target.PeerId); buffer.WriteUShort(target.PeerId);
evnt.Serialize(buffer); evnt.Serialize(buffer);
var sendData = buffer.ToArray(); var sendData = buffer.ToArray();
_client.Reliable.Send(sendData); _client.Reliable.Send(sendData);
} }
@@ -128,31 +180,39 @@ namespace Ragon.Client
RagonReplicationMode replicationMode = RagonReplicationMode.Server) RagonReplicationMode replicationMode = RagonReplicationMode.Server)
where TEvent : IRagonEvent, new() where TEvent : IRagonEvent, new()
{ {
if (!IsAttached)
{
RagonLog.Error("Entity not attached");
return;
}
var eventCode = _client.Event.GetEventCode(evnt);
if (target != RagonTarget.ExceptOwner) if (target != RagonTarget.ExceptOwner)
{ {
if (replicationMode == RagonReplicationMode.Local) if (replicationMode == RagonReplicationMode.Local)
{ {
var eventCode = _client.Event.GetEventCode(evnt); var localListeners = _localListeners[eventCode];
_localEvents[eventCode].Invoke(_client.Room.Local, evnt); foreach (var listener in localListeners)
listener.Invoke(_client.Room.Local, evnt);
return; return;
} }
if (replicationMode == RagonReplicationMode.LocalAndServer) if (replicationMode == RagonReplicationMode.LocalAndServer)
{ {
var eventCode = _client.Event.GetEventCode(evnt); var localListeners = _localListeners[eventCode];
_localEvents[eventCode].Invoke(_client.Room.Local, evnt); foreach (var listener in localListeners)
listener.Invoke(_client.Room.Local, evnt);
} }
} }
var evntId = _client.Event.GetEventCode(evnt);
var buffer = _client.Buffer; var buffer = _client.Buffer;
buffer.Clear(); buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT); buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
buffer.WriteUShort(Id); buffer.WriteUShort(Id);
buffer.WriteUShort(evntId); buffer.WriteUShort(eventCode);
buffer.WriteByte((byte) replicationMode); buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte) target); buffer.WriteByte((byte)target);
evnt.Serialize(buffer); evnt.Serialize(buffer);
@@ -160,23 +220,39 @@ namespace Ragon.Client
_client.Reliable.Send(sendData); _client.Reliable.Send(sendData);
} }
public void OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new() public IDisposable OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
{ {
var t = new TEvent(); var t = new TEvent();
var eventCode = _client.Event.GetEventCode(t); var eventCode = _client.Event.GetEventCode(t);
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
if (_events.ContainsKey(eventCode)) if (!_listeners.TryGetValue(eventCode, out var callbacks))
{ {
RagonLog.Warn($"Event already {eventCode} subscribed"); callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
return; _listeners.Add(eventCode, callbacks);
} }
_localEvents.Add(eventCode, (player, eventData) => { callback.Invoke(player, (TEvent) eventData); }); if (!_localListeners.TryGetValue(eventCode, out var localCallbacks))
_events.Add(eventCode, (player, serializer) =>
{ {
t.Deserialize(serializer); localCallbacks = new List<Action<RagonPlayer, IRagonEvent>>();
callback.Invoke(player, t); _localListeners.Add(eventCode, localCallbacks);
}); }
callbacks.Add(action);
localCallbacks.Add(action);
if (!_events.ContainsKey(eventCode))
{
_events.Add(eventCode, (player, serializer) =>
{
t.Deserialize(serializer);
foreach (var callbackListener in callbacks)
callbackListener.Invoke(player, t);
});
}
return new EventSubscription(callbacks, localCallbacks, action);
} }
internal void Write(RagonBuffer buffer) internal void Write(RagonBuffer buffer)
@@ -187,8 +263,7 @@ namespace Ragon.Client
_propertiesChanged = false; _propertiesChanged = false;
} }
internal void Read(RagonBuffer buffer) internal void Read(RagonBuffer buffer)
{ {
State.ReadState(buffer); State.ReadState(buffer);
@@ -196,8 +271,12 @@ namespace Ragon.Client
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer) internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
{ {
if (_events.ContainsKey(eventCode)) if (!IsReplicated) return;
_events[eventCode]?.Invoke(caller, buffer);
if (_events.TryGetValue(eventCode, out var evnt))
evnt?.Invoke(caller, buffer);
else
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
} }
internal void TrackChangedProperty(RagonProperty property) internal void TrackChangedProperty(RagonProperty property)
@@ -208,11 +287,18 @@ namespace Ragon.Client
public void OnOwnershipChanged(RagonPlayer player) public void OnOwnershipChanged(RagonPlayer player)
{ {
var prevOwner = Owner; var prevOwner = Owner;
Owner = player; Owner = player;
HasAuthority = player.PeerId == _client.Room.Local.PeerId; HasAuthority = player.IsLocal;
OwnershipChanged?.Invoke(prevOwner, player); OwnershipChanged?.Invoke(prevOwner, player);
} }
public void Dispose()
{
_events.Clear();
_listeners.Clear();
_localListeners.Clear();
}
} }
} }
@@ -53,7 +53,7 @@ public sealed class RagonEntityState
{ {
var changed = buffer.ReadBool(); var changed = buffer.ReadBool();
if (changed) if (changed)
property.Deserialize(buffer); property.Read(buffer);
} }
} }
+7 -9
View File
@@ -19,10 +19,12 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
public struct RagonPayload public class RagonPayload
{ {
private uint[] _data = new uint[128]; public static RagonPayload Empty = new RagonPayload(0);
private int _size = 0;
private readonly uint[] _data = new uint[128];
private readonly int _size = 0;
public RagonPayload(int capacity) public RagonPayload(int capacity)
{ {
@@ -32,16 +34,12 @@ public struct RagonPayload
public void Read(RagonBuffer buffer) public void Read(RagonBuffer buffer)
{ {
var readOnlySpan = _data.AsSpan(); buffer.ReadArray(_data, _size);
buffer.ReadSpan(ref readOnlySpan, _size);
} }
public void Write(RagonBuffer buffer) public void Write(RagonBuffer buffer)
{ {
ReadOnlySpan<uint> readOnlySpan = _data.AsSpan(); buffer.WriteArray(_data, _size);
buffer.WriteSpan(ref readOnlySpan, _size);
} }
public override string ToString() public override string ToString()
+50 -27
View File
@@ -23,35 +23,38 @@ namespace Ragon.Client
{ {
public string Name => _name; public string Name => _name;
public RagonEntity Entity => _entity; public RagonEntity Entity => _entity;
public event Action Changed; public event Action Changed;
public bool IsDirty => _dirty && _ticks >= _priority; public bool IsDirty => _dirty && _ticks >= _priority;
public bool IsFixed => _fixed; public bool IsFixed => _fixed;
public int Size => _size; public int Size => _size;
private bool _fixed; private RagonBuffer _propertyBuffer;
private string _name;
protected bool _invokeLocal;
private RagonEntity _entity; private RagonEntity _entity;
private bool _dirty; private bool _dirty;
private int _size; private int _size;
private int _ticks; private int _ticks;
private int _priority; private int _priority;
private bool _fixed;
private string _name;
protected bool InvokeLocal;
protected RagonProperty(int priority, bool invokeLocal) protected RagonProperty(int priority, bool invokeLocal)
{ {
_size = 0; _size = 0;
_priority = priority; _priority = priority;
_fixed = false; _fixed = false;
_invokeLocal = invokeLocal; _propertyBuffer = new RagonBuffer();
InvokeLocal = invokeLocal;
} }
public void SetName(string name) public void SetName(string name)
{ {
_name = name; _name = name;
} }
protected void SetFixedSize(int size) protected void SetFixedSize(int size)
{ {
_size = size; _size = size;
@@ -60,20 +63,22 @@ namespace Ragon.Client
protected void InvokeChanged() protected void InvokeChanged()
{ {
if (!_invokeLocal) if (_entity.HasAuthority)
return; return;
Changed?.Invoke(); Changed?.Invoke();
} }
protected void MarkAsChanged() protected void MarkAsChanged()
{ {
InvokeChanged(); if (InvokeLocal)
Changed?.Invoke();
if (_dirty) return; if (_dirty || _entity == null)
_dirty = true; return;
_entity?.TrackChangedProperty(this); _dirty = true;
_entity.TrackChangedProperty(this);
} }
internal void Flush() internal void Flush()
@@ -86,30 +91,48 @@ namespace Ragon.Client
{ {
_ticks++; _ticks++;
} }
internal void AssignEntity(RagonEntity obj) internal void AssignEntity(RagonEntity ent)
{ {
_entity = obj; _entity = ent;
Changed?.Invoke(); Changed?.Invoke();
} }
internal void Write(RagonBuffer buffer) internal void Write(RagonBuffer buffer)
{ {
_propertyBuffer.Clear();
if (_fixed) if (_fixed)
{ {
Serialize(buffer); Serialize(_propertyBuffer);
buffer.CopyFrom(_propertyBuffer, _size);
return; return;
} }
var sizeOffset = buffer.WriteOffset; Serialize(_propertyBuffer);
buffer.Write(0, 16);
var propOffset = buffer.WriteOffset;
Serialize(buffer); var propertySize = (ushort)_propertyBuffer.WriteOffset;
buffer.WriteUShort(propertySize);
var propSize = (uint) (buffer.WriteOffset - propOffset); buffer.CopyFrom(_propertyBuffer, propertySize);
buffer.Write(propSize, 16, sizeOffset); }
internal void Read(RagonBuffer buffer)
{
_propertyBuffer.Clear();
if (_fixed)
{
buffer.ToBuffer(_propertyBuffer, _size);
Deserialize(_propertyBuffer);
return;
}
var propSize = buffer.ReadUShort();
buffer.ToBuffer(_propertyBuffer, propSize);
Deserialize(_propertyBuffer);
} }
public virtual void Serialize(RagonBuffer buffer) public virtual void Serialize(RagonBuffer buffer)
@@ -19,7 +19,7 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class AuthorizeFailedHandler: Handler internal class AuthorizeFailedHandler: IHandler
{ {
private readonly RagonListenerList _listenerList; private readonly RagonListenerList _listenerList;
public AuthorizeFailedHandler(RagonListenerList list) public AuthorizeFailedHandler(RagonListenerList list)
@@ -27,9 +27,9 @@ internal class AuthorizeFailedHandler: Handler
_listenerList = list; _listenerList = list;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
var message = buffer.ReadString(); var message = reader.ReadString();
_listenerList.OnAuthorizationFailed(message); _listenerList.OnAuthorizationFailed(message);
} }
} }
@@ -19,20 +19,26 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class AuthorizeSuccessHandler: Handler internal class AuthorizeSuccessHandler: IHandler
{ {
private readonly RagonListenerList _listenerList; private readonly RagonListenerList _listenerList;
private readonly RagonClient _client;
public AuthorizeSuccessHandler(RagonListenerList listenerList) public AuthorizeSuccessHandler(
RagonClient client,
RagonListenerList listenerList)
{ {
_client = client;
_listenerList = listenerList; _listenerList = listenerList;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
var playerId = buffer.ReadString(); var playerId = reader.ReadString();
var playerName = buffer.ReadString(); var playerName = reader.ReadString();
var playerPayload = reader.ReadString();
_listenerList.OnAuthorizationSuccess(playerId, playerName); _client.SetStatus(RagonStatus.LOBBY);
_listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload);
} }
} }
@@ -18,41 +18,52 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class EntityCreateHandler : Handler internal class EntityCreateHandler : IHandler
{ {
private readonly RagonClient _client; private readonly RagonClient _client;
private readonly RagonPlayerCache _playerCache; private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache; private readonly RagonEntityCache _entityCache;
private readonly IRagonEntityListener _entityListener;
public EntityCreateHandler( public EntityCreateHandler(
RagonClient client, RagonClient client,
RagonPlayerCache playerCache, RagonPlayerCache playerCache,
RagonEntityCache entityCache RagonEntityCache entityCache,
IRagonEntityListener entityListener
) )
{ {
_client = client; _client = client;
_entityCache = entityCache; _entityCache = entityCache;
_playerCache = playerCache; _playerCache = playerCache;
_entityListener = entityListener;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
var attachId = buffer.ReadUShort(); var attachId = reader.ReadUShort();
var entityType = buffer.ReadUShort(); var entityType = reader.ReadUShort();
var entityId = buffer.ReadUShort(); var entityId = reader.ReadUShort();
var ownerId = buffer.ReadUShort(); var ownerId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(ownerId); var player = _playerCache.GetPlayerByPeer(ownerId);
var payload = new RagonPayload(buffer.Capacity); var payload = new RagonPayload(reader.Capacity);
payload.Read(buffer); payload.Read(reader);
if (player == null) if (player == null)
{ {
RagonLog.Warn($"Owner {ownerId}|{player.Name} not found in players"); RagonLog.Warn($"Owner {ownerId}|{player.Name} not found in players");
_playerCache.Dump();
return; return;
} }
var hasAuthority = _playerCache.LocalPlayer.Id == player.Id; var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.OnCreate(attachId, entityType, 0, entityId, hasAuthority); var entity = _entityCache.TryGetEntity(attachId, entityType, 0, entityId, hasAuthority, out var hasCreated);
entity.Attach(_client, entityId, entityType, hasAuthority, player, payload);
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
if (hasCreated)
_entityListener.OnEntityCreated(entity);
entity.Attach();
} }
} }
@@ -18,40 +18,39 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class EntityEventHandler : Handler internal class EntityEventHandler : IHandler
{ {
private readonly RagonClient _client; private readonly RagonPlayerCache _playerCache;
private readonly RagonPlayerCache _playerCache; private readonly RagonEntityCache _entityCache;
private readonly RagonEntityCache _entityCache;
public EntityEventHandler( public EntityEventHandler(
RagonClient client, RagonPlayerCache playerCache,
RagonPlayerCache playerCache, RagonEntityCache entityCache
RagonEntityCache entityCache )
) {
_playerCache = playerCache;
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
{
var eventCode = reader.ReadUShort();
var peerId = reader.ReadUShort();
var executionMode = (RagonReplicationMode)reader.ReadByte();
var entityId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(peerId);
if (player == null)
{ {
_client = client; RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}");
_playerCache = playerCache;
_entityCache = entityCache; _playerCache.Dump();
return;
} }
public void Handle(RagonBuffer buffer) if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
{ return;
var eventCode = buffer.ReadUShort();
var peerId = buffer.ReadUShort();
var executionMode = (RagonReplicationMode)buffer.ReadByte();
var entityId = buffer.ReadUShort();
var player = _playerCache.GetPlayerByPeer(peerId); _entityCache.OnEvent(player, entityId, eventCode, reader);
if (player == null) }
{
RagonLog.Warn($"Player not found for event {eventCode}");
return;
}
if (player.IsMe && executionMode == RagonReplicationMode.LocalAndServer)
return;
_entityCache.OnEvent(player, entityId, eventCode, buffer);
}
} }
@@ -0,0 +1,60 @@
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
namespace Ragon.Client;
internal class EntityOwnershipHandler: IHandler
{
private readonly RagonListenerList _listenerList;
private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache;
public EntityOwnershipHandler(
RagonListenerList listenerList,
RagonPlayerCache playerCache,
RagonEntityCache entityCache)
{
_listenerList = listenerList;
_playerCache = playerCache;
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
{
var newOwnerId = reader.ReadUShort();
var entities = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(newOwnerId);
if (player == null)
{
RagonLog.Error($"Player with Id:{newOwnerId} not found in cache");
_playerCache.Dump();
return;
}
for (var i = 0; i < entities; i++)
{
var entityId = reader.ReadUShort();
_entityCache.OnOwnershipChanged(player, entityId);
RagonLog.Trace("Entity changed owner: " + entityId);
}
}
}
@@ -19,20 +19,20 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class EntityDestroyHandler: Handler internal class EntityRemoveHandler: IHandler
{ {
private readonly RagonEntityCache _entityCache; private readonly RagonEntityCache _entityCache;
public EntityDestroyHandler(RagonEntityCache entityCache) public EntityRemoveHandler(RagonEntityCache entityCache)
{ {
_entityCache = entityCache; _entityCache = entityCache;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
var entityId = buffer.ReadUShort(); var entityId = reader.ReadUShort();
var payload = new RagonPayload(buffer.Capacity); var payload = new RagonPayload(reader.Capacity);
payload.Read(buffer); payload.Read(reader);
_entityCache.OnDestroy(entityId, payload); _entityCache.OnDestroy(entityId, payload);
} }
@@ -18,7 +18,7 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class StateEntityHandler: Handler internal class StateEntityHandler: IHandler
{ {
private readonly RagonEntityCache _entityCache; private readonly RagonEntityCache _entityCache;
@@ -27,13 +27,13 @@ internal class StateEntityHandler: Handler
_entityCache = entityCache; _entityCache = entityCache;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
var entitiesCount = buffer.ReadUShort(); var entitiesCount = reader.ReadUShort();
for (var i = 0; i < entitiesCount; i++) for (var i = 0; i < entitiesCount; i++)
{ {
var entityId = buffer.ReadUShort(); var entityId = reader.ReadUShort();
_entityCache.OnState(entityId, buffer); _entityCache.OnState(entityId, reader);
} }
} }
} }
@@ -19,7 +19,7 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
public interface Handler public interface IHandler
{ {
public void Handle(RagonBuffer buffer); public void Handle(RagonBuffer reader);
} }
@@ -19,7 +19,7 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class JoinFailedHandler: Handler internal class JoinFailedHandler: IHandler
{ {
private readonly RagonListenerList _listenerList; private readonly RagonListenerList _listenerList;
@@ -28,9 +28,9 @@ internal class JoinFailedHandler: Handler
_listenerList = listenerList; _listenerList = listenerList;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
var message = buffer.ReadString(); var message = reader.ReadString();
_listenerList.OnFailed(message); _listenerList.OnFailed(message);
} }
} }
@@ -19,9 +19,9 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
public struct RagonRoomInformation public struct RoomParameters
{ {
public RagonRoomInformation(string roomId, string playerId, string ownerId, ushort min, ushort max) public RoomParameters(string roomId, string playerId, string ownerId, ushort min, ushort max)
{ {
RoomId = roomId; RoomId = roomId;
PlayerId = playerId; PlayerId = playerId;
@@ -37,44 +37,44 @@ public struct RagonRoomInformation
public ushort Max { get; private set; } public ushort Max { get; private set; }
} }
internal class JoinSuccessHandler : Handler internal class JoinSuccessHandler : IHandler
{ {
private readonly RagonListenerList _listenerList; private readonly RagonListenerList _listenerList;
private readonly RagonPlayerCache _playerCache; private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache; private readonly RagonEntityCache _entityCache;
private readonly RagonClient _client; private readonly RagonClient _client;
private readonly RagonBuffer _buffer;
public JoinSuccessHandler( public JoinSuccessHandler(
RagonClient client, RagonClient client,
RagonBuffer buffer,
RagonListenerList listenerList, RagonListenerList listenerList,
RagonPlayerCache playerCache, RagonPlayerCache playerCache,
RagonEntityCache entityCache RagonEntityCache entityCache
) )
{ {
_buffer = buffer;
_client = client; _client = client;
_listenerList = listenerList; _listenerList = listenerList;
_entityCache = entityCache; _entityCache = entityCache;
_playerCache = playerCache; _playerCache = playerCache;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
var roomId = buffer.ReadString(); var roomId = reader.ReadString();
var localId = buffer.ReadString(); var localId = reader.ReadString();
var ownerId = buffer.ReadString(); var ownerId = reader.ReadString();
var min = buffer.ReadUShort(); var min = reader.ReadUShort();
var max = buffer.ReadUShort(); var max = reader.ReadUShort();
var map = buffer.ReadString(); var sceneName = reader.ReadString();
var scene = new RagonScene(_client, _playerCache, _entityCache); var scene = new RagonScene(_client, _playerCache, _entityCache, sceneName);
var roomInfo = new RagonRoomInformation(roomId, localId, ownerId, min, max); var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max);
var room = new RagonRoom(_client, _entityCache, _playerCache, roomInfo, scene); var room = new RagonRoom(_client, _entityCache, _playerCache, roomInfo, scene);
_playerCache.SetOwnerAndLocal(ownerId, localId); _playerCache.SetOwnerAndLocal(ownerId, localId);
_client.AssignRoom(room); _client.AssignRoom(room);
_listenerList.OnLevel(map);
_listenerList.OnSceneRequest(sceneName);
} }
} }
@@ -19,7 +19,7 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class LeaveRoomHandler : Handler internal class LeaveRoomHandler : IHandler
{ {
private readonly RagonClient _client; private readonly RagonClient _client;
private readonly RagonListenerList _listenerList; private readonly RagonListenerList _listenerList;
@@ -35,7 +35,7 @@ internal class LeaveRoomHandler : Handler
_entityCache = entityCache; _entityCache = entityCache;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
_listenerList.OnLeft(); _listenerList.OnLeft();
_entityCache.Cleanup(); _entityCache.Cleanup();
@@ -18,7 +18,7 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class SceneLoadHandler: Handler internal class SceneLoadHandler: IHandler
{ {
private readonly RagonClient _client; private readonly RagonClient _client;
private readonly RagonListenerList _listenerList; private readonly RagonListenerList _listenerList;
@@ -32,13 +32,14 @@ internal class SceneLoadHandler: Handler
_listenerList = listenerList; _listenerList = listenerList;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
var sceneName = buffer.ReadString(); var sceneName = reader.ReadString();
var room = _client.Room; var room = _client.Room;
room.Cleanup(); room.Cleanup();
room.Update(sceneName);
_listenerList.OnLevel(sceneName); _listenerList.OnSceneRequest(sceneName);
} }
} }
@@ -19,13 +19,13 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class OwnershipHandler: Handler internal class OwnershipRoomHandler : IHandler
{ {
private readonly RagonListenerList _listenerList; private readonly RagonListenerList _listenerList;
private readonly RagonPlayerCache _playerCache; private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache; private readonly RagonEntityCache _entityCache;
public OwnershipHandler( public OwnershipRoomHandler(
RagonListenerList listenerList, RagonListenerList listenerList,
RagonPlayerCache playerCache, RagonPlayerCache playerCache,
RagonEntityCache entityCache) RagonEntityCache entityCache)
@@ -34,20 +34,20 @@ internal class OwnershipHandler: Handler
_playerCache = playerCache; _playerCache = playerCache;
_entityCache = entityCache; _entityCache = entityCache;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
var newOwnerId = buffer.ReadString(); var newOwnerId = reader.ReadUShort();
var player = _playerCache.GetPlayerById(newOwnerId); var player = _playerCache.GetPlayerByPeer(newOwnerId);
if (player == null)
{
RagonLog.Warn($"Player with peerId:{newOwnerId} not found in cache");
_playerCache.Dump();
return;
}
_playerCache.OnOwnershipChanged(newOwnerId); _playerCache.OnOwnershipChanged(newOwnerId);
_listenerList.OnOwnershipChanged(player); _listenerList.OnOwnershipChanged(player);
var entities = buffer.ReadUShort();
for (var i = 0; i < entities; i++)
{
var entityId = buffer.ReadUShort();
_entityCache.OnOwnershipChanged(player, entityId);
}
} }
} }
@@ -19,7 +19,7 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class PlayerJoinHandler : Handler internal class PlayerJoinHandler : IHandler
{ {
private RagonPlayerCache _playerCache; private RagonPlayerCache _playerCache;
private RagonListenerList _listenerList; private RagonListenerList _listenerList;
@@ -33,11 +33,11 @@ internal class PlayerJoinHandler : Handler
_listenerList = listenerList; _listenerList = listenerList;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
var playerPeerId = buffer.ReadUShort(); var playerPeerId = reader.ReadUShort();
var playerId = buffer.ReadString(); var playerId = reader.ReadString();
var playerName = buffer.ReadString(); var playerName = reader.ReadString();
_playerCache.AddPlayer(playerPeerId, playerId, playerName); _playerCache.AddPlayer(playerPeerId, playerId, playerName);
@@ -45,6 +45,6 @@ internal class PlayerJoinHandler : Handler
if (player != null) if (player != null)
_listenerList.OnPlayerJoined(player); _listenerList.OnPlayerJoined(player);
else else
RagonLog.Trace($"[Joined] {playerId}"); RagonLog.Warn($"Player with Id:{playerId} not found in cache");
} }
} }
@@ -19,7 +19,7 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class PlayerLeftHandler : Handler internal class PlayerLeftHandler : IHandler
{ {
private RagonPlayerCache _playerCache; private RagonPlayerCache _playerCache;
private RagonEntityCache _entityCache; private RagonEntityCache _entityCache;
@@ -36,25 +36,30 @@ internal class PlayerLeftHandler : Handler
_listenerList = listenerList; _listenerList = listenerList;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer reader)
{ {
var playerId = buffer.ReadString(); var playerId = reader.ReadString();
var player = _playerCache.GetPlayerById(playerId); var player = _playerCache.GetPlayerById(playerId);
if (player != null) if (player != null)
{ {
_playerCache.RemovePlayer(playerId); _playerCache.RemovePlayer(playerId);
_listenerList.OnPlayerLeft(player); _listenerList.OnPlayerLeft(player);
var entities = buffer.ReadUShort(); var entities = reader.ReadUShort();
var toDeleteIds = new ushort[entities]; var toDeleteIds = new ushort[entities];
for (var i = 0; i < entities; i++) for (var i = 0; i < entities; i++)
{ {
var entityId = buffer.ReadUShort(); var entityId = reader.ReadUShort();
toDeleteIds[i] = entityId; toDeleteIds[i] = entityId;
} }
var emptyPayload = new RagonPayload(0);
foreach (var id in toDeleteIds) foreach (var id in toDeleteIds)
_entityCache.OnDestroy(id, new RagonPayload()); _entityCache.OnDestroy(id, emptyPayload);
}
else
{
RagonLog.Warn($"Player with Id:{playerId} not found in cache");
} }
} }
} }
@@ -0,0 +1,56 @@
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
namespace Ragon.Client;
internal class RoomDataHandler: IHandler
{
private readonly RagonListenerList _listeners;
private readonly RagonPlayerCache _playerCache;
public RoomDataHandler(
RagonPlayerCache playerCache,
RagonListenerList listeners)
{
_playerCache = playerCache;
_listeners = listeners;
}
public void Handle(RagonBuffer reader)
{
var rawData = reader.RawData;
var peerId = (ushort)(rawData[1] + (rawData[2] << 8));
var player = _playerCache.GetPlayerByPeer(peerId);
if (player == null)
{
RagonLog.Error($"Player with peerId:{peerId} not found");
_playerCache.Dump();
return;
}
var headerSize = 3;
var payload = new byte[rawData.Length - headerSize];
Array.Copy(rawData, headerSize, payload, 0, payload.Length);
_listeners.OnData(player, payload);
}
}
@@ -0,0 +1,55 @@
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
namespace Ragon.Client;
public class RoomEventHandler : IHandler
{
private readonly RagonClient _client;
private readonly RagonPlayerCache _playerCache;
public RoomEventHandler(
RagonClient client,
RagonPlayerCache playerCache
)
{
_client = client;
_playerCache = playerCache;
}
public void Handle(RagonBuffer buffer)
{
var eventCode = buffer.ReadUShort();
var peerId = buffer.ReadUShort();
var executionMode = (RagonReplicationMode)buffer.ReadByte();
var player = _playerCache.GetPlayerByPeer(peerId);
if (player == null)
{
RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}");
_playerCache.Dump();
return;
}
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
return;
_client.Room.Event(eventCode, player, buffer);
}
}
@@ -0,0 +1,43 @@
using Ragon.Protocol;
namespace Ragon.Client;
internal class RoomListHandler: IHandler
{
private RagonListenerList _listenerList;
private RagonSession _session;
public RoomListHandler(RagonSession session, RagonListenerList list)
{
_session = session;
_listenerList = list;
}
public void Handle(RagonBuffer reader)
{
var roomCount = reader.ReadUShort();
var roomList = new RagonRoomInformation[roomCount];
for (int i = 0; i < roomCount; i++)
{
var id = reader.ReadString();
var scene = reader.ReadString();
var maxPlayers = reader.ReadUShort();
var minPlayers = reader.ReadUShort();
var players = reader.ReadUShort();
var roomInfo = new RagonRoomInformation()
{
Id = id,
Scene = scene,
PlayerCount = players,
PlayerMax = maxPlayers,
PlayerMin = minPlayers,
Properties = new Dictionary<string, byte[]>()
};
roomList[i] = roomInfo;
}
_listenerList.OnRoomList(roomList);
}
}
+62 -22
View File
@@ -15,25 +15,29 @@
*/ */
using System.Diagnostics;
using Ragon.Protocol; using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class SnapshotHandler : Handler internal class SnapshotHandler : IHandler
{ {
private RagonClient _client; private readonly IRagonEntityListener _entityListener;
private RagonListenerList _listenerList; private readonly RagonClient _client;
private RagonEntityCache _entityCache; private readonly RagonListenerList _listenerList;
private RagonPlayerCache _playerCache; private readonly RagonEntityCache _entityCache;
private readonly RagonPlayerCache _playerCache;
public SnapshotHandler( public SnapshotHandler(
RagonClient ragonClient, RagonClient ragonClient,
RagonListenerList listenerList, RagonListenerList listenerList,
RagonEntityCache entityCache, RagonEntityCache entityCache,
RagonPlayerCache playerCache RagonPlayerCache playerCache,
IRagonEntityListener entityListener
) )
{ {
_client = ragonClient; _client = ragonClient;
_entityListener = entityListener;
_listenerList = listenerList; _listenerList = listenerList;
_entityCache = entityCache; _entityCache = entityCache;
_playerCache = playerCache; _playerCache = playerCache;
@@ -41,6 +45,7 @@ internal class SnapshotHandler : Handler
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer buffer)
{ {
var entities = new List<RagonEntity>();
var playersCount = buffer.ReadUShort(); var playersCount = buffer.ReadUShort();
RagonLog.Trace("Players: " + playersCount); RagonLog.Trace("Players: " + playersCount);
for (var i = 0; i < playersCount; i++) for (var i = 0; i < playersCount; i++)
@@ -50,7 +55,10 @@ internal class SnapshotHandler : Handler
var playerName = buffer.ReadString(); var playerName = buffer.ReadString();
_playerCache.AddPlayer(playerPeerId, playerId, playerName); _playerCache.AddPlayer(playerPeerId, playerId, playerName);
RagonLog.Trace($"Player {playerPeerId} - {playerId} - {playerName}");
} }
var dynamicEntities = buffer.ReadUShort(); var dynamicEntities = buffer.ReadUShort();
RagonLog.Trace("Dynamic Entities: " + dynamicEntities); RagonLog.Trace("Dynamic Entities: " + dynamicEntities);
for (var i = 0; i < dynamicEntities; i++) for (var i = 0; i < dynamicEntities; i++)
@@ -59,16 +67,32 @@ internal class SnapshotHandler : Handler
var entityId = buffer.ReadUShort(); var entityId = buffer.ReadUShort();
var ownerPeerId = buffer.ReadUShort(); var ownerPeerId = buffer.ReadUShort();
var payloadSize = buffer.ReadUShort(); var payloadSize = buffer.ReadUShort();
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
var payload = new RagonPayload(payloadSize); var player = _playerCache.GetPlayerByPeer(ownerPeerId);
payload.Read(buffer); if (player == null)
{
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
var hasAuthority = _playerCache.LocalPlayer.Id == player.Id; _playerCache.Dump();
var entity = _entityCache.OnCreate(0, entityType, 0, entityId, hasAuthority); return;
}
var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.TryGetEntity(0, entityType, 0, entityId, hasAuthority, out _);
var payload = RagonPayload.Empty;
if (payloadSize > 0)
{
payload = new RagonPayload(payloadSize);
payload.Read(buffer);
}
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
_entityListener.OnEntityCreated(entity);
entity.Read(buffer); entity.Read(buffer);
entity.Attach(_client, entityId, entityType, hasAuthority, player, payload);
entities.Add(entity);
} }
var staticEntities = buffer.ReadUShort(); var staticEntities = buffer.ReadUShort();
@@ -80,18 +104,34 @@ internal class SnapshotHandler : Handler
var staticId = buffer.ReadUShort(); var staticId = buffer.ReadUShort();
var ownerPeerId = buffer.ReadUShort(); var ownerPeerId = buffer.ReadUShort();
var payloadSize = buffer.ReadUShort(); var payloadSize = buffer.ReadUShort();
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
var payload = new RagonPayload(payloadSize);
payload.Read(buffer);
var hasAuthority = _playerCache.LocalPlayer.Id == player.Id; var player = _playerCache.GetPlayerByPeer(ownerPeerId);
var entity = _entityCache.OnCreate(0, entityType, staticId, entityId, hasAuthority); if (player == null)
{
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
_playerCache.Dump();
return;
}
var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _);
entity.Prepare(_client, entityId, entityType, hasAuthority, player, RagonPayload.Empty);
entity.Read(buffer); entity.Read(buffer);
entity.Attach(_client, entityId, entityType, hasAuthority, player, payload);
entities.Add(entity);
} }
_listenerList.OnJoined(); if (_client.Status == RagonStatus.LOBBY)
{
_client.SetStatus(RagonStatus.ROOM);
_listenerList.OnJoined();
}
foreach (var entity in entities)
entity.Attach();
_listenerList.OnSceneLoaded();
} }
} }
@@ -0,0 +1,21 @@
using Ragon.Protocol;
namespace Ragon.Client;
public class TimestampHandler: IHandler
{
private readonly RagonClient _client;
public TimestampHandler(RagonClient client)
{
_client = client;
}
public void Handle(RagonBuffer buffer)
{
var timestamp0 = buffer.Read(32);
var timestamp1 = buffer.Read(32);
var value = new DoubleToUInt { Int0 = timestamp0, Int1 = timestamp1 };
_client.SetTimestamp(value.Double);
}
}
@@ -15,6 +15,8 @@
*/ */
using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
public interface INetworkConnection: IRagonConnection public interface INetworkConnection: IRagonConnection
@@ -23,7 +25,7 @@ public interface INetworkConnection: IRagonConnection
public INetworkChannel Unreliable { get; } public INetworkChannel Unreliable { get; }
public Action<byte[]> OnData { get; set; } public Action<byte[]> OnData { get; set; }
public Action OnConnected { get; set; } public Action OnConnected { get; set; }
public Action<DisconnectReason> OnDisconnected { get; set; } public Action<RagonDisconnect> OnDisconnected { get; set; }
public ulong BytesSent { get; } public ulong BytesSent { get; }
public ulong BytesReceived { get; } public ulong BytesReceived { get; }
public int Ping { get; } public int Ping { get; }
+1 -1
View File
@@ -18,5 +18,5 @@ namespace Ragon.Client;
public interface IRagonConnection public interface IRagonConnection
{ {
public void Close();
} }
@@ -14,10 +14,12 @@
* limitations under the License. * limitations under the License.
*/ */
using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
public interface IRagonConnectedListener public interface IRagonConnectionListener
{ {
void OnConnected(RagonClient client); void OnConnected(RagonClient client);
void OnDisconnected(RagonClient client); void OnDisconnected(RagonClient client, RagonDisconnect reason);
} }
@@ -16,7 +16,7 @@
namespace Ragon.Client; namespace Ragon.Client;
public interface IRagonLevelListener public interface IRagonDataListener
{ {
void OnLevel(RagonClient client, string sceneName); public void OnData(RagonPlayer player, byte[] data);
} }
@@ -18,14 +18,15 @@ namespace Ragon.Client
{ {
public interface IRagonListener : public interface IRagonListener :
IRagonAuthorizationListener, IRagonAuthorizationListener,
IRagonConnectedListener, IRagonConnectionListener,
IRagonFailedListener, IRagonFailedListener,
IRagonJoinListener, IRagonJoinListener,
IRagonLeftListener, IRagonLeftListener,
IRagonLevelListener, IRagonSceneListener,
IRagonOwnershipChangedListener, IRagonOwnershipChangedListener,
IRagonPlayerJoinListener, IRagonPlayerJoinListener,
IRagonPlayerLeftListener IRagonPlayerLeftListener
{ {
} }
} }
@@ -0,0 +1,6 @@
namespace Ragon.Client;
public interface IRagonRoomListListener
{
public void OnRoomListUpdate(IReadOnlyList<RagonRoomInformation> roomsInfos);
}
@@ -16,8 +16,7 @@
namespace Ragon.Client; namespace Ragon.Client;
public enum DisconnectReason public interface IRagonSceneListener
{ {
MANUAL, void OnSceneLoaded(RagonClient client);
TIMEOUT,
} }
@@ -0,0 +1,6 @@
namespace Ragon.Client;
public interface IRagonSceneRequestListener
{
void OnRequestScene(RagonClient client, string sceneName);
}
+131 -60
View File
@@ -21,23 +21,25 @@ namespace Ragon.Client
public sealed class RagonClient public sealed class RagonClient
{ {
private readonly INetworkConnection _connection; private readonly INetworkConnection _connection;
private readonly IRagonEntityListener _entityListener; private readonly NetworkStatistics _stats;
private readonly IRagonSceneCollector _sceneCollector; private IRagonEntityListener _entityListener;
private Handler[] _processors; private IRagonSceneCollector _sceneCollector;
private IHandler[] _handlers;
private RagonBuffer _readBuffer; private RagonBuffer _readBuffer;
private RagonBuffer _writeBuffer; private RagonBuffer _writeBuffer;
private RagonRoom _room; private RagonRoom _room;
private RagonSession _session; private RagonSession _session;
private RagonListenerList _listenerList; private RagonListenerList _listeners;
private RagonPlayerCache _playerCache; private RagonPlayerCache _playerCache;
private RagonEntityCache _entityCache; private RagonEntityCache _entityCache;
private RagonEventCache _eventCache; private RagonEventCache _eventCache;
private RagonStatus _status; private RagonStatus _status;
private NetworkStatistics _stats;
private float _replicatationRate = 0; private double _serverTimestamp;
private float _replicatationTime = 0; private float _replicationRate = 0;
private float _replicationTime = 0;
public double ServerTimestamp => _serverTimestamp;
public IRagonConnection Connection => _connection; public IRagonConnection Connection => _connection;
public RagonStatus Status => _status; public RagonStatus Status => _status;
public RagonSession Session => _session; public RagonSession Session => _session;
@@ -45,76 +47,83 @@ namespace Ragon.Client
public RagonEntityCache Entity => _entityCache; public RagonEntityCache Entity => _entityCache;
public NetworkStatistics Statistics => _stats; public NetworkStatistics Statistics => _stats;
public RagonRoom Room => _room; public RagonRoom Room => _room;
internal RagonBuffer Buffer => _writeBuffer; internal RagonBuffer Buffer => _writeBuffer;
internal INetworkChannel Reliable => _connection.Reliable; internal INetworkChannel Reliable => _connection.Reliable;
internal INetworkChannel Unreliable => _connection.Unreliable; internal INetworkChannel Unreliable => _connection.Unreliable;
#region PUBLIC #region PUBLIC
public RagonClient( public RagonClient(INetworkConnection connection, int rate)
INetworkConnection connection,
IRagonEntityListener entityListener,
IRagonSceneCollector sceneCollector,
int rate)
{ {
_listenerList = new RagonListenerList(this); _listeners = new RagonListenerList(this);
_entityListener = entityListener;
_sceneCollector = sceneCollector;
_connection = connection; _connection = connection;
_connection.OnData += OnData; _connection.OnData += OnData;
_connection.OnConnected += OnConnected; _connection.OnConnected += OnConnected;
_connection.OnDisconnected += OnDisconnected; _connection.OnDisconnected += OnDisconnected;
_replicatationRate = (1000.0f / rate) / 1000.0f; _replicationRate = (1000.0f / rate) / 1000.0f;
_replicatationTime = 0; _replicationTime = 0;
_eventCache = new RagonEventCache(); _eventCache = new RagonEventCache();
_stats = new NetworkStatistics(); _stats = new NetworkStatistics();
_status = RagonStatus.DISCONNECTED; _status = RagonStatus.DISCONNECTED;
} }
public void AddListener(IRagonListener listener)
public void Configure(IRagonSceneCollector sceneCollector)
{ {
_listenerList.Add(listener); _sceneCollector = sceneCollector;
} }
public void RemoveListener(IRagonListener listener) public void Configure(IRagonEntityListener listener)
{ {
_listenerList.Remove(listener); _entityListener = listener;
} }
public void Connect(string address, ushort port, string protocol) public void Connect(string address, ushort port, string protocol)
{ {
if (_sceneCollector == null)
{
RagonLog.Error("Scene collector is not defined!");
return;
}
if (_entityListener == null)
{
RagonLog.Error("Entity Listener is not defined!");
return;
}
_writeBuffer = new RagonBuffer(); _writeBuffer = new RagonBuffer();
_readBuffer = new RagonBuffer(); _readBuffer = new RagonBuffer();
_session = new RagonSession(this, _readBuffer); _session = new RagonSession(this, _writeBuffer);
_playerCache = new RagonPlayerCache(); _playerCache = new RagonPlayerCache();
_entityCache = new RagonEntityCache(this, _playerCache, _entityListener, _sceneCollector); _entityCache = new RagonEntityCache(this, _playerCache, _sceneCollector);
_processors = new Handler[byte.MaxValue];
_processors[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(_listenerList);
_processors[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listenerList);
_processors[(byte)RagonOperation.JOIN_SUCCESS] =
new JoinSuccessHandler(this, _readBuffer, _listenerList, _playerCache, _entityCache);
_processors[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listenerList);
_processors[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listenerList, _entityCache);
_processors[(byte)RagonOperation.OWNERSHIP_CHANGED] =
new OwnershipHandler(_listenerList, _playerCache, _entityCache);
_processors[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, _listenerList);
_processors[(byte)RagonOperation.PLAYER_LEAVED] =
new PlayerLeftHandler(_entityCache, _playerCache, _listenerList);
_processors[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, _listenerList);
_processors[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateHandler(this, _playerCache, _entityCache);
_processors[(byte)RagonOperation.DESTROY_ENTITY] = new EntityDestroyHandler(_entityCache);
_processors[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache);
_processors[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] =
new EntityEventHandler(this, _playerCache, _entityCache);
_processors[(byte)RagonOperation.SNAPSHOT] =
new SnapshotHandler(this, _listenerList, _entityCache, _playerCache);
_handlers = new IHandler[byte.MaxValue];
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, _listeners);
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listeners);
_handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, _listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listeners);
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners, _entityCache);
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = new OwnershipRoomHandler(_listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.OWNERSHIP_ENTITY_CHANGED] = new EntityOwnershipHandler(_listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, _listeners);
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, _listeners);
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, _listeners);
_handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateHandler(this, _playerCache, _entityCache, _entityListener);
_handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityRemoveHandler(_entityCache);
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache);
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventHandler(_playerCache, _entityCache);
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache);
_handlers[(byte)RagonOperation.SNAPSHOT] = new SnapshotHandler(this, _listeners, _entityCache, _playerCache, _entityListener);
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampHandler(this);
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataHandler(_playerCache, _listeners);
_handlers[(byte)RagonOperation.ROOM_LIST_UPDATED] = new RoomListHandler(_session, _listeners);
var protocolRaw = RagonVersion.Parse(protocol); var protocolRaw = RagonVersion.Parse(protocol);
_connection.Connect(address, port, protocolRaw); _connection.Connect(address, port, protocolRaw);
} }
@@ -124,66 +133,128 @@ namespace Ragon.Client
_status = RagonStatus.DISCONNECTED; _status = RagonStatus.DISCONNECTED;
_room.Cleanup(); _room.Cleanup();
_connection.Disconnect(); _connection.Disconnect();
OnDisconnected(DisconnectReason.MANUAL);
OnDisconnected(RagonDisconnect.MANUAL);
} }
public void Update(float dt) public void Update(float dt)
{ {
_replicatationTime += dt; if (_status != RagonStatus.DISCONNECTED)
if (_replicatationTime >= _replicatationRate)
{ {
_entityCache.WriteState(_readBuffer); _replicationTime += dt;
_replicatationTime = 0; if (_replicationTime >= _replicationRate)
{
_replicationTime = 0;
_entityCache.WriteState(_writeBuffer);
SendTimestamp();
}
_stats.Update(_connection.BytesSent, _connection.BytesReceived, _connection.Ping, dt);
} }
_stats.Update(_connection.BytesSent, _connection.BytesReceived, _connection.Ping, dt); _listeners.Update();
_connection.Update(); _connection.Update();
} }
public void Dispose() public void Dispose()
{ {
_status = RagonStatus.DISCONNECTED; if (_status != RagonStatus.DISCONNECTED)
_connection.Disconnect(); {
_status = RagonStatus.DISCONNECTED;
_connection.Disconnect();
}
_connection.Dispose(); _connection.Dispose();
} }
public void AddListener(IRagonListener listener) => _listeners.Add(listener);
public void AddListener(IRagonAuthorizationListener listener) => _listeners.Add(listener);
public void AddListener(IRagonConnectionListener listener) => _listeners.Add(listener);
public void AddListener(IRagonFailedListener listener) => _listeners.Add(listener);
public void AddListener(IRagonJoinListener listener) => _listeners.Add(listener);
public void AddListener(IRagonLeftListener listener) => _listeners.Add(listener);
public void AddListener(IRagonOwnershipChangedListener listener) => _listeners.Add(listener);
public void AddListener(IRagonPlayerJoinListener listener) => _listeners.Add(listener);
public void AddListener(IRagonPlayerLeftListener listener) => _listeners.Add(listener);
public void AddListener(IRagonSceneListener listener) => _listeners.Add(listener);
public void AddListener(IRagonSceneRequestListener listener) => _listeners.Add(listener);
public void AddListener(IRagonDataListener listener) => _listeners.Add(listener);
public void AddListener(IRagonRoomListListener listener) => _listeners.Add(listener);
public void RemoveListener(IRagonListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonAuthorizationListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonConnectionListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonFailedListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonJoinListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonLeftListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonOwnershipChangedListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonPlayerJoinListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonPlayerLeftListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonSceneListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonSceneRequestListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonDataListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonRoomListListener listener) => _listeners.Remove(listener);
#endregion #endregion
#region INTERNAL #region INTERNAL
internal void AssignRoom(RagonRoom room) internal void AssignRoom(RagonRoom room)
{ {
_room?.Dispose();
_room = room; _room = room;
_status = RagonStatus.ROOM; }
internal void SetStatus(RagonStatus status)
{
_status = status;
}
internal void SetTimestamp(double time)
{
_serverTimestamp = time;
} }
#endregion #endregion
#region PRIVATE #region PRIVATE
private void SendTimestamp()
{
var timestamp = RagonTime.CurrentTimestamp();
var value = new DoubleToUInt()
{
Double = timestamp,
};
_writeBuffer.Clear();
_writeBuffer.WriteOperation(RagonOperation.TIMESTAMP_SYNCHRONIZATION);
_writeBuffer.Write(value.Int0, 32);
_writeBuffer.Write(value.Int1, 32);
}
private void OnConnected() private void OnConnected()
{ {
RagonLog.Trace("Connected"); RagonLog.Trace("Connected");
_listenerList.OnConnected(); _listeners.OnConnected();
_status = RagonStatus.CONNECTED; _status = RagonStatus.CONNECTED;
} }
private void OnDisconnected(DisconnectReason reason) private void OnDisconnected(RagonDisconnect reason)
{ {
RagonLog.Trace($"Disconnected: {reason}"); RagonLog.Trace($"Disconnected: {reason}");
_listenerList.OnDisconnected(); _listeners.OnDisconnected(reason);
_status = RagonStatus.DISCONNECTED; _status = RagonStatus.DISCONNECTED;
} }
public void OnData(byte[] data) private void OnData(byte[] data)
{ {
_readBuffer.Clear(); _readBuffer.Clear();
_readBuffer.FromArray(data); _readBuffer.FromArray(data);
var operation = _readBuffer.ReadByte(); var operation = _readBuffer.ReadByte();
_processors[operation].Handle(_readBuffer); _handlers[operation].Handle(_readBuffer);
} }
#endregion #endregion
+100 -75
View File
@@ -24,67 +24,80 @@ public sealed class RagonEntityCache
private readonly Dictionary<uint, RagonEntity> _entityMap = new(); private readonly Dictionary<uint, RagonEntity> _entityMap = new();
private readonly Dictionary<uint, RagonEntity> _pendingEntities = new(); private readonly Dictionary<uint, RagonEntity> _pendingEntities = new();
private readonly Dictionary<uint, RagonEntity> _sceneEntities = new(); private readonly Dictionary<uint, RagonEntity> _sceneEntities = new();
private readonly RagonClient _client; private readonly RagonClient _client;
private readonly IRagonEntityListener _entityListener;
private readonly IRagonSceneCollector _sceneCollector; private readonly IRagonSceneCollector _sceneCollector;
private readonly RagonPlayerCache _playerCache; private readonly RagonPlayerCache _playerCache;
private int _localEntitiesCounter = 0; private int _localEntitiesCounter = 0;
public RagonEntityCache( public RagonEntityCache(
RagonClient client, RagonClient client,
RagonPlayerCache playerCache, RagonPlayerCache playerCache,
IRagonEntityListener listener,
IRagonSceneCollector sceneCollector IRagonSceneCollector sceneCollector
) )
{ {
_client = client; _client = client;
_entityListener = listener;
_sceneCollector = sceneCollector; _sceneCollector = sceneCollector;
_playerCache = playerCache; _playerCache = playerCache;
} }
public RagonEntity FindById(ushort id) public bool TryGetEntity(ushort id, out RagonEntity entity)
{ {
return _entityMap[id]; return _entityMap.TryGetValue(id, out entity);
} }
public void Create(RagonEntity entity, IRagonPayload? spawnPayload) public void Create(RagonEntity entity, RagonPayload spawnPayload)
{ {
var attachId = (ushort) (_playerCache.LocalPlayer.PeerId + _localEntitiesCounter++) ; var attachId = (ushort)(_playerCache.Local.PeerId + _localEntitiesCounter++);
var buffer = _client.Buffer; var buffer = _client.Buffer;
buffer.Clear(); buffer.Clear();
buffer.WriteOperation(RagonOperation.CREATE_ENTITY); buffer.WriteOperation(RagonOperation.CREATE_ENTITY);
buffer.WriteUShort(attachId); buffer.WriteUShort(attachId);
buffer.WriteUShort(entity.Type); buffer.WriteUShort(entity.Type);
buffer.WriteByte((byte) entity.Authority); buffer.WriteByte((byte)entity.Authority);
entity.State.WriteInfo(buffer); entity.State.WriteInfo(buffer);
spawnPayload?.Serialize(buffer); spawnPayload?.Write(buffer);
_pendingEntities.Add(attachId, entity); _pendingEntities.Add(attachId, entity);
var sendData = buffer.ToArray(); var sendData = buffer.ToArray();
_client.Reliable.Send(sendData); _client.Reliable.Send(sendData);
} }
public void Destroy(RagonEntity entity, IRagonPayload? destroyPayload) public void Transfer(RagonEntity entity, RagonPlayer player)
{ {
if (!entity.IsAttached) var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.TRANSFER_ENTITY_OWNERSHIP);
buffer.WriteUShort(entity.Id);
buffer.WriteUShort(player.PeerId);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
public void Destroy(RagonEntity entity, RagonPayload destroyPayload)
{
if (!entity.IsAttached && !entity.HasAuthority)
{ {
RagonLog.Warn("Can't destroy object, he is not created"); RagonLog.Warn("Can't destroy object");
return; return;
} }
entity.SetReplication(false);
var buffer = _client.Buffer; var buffer = _client.Buffer;
buffer.Clear(); buffer.Clear();
buffer.WriteOperation(RagonOperation.DESTROY_ENTITY); buffer.WriteOperation(RagonOperation.REMOVE_ENTITY);
buffer.WriteUShort(entity.Id); buffer.WriteUShort(entity.Id);
destroyPayload?.Serialize(buffer); destroyPayload?.Write(buffer);
var sendData = buffer.ToArray(); var sendData = buffer.ToArray();
_client.Reliable.Send(sendData); _client.Reliable.Send(sendData);
@@ -93,7 +106,7 @@ public sealed class RagonEntityCache
internal void WriteState(RagonBuffer buffer) internal void WriteState(RagonBuffer buffer)
{ {
var changedEntities = 0u; var changedEntities = 0u;
buffer.Clear(); buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE); buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
@@ -103,36 +116,36 @@ public sealed class RagonEntityCache
foreach (var ent in _entityList) foreach (var ent in _entityList)
{ {
if (!ent.IsAttached || if (!ent.IsAttached ||
!ent.Replication || !ent.IsReplicated ||
!ent.PropertiesChanged) continue; !ent.PropertiesChanged) continue;
ent.Write(buffer); ent.Write(buffer);
changedEntities++; changedEntities++;
} }
if (changedEntities <= 0) return; if (changedEntities <= 0) return;
buffer.Write(changedEntities, 16, offset); buffer.Write(changedEntities, 16, offset);
var data = buffer.ToArray(); var data = buffer.ToArray();
_client.Unreliable.Send(data); _client.Unreliable.Send(data);
} }
internal void WriteScene(RagonBuffer buffer) internal void WriteScene(RagonBuffer buffer)
{ {
_sceneEntities.Clear(); _sceneEntities.Clear();
var entities = _sceneCollector.Collect(); var entities = _sceneCollector.Collect();
buffer.WriteUShort((ushort) entities.Length); buffer.WriteUShort((ushort)entities.Length);
foreach (var entity in entities) foreach (var entity in entities)
{ {
buffer.WriteUShort(entity.Type); buffer.WriteUShort(entity.Type);
buffer.WriteByte((byte) entity.Authority); buffer.WriteByte((byte)entity.Authority);
buffer.WriteUShort(entity.SceneId); buffer.WriteUShort(entity.SceneId);
entity.State.WriteInfo(buffer); entity.State.WriteInfo(buffer);
_sceneEntities.Add(entity.SceneId, entity); _sceneEntities.Add(entity.SceneId, entity);
} }
} }
@@ -140,69 +153,72 @@ public sealed class RagonEntityCache
internal void CacheScene() internal void CacheScene()
{ {
_sceneEntities.Clear(); _sceneEntities.Clear();
var entities = _sceneCollector.Collect(); var entities = _sceneCollector.Collect();
foreach (var entity in entities) foreach (var entity in entities)
_sceneEntities.Add(entity.SceneId, entity); _sceneEntities.Add(entity.SceneId, entity);
} }
internal void Cleanup() internal void Cleanup()
{ {
var payload = new RagonPayload(); var payload = new RagonPayload(0);
foreach (var ent in _entityList) foreach (var ent in _entityList)
ent.Detach(payload); ent.Detach(payload);
_entityMap.Clear(); _entityMap.Clear();
_entityList.Clear(); _entityList.Clear();
} }
internal RagonEntity OnCreate(ushort attachId, ushort entityType, ushort sceneId, ushort entityId, bool hasAuthority) internal RagonEntity TryGetEntity(ushort attachId, ushort entityType, ushort sceneId, ushort entityId, bool hasAuthority, out bool hasCreated)
{ {
if (sceneId > 0) if (sceneId > 0)
{ {
if (_sceneEntities.TryGetValue(sceneId, out var entity)) if (_sceneEntities.TryGetValue(sceneId, out var sceneEntity))
{ {
_entityMap.Add(entityId, entity); _entityMap.Add(entityId, sceneEntity);
if (hasAuthority) if (hasAuthority)
_entityList.Add(entity); _entityList.Add(sceneEntity);
return entity; hasCreated = false;
return sceneEntity;
} }
} }
if (_pendingEntities.Remove(attachId, out var existsEntity))
{
_entityMap.Add(entityId, existsEntity);
if (hasAuthority)
_entityList.Add(existsEntity);
return existsEntity;
}
else
{
var entity = new RagonEntity(entityType, sceneId);
_entityMap.Add(entityId, entity);
if (hasAuthority)
_entityList.Add(entity);
_entityListener.OnEntityCreated(entity);
return entity; if (_pendingEntities.TryGetValue(attachId, out var pendingEntity) && hasAuthority)
{
_pendingEntities.Remove(attachId);
_entityMap.Add(entityId, pendingEntity);
_entityList.Add(pendingEntity);
hasCreated = false;
return pendingEntity;
} }
var entity = new RagonEntity(entityType, sceneId);
_entityMap.Add(entityId, entity);
if (hasAuthority)
_entityList.Add(entity);
hasCreated = true;
return entity;
} }
internal void OnDestroy(ushort entityId, RagonPayload payload) internal void OnDestroy(ushort entityId, RagonPayload payload)
{ {
if (_entityMap.Remove(entityId, out var ragonEntity)) if (_entityMap.TryGetValue(entityId, out var entity))
{ {
_entityList.Remove(ragonEntity); _entityMap.Remove(entityId);
_entityList.Remove(entity);
ragonEntity.Detach(payload);
entity.Detach(payload);
entity.Dispose();
} }
} }
@@ -210,7 +226,7 @@ public sealed class RagonEntityCache
{ {
if (_entityMap.TryGetValue(entityId, out var entity)) if (_entityMap.TryGetValue(entityId, out var entity))
entity.Read(buffer); entity.Read(buffer);
else else
RagonLog.Warn($"Entity {entityId} not found!"); RagonLog.Warn($"Entity {entityId} not found!");
} }
@@ -218,15 +234,24 @@ public sealed class RagonEntityCache
{ {
if (_entityMap.TryGetValue(entityId, out var entity)) if (_entityMap.TryGetValue(entityId, out var entity))
entity.Event(eventCode, player, buffer); entity.Event(eventCode, player, buffer);
else else
RagonLog.Warn($"Entity {entityId} not found!"); RagonLog.Warn($"Entity {entityId} not found!");
} }
internal void OnOwnershipChanged(RagonPlayer player, ushort entityId) internal void OnOwnershipChanged(RagonPlayer player, ushort entityId)
{ {
if (_entityMap.TryGetValue(entityId, out var entity)) if (_entityMap.TryGetValue(entityId, out var entity))
{
if (player.IsLocal)
_entityList.Add(entity);
else
_entityList.Remove(entity);
entity.OnOwnershipChanged(player); entity.OnOwnershipChanged(player);
}
else else
{
RagonLog.Warn($"Entity {entityId} not found!"); RagonLog.Warn($"Entity {entityId} not found!");
}
} }
} }
+6 -2
View File
@@ -26,8 +26,12 @@ public class RagonEventCache
public ushort GetEventCode<TEvent>(TEvent _) where TEvent : IRagonEvent public ushort GetEventCode<TEvent>(TEvent _) where TEvent : IRagonEvent
{ {
var type = typeof(TEvent); var type = typeof(TEvent);
var evntCode = _eventsRegistryByType[type]; if (!_eventsRegistryByType.TryGetValue(type, out var eventCode))
return evntCode; {
RagonLog.Error($"Event with type {type} not registered");
return 0;
}
return eventCode;
} }
public void Register<T>() where T : IRagonEvent, new() public void Register<T>() where T : IRagonEvent, new()
+202 -24
View File
@@ -14,93 +14,271 @@
* limitations under the License. * limitations under the License.
*/ */
using Ragon.Protocol;
namespace Ragon.Client namespace Ragon.Client
{ {
internal class RagonListenerList internal class RagonListenerList
{ {
public int Count => _listeners.Count; private readonly RagonClient _client;
private List<IRagonListener> _listeners = new(); private readonly List<IRagonAuthorizationListener> _authorizationListeners = new();
private RagonClient _client; private readonly List<IRagonConnectionListener> _connectionListeners = new();
private readonly List<IRagonFailedListener> _failedListeners = new();
private readonly List<IRagonJoinListener> _joinListeners = new();
private readonly List<IRagonLeftListener> _leftListeners = new();
private readonly List<IRagonSceneListener> _sceneListeners = new();
private readonly List<IRagonSceneRequestListener> _sceneRequestListeners = new();
private readonly List<IRagonOwnershipChangedListener> _ownershipChangedListeners = new();
private readonly List<IRagonPlayerJoinListener> _playerJoinListeners = new();
private readonly List<IRagonPlayerLeftListener> _playerLeftListeners = new();
private readonly List<IRagonDataListener> _dataListeners = new();
private readonly List<IRagonRoomListListener> _roomListListeners = new();
private readonly List<Action> _delayedActions = new();
public RagonListenerList(RagonClient client) public RagonListenerList(RagonClient client)
{ {
_client = client; _client = client;
} }
public void Add(IRagonListener listener) public void Add(IRagonListener listener)
{ {
_listeners.Add(listener); _authorizationListeners.Add(listener);
_connectionListeners.Add(listener);
_failedListeners.Add(listener);
_joinListeners.Add(listener);
_leftListeners.Add(listener);
_sceneListeners.Add(listener);
_ownershipChangedListeners.Add(listener);
_playerJoinListeners.Add(listener);
_playerLeftListeners.Add(listener);
} }
public void Remove(IRagonListener listener) public void Remove(IRagonListener listener)
{ {
_listeners.Remove(listener); _delayedActions.Add(() =>
{
_authorizationListeners.Remove(listener);
_connectionListeners.Remove(listener);
_failedListeners.Remove(listener);
_joinListeners.Remove(listener);
_leftListeners.Remove(listener);
_sceneListeners.Remove(listener);
_ownershipChangedListeners.Remove(listener);
_playerJoinListeners.Remove(listener);
_playerLeftListeners.Remove(listener);
});
}
public void Update()
{
foreach (var action in _delayedActions)
action.Invoke();
_delayedActions.Clear();
}
public void Add(IRagonDataListener dataListener)
{
_dataListeners.Add(dataListener);
} }
public void OnAuthorizationSuccess(string playerId, string playerName) public void Add(IRagonAuthorizationListener listener)
{ {
foreach (var listener in _listeners) _authorizationListeners.Add(listener);
}
public void Add(IRagonSceneRequestListener listener)
{
_sceneRequestListeners.Add(listener);
}
public void Add(IRagonConnectionListener listener)
{
_connectionListeners.Add(listener);
}
public void Add(IRagonFailedListener listener)
{
_failedListeners.Add(listener);
}
public void Add(IRagonJoinListener listener)
{
_joinListeners.Add(listener);
}
public void Add(IRagonLeftListener listener)
{
_leftListeners.Add(listener);
}
public void Add(IRagonSceneListener listener)
{
_sceneListeners.Add(listener);
}
public void Add(IRagonOwnershipChangedListener listener)
{
_ownershipChangedListeners.Add(listener);
}
public void Add(IRagonPlayerJoinListener listener)
{
_playerJoinListeners.Add(listener);
}
public void Add(IRagonPlayerLeftListener listener)
{
_playerLeftListeners.Add(listener);
}
public void Add(IRagonRoomListListener listener)
{
_roomListListeners.Add(listener);
}
public void Remove(IRagonDataListener listener)
{
_delayedActions.Add(() => _dataListeners.Remove(listener));
}
public void Remove(IRagonSceneRequestListener listener)
{
_delayedActions.Add(() => _sceneRequestListeners.Remove(listener));
}
public void Remove(IRagonAuthorizationListener listener)
{
_delayedActions.Add(() => _authorizationListeners.Remove(listener));
}
public void Remove(IRagonConnectionListener listener)
{
_delayedActions.Add(() => _connectionListeners.Remove(listener));
}
public void Remove(IRagonFailedListener listener)
{
_delayedActions.Add(() => _failedListeners.Remove(listener));
}
public void Remove(IRagonJoinListener listener)
{
_delayedActions.Add(() => _joinListeners.Remove(listener));
}
public void Remove(IRagonLeftListener listener)
{
_delayedActions.Add(() => _leftListeners.Remove(listener));
}
public void Remove(IRagonSceneListener listener)
{
_delayedActions.Add(() => _sceneListeners.Remove(listener));
}
public void Remove(IRagonOwnershipChangedListener listener)
{
_delayedActions.Add(() => _ownershipChangedListeners.Remove(listener));
}
public void Remove(IRagonPlayerJoinListener listener)
{
_delayedActions.Add(() => _playerJoinListeners.Remove(listener));
}
public void Remove(IRagonPlayerLeftListener listener)
{
_delayedActions.Add(() => _playerLeftListeners.Remove(listener));
}
public void Remove(IRagonRoomListListener listener)
{
_delayedActions.Add(() => _roomListListeners.Remove(listener));
}
public void OnAuthorizationSuccess(string playerId, string playerName, string payload)
{
foreach (var listener in _authorizationListeners)
listener.OnAuthorizationSuccess(_client, playerId, playerName); listener.OnAuthorizationSuccess(_client, playerId, playerName);
} }
public void OnAuthorizationFailed(string message) public void OnAuthorizationFailed(string message)
{ {
foreach (var listener in _listeners) foreach (var listener in _authorizationListeners)
listener.OnAuthorizationFailed(_client, message); listener.OnAuthorizationFailed(_client, message);
} }
public void OnLeft() public void OnLeft()
{ {
foreach (var listener in _listeners) foreach (var listener in _leftListeners)
listener.OnLeft(_client); listener.OnLeft(_client);
} }
public void OnFailed(string message) public void OnFailed(string message)
{ {
foreach (var listener in _listeners) foreach (var listener in _failedListeners)
listener.OnFailed(_client, message); listener.OnFailed(_client, message);
} }
public void OnOwnershipChanged(RagonPlayer player) public void OnOwnershipChanged(RagonPlayer player)
{ {
foreach (var listener in _listeners) foreach (var listener in _ownershipChangedListeners)
listener.OnOwnershipChanged(_client, player); listener.OnOwnershipChanged(_client, player);
} }
public void OnPlayerLeft(RagonPlayer player) public void OnPlayerLeft(RagonPlayer player)
{ {
foreach (var listener in _listeners) foreach (var listener in _playerLeftListeners)
listener.OnPlayerLeft(_client, player); listener.OnPlayerLeft(_client, player);
} }
public void OnPlayerJoined(RagonPlayer player) public void OnPlayerJoined(RagonPlayer player)
{ {
foreach (var listener in _listeners) foreach (var listener in _playerJoinListeners)
listener.OnPlayerJoined(_client, player); listener.OnPlayerJoined(_client, player);
} }
public void OnLevel(string sceneName) public void OnSceneLoaded()
{ {
foreach (var listener in _listeners) foreach (var listener in _sceneListeners)
listener.OnLevel(_client, sceneName); listener.OnSceneLoaded(_client);
}
public void OnSceneRequest(string sceneName)
{
foreach (var listener in _sceneRequestListeners)
listener.OnRequestScene(_client, sceneName);
} }
public void OnJoined() public void OnJoined()
{ {
foreach (var listener in _listeners) foreach (var listener in _joinListeners)
listener.OnJoined(_client); listener.OnJoined(_client);
} }
public void OnConnected() public void OnConnected()
{ {
foreach (var listener in _listeners) foreach (var listener in _connectionListeners)
listener.OnConnected(_client); listener.OnConnected(_client);
} }
public void OnDisconnected() public void OnDisconnected(RagonDisconnect disconnect)
{ {
foreach (var listener in _listeners) foreach (var listener in _connectionListeners)
listener.OnDisconnected(_client); listener.OnDisconnected(_client, disconnect);
}
public void OnData(RagonPlayer player, byte[] data)
{
foreach (var listener in _dataListeners)
listener.OnData(player, data);
}
public void OnRoomList(RagonRoomInformation[] roomInfos)
{
foreach (var listListener in _roomListListeners)
listListener.OnRoomListUpdate(roomInfos);
} }
} }
} }
+3 -3
View File
@@ -23,13 +23,13 @@ namespace Ragon.Client
public string Name { get; set; } public string Name { get; set; }
public ushort PeerId { get; set; } public ushort PeerId { get; set; }
public bool IsRoomOwner { get; set; } public bool IsRoomOwner { get; set; }
public bool IsMe { get; set; } public bool IsLocal { get; set; }
public RagonPlayer(ushort peerId, string playerId, string name, bool isRoomOwner, bool isMe) public RagonPlayer(ushort peerId, string playerId, string name, bool isRoomOwner, bool isLocal)
{ {
PeerId = peerId; PeerId = peerId;
IsRoomOwner = isRoomOwner; IsRoomOwner = isRoomOwner;
IsMe = isMe; IsLocal = isLocal;
Name = name; Name = name;
Id = playerId; Id = playerId;
} }
+40 -13
View File
@@ -18,16 +18,30 @@ namespace Ragon.Client;
public sealed class RagonPlayerCache public sealed class RagonPlayerCache
{ {
private List<RagonPlayer> _players = new List<RagonPlayer>(); private readonly List<RagonPlayer> _players = new();
private Dictionary<string, RagonPlayer> _playersById = new(); private readonly Dictionary<string, RagonPlayer> _playersById = new();
private Dictionary<ushort, RagonPlayer> _playersByConnection = new(); private readonly Dictionary<ushort, RagonPlayer> _playersByConnection = new();
public IReadOnlyList<RagonPlayer> Players => _players;
public RagonPlayer Owner { get; private set; } public RagonPlayer Owner { get; private set; }
public RagonPlayer LocalPlayer { get; private set; } public RagonPlayer Local { get; private set; }
public bool IsRoomOwner => _ownerId == _localId; public bool IsRoomOwner => _ownerId == _localId;
public RagonPlayer? GetPlayerById(string playerId) => _playersById[playerId]; public RagonPlayer? GetPlayerById(string playerId)
public RagonPlayer? GetPlayerByPeer(ushort peerId) => _playersByConnection[peerId]; {
if (_playersById.TryGetValue(playerId, out var player))
return player;
return null;
}
public RagonPlayer? GetPlayerByPeer(ushort peerId)
{
if (_playersByConnection.TryGetValue(peerId, out var player))
return player;
return null;
}
private string _ownerId; private string _ownerId;
private string _localId; private string _localId;
@@ -50,8 +64,8 @@ public sealed class RagonPlayerCache
var player = new RagonPlayer(peerId, playerId, playerName, isOwner, isLocal); var player = new RagonPlayer(peerId, playerId, playerName, isOwner, isLocal);
if (player.IsMe) if (player.IsLocal)
LocalPlayer = player; Local = player;
if (player.IsRoomOwner) if (player.IsRoomOwner)
Owner = player; Owner = player;
@@ -63,20 +77,23 @@ public sealed class RagonPlayerCache
public void RemovePlayer(string playerId) public void RemovePlayer(string playerId)
{ {
if (_playersById.Remove(playerId, out var player)) if (_playersById.TryGetValue(playerId, out var player))
{ {
_players.Remove(player); _players.Remove(player);
_playersById.Remove(playerId);
_playersByConnection.Remove(player.PeerId); _playersByConnection.Remove(player.PeerId);
} }
} }
public void OnOwnershipChanged(string playerId) public void OnOwnershipChanged(ushort playerPeerId)
{ {
foreach (var player in _players) foreach (var player in _players)
{ {
if (player.Id == playerId) if (player.PeerId == playerPeerId)
{
Owner = player; Owner = player;
player.IsRoomOwner = player.Id == playerId; Owner.IsRoomOwner = true;
}
} }
} }
@@ -87,4 +104,14 @@ public sealed class RagonPlayerCache
_playersByConnection.Clear(); _playersByConnection.Clear();
_playersById.Clear(); _playersById.Clear();
} }
public void Dump()
{
RagonLog.Trace("Players: ");
RagonLog.Trace("[Connection] [ID] [Name]");
foreach (var player in _players)
{
RagonLog.Trace($"[{player.PeerId}] {player.Id} {player.Name}");
}
}
} }
+110 -11
View File
@@ -14,31 +14,68 @@
* limitations under the License. * limitations under the License.
*/ */
using Ragon.Protocol;
namespace Ragon.Client namespace Ragon.Client
{ {
public class RagonRoom public class RagonRoom: IDisposable
{ {
private class EventSubscription : IDisposable
{
private List<Action<RagonPlayer, IRagonEvent>> _callbacks;
private List<Action<RagonPlayer, IRagonEvent>> _localCallbacks;
private Action<RagonPlayer, IRagonEvent> _callback;
public EventSubscription(
List<Action<RagonPlayer, IRagonEvent>> callbacks,
List<Action<RagonPlayer, IRagonEvent>> localCallbacks,
Action<RagonPlayer, IRagonEvent> callback)
{
_callbacks = callbacks;
_localCallbacks = localCallbacks;
_callback = callback;
}
public void Dispose()
{
_callbacks?.Remove(_callback);
_localCallbacks?.Remove(_callback);
_callbacks = null!;
_localCallbacks = null!;
_callback = null!;
}
}
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
private RagonClient _client; private RagonClient _client;
private RagonScene _scene; private RagonScene _scene;
private RagonEntityCache _entityCache; private RagonEntityCache _entityCache;
private RagonPlayerCache _playerCache; private RagonPlayerCache _playerCache;
private RagonRoomInformation _information; private RoomParameters _parameters;
public string Id => _information.RoomId; public string Id => _parameters.RoomId;
public int MinPlayers => _information.Min; public int MinPlayers => _parameters.Min;
public int MaxPlayers => _information.Max; public int MaxPlayers => _parameters.Max;
public string Scene => _scene.Name;
public RagonPlayer Local => _playerCache.LocalPlayer; public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
public RagonPlayer Local => _playerCache.Local;
public RagonPlayer Owner => _playerCache.Owner; public RagonPlayer Owner => _playerCache.Owner;
private readonly Dictionary<int, OnEventDelegate> _events = new Dictionary<int, OnEventDelegate>();
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
public RagonRoom(RagonClient client, public RagonRoom(RagonClient client,
RagonEntityCache entityCache, RagonEntityCache entityCache,
RagonPlayerCache playerCache, RagonPlayerCache playerCache,
RagonRoomInformation information, RoomParameters parameters,
RagonScene scene) RagonScene scene)
{ {
_client = client; _client = client;
_information = information; _parameters = parameters;
_entityCache = entityCache; _entityCache = entityCache;
_playerCache = playerCache; _playerCache = playerCache;
_scene = scene; _scene = scene;
@@ -50,13 +87,75 @@ namespace Ragon.Client
_playerCache.Cleanup(); _playerCache.Cleanup();
} }
public void LoadScene(string map) => _scene.Load(map); internal void Update(string sceneName)
{
_scene.Update(sceneName);
}
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
{
if (_events.TryGetValue(eventCode, out var evnt))
evnt?.Invoke(caller, buffer);
else
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
}
public IDisposable OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
{
var t = new TEvent();
var eventCode = _client.Event.GetEventCode(t);
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
if (!_listeners.TryGetValue(eventCode, out var callbacks))
{
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
_listeners.Add(eventCode, callbacks);
}
if (!_localListeners.TryGetValue(eventCode, out var localCallbacks))
{
localCallbacks = new List<Action<RagonPlayer, IRagonEvent>>();
_localListeners.Add(eventCode, localCallbacks);
}
callbacks.Add(action);
localCallbacks.Add(action);
if (!_events.ContainsKey(eventCode))
{
_events.Add(eventCode, (player, serializer) =>
{
t.Deserialize(serializer);
foreach (var callbackListener in callbacks)
callbackListener.Invoke(player, t);
});
}
return new EventSubscription(callbacks, localCallbacks, action);
}
public void LoadScene(string sceneName) => _scene.Load(sceneName);
public void SceneLoaded() => _scene.SceneLoaded(); public void SceneLoaded() => _scene.SceneLoaded();
public void ReplicateEvent<TEvent>(TEvent evnt, RagonTarget target, RagonReplicationMode mode) where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode mode) where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
public void ReplicateData(byte[] data, bool reliable = false) => _scene.ReplicateData(data, reliable);
public void CreateEntity(RagonEntity entity) => CreateEntity(entity, null); public void CreateEntity(RagonEntity entity) => CreateEntity(entity, null);
public void CreateEntity(RagonEntity entity, IRagonPayload? payload) => _entityCache.Create(entity, payload); public void CreateEntity(RagonEntity entity, RagonPayload payload) => _entityCache.Create(entity, payload);
public void TransferEntity(RagonEntity entity, RagonPlayer player) => _entityCache.Transfer(entity, player);
public void DestroyEntity(RagonEntity entityId) => DestroyEntity(entityId, null); public void DestroyEntity(RagonEntity entityId) => DestroyEntity(entityId, null);
public void DestroyEntity(RagonEntity entityId, IRagonPayload? payload) => _entityCache.Destroy(entityId, payload); public void DestroyEntity(RagonEntity entityId, RagonPayload payload) => _entityCache.Destroy(entityId, payload);
public void Dispose()
{
Cleanup();
_events.Clear();
_listeners.Clear();
_localListeners.Clear();
}
} }
} }
@@ -0,0 +1,12 @@
namespace Ragon.Client
{
public struct RagonRoomInformation
{
public string Id;
public string Scene;
public int PlayerMax;
public int PlayerMin;
public int PlayerCount;
public Dictionary<string, byte[]> Properties;
}
}
+59 -3
View File
@@ -20,24 +20,33 @@ namespace Ragon.Client;
public class RagonScene public class RagonScene
{ {
public string Name { get; private set; }
private readonly RagonClient _client; private readonly RagonClient _client;
private readonly RagonEntityCache _entityCache; private readonly RagonEntityCache _entityCache;
private readonly RagonPlayerCache _playerCache; private readonly RagonPlayerCache _playerCache;
public RagonScene(RagonClient client, RagonPlayerCache playerCache, RagonEntityCache entityCache) public RagonScene(RagonClient client, RagonPlayerCache playerCache, RagonEntityCache entityCache, string sceneName)
{ {
Name = sceneName;
_client = client; _client = client;
_playerCache = playerCache; _playerCache = playerCache;
_entityCache = entityCache; _entityCache = entityCache;
} }
internal void Load(string map) internal void Update(string scene)
{
Name = scene;
}
internal void Load(string sceneName)
{ {
var buffer = _client.Buffer; var buffer = _client.Buffer;
buffer.Clear(); buffer.Clear();
buffer.WriteOperation(RagonOperation.LOAD_SCENE); buffer.WriteOperation(RagonOperation.LOAD_SCENE);
buffer.WriteString(map); buffer.WriteString(sceneName);
var sendData = buffer.ToArray(); var sendData = buffer.ToArray();
_client.Reliable.Send(sendData); _client.Reliable.Send(sendData);
@@ -58,4 +67,51 @@ public class RagonScene
var sendData = buffer.ToArray(); var sendData = buffer.ToArray();
_client.Reliable.Send(sendData); _client.Reliable.Send(sendData);
} }
internal void ReplicateEvent<TEvent>(TEvent evnt, RagonTarget target, RagonReplicationMode replicationMode)
where TEvent : IRagonEvent, new()
{
var evntId = _client.Event.GetEventCode(evnt);
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
buffer.WriteUShort(evntId);
buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte)target);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
internal void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
where TEvent : IRagonEvent, new()
{
var evntId = _client.Event.GetEventCode(evnt);
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
buffer.WriteUShort(evntId);
buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte)RagonTarget.Player);
buffer.WriteUShort(target.PeerId);
evnt.Serialize(buffer);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
public void ReplicateData(byte[] data, bool reliable)
{
var sendData = new byte[data.Length + 1];
sendData[0] = (byte) RagonOperation.REPLICATE_RAW_DATA;
Array.Copy(data, 0, sendData, 1, data.Length);
if (reliable)
_client.Reliable.Send(sendData);
else
_client.Unreliable.Send(sendData);
}
} }
+10 -10
View File
@@ -29,13 +29,13 @@ namespace Ragon.Client
_buffer = buffer; _buffer = buffer;
} }
public void CreateOrJoin(string map, int minPlayers, int maxPlayers) public void CreateOrJoin(string sceneName, int minPlayers, int maxPlayers)
{ {
var parameters = new RagonRoomParameters() {Map = map, Min = minPlayers, Max = maxPlayers}; var parameters = new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers};
CreateOrJoin(parameters); CreateOrJoin(parameters);
} }
public void CreateOrJoin(RagonRoomParameters parameters) public void CreateOrJoin(RagonRoomPayload parameters)
{ {
_buffer.Clear(); _buffer.Clear();
_buffer.WriteOperation(RagonOperation.JOIN_OR_CREATE_ROOM); _buffer.WriteOperation(RagonOperation.JOIN_OR_CREATE_ROOM);
@@ -46,17 +46,17 @@ namespace Ragon.Client
_client.Reliable.Send(sendData); _client.Reliable.Send(sendData);
} }
public void Create(string map, int minPlayers, int maxPlayers) public void Create(string sceneName, int minPlayers, int maxPlayers)
{ {
Create(null, new RagonRoomParameters() {Map = map, Min = minPlayers, Max = maxPlayers}); Create(null, new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
} }
public void Create(string roomId, string map, int minPlayers, int maxPlayers) public void Create(string roomId, string sceneName, int minPlayers, int maxPlayers)
{ {
Create(roomId, new RagonRoomParameters() {Map = map, Min = minPlayers, Max = maxPlayers}); Create(roomId, new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
} }
public void Create(string roomId, RagonRoomParameters parameters) public void Create(string roomId, RagonRoomPayload parameters)
{ {
_buffer.Clear(); _buffer.Clear();
_buffer.WriteOperation(RagonOperation.CREATE_ROOM); _buffer.WriteOperation(RagonOperation.CREATE_ROOM);
@@ -93,13 +93,13 @@ namespace Ragon.Client
_client.Reliable.Send(sendData); _client.Reliable.Send(sendData);
} }
public void AuthorizeWithKey(string key, string playerName, byte[] additonalData) public void AuthorizeWithKey(string key, string playerName, string payload = "")
{ {
_buffer.Clear(); _buffer.Clear();
_buffer.WriteOperation(RagonOperation.AUTHORIZE); _buffer.WriteOperation(RagonOperation.AUTHORIZE);
_buffer.WriteString(key); _buffer.WriteString(key);
_buffer.WriteString(playerName); _buffer.WriteString(playerName);
_buffer.WriteBytes(additonalData); _buffer.WriteString(payload);
var sendData = _buffer.ToArray(); var sendData = _buffer.ToArray();
_client.Reliable.Send(sendData); _client.Reliable.Send(sendData);
@@ -0,0 +1,27 @@
using Ragon.Client.Compressor;
using Ragon.Protocol;
namespace Ragon.Client.Utils;
public static class CompressorExtension
{
public static float Read(this FloatCompressor compressor, RagonBuffer buffer)
{
return compressor.Decompress(buffer.Read(compressor.RequiredBits));
}
public static void Write(this FloatCompressor compressor, RagonBuffer buffer, float value)
{
buffer.Write(compressor.Compress(value), compressor.RequiredBits);
}
public static float Read(this IntCompressor compressor, RagonBuffer buffer)
{
return compressor.Decompress(buffer.Read(compressor.RequiredBits));
}
public static void Write(this IntCompressor compressor, RagonBuffer buffer, int value)
{
buffer.Write(compressor.Compress(value), compressor.RequiredBits);
}
}
+10 -3
View File
@@ -4,8 +4,15 @@
<ImplicitUsings>disable</ImplicitUsings> <ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable> <Nullable>disable</Nullable>
<LangVersion>8</LangVersion> <LangVersion>8</LangVersion>
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>Ragon.Common</RootNamespace> <RootNamespace>Ragon.Common</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>Ragon.Protocol</Title>
<Copyright>Eduard Kargin</Copyright>
<PackageProjectUrl>https://ragon-server.com</PackageProjectUrl>
<RepositoryUrl>https://github.com/edmand46/Ragon</RepositoryUrl>
<RepositoryType>Source</RepositoryType>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
@@ -16,8 +23,8 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>C:\Users\edmand46\RagonProjects\ragon-unity-sdk\Assets\Ragon-Unity-SDK\Runtime\Plugins</OutputPath> <OutputPath></OutputPath>
<DefineConstants>TRACE;NETSTACK_SPAN</DefineConstants> <DefineConstants>TRACE;</DefineConstants>
<DebugType>none</DebugType> <DebugType>none</DebugType>
</PropertyGroup> </PropertyGroup>
</Project> </Project>
+97 -23
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com> * Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -58,7 +58,6 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
@@ -69,16 +68,19 @@ namespace Ragon.Protocol
private int _read; private int _read;
private int _write; private int _write;
private uint[] _buckets; private uint[] _buckets;
private byte[] _rawData;
private readonly UTF8Encoding _utf8Encoding = new UTF8Encoding(false, true); private readonly UTF8Encoding _utf8Encoding = new UTF8Encoding(false, true);
public byte[] RawData => _rawData;
public int ReadOffset => _read; public int ReadOffset => _read;
public int WriteOffset => _write; public int WriteOffset => _write;
public int Length => ((_write - 1) >> 3) + 1; public int Length => ((_write - 1) >> 3) + 1;
public int Capacity => _write - _read; public int Capacity => _write - _read - 1;
public RagonBuffer(int capacity = 128) public RagonBuffer(int capacity = 128)
{ {
_buckets = new uint[capacity]; _buckets = new uint[capacity];
_rawData = Array.Empty<byte>();
_read = 0; _read = 0;
_write = 0; _write = 0;
} }
@@ -119,6 +121,21 @@ namespace Ragon.Protocol
return (RagonOperation)Read(8); return (RagonOperation)Read(8);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteFloat(float value)
{
var bytes = BitConverter.GetBytes(value);
WriteBytes(bytes);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float ReadFloat()
{
var bytes = ReadBytes(4);
var value = BitConverter.ToSingle(bytes, 0);
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteFloat(float value, float min, float max, float precision) public void WriteFloat(float value, float min, float max, float precision)
{ {
@@ -145,6 +162,23 @@ namespace Ragon.Protocol
return adjusted; return adjusted;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteInt(int value)
{
var bytes = BitConverter.GetBytes(value);
WriteBytes(bytes);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadInt()
{
var bytes = ReadBytes(4);
var value = BitConverter.ToInt32(bytes, 0);
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteInt(int value, int min, int max) public void WriteInt(int value, int min, int max)
{ {
@@ -234,9 +268,6 @@ namespace Ragon.Protocol
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write(uint value, int numBits = 16) public void Write(uint value, int numBits = 16)
{ {
Debug.Assert(!(numBits < 0));
Debug.Assert(!(numBits > 32));
var currentBucketIndex = _write >> 5; var currentBucketIndex = _write >> 5;
var used = _write & 0x0000001F; var used = _write & 0x0000001F;
var mask = (1UL << used) - 1; var mask = (1UL << used) - 1;
@@ -245,7 +276,7 @@ namespace Ragon.Protocol
if (currentBucketIndex + 1 >= _buckets.Length) if (currentBucketIndex + 1 >= _buckets.Length)
Resize(1); Resize(1);
_buckets[currentBucketIndex] = (uint)result; _buckets[currentBucketIndex] = (uint)result;
_buckets[currentBucketIndex + 1] = (uint)(result >> 32); _buckets[currentBucketIndex + 1] = (uint)(result >> 32);
@@ -282,23 +313,23 @@ namespace Ragon.Protocol
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Do not use this method, will be removed")] [Obsolete("Do not use this method, will be removed")]
public byte[] ReadBytes(int lenght) public byte[] ReadBytes(int len)
{ {
var data = new byte[lenght]; var data = new byte[len];
for (int i = 0; i < lenght; i++) for (int i = 0; i < len; i++)
data[i] = (byte)Read(8); data[i] = (byte)Read(8);
return data; return data;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadSpan(ref Span<uint> data, int size) public void ReadArray(uint[] data, int size)
{ {
var used = _read & 0x0000001F; var used = _read & 0x0000001F;
var index = _read >> 5; var index = _read >> 5;
var limit = (size + 32 - 1) / 32; var limit = (size + 32 - 1) / 32;
var capacity = size; var capacity = size;
for (int i = 0; i < limit; i++) for (int i = 0; i < limit; i++)
{ {
var dataSize = capacity > 32 ? 32 : capacity; var dataSize = capacity > 32 ? 32 : capacity;
@@ -306,22 +337,24 @@ namespace Ragon.Protocol
var bucketRaw = (ulong)_buckets[index]; var bucketRaw = (ulong)_buckets[index];
if (index + 1 < _buckets.Length) if (index + 1 < _buckets.Length)
bucketRaw |= (ulong)_buckets[index + 1] << 32; bucketRaw |= (ulong)_buckets[index + 1] << 32;
var bucket = bucketRaw >> used; var bucket = bucketRaw >> used;
var result = bucket & mask; var result = bucket & mask;
data[i] = (uint)result; data[i] = (uint)result;
if (i + 1 < data.Length) if (i + 1 < data.Length)
data[i + 1] = (uint)(result >> 32); data[i + 1] = (uint)(result >> 32);
index += 1; index += 1;
capacity -= dataSize; capacity -= dataSize;
} }
_read += size; _read += size;
} }
public void WriteSpan(ref ReadOnlySpan<uint> data, int size)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteArray(uint[] data, int size)
{ {
var used = _write & 0x0000001F; var used = _write & 0x0000001F;
var index = _write >> 5; var index = _write >> 5;
@@ -329,20 +362,20 @@ namespace Ragon.Protocol
if (index + limit >= _buckets.Length) if (index + limit >= _buckets.Length)
Resize(size); Resize(size);
for (var i = 0; i < limit; i += 1) for (var i = 0; i < limit; i += 1)
{ {
var prepared = (ulong) data[i] << used; var prepared = (ulong)data[i] << used;
var mask = (1UL << used) - 1; var mask = (1UL << used) - 1;
var scratch = _buckets[index] & mask; var scratch = _buckets[index] & mask;
var result = scratch | prepared; var result = scratch | prepared;
_buckets[index] = (uint)result; _buckets[index] = (uint)result;
_buckets[index + 1] = (uint)(result >> 32); _buckets[index + 1] = (uint)(result >> 32);
index += 1; index += 1;
} }
_write += size; _write += size;
} }
@@ -352,6 +385,18 @@ namespace Ragon.Protocol
_write = 0; _write = 0;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyFrom(RagonBuffer buffer, int size)
{
WriteArray(buffer._buckets, size);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToBuffer(RagonBuffer buffer, int size)
{
ReadArray(buffer._buckets, size);
}
public void FromArray(byte[] data) public void FromArray(byte[] data)
{ {
var length = data.Length; var length = data.Length;
@@ -384,10 +429,13 @@ namespace Ragon.Protocol
_write = ((length - 1) * 8) + positionInByte; _write = ((length - 1) * 8) + positionInByte;
_read = 0; _read = 0;
_rawData = data;
} }
public byte[] ToArray() public byte[] ToArray()
{ {
Write(1, 1);
var data = new byte[Length]; var data = new byte[Length];
int bucketsCount = (_write >> 5) + 1; int bucketsCount = (_write >> 5) + 1;
int length = data.Length; int length = data.Length;
@@ -413,6 +461,32 @@ namespace Ragon.Protocol
return data; return data;
} }
public void ToArray(byte[] outData)
{
Write(1, 1);
var bucketsCount = (_write >> 5) + 1;
var length = Length;
for (int i = 0; i < bucketsCount; i++)
{
var dataIdx = i * 4;
var bucket = _buckets[i];
if (dataIdx < length)
outData[dataIdx] = (byte)bucket;
if (dataIdx + 1 < length)
outData[dataIdx + 1] = (byte)(bucket >> 8);
if (dataIdx + 2 < length)
outData[dataIdx + 2] = (byte)(bucket >> 16);
if (dataIdx + 3 < length)
outData[dataIdx + 3] = (byte)(bucket >> 24);
}
}
private void Resize(int capacity) private void Resize(int capacity)
{ {
var buckets = new uint[_buckets.Length * 2 + capacity]; var buckets = new uint[_buckets.Length * 2 + capacity];
@@ -0,0 +1,9 @@
namespace Ragon.Protocol
{
public enum RagonDisconnect
{
MANUAL,
TIMEOUT,
SERVER,
}
}
+26 -19
View File
@@ -19,24 +19,31 @@ namespace Ragon.Protocol
{ {
public enum RagonOperation: byte public enum RagonOperation: byte
{ {
AUTHORIZE, AUTHORIZE = 1,
AUTHORIZED_SUCCESS, AUTHORIZED_SUCCESS = 2,
AUTHORIZED_FAILED, AUTHORIZED_FAILED = 3,
JOIN_OR_CREATE_ROOM, JOIN_OR_CREATE_ROOM = 4,
CREATE_ROOM, CREATE_ROOM = 5,
JOIN_ROOM, JOIN_ROOM = 6,
LEAVE_ROOM, LEAVE_ROOM = 7,
OWNERSHIP_CHANGED, OWNERSHIP_ENTITY_CHANGED = 8,
JOIN_SUCCESS, OWNERSHIP_ROOM_CHANGED= 9,
JOIN_FAILED, JOIN_SUCCESS = 10,
LOAD_SCENE, JOIN_FAILED = 11,
SCENE_LOADED, LOAD_SCENE = 12,
PLAYER_JOINED, SCENE_LOADED = 13,
PLAYER_LEAVED, PLAYER_JOINED = 14,
CREATE_ENTITY, PLAYER_LEAVED = 15,
DESTROY_ENTITY, CREATE_ENTITY = 16,
SNAPSHOT, REMOVE_ENTITY = 17,
REPLICATE_ENTITY_STATE, SNAPSHOT = 18,
REPLICATE_ENTITY_EVENT, REPLICATE_ENTITY_STATE = 19,
REPLICATE_ENTITY_EVENT = 20,
REPLICATE_RAW_DATA = 21,
REPLICATE_ROOM_EVENT = 22,
TRANSFER_ROOM_OWNERSHIP = 23,
TRANSFER_ENTITY_OWNERSHIP = 24,
TIMESTAMP_SYNCHRONIZATION = 25,
ROOM_LIST_UPDATED = 26,
} }
} }
@@ -17,22 +17,22 @@
namespace Ragon.Protocol namespace Ragon.Protocol
{ {
public class RagonRoomParameters: IRagonSerializable public class RagonRoomPayload: IRagonSerializable
{ {
public string Map { get; set; } public string Scene { get; set; }
public int Min { get; set; } public int Min { get; set; }
public int Max { get; set; } public int Max { get; set; }
public void Serialize(RagonBuffer buffer) public void Serialize(RagonBuffer buffer)
{ {
buffer.WriteString(Map); buffer.WriteString(Scene);
buffer.WriteInt(Min, 1, 32); buffer.WriteInt(Min, 1, 32);
buffer.WriteInt(Max, 1, 32); buffer.WriteInt(Max, 1, 32);
} }
public void Deserialize(RagonBuffer buffer) public void Deserialize(RagonBuffer buffer)
{ {
Map = buffer.ReadString(); Scene = buffer.ReadString();
Min = buffer.ReadInt(1, 32); Min = buffer.ReadInt(1, 32);
Max = buffer.ReadInt(1, 32); Max = buffer.ReadInt(1, 32);
} }
+309
View File
@@ -0,0 +1,309 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace Ragon.Protocol
{
[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 RagonStream
{
private byte[] _data;
private int _offset;
private int _size;
public int Lenght => _offset;
public int Size => _size - _offset;
public RagonStream(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);
var len = rawData.Length;
ResizeIfNeed(2 + len);
WriteUShort((ushort)len);
Buffer.BlockCopy(rawData, 0, _data, _offset, len);
_offset += len;
return len + 2;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString()
{
var len = ReadUShort();
var rawData = new byte[len];
Buffer.BlockCopy(_data, _offset, rawData, 0, len);
var str = Encoding.UTF8.GetString(rawData);
_offset += len;
return str;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte[] ReadBinary(int len)
{
if (len >= _data.Length)
return Array.Empty<byte>();
var payload = new byte[len];
Buffer.BlockCopy(_data, _offset, payload, 0, len);
_offset += len;
return payload;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteBinary(byte[] payload)
{
ResizeIfNeed(payload.Length);
Array.Copy(payload, 0, _data, _offset, payload.Length);
_offset += payload.Length;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteOperation(RagonOperation operation)
{
ResizeIfNeed(1);
_data[_offset] = (byte)operation;
_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 void WriteUShort(ushort value, int offset)
{
ResizeIfNeed(2);
_data[offset] = (byte)(value & 0x00FF);
_data[offset + 1] = (byte)((value & 0xFF00) >> 8);
}
[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 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;
}
}
}
+25
View File
@@ -16,9 +16,34 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ragon.Protocol namespace Ragon.Protocol
{ {
[StructLayout(LayoutKind.Explicit)]
public struct DoubleToUInt
{
[FieldOffset(0)]
public double Double;
[FieldOffset(0)]
public uint Int0;
[FieldOffset(4)]
public uint Int1;
}
public static class RagonTime {
public static double CurrentTimestamp()
{
var currentTime = System.DateTime.UtcNow.ToUniversalTime().Subtract(
new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc)
).TotalMilliseconds;
return currentTime;
}
}
public static class DeBruijn public static class DeBruijn
{ {
private static readonly int[] _lookup = new int[32] private static readonly int[] _lookup = new int[32]
+1 -1
View File
@@ -21,7 +21,7 @@ namespace Ragon.Protocol
{ {
public static uint Parse(string version) public static uint Parse(string version)
{ {
var strings = version.Split("."); var strings = version.Split('.');
if (strings.Length < 3) if (strings.Length < 3)
return 0; return 0;
+6 -6
View File
@@ -16,12 +16,12 @@
namespace Ragon.Relay namespace Ragon.Relay
{ {
class Program class Program
{
static void Main(string[] args)
{ {
static void Main(string[] args) var relay = new Relay();
{ relay.Start();
var relay = new Relay();
relay.Start();
}
} }
}
} }
+3 -3
View File
@@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<RootNamespace>Ragon.Relay</RootNamespace> <RootNamespace>Ragon.Relay</RootNamespace>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
@@ -23,8 +23,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Ragon.Server.ENet\Ragon.Server.ENet.csproj" /> <ProjectReference Include="..\Ragon.Server.ENetServer\Ragon.Server.ENetServer.csproj" />
<ProjectReference Include="..\Ragon.Server.DotNetWebSockets\Ragon.Server.DotNetWebSockets.csproj" /> <ProjectReference Include="..\Ragon.Server.WebSocketServer\Ragon.Server.WebSocketServer.csproj" />
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" /> <ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
</ItemGroup> </ItemGroup>
@@ -0,0 +1,6 @@
namespace Ragon.Relay;
public class KickPlayerCommand
{
public string Id;
}
@@ -16,9 +16,10 @@
using NLog; using NLog;
using Ragon.Server; using Ragon.Server;
using Ragon.Server.ENet; using Ragon.Server.ENetServer;
using Ragon.Server.DotNetWebsockets; using Ragon.Server.WebSocketServer;
using Ragon.Server.IO;
using Ragon.Server.Plugin;
namespace Ragon.Relay; namespace Ragon.Relay;
@@ -29,24 +30,22 @@ public class Relay
var logger = LogManager.GetLogger("Ragon.Relay"); var logger = LogManager.GetLogger("Ragon.Relay");
logger.Info("Relay Application"); logger.Info("Relay Application");
var configuration = Configuration.Load("relay.config.json"); var configuration = RagonServerConfiguration.Load("relay.config.json");
var serverType = Configuration.GetServerType(configuration.ServerType); var serverType = RagonServerConfiguration.GetServerType(configuration.ServerType);
INetworkServer server = null; INetworkServer networkServer = new ENetServer();
IServerPlugin plugin = new RelayServerPlugin();
switch (serverType) switch (serverType)
{ {
case ServerType.ENET: case ServerType.ENET:
server = new ENetServer(); networkServer = new ENetServer();
break; break;
case ServerType.WEBSOCKET: case ServerType.WEBSOCKET:
server = new DotNetWebSocketServer(); networkServer = new WebSocketServer();
break;
default:
server = new ENetServer();
break; break;
} }
var relay = new RagonServer(server, configuration); var relay = new RagonServer(networkServer, plugin, configuration);
logger.Info("Started"); logger.Info("Started");
relay.Start(); relay.Start();
} }
+37
View File
@@ -0,0 +1,37 @@
using System;
using Ragon.Server;
using Ragon.Server.Entity;
using Ragon.Server.Plugin;
using Ragon.Server.Room;
namespace Ragon.Relay;
public class RelayRoomPlugin: BaseRoomPlugin
{
public void Tick(float dt)
{
}
public void OnAttached()
{
Console.WriteLine("Room attached");
}
public void OnDetached()
{
Console.WriteLine("Room detached");
}
public bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity)
{
Console.WriteLine($"Entity created: {entity.Id}");
return true;
}
public bool OnEntityRemove(RagonRoomPlayer destroyer, RagonEntity entity)
{
Console.WriteLine($"Entity destroyed: {entity.Id}");
return true;
}
}
+30
View File
@@ -0,0 +1,30 @@
using System;
using Newtonsoft.Json;
using Ragon.Server;
using Ragon.Server.Plugin;
namespace Ragon.Relay;
public class RelayServerPlugin: BaseServerPlugin
{
public override bool OnCommand(string command, string payload)
{
Console.WriteLine(command);
if (command == "kick-player")
{
var commandPayload = JsonConvert.DeserializeObject<KickPlayerCommand>(payload);
var player = Server.GetPlayerById(commandPayload.Id);
if (player != null)
player.Connection.Close();
else
Console.WriteLine($"Player not found with Id {commandPayload.Id}");
}
return true;
}
public override IRoomPlugin CreateRoomPlugin(RoomInformation information)
{
return new RelayRoomPlugin();
}
}
+12 -2
View File
@@ -3,8 +3,18 @@
"serverType": "enet", "serverType": "enet",
"serverTickRate": 30, "serverTickRate": 30,
"gameProtocol": "1.0.0", "gameProtocol": "1.0.0",
"port": 5001, "port": 5000,
"httpPort": 5001,
"httpKey": "defaultkey",
"limitConnections": 4095, "limitConnections": 4095,
"limitPlayersPerRoom": 20, "limitPlayersPerRoom": 20,
"limitRooms": 200 "limitRooms": 200,
"limitBufferedEvents": 50,
"webHooks":
{
"room-created": "http://127.0.0.1:3000/service/create-room",
"room-removed": "http://127.0.0.1:3000/service/remove-room",
"room-joined": "http://127.0.0.1:3000/service/join-room",
"room-leaved": "http://127.0.0.1:3000/service/leave-room"
}
} }
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<RootNamespace>Ragon.ENet</RootNamespace> <RootNamespace>Ragon.ENet</RootNamespace>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="ENet-CSharp" Version="2.4.8" /> <PackageReference Include="ENet-CSharp" Version="2.4.8" />
<PackageReference Include="NLog" Version="5.1.1" /> <PackageReference Include="NLog" Version="5.2.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -15,19 +15,28 @@
*/ */
using ENet; using ENet;
using Ragon.Server.IO;
namespace Ragon.Server.ENet; namespace Ragon.Server.ENetServer;
public sealed class ENetConnection: INetworkConnection public sealed class ENetConnection: INetworkConnection
{ {
public ushort Id { get; } public ushort Id { get; }
public INetworkChannel Reliable { get; private set; } public INetworkChannel Reliable { get; private set; }
public INetworkChannel Unreliable { get; private set; } public INetworkChannel Unreliable { get; private set; }
private Peer _peer;
public ENetConnection(Peer peer) public ENetConnection(Peer peer)
{ {
_peer = peer;
Id = (ushort) peer.ID; Id = (ushort) peer.ID;
Reliable = new ENetReliableChannel(peer, 0); Reliable = new ENetReliableChannel(peer, NetworkChannel.RELIABLE);
Unreliable = new ENetUnreliableChannel(peer, 1); Unreliable = new ENetUnreliableChannel(peer, NetworkChannel.UNRELIABLE);
}
public void Close()
{
_peer.Disconnect(0);
} }
} }
@@ -14,19 +14,24 @@
* limitations under the License. * limitations under the License.
*/ */
using System.Net;
using ENet; using ENet;
using Ragon.Protocol;
using Ragon.Server.IO;
namespace Ragon.Server.ENet; namespace Ragon.Server.ENetServer;
public sealed class ENetReliableChannel: INetworkChannel public sealed class ENetReliableChannel: INetworkChannel
{ {
private Peer _peer; private Peer _peer;
private byte _channelId; private byte _channelId;
private byte[] _data;
public ENetReliableChannel(Peer peer, int channelId) public ENetReliableChannel(Peer peer, NetworkChannel channel)
{ {
_peer = peer; _peer = peer;
_channelId = (byte) channelId; _data = new byte[1500];
_channelId = (byte) channel;
} }
public void Send(byte[] data) public void Send(byte[] data)
@@ -36,4 +41,14 @@ public sealed class ENetReliableChannel: INetworkChannel
_peer.Send(_channelId, ref newPacket); _peer.Send(_channelId, ref newPacket);
} }
public void Send(RagonBuffer buffer)
{
buffer.ToArray(_data);
var newPacket = new Packet();
newPacket.Create(_data, buffer.Length, PacketFlags.Reliable);
_peer.Send(_channelId, ref newPacket);
}
} }
@@ -17,41 +17,35 @@
using ENet; using ENet;
using NLog; using NLog;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO;
namespace Ragon.Server.ENet namespace Ragon.Server.ENetServer
{ {
public sealed class ENetServer: INetworkServer public sealed class ENetServer : INetworkServer
{ {
public Executor Executor => _executor; public Executor Executor => _executor;
private readonly Host _host; private readonly Host _host = new();
private readonly ILogger _logger = LogManager.GetCurrentClassLogger(); private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
private ENetConnection[] _connections; private ENetConnection[] _connections = Array.Empty<ENetConnection>();
private INetworkListener _listener; private INetworkListener _listener;
private uint _protocol; private uint _protocol;
private Event _event; private ENet.Event _event;
private Executor _executor; private Executor _executor = new();
public ENetServer()
{
_host = new Host();
_executor = new Executor();
_connections = Array.Empty<ENetConnection>();
}
public void Start(INetworkListener listener, NetworkConfiguration configuration) public void Start(INetworkListener listener, NetworkConfiguration configuration)
{ {
Library.Initialize(); Library.Initialize();
_connections = new ENetConnection[configuration.LimitConnections]; _connections = new ENetConnection[configuration.LimitConnections];
_listener = listener; _listener = listener;
_protocol = configuration.Protocol; _protocol = configuration.Protocol;
var address = new Address var address = new Address
{ {
Port = (ushort) configuration.Port, Port = (ushort)configuration.Port,
}; };
_host.Create(address, _connections.Length, 2, 0, 0, 1024 * 1024); _host.Create(address, _connections.Length, 2, 0, 0, 1024 * 1024);
@@ -73,7 +67,7 @@ namespace Ragon.Server.ENet
polled = true; polled = true;
} }
switch (_event.Type) switch (_event.Type)
{ {
case EventType.None: case EventType.None:
@@ -89,8 +83,9 @@ namespace Ragon.Server.ENet
_event.Peer.DisconnectNow(0); _event.Peer.DisconnectNow(0);
break; break;
} }
var connection = new ENetConnection(_event.Peer); var connection = new ENetConnection(_event.Peer);
_connections[_event.Peer.ID] = connection; _connections[_event.Peer.ID] = connection;
_listener.OnConnected(connection); _listener.OnConnected(connection);
break; break;
@@ -109,24 +104,34 @@ namespace Ragon.Server.ENet
} }
case EventType.Receive: case EventType.Receive:
{ {
var peerId = (ushort) _event.Peer.ID; var peerId = (ushort)_event.Peer.ID;
var connection = _connections[peerId]; var connection = _connections[peerId];
var dataRaw = new byte[_event.Packet.Length]; var dataRaw = new byte[_event.Packet.Length];
_event.Packet.CopyTo(dataRaw); _event.Packet.CopyTo(dataRaw);
_event.Packet.Dispose(); _event.Packet.Dispose();
_listener.OnData(connection, dataRaw); _listener.OnData(connection, (NetworkChannel)_event.ChannelID, dataRaw);
break; break;
} }
} }
} }
} }
public void Broadcast(byte[] data, NetworkChannel channel)
{
var packet = new Packet();
var flag = channel == NetworkChannel.RELIABLE? PacketFlags.Reliable: PacketFlags.None;
packet.Create(data, flag);
_host.Broadcast((byte)channel, ref packet);
}
public void Stop() public void Stop()
{ {
_host?.Dispose(); _host?.Dispose();
Library.Deinitialize(); Library.Deinitialize();
} }
@@ -15,18 +15,21 @@
*/ */
using ENet; using ENet;
using Ragon.Protocol;
using Ragon.Server.IO;
namespace Ragon.Server.ENet; namespace Ragon.Server.ENetServer;
public sealed class ENetUnreliableChannel: INetworkChannel public sealed class ENetUnreliableChannel: INetworkChannel
{ {
private Peer _peer; private Peer _peer;
private byte _channelId; private byte _channelId;
private byte[] _data;
public ENetUnreliableChannel(Peer peer, int channelId) public ENetUnreliableChannel(Peer peer, NetworkChannel channel)
{ {
_peer = peer; _peer = peer;
_channelId = (byte) channelId; _channelId = (byte) channel;
} }
public void Send(byte[] data) public void Send(byte[] data)
@@ -36,4 +39,14 @@ public sealed class ENetUnreliableChannel: INetworkChannel
_peer.Send(_channelId, ref newPacket); _peer.Send(_channelId, ref newPacket);
} }
public void Send(RagonBuffer buffer)
{
buffer.ToArray(_data);
var newPacket = new Packet();
newPacket.Create(_data, buffer.Length, PacketFlags.None);
_peer.Send(_channelId, ref newPacket);
}
} }
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<RootNamespace>Ragon.WebSockets</RootNamespace> <RootNamespace>Ragon.WebSockets</RootNamespace>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="NLog" Version="5.1.1" /> <PackageReference Include="NLog" Version="5.2.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -14,10 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
using System.Net.WebSockets;
using NLog; using NLog;
using System.Net.WebSockets;
using Ragon.Server.IO;
namespace Ragon.Server.DotNetWebsockets; namespace Ragon.Server.WebSocketServer;
public sealed class WebSocketConnection : INetworkConnection public sealed class WebSocketConnection : INetworkConnection
{ {
@@ -43,6 +44,11 @@ public sealed class WebSocketConnection : INetworkConnection
Unreliable = unreliableChannel; Unreliable = unreliableChannel;
} }
public void Close()
{
Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
}
public async Task Flush() public async Task Flush()
{ {
foreach (var channel in _channels) foreach (var channel in _channels)
@@ -15,9 +15,10 @@
*/ */
using System.Net.WebSockets; using System.Net.WebSockets;
using Ragon.Server; using Ragon.Protocol;
using Ragon.Server.IO;
namespace Ragon.Server.DotNetWebsockets; namespace Ragon.Server.WebSocketServer;
public class WebSocketReliableChannel : INetworkChannel public class WebSocketReliableChannel : INetworkChannel
{ {
@@ -35,9 +36,17 @@ public class WebSocketReliableChannel : INetworkChannel
_queue.Enqueue(data); _queue.Enqueue(data);
} }
public void Send(RagonBuffer buffer)
{
var sendData = buffer.ToArray();
_queue.Enqueue(sendData);
}
public async Task Flush() public async Task Flush()
{ {
while (_queue.TryDequeue(out var sendData) && _socket.State == WebSocketState.Open) while (_queue.TryDequeue(out var sendData) && _socket.State == WebSocketState.Open)
{
await _socket.SendAsync(sendData, WebSocketMessageType.Binary, WebSocketMessageFlags.EndOfMessage, CancellationToken.None); await _socket.SendAsync(sendData, WebSocketMessageType.Binary, WebSocketMessageFlags.EndOfMessage, CancellationToken.None);
}
} }
} }
@@ -18,13 +18,14 @@ using System.Net;
using System.Net.WebSockets; using System.Net.WebSockets;
using NLog; using NLog;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO;
namespace Ragon.Server.DotNetWebsockets; namespace Ragon.Server.WebSocketServer;
public class DotNetWebSocketServer : INetworkServer public class WebSocketServer : INetworkServer
{ {
public Executor Executor => _executor; public Executor Executor => _executor;
private ILogger _logger = LogManager.GetCurrentClassLogger(); private ILogger _logger = LogManager.GetCurrentClassLogger();
private INetworkListener _networkListener; private INetworkListener _networkListener;
private Stack<ushort> _sequencer; private Stack<ushort> _sequencer;
@@ -33,35 +34,45 @@ public class DotNetWebSocketServer : INetworkServer
private WebSocketConnection[] _connections; private WebSocketConnection[] _connections;
private List<WebSocketConnection> _activeConnections; private List<WebSocketConnection> _activeConnections;
private CancellationTokenSource _cancellationTokenSource; private CancellationTokenSource _cancellationTokenSource;
public DotNetWebSocketServer() public WebSocketServer()
{ {
_sequencer = new Stack<ushort>(); _sequencer = new Stack<ushort>();
_connections = Array.Empty<WebSocketConnection>(); _connections = Array.Empty<WebSocketConnection>();
_activeConnections = new List<WebSocketConnection>(); _activeConnections = new List<WebSocketConnection>();
_executor = new Executor(); _executor = new Executor();
} }
public async void StartAccept(CancellationToken cancellationToken) public async void StartAccept(CancellationToken cancellationToken)
{ {
while (!cancellationToken.IsCancellationRequested) while (!cancellationToken.IsCancellationRequested)
{ {
var context = await _httpListener.GetContextAsync(); WebSocketConnection connection = null!;
if (!context.Request.IsWebSocketRequest) try
{ {
context.Response.StatusCode = 200; var context = await _httpListener.GetContextAsync();
context.Response.ContentLength64 = 0; if (!context.Request.IsWebSocketRequest)
context.Response.Close(); {
continue; context.Response.StatusCode = 200;
context.Response.ContentLength64 = 0;
context.Response.Close();
continue;
}
var webSocketContext = await context.AcceptWebSocketAsync(null);
var webSocket = webSocketContext.WebSocket;
var peerId = _sequencer.Pop();
connection = new WebSocketConnection(webSocket, peerId);
}
catch (Exception ex)
{
_logger.Warn(ex);
continue;
} }
var webSocketContext = await context.AcceptWebSocketAsync(null);
var webSocket = webSocketContext.WebSocket;
var peerId = _sequencer.Pop();
var connection = new WebSocketConnection(webSocket, peerId);
_connections[peerId] = connection; _connections[connection.Id] = connection;
StartListen(connection, cancellationToken); StartListen(connection, cancellationToken);
} }
} }
@@ -74,6 +85,7 @@ public class DotNetWebSocketServer : INetworkServer
var webSocket = connection.Socket; var webSocket = connection.Socket;
var bytes = new byte[2048]; var bytes = new byte[2048];
var buffer = new Memory<byte>(bytes); var buffer = new Memory<byte>(bytes);
while ( while (
webSocket.State == WebSocketState.Open || webSocket.State == WebSocketState.Open ||
!cancellationToken.IsCancellationRequested) !cancellationToken.IsCancellationRequested)
@@ -81,9 +93,11 @@ public class DotNetWebSocketServer : INetworkServer
try try
{ {
var result = await webSocket.ReceiveAsync(buffer, cancellationToken); var result = await webSocket.ReceiveAsync(buffer, cancellationToken);
var dataRaw = buffer.Slice(0, result.Count); if (result.Count > 0)
if (dataRaw.Length > 0) {
_networkListener.OnData(connection, dataRaw.ToArray()); var payload = buffer.Slice(0, result.Count);
_networkListener.OnData(connection, NetworkChannel.RELIABLE, payload.ToArray());
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -93,6 +107,7 @@ public class DotNetWebSocketServer : INetworkServer
_sequencer.Push(connection.Id); _sequencer.Push(connection.Id);
_activeConnections.Remove(connection); _activeConnections.Remove(connection);
_networkListener.OnDisconnected(connection); _networkListener.OnDisconnected(connection);
} }
@@ -101,6 +116,12 @@ public class DotNetWebSocketServer : INetworkServer
Flush(); Flush();
} }
public void Broadcast(byte[] data, NetworkChannel channel)
{
foreach (var activeConnection in _activeConnections)
activeConnection.Reliable.Send(data);
}
public async void Flush() public async void Flush()
{ {
foreach (var conn in _activeConnections) foreach (var conn in _activeConnections)
+14 -4
View File
@@ -1,19 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<RootNamespace>Ragon.Core</RootNamespace> <RootNamespace>Ragon.Core</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.3.1</Version>
<Title>Ragon.Server</Title>
<Copyright>Eduard Kargin</Copyright>
<PackageProjectUrl>https://ragon-server.com</PackageProjectUrl>
<RepositoryUrl>https://github.com/edmand46/Ragon</RepositoryUrl>
<RepositoryType>Source</RepositoryType>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<LangVersion>10</LangVersion>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta2" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.1.1" /> <PackageReference Include="NLog" Version="5.2.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Ragon.Protocol\Ragon.Protocol.csproj" /> <ProjectReference Include="..\Ragon.Protocol\Ragon.Protocol.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>
@@ -0,0 +1,16 @@
using Ragon.Protocol;
using Ragon.Server.Room;
namespace Ragon.Server.Entity;
public interface IRagonEntity
{
public ushort Id { get; }
public ushort Type { get; }
public ushort StaticId { get; }
public ushort AttachId { get; }
public RagonRoomPlayer Owner { get; }
public RagonAuthority Authority { get; }
public RagonPayload Payload { get; }
public IRagonEntityState State { get; }
}
@@ -0,0 +1,8 @@
using Ragon.Protocol;
namespace Ragon.Server.Entity;
public interface IRagonEntityState
{
}
+60 -26
View File
@@ -16,10 +16,12 @@
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.Event;
using Ragon.Server.Room;
namespace Ragon.Server; namespace Ragon.Server.Entity;
public class RagonEntity public class RagonEntity : IRagonEntity
{ {
private static ushort _idGenerator = 100; private static ushort _idGenerator = 100;
public ushort Id { get; private set; } public ushort Id { get; private set; }
@@ -29,30 +31,36 @@ public class RagonEntity
public RagonRoomPlayer Owner { get; private set; } public RagonRoomPlayer Owner { get; private set; }
public RagonAuthority Authority { get; private set; } public RagonAuthority Authority { get; private set; }
public RagonPayload Payload { get; private set; } public RagonPayload Payload { get; private set; }
public RagonEntityState State { get; private set; } public IRagonEntityState State => _state;
private readonly List<RagonEvent> _bufferedEvents; private readonly List<RagonEvent> _bufferedEvents;
private readonly int _limitBufferedEvents;
private readonly RagonEntityState _state;
public RagonEntity(RagonRoomPlayer owner, ushort type, ushort staticId, ushort attachId, RagonAuthority eventAuthority) public RagonEntity(RagonEntityParameters parameters)
{ {
Owner = owner;
StaticId = staticId;
Type = type;
AttachId = attachId;
Id = _idGenerator++; Id = _idGenerator++;
Authority = eventAuthority;
State = new RagonEntityState(this); StaticId = parameters.StaticId;
Type = parameters.Type;
AttachId = parameters.AttachId;
Authority = parameters.Authority;
Payload = new RagonPayload(); Payload = new RagonPayload();
_state = new RagonEntityState(this);
_bufferedEvents = new List<RagonEvent>(); _bufferedEvents = new List<RagonEvent>();
_limitBufferedEvents = parameters.BufferedEvents;
} }
public void Attach(RagonRoomPlayer owner)
public void SetOwner(RagonRoomPlayer owner)
{ {
Owner = owner; Owner = owner;
} }
public void Detach()
{
}
public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer, RagonBuffer writer) public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer, RagonBuffer writer)
{ {
foreach (var evnt in _bufferedEvents) foreach (var evnt in _bufferedEvents)
@@ -82,7 +90,7 @@ public class RagonEntity
buffer.WriteUShort(Type); buffer.WriteUShort(Type);
buffer.WriteUShort(Id); buffer.WriteUShort(Id);
buffer.WriteUShort(Owner.Connection.Id); buffer.WriteUShort(Owner.Connection.Id);
Payload.Write(buffer); Payload.Write(buffer);
var sendData = buffer.ToArray(); var sendData = buffer.ToArray();
@@ -96,7 +104,7 @@ public class RagonEntity
var buffer = room.Writer; var buffer = room.Writer;
buffer.Clear(); buffer.Clear();
buffer.WriteOperation(RagonOperation.DESTROY_ENTITY); buffer.WriteOperation(RagonOperation.REMOVE_ENTITY);
buffer.WriteUShort(Id); buffer.WriteUShort(Id);
Payload.Write(buffer); Payload.Write(buffer);
@@ -110,29 +118,37 @@ public class RagonEntity
{ {
buffer.WriteUShort(Type); buffer.WriteUShort(Type);
buffer.WriteUShort(Id); buffer.WriteUShort(Id);
if (StaticId != 0) if (StaticId != 0)
buffer.WriteUShort(StaticId); buffer.WriteUShort(StaticId);
buffer.WriteUShort(Owner.Connection.Id);
buffer.WriteUShort(Owner.Connection.Id);
buffer.WriteUShort(Payload.Size); buffer.WriteUShort(Payload.Size);
Payload.Write(buffer); Payload.Write(buffer);
State.Snapshot(buffer);
_state.Snapshot(buffer);
} }
public void ReplicateEvent( public void ReplicateEvent(
RagonRoomPlayer caller, RagonRoomPlayer invoker,
RagonEvent evnt, RagonEvent evnt,
RagonReplicationMode eventMode, RagonReplicationMode eventMode,
RagonRoomPlayer targetPlayer RagonRoomPlayer targetPlayer
) )
{ {
if (Authority == RagonAuthority.OwnerOnly && invoker.Connection.Id != Owner.Connection.Id)
{
return;
}
var room = Owner.Room; var room = Owner.Room;
var buffer = room.Writer; var buffer = room.Writer;
buffer.Clear(); buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT); buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
buffer.WriteUShort(evnt.EventCode); buffer.WriteUShort(evnt.EventCode);
buffer.WriteUShort(caller.Connection.Id); buffer.WriteUShort(invoker.Connection.Id);
buffer.WriteByte((byte)eventMode); buffer.WriteByte((byte)eventMode);
buffer.WriteUShort(Id); buffer.WriteUShort(Id);
@@ -143,20 +159,18 @@ public class RagonEntity
} }
public void ReplicateEvent( public void ReplicateEvent(
RagonRoomPlayer caller, RagonRoomPlayer invoker,
RagonEvent evnt, RagonEvent evnt,
RagonReplicationMode eventMode, RagonReplicationMode eventMode,
RagonTarget targetMode RagonTarget targetMode
) )
{ {
if (Authority == RagonAuthority.OwnerOnly && if (Authority == RagonAuthority.OwnerOnly && invoker.Connection.Id != Owner.Connection.Id)
Owner.Connection.Id != caller.Connection.Id)
{ {
Console.WriteLine($"Player have not enough authority for event with Id {evnt.EventCode}");
return; return;
} }
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner) if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner && _bufferedEvents.Count < _limitBufferedEvents)
{ {
_bufferedEvents.Add(evnt); _bufferedEvents.Add(evnt);
} }
@@ -167,7 +181,7 @@ public class RagonEntity
buffer.Clear(); buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT); buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
buffer.WriteUShort(evnt.EventCode); buffer.WriteUShort(evnt.EventCode);
buffer.WriteUShort(caller.Connection.Id); buffer.WriteUShort(invoker.Connection.Id);
buffer.WriteByte((byte)eventMode); buffer.WriteByte((byte)eventMode);
buffer.WriteUShort(Id); buffer.WriteUShort(Id);
@@ -195,7 +209,7 @@ public class RagonEntity
{ {
foreach (var roomPlayer in room.ReadyPlayersList) foreach (var roomPlayer in room.ReadyPlayersList)
{ {
if (roomPlayer.Connection.Id != caller.Connection.Id) if (roomPlayer.Connection.Id != invoker.Connection.Id)
roomPlayer.Connection.Reliable.Send(sendData); roomPlayer.Connection.Reliable.Send(sendData);
} }
@@ -209,4 +223,24 @@ public class RagonEntity
} }
} }
} }
public void AddProperty(RagonProperty property)
{
_state.AddProperty(property);
}
public void WriteState(RagonBuffer writer)
{
_state.Write(writer);
}
public bool TryReadState(RagonRoomPlayer player, RagonBuffer reader)
{
if (Owner.Connection.Id != player.Connection.Id)
return false;
_state.Read(reader);
return true;
}
} }
@@ -0,0 +1,28 @@
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
namespace Ragon.Server.Entity;
public ref struct RagonEntityParameters
{
public ushort Type;
public ushort StaticId;
public ushort AttachId;
public RagonAuthority Authority;
public int BufferedEvents;
}
@@ -14,20 +14,19 @@
* limitations under the License. * limitations under the License.
*/ */
using Ragon.Protocol; using Ragon.Protocol;
namespace Ragon.Server; namespace Ragon.Server.Entity;
public class RagonEntityState public class RagonEntityState: IRagonEntityState
{ {
private List<RagonProperty> _properties; private readonly List<RagonProperty> _properties;
private RagonEntity _entity; private readonly RagonEntity _entity;
public RagonEntityState(RagonEntity entity, int capacity = 10) public RagonEntityState(RagonEntity entity, int capacity = 10)
{ {
_entity = entity; _entity = entity;
_properties = new List<RagonProperty>(10); _properties = new List<RagonProperty>(capacity);
} }
public void AddProperty(RagonProperty property) public void AddProperty(RagonProperty property)
@@ -65,8 +64,7 @@ public class RagonEntityState
{ {
foreach (var property in _properties) foreach (var property in _properties)
{ {
var hasPayload = property.IsFixed || !property.IsFixed && property.Size > 0; if (property.HasData)
if (hasPayload)
{ {
buffer.WriteBool(true); buffer.WriteBool(true);
property.Write(buffer); property.Write(buffer);
+3 -9
View File
@@ -17,7 +17,7 @@
using Ragon.Protocol; using Ragon.Protocol;
namespace Ragon.Server; namespace Ragon.Server.Entity;
public class RagonPayload public class RagonPayload
{ {
@@ -28,19 +28,13 @@ public class RagonPayload
public void Read(RagonBuffer buffer) public void Read(RagonBuffer buffer)
{ {
var readOnlySpan = _data.AsSpan();
_size = buffer.Capacity; _size = buffer.Capacity;
buffer.ReadArray(_data, _size);
buffer.ReadSpan(ref readOnlySpan, _size);
} }
public void Write(RagonBuffer buffer) public void Write(RagonBuffer buffer)
{ {
if (_size == 0) return; if (_size == 0) return;
buffer.WriteArray(_data, _size);
ReadOnlySpan<uint> readOnlySpan = _data.AsSpan();
buffer.WriteSpan(ref readOnlySpan, _size);
} }
} }
+13 -8
View File
@@ -14,17 +14,16 @@
* limitations under the License. * limitations under the License.
*/ */
using System.ComponentModel;
using Ragon.Protocol; using Ragon.Protocol;
namespace Ragon.Server; namespace Ragon.Server.Entity;
public class RagonProperty : RagonPayload public class RagonProperty : RagonPayload
{ {
public int Size { get; set; } public int Size { get; set; }
public bool IsDirty { get; private set; } public bool IsDirty { get; private set; }
public bool IsFixed { get; private set; } public bool IsFixed { get; private set; }
public bool HasData { get; private set; }
private uint[] _data; private uint[] _data;
@@ -39,24 +38,30 @@ public class RagonProperty : RagonPayload
public void Read(RagonBuffer buffer) public void Read(RagonBuffer buffer)
{ {
var readOnlySpan = _data.AsSpan();
if (IsFixed) if (IsFixed)
{ {
buffer.ReadSpan(ref readOnlySpan, Size); buffer.ReadArray(_data, Size);
} }
else else
{ {
Size = (int) buffer.Read(); Size = (int) buffer.Read();
buffer.ReadSpan(ref readOnlySpan, Size); buffer.ReadArray(_data, Size);
} }
HasData = true;
IsDirty = true; IsDirty = true;
} }
public void Write(RagonBuffer buffer) public void Write(RagonBuffer buffer)
{ {
ReadOnlySpan<uint> readOnlySpan = _data.AsSpan(); if (IsFixed)
buffer.WriteSpan(ref readOnlySpan, Size); {
buffer.WriteArray(_data, Size);
return;
}
buffer.Write((ushort) Size);
buffer.WriteArray(_data, Size);
} }
public void Clear() public void Clear()
@@ -14,10 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.Room;
namespace Ragon.Server; namespace Ragon.Server.Event;
public class RagonEvent public class RagonEvent
{ {
@@ -39,15 +39,13 @@ public class RagonEvent
public void Read(RagonBuffer buffer) public void Read(RagonBuffer buffer)
{ {
var readOnlySpan = _data.AsSpan();
_size = buffer.Capacity; _size = buffer.Capacity;
buffer.ReadSpan(ref readOnlySpan, _size); buffer.ReadArray(_data, _size);
} }
public void Write(RagonBuffer buffer) public void Write(RagonBuffer buffer)
{ {
if (_size == 0) return; if (_size <= 0) return;
ReadOnlySpan<uint> readOnlySpan = _data.AsSpan(); buffer.WriteArray(_data, _size);
buffer.WriteSpan(ref readOnlySpan, _size);
} }
} }
@@ -16,40 +16,100 @@
using NLog; using NLog;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Plugin.Web;
namespace Ragon.Server;
public sealed class AuthorizationOperation: IRagonOperation namespace Ragon.Server.Handler;
public sealed class AuthorizationOperation: BaseOperation
{ {
private Logger _logger = LogManager.GetCurrentClassLogger(); private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly RagonWebHookPlugin _webhook;
private readonly RagonContextObserver _observer;
private readonly RagonBuffer _writer;
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer) public AuthorizationOperation(
RagonBuffer reader,
RagonBuffer writer,
RagonWebHookPlugin webhook,
RagonContextObserver observer): base(reader, writer)
{ {
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Authorized) _webhook = webhook;
_observer = observer;
_writer = writer;
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
if (context.ConnectionStatus == ConnectionStatus.Authorized)
{ {
_logger.Warn("Player already authorized"); _logger.Warn("Player already authorized!");
return; return;
} }
if (context.ConnectionStatus == ConnectionStatus.InProcess)
{
_logger.Warn("Player already request authorization!");
return;
}
var configuration = context.Configuration;
var key = Reader.ReadString();
var name = Reader.ReadString();
var payload = Reader.ReadString();
var key = reader.ReadString(); if (key == configuration.ServerKey)
var playerName = reader.ReadString(); {
var additionalPayload = new RagonPayload(); if (_webhook.RequestAuthorization(context, name, payload))
additionalPayload.Read(reader); return;
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload);
context.SetPlayer(lobbyPlayer);
Approve(context);
}
else
{
Reject(context);
}
}
context.LobbyPlayer.Name = playerName; public void Approve(RagonContext context)
context.LobbyPlayer.AdditionalData = Array.Empty<byte>(); {
context.LobbyPlayer.Status = LobbyPlayerStatus.Authorized; context.ConnectionStatus = ConnectionStatus.Authorized;
_observer.OnAuthorized(context);
var playerId = context.LobbyPlayer.Id; var playerId = context.LobbyPlayer.Id;
var playerName = context.LobbyPlayer.Name;
var playerPayload = context.LobbyPlayer.Payload;
_writer.Clear();
_writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
_writer.WriteString(playerId);
_writer.WriteString(playerName);
_writer.WriteString(playerPayload);
writer.Clear(); var sendData = _writer.ToArray();
writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
writer.WriteString(playerId);
writer.WriteString(playerName);
var sendData = writer.ToArray();
context.Connection.Reliable.Send(sendData); context.Connection.Reliable.Send(sendData);
_logger.Trace($"Connection {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name} authorized"); _logger.Trace($"Connection {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name} authorized");
} }
public void Reject(RagonContext context)
{
_writer.Clear();
_writer.WriteOperation(RagonOperation.AUTHORIZED_FAILED);
var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData);
context.Connection.Close();
_logger.Trace($"Connection {context.Connection.Id}");
}
} }
@@ -0,0 +1,34 @@
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
using Ragon.Server.IO;
namespace Ragon.Server.Handler;
public abstract class BaseOperation
{
protected readonly RagonBuffer Reader;
protected readonly RagonBuffer Writer;
public BaseOperation(RagonBuffer reader, RagonBuffer writer)
{
Reader = reader;
Writer = writer;
}
public abstract void Handle(RagonContext context, NetworkChannel channel);
}
@@ -16,39 +16,61 @@
using NLog; using NLog;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.Entity;
using Ragon.Server.IO;
namespace Ragon.Server; namespace Ragon.Server.Handler;
public sealed class EntityCreateOperation : IRagonOperation public sealed class EntityCreateOperation : BaseOperation
{ {
private Logger _logger = LogManager.GetCurrentClassLogger(); private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer) public EntityCreateOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{ {
var player = context.RoomPlayer; var player = context.RoomPlayer;
var room = context.Room; var room = context.Room;
var attachId = reader.ReadUShort(); var attachId = Reader.ReadUShort();
var entityType = reader.ReadUShort(); var entityType = Reader.ReadUShort();
var eventAuthority = (RagonAuthority) reader.ReadByte(); var eventAuthority = (RagonAuthority) Reader.ReadByte();
var propertiesCount = reader.ReadUShort(); var propertiesCount = Reader.ReadUShort();
var entity = new RagonEntity(player, entityType, 0, attachId, eventAuthority); var entityParameters = new RagonEntityParameters()
{
Type = entityType,
Authority = eventAuthority,
AttachId = attachId,
StaticId = 0,
BufferedEvents = context.Configuration.LimitBufferedEvents,
};
var entity = new RagonEntity(entityParameters);
for (var i = 0; i < propertiesCount; i++) for (var i = 0; i < propertiesCount; i++)
{ {
var propertyType = reader.ReadBool(); var propertyType = Reader.ReadBool();
var propertySize = reader.ReadUShort(); var propertySize = Reader.ReadUShort();
entity.State.AddProperty(new RagonProperty(propertySize, propertyType)); entity.AddProperty(new RagonProperty(propertySize, propertyType));
} }
if (reader.Capacity > 0) if (Reader.Capacity > 0)
entity.Payload.Read(reader); entity.Payload.Read(Reader);
var plugin = room.Plugin;
if (!plugin.OnEntityCreate(player, entity))
return;
entity.Attach(player);
room.AttachEntity(entity); room.AttachEntity(entity);
player.AttachEntity(entity); player.AttachEntity(entity);
entity.Create(); entity.Create();
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} created entity {entity.Id}:{entity.Type}"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} created entity {entity.Id}:{entity.Type}");
} }
} }
@@ -16,18 +16,24 @@
using NLog; using NLog;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.Event;
using Ragon.Server.IO;
namespace Ragon.Server; namespace Ragon.Server.Handler;
public sealed class EntityEventOperation : IRagonOperation public sealed class EntityEventOperation : BaseOperation
{ {
private Logger _logger = LogManager.GetCurrentClassLogger(); private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public EntityEventOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer) public override void Handle(RagonContext context, NetworkChannel channel)
{ {
var player = context.RoomPlayer; var player = context.RoomPlayer;
var room = context.Room; var room = context.Room;
var entityId = reader.ReadUShort(); var entityId = Reader.ReadUShort();
if (!room.Entities.TryGetValue(entityId, out var ent)) if (!room.Entities.TryGetValue(entityId, out var ent))
{ {
@@ -35,24 +41,23 @@ public sealed class EntityEventOperation : IRagonOperation
return; return;
} }
var eventId = reader.ReadUShort(); var eventId = Reader.ReadUShort();
var eventMode = (RagonReplicationMode)reader.ReadByte(); var eventMode = (RagonReplicationMode)Reader.ReadByte();
var targetMode = (RagonTarget)reader.ReadByte(); var targetMode = (RagonTarget)Reader.ReadByte();
var targetPlayerPeerId = (ushort)0; var targetPlayerPeerId = (ushort)0;
if (targetMode == RagonTarget.Player) if (targetMode == RagonTarget.Player)
targetPlayerPeerId = reader.ReadUShort(); targetPlayerPeerId = Reader.ReadUShort();
var ragonEvent = new RagonEvent(player, eventId); var @event = new RagonEvent(player, eventId);
ragonEvent.Read(reader); @event.Read(Reader);
if (targetMode == RagonTarget.Player && if (targetMode == RagonTarget.Player && room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer))
context.Room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer))
{ {
ent.ReplicateEvent(player, ragonEvent, eventMode, targetPlayer); ent.ReplicateEvent(player, @event, eventMode, targetPlayer);
return; return;
} }
ent.ReplicateEvent(player, ragonEvent, eventMode, targetMode); ent.ReplicateEvent(player, @event, eventMode, targetMode);
} }
} }
@@ -0,0 +1,75 @@
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
namespace Ragon.Server.Handler;
public sealed class EntityOwnershipOperation : BaseOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public EntityOwnershipOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
var currentOwner = context.RoomPlayer;
var room = context.Room;
var entityId = Reader.ReadUShort();
var playerPeerId = Reader.ReadUShort();
if (!room.Entities.TryGetValue(entityId, out var entity))
{
_logger.Error($"Entity not found with id {entityId}");
return;
}
if (entity.Owner.Connection.Id != currentOwner.Connection.Id)
{
_logger.Error($"Player not owner of entity with id {entityId}");
return;
}
if (!room.Players.TryGetValue(playerPeerId, out var nextOwner))
{
_logger.Error($"Player not found with id {playerPeerId}");
return;
}
currentOwner.Entities.Remove(entity);
nextOwner.Entities.Add(entity);
entity.Attach(nextOwner);
_logger.Trace($"Entity {entity.Id} next owner {nextOwner.Connection.Id}");
Writer.Clear();
Writer.WriteOperation(RagonOperation.OWNERSHIP_ENTITY_CHANGED);
Writer.WriteUShort(playerPeerId);
Writer.WriteUShort(1);
Writer.WriteUShort(entity.Id);
var sendData = Writer.ToArray();
foreach (var player in room.PlayerList)
player.Connection.Reliable.Send(sendData);
}
}
@@ -16,23 +16,29 @@
using NLog; using NLog;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.Entity;
using Ragon.Server.IO;
namespace Ragon.Server; namespace Ragon.Server.Handler;
public sealed class EntityDestroyOperation: IRagonOperation public sealed class EntityDestroyOperation: BaseOperation
{ {
private Logger _logger = LogManager.GetCurrentClassLogger(); private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer) public EntityDestroyOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{ {
var player = context.RoomPlayer; var player = context.RoomPlayer;
var room = context.Room; var room = context.Room;
var entityId = reader.ReadUShort(); var entityId = Reader.ReadUShort();
if (room.Entities.TryGetValue(entityId, out var entity)) if (room.Entities.TryGetValue(entityId, out var entity) && entity.Owner.Connection.Id == player.Connection.Id)
{ {
var payload = new RagonPayload(); var payload = new RagonPayload();
payload.Read(reader); payload.Read(Reader);
room.DetachEntity(entity); room.DetachEntity(entity);
player.DetachEntity(entity); player.DetachEntity(entity);
@@ -41,5 +47,9 @@ public sealed class EntityDestroyOperation: IRagonOperation
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} destoyed entity {entity.Id}"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} destoyed entity {entity.Id}");
} }
else
{
_logger.Trace($"Entity {entity.Id} not found or Player {context.Connection.Id}|{context.LobbyPlayer.Name} have not authority");
}
} }
} }
@@ -16,24 +16,29 @@
using NLog; using NLog;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO;
namespace Ragon.Server; namespace Ragon.Server.Handler;
public sealed class EntityStateOperation: IRagonOperation public sealed class EntityStateOperation: BaseOperation
{ {
private ILogger _logger = LogManager.GetCurrentClassLogger(); private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer) public EntityStateOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{ {
var room = context.Room; var room = context.Room;
var entitiesCount = reader.ReadUShort(); var player = context.RoomPlayer;
var entitiesCount = Reader.ReadUShort();
for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++) for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++)
{ {
var entityId = reader.ReadUShort(); var entityId = Reader.ReadUShort();
if (room.Entities.TryGetValue(entityId, out var entity)) if (room.Entities.TryGetValue(entityId, out var entity) && entity.TryReadState(player, Reader))
{ {
entity.State.Read(reader);
room.Track(entity); room.Track(entity);
} }
else else
@@ -16,67 +16,88 @@
using NLog; using NLog;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Plugin;
using Ragon.Server.Plugin.Web;
using Ragon.Server.Room;
namespace Ragon.Server; namespace Ragon.Server.Handler;
public sealed class RoomCreateOperation: IRagonOperation public sealed class RoomCreateOperation : BaseOperation
{ {
private RagonRoomParameters _roomParameters = new(); private readonly RagonRoomPayload _roomPayload = new();
private Logger _logger = LogManager.GetCurrentClassLogger(); private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer) private readonly RagonWebHookPlugin _ragonWebHookPlugin;
public RoomCreateOperation(RagonBuffer reader, RagonBuffer writer, IServerPlugin serverPlugin, RagonWebHookPlugin ragonWebHook) : base(reader, writer)
{ {
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized) _serverPlugin = serverPlugin;
_ragonWebHookPlugin = ragonWebHook;
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
{ {
_logger.Warn($"Player {context.Connection.Id} not authorized for this request"); _logger.Warn($"Player {context.Connection.Id} not authorized for this request");
return; return;
} }
var custom = reader.ReadBool(); var custom = Reader.ReadBool();
var roomId = Guid.NewGuid().ToString(); var roomId = Guid.NewGuid().ToString();
if (custom) if (custom)
{ {
roomId = reader.ReadString(); roomId = Reader.ReadString();
if (context.Lobby.FindRoomById(roomId, out _)) if (context.Lobby.FindRoomById(roomId, out _))
{ {
writer.Clear(); Writer.Clear();
writer.WriteOperation(RagonOperation.JOIN_FAILED); Writer.WriteOperation(RagonOperation.JOIN_FAILED);
writer.WriteString($"Room with id {roomId} already exists"); Writer.WriteString($"Room with id {roomId} already exists");
var sendData = writer.ToArray(); var sendData = Writer.ToArray();
context.Connection.Reliable.Send(sendData); context.Connection.Reliable.Send(sendData);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} join failed to room {roomId}, room already exist"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} join failed to room {roomId}, room already exist");
return; return;
} }
} }
_roomParameters.Deserialize(reader); _roomPayload.Deserialize(Reader);
var information = new RoomInformation() var information = new RoomInformation()
{ {
Map = _roomParameters.Map, Scene = _roomPayload.Scene,
Max = _roomParameters.Max, Max = _roomPayload.Max,
Min = _roomParameters.Min, Min = _roomPayload.Min,
}; };
var lobbyPlayer = context.LobbyPlayer; var lobbyPlayer = context.LobbyPlayer;
var roomPlayer = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
if (!roomPlugin.OnPlayerJoined(roomPlayer))
return;
var room = new RagonRoom(roomId, information); roomPlayer.OnAttached(room);
context.Scheduler.Run(room); context.Scheduler.Run(room);
context.Lobby.Persist(room); context.Lobby.Persist(room);
context.SetRoom(room, roomPlayer);
var player = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(room, player); _ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} {information}"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} with scene {information.Scene}");
JoinSuccess(player, room, writer); JoinSuccess(roomPlayer, room, Writer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}");
} }
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer) private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer)
{ {
writer.Clear(); writer.Clear();
@@ -84,9 +105,9 @@ public sealed class RoomCreateOperation: IRagonOperation
writer.WriteString(room.Id); writer.WriteString(room.Id);
writer.WriteString(player.Id); writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id); writer.WriteString(room.Owner.Id);
writer.WriteUShort((ushort) room.Info.Min); writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort) room.Info.Max); writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteString(room.Info.Map); writer.WriteString(room.Scene);
var sendData = writer.ToArray(); var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData); player.Connection.Reliable.Send(sendData);
@@ -0,0 +1,49 @@
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
namespace Ragon.Server.Handler;
public sealed class RoomDataOperation : BaseOperation
{
public RoomDataOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
var player = context.RoomPlayer;
var room = context.Room;
var data = Reader.RawData;
var dataSize = data.Length - 1;
var headerSize = 3;
var size = headerSize + dataSize;
var sendData = new byte[size];
var peerId = player.Connection.Id;
sendData[0] = (byte)RagonOperation.REPLICATE_RAW_DATA;
sendData[1] = (byte)peerId;
sendData[2] = (byte)(peerId >> 8);
Array.Copy(data, 1, sendData, headerSize, dataSize);
room.Broadcast(sendData, channel);
}
}
@@ -0,0 +1,45 @@
using Ragon.Protocol;
using Ragon.Server.Event;
using Ragon.Server.IO;
namespace Ragon.Server.Handler;
public class RoomEventOperation : BaseOperation
{
public RoomEventOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
var room = context.Room;
var player = context.RoomPlayer;
var eventId = Reader.ReadUShort();
var replicationMode = (RagonReplicationMode)Reader.ReadByte();
var targetMode = (RagonTarget)Reader.ReadByte();
var targetPlayerPeerId = (ushort)0;
if (targetMode == RagonTarget.Player)
targetPlayerPeerId = Reader.ReadUShort();
var @event = new RagonEvent(player, eventId);
@event.Read(Reader);
Writer.Clear();
Writer.WriteUShort(eventId);
Writer.WriteUShort(player.Connection.Id);
Writer.WriteUShort((ushort) replicationMode);
var sendData = Writer.ToArray();
if (targetMode == RagonTarget.Player && room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer))
{
targetPlayer.Connection.Reliable.Send(sendData);
return;
}
foreach (var roomPlayer in room.ReadyPlayersList)
roomPlayer.Connection.Reliable.Send(sendData);
}
}
@@ -16,56 +16,71 @@
using NLog; using NLog;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Plugin.Web;
using Ragon.Server.Room;
namespace Ragon.Server; namespace Ragon.Server.Handler;
public sealed class RoomJoinOperation : IRagonOperation public sealed class RoomJoinOperation : BaseOperation
{ {
private Logger _logger = LogManager.GetCurrentClassLogger(); private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly RagonWebHookPlugin _webHook;
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer) public RoomJoinOperation(RagonBuffer reader, RagonBuffer writer, RagonWebHookPlugin plugin) : base(reader, writer)
{ {
var roomId = reader.ReadString(); _webHook = plugin;
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
var roomId = Reader.ReadString();
var lobbyPlayer = context.LobbyPlayer; var lobbyPlayer = context.LobbyPlayer;
if (!context.Lobby.FindRoomById(roomId, out var existsRoom)) if (!context.Lobby.FindRoomById(roomId, out var existsRoom))
{ {
JoinFailed(lobbyPlayer, writer); JoinFailed(context, Writer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} failed to join room {roomId}"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} failed to join room {roomId}");
return; return;
} }
var player = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name); var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(existsRoom, player); context.SetRoom(existsRoom, player);
JoinSuccess(player, existsRoom, writer); if (!existsRoom.Plugin.OnPlayerJoined(player))
return;
_webHook.RoomJoined(context, existsRoom, player);
JoinSuccess(context, existsRoom, Writer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to {existsRoom.Id}"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to {existsRoom.Id}");
} }
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer) private void JoinSuccess(RagonContext context, RagonRoom room, RagonBuffer writer)
{ {
writer.Clear(); writer.Clear();
writer.WriteOperation(RagonOperation.JOIN_SUCCESS); writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
writer.WriteString(room.Id); writer.WriteString(room.Id);
writer.WriteString(player.Id); writer.WriteString(context.RoomPlayer.Id);
writer.WriteString(room.Owner.Id); writer.WriteString(room.Owner.Id);
writer.WriteUShort((ushort) room.Info.Min); writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort) room.Info.Max); writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteString(room.Info.Map); writer.WriteString(room.Scene);
var sendData = writer.ToArray(); var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData); context.Connection.Reliable.Send(sendData);
} }
private void JoinFailed(RagonLobbyPlayer player, RagonBuffer writer) private void JoinFailed(RagonContext context, RagonBuffer writer)
{ {
writer.Clear(); writer.Clear();
writer.WriteOperation(RagonOperation.JOIN_FAILED); writer.WriteOperation(RagonOperation.JOIN_FAILED);
writer.WriteString($"Room not exists"); writer.WriteString($"Room not exists");
var sendData = writer.ToArray(); var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData); context.Connection.Reliable.Send(sendData);
} }
} }
@@ -16,17 +16,30 @@
using NLog; using NLog;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Plugin;
using Ragon.Server.Plugin.Web;
using Ragon.Server.Room;
namespace Ragon.Server; namespace Ragon.Server.Handler;
public sealed class RoomJoinOrCreateOperation : IRagonOperation public sealed class RoomJoinOrCreateOperation : BaseOperation
{ {
private RagonRoomParameters _roomParameters = new(); private readonly RagonRoomPayload _roomPayload = new();
private Logger _logger = LogManager.GetCurrentClassLogger(); private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer) public RoomJoinOrCreateOperation(RagonBuffer reader, RagonBuffer writer, IServerPlugin serverPlugin, RagonWebHookPlugin plugin): base(reader, writer)
{ {
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized) _serverPlugin = serverPlugin;
_ragonWebHookPlugin = plugin;
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
{ {
_logger.Warn("Player not authorized for this request"); _logger.Warn("Player not authorized for this request");
return; return;
@@ -35,34 +48,46 @@ public sealed class RoomJoinOrCreateOperation : IRagonOperation
var roomId = Guid.NewGuid().ToString(); var roomId = Guid.NewGuid().ToString();
var lobbyPlayer = context.LobbyPlayer; var lobbyPlayer = context.LobbyPlayer;
_roomParameters.Deserialize(reader); _roomPayload.Deserialize(Reader);
if (context.Lobby.FindRoomByMap(_roomParameters.Map, out var existsRoom)) if (context.Lobby.FindRoomByScene(_roomPayload.Scene, out var existsRoom))
{ {
var player = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name); var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
if (!existsRoom.Plugin.OnPlayerJoined(player))
return;
context.SetRoom(existsRoom, player); context.SetRoom(existsRoom, player);
JoinSuccess(player, existsRoom, writer); _ragonWebHookPlugin.RoomJoined(context, existsRoom, player);
JoinSuccess(player, existsRoom, Writer);
} }
else else
{ {
var information = new RoomInformation() var information = new RoomInformation()
{ {
Map = _roomParameters.Map, Scene = _roomPayload.Scene,
Max = _roomParameters.Max, Max = _roomPayload.Max,
Min = _roomParameters.Min, Min = _roomPayload.Min,
}; };
var room = new RagonRoom(roomId, information); var roomPlayer = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
if (!roomPlugin.OnPlayerJoined(roomPlayer))
return;
_ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
context.Lobby.Persist(room); context.Lobby.Persist(room);
context.Scheduler.Run(room); context.Scheduler.Run(room);
var roomPlayer = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(room, roomPlayer); context.SetRoom(room, roomPlayer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} {information}"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} with scene {information.Scene}");
JoinSuccess(roomPlayer, room, writer); JoinSuccess(roomPlayer, room, Writer);
} }
} }
@@ -73,9 +98,9 @@ public sealed class RoomJoinOrCreateOperation : IRagonOperation
writer.WriteString(room.Id); writer.WriteString(room.Id);
writer.WriteString(player.Id); writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id); writer.WriteString(room.Owner.Id);
writer.WriteUShort((ushort) room.Info.Min); writer.WriteUShort((ushort) room.PlayerMin);
writer.WriteUShort((ushort) room.Info.Max); writer.WriteUShort((ushort) room.PlayerMax);
writer.WriteString(room.Info.Map); writer.WriteString(room.Scene);
var sendData = writer.ToArray(); var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData); player.Connection.Reliable.Send(sendData);
@@ -16,19 +16,36 @@
using NLog; using NLog;
using Ragon.Protocol; using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Plugin;
using Ragon.Server.Plugin.Web;
namespace Ragon.Server; namespace Ragon.Server.Handler;
public sealed class RoomLeaveOperation: IRagonOperation public sealed class RoomLeaveOperation: BaseOperation
{ {
private Logger _logger = LogManager.GetCurrentClassLogger(); private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer) private readonly RagonWebHookPlugin _webHook;
public RoomLeaveOperation(RagonBuffer reader, RagonBuffer writer, RagonWebHookPlugin plugin): base(reader, writer)
{
_webHook = plugin;
}
public override void Handle(RagonContext context, NetworkChannel channel)
{ {
var room = context.Room; var room = context.Room;
var roomPlayer = context.RoomPlayer; var roomPlayer = context.RoomPlayer;
if (room != null) if (room != null)
{ {
context.Room?.DetachPlayer(roomPlayer); var plugin = room.Plugin;
plugin.OnPlayerLeaved(roomPlayer);
room.DetachPlayer(roomPlayer);
_webHook.RoomLeaved(context, room, roomPlayer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} leaved from {room.Id}"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} leaved from {room.Id}");
} }
} }

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