This commit is contained in:
2024-09-28 20:11:56 +03:00
parent 5136f08dab
commit 672bb1ff6d
95 changed files with 741 additions and 601 deletions
@@ -27,7 +27,7 @@ internal class AuthorizeFailedHandler: IHandler
_listenerList = list;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
var message = reader.ReadString();
_listenerList.OnAuthorizationFailed(message);
@@ -32,7 +32,7 @@ internal class AuthorizeSuccessHandler: IHandler
_listenerList = listenerList;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
var playerId = reader.ReadString();
var playerName = reader.ReadString();
@@ -1,69 +0,0 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
namespace Ragon.Client;
internal class EntityCreateHandler : IHandler
{
private readonly RagonClient _client;
private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache;
private readonly IRagonEntityListener _entityListener;
public EntityCreateHandler(
RagonClient client,
RagonPlayerCache playerCache,
RagonEntityCache entityCache,
IRagonEntityListener entityListener
)
{
_client = client;
_entityCache = entityCache;
_playerCache = playerCache;
_entityListener = entityListener;
}
public void Handle(RagonBuffer reader)
{
var attachId = reader.ReadUShort();
var entityType = reader.ReadUShort();
var entityId = reader.ReadUShort();
var ownerId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(ownerId);
var payload = new RagonPayload(reader.Capacity);
payload.Read(reader);
if (player == null)
{
RagonLog.Warn($"Owner {ownerId}|{player.Name} not found in players");
_playerCache.Dump();
return;
}
var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.TryGetEntity(attachId, entityType, 0, entityId, hasAuthority, out var hasCreated);
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
if (hasCreated)
_entityListener.OnEntityCreated(entity);
entity.Attach();
}
}
@@ -1,56 +0,0 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
namespace Ragon.Client;
internal class EntityEventHandler : IHandler
{
private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache;
public EntityEventHandler(
RagonPlayerCache playerCache,
RagonEntityCache entityCache
)
{
_playerCache = playerCache;
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
{
var eventCode = reader.ReadUShort();
var peerId = reader.ReadUShort();
var executionMode = (RagonReplicationMode)reader.ReadByte();
var entityId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(peerId);
if (player == null)
{
RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}");
_playerCache.Dump();
return;
}
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
return;
_entityCache.OnEvent(player, entityId, eventCode, reader);
}
}
@@ -1,60 +0,0 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using 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);
}
}
}
@@ -1,39 +0,0 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
namespace Ragon.Client;
internal class EntityRemoveHandler: IHandler
{
private readonly RagonEntityCache _entityCache;
public EntityRemoveHandler(RagonEntityCache entityCache)
{
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
{
var entityId = reader.ReadUShort();
var payload = new RagonPayload(reader.Capacity);
payload.Read(reader);
_entityCache.OnDestroy(entityId, payload);
}
}
@@ -1,39 +0,0 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
namespace Ragon.Client;
internal class StateEntityHandler: IHandler
{
private readonly RagonEntityCache _entityCache;
public StateEntityHandler(RagonEntityCache entityCache)
{
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
{
var entitiesCount = reader.ReadUShort();
for (var i = 0; i < entitiesCount; i++)
{
var entityId = reader.ReadUShort();
_entityCache.OnState(entityId, reader);
}
}
}
+1 -1
View File
@@ -21,5 +21,5 @@ namespace Ragon.Client;
public interface IHandler
{
public void Handle(RagonBuffer reader);
public void Handle(RagonStream reader);
}
@@ -28,7 +28,7 @@ internal class JoinFailedHandler: IHandler
_listenerList = listenerList;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
var message = reader.ReadString();
_listenerList.OnFailed(message);
@@ -41,23 +41,20 @@ internal class JoinSuccessHandler : IHandler
{
private readonly RagonListenerList _listenerList;
private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache;
private readonly RagonClient _client;
public JoinSuccessHandler(
RagonClient client,
RagonListenerList listenerList,
RagonPlayerCache playerCache,
RagonEntityCache entityCache
RagonPlayerCache playerCache
)
{
_client = client;
_listenerList = listenerList;
_entityCache = entityCache;
_playerCache = playerCache;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
var roomId = reader.ReadString();
var min = reader.ReadUShort();
@@ -68,9 +65,8 @@ internal class JoinSuccessHandler : IHandler
_playerCache.SetOwnerAndLocal(ownerId, localId);
var scene = new RagonScene(_client, _playerCache, _entityCache, sceneName);
var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max);
var room = new RagonRoom(_client, _entityCache, _playerCache, roomInfo, scene);
var room = new RagonRoom(_client, _playerCache, roomInfo);
room.UserData.Read(reader);
@@ -93,6 +89,5 @@ internal class JoinSuccessHandler : IHandler
_client.SetStatus(RagonStatus.ROOM);
_listenerList.OnJoined();
_listenerList.OnSceneRequest(sceneName);
}
}
@@ -23,22 +23,18 @@ internal class LeaveRoomHandler : IHandler
{
private readonly RagonClient _client;
private readonly RagonListenerList _listenerList;
private readonly RagonEntityCache _entityCache;
public LeaveRoomHandler(
RagonClient client,
RagonListenerList listenerList,
RagonEntityCache entityCache)
RagonListenerList listenerList)
{
_client = client;
_listenerList = listenerList;
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
_listenerList.OnLeft();
_entityCache.Cleanup();
_client.Room.Cleanup();
}
}
@@ -1,45 +0,0 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
namespace Ragon.Client;
internal class SceneLoadHandler: IHandler
{
private readonly RagonClient _client;
private readonly RagonListenerList _listenerList;
public SceneLoadHandler(
RagonClient ragonClient,
RagonListenerList listenerList
)
{
_client = ragonClient;
_listenerList = listenerList;
}
public void Handle(RagonBuffer reader)
{
var sceneName = reader.ReadString();
var room = _client.Room;
room.Cleanup();
room.Update(sceneName);
_listenerList.OnSceneRequest(sceneName);
}
}
@@ -23,19 +23,16 @@ internal class OwnershipRoomHandler : IHandler
{
private readonly RagonListenerList _listenerList;
private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache;
public OwnershipRoomHandler(
RagonListenerList listenerList,
RagonPlayerCache playerCache,
RagonEntityCache entityCache)
RagonPlayerCache playerCache)
{
_listenerList = listenerList;
_playerCache = playerCache;
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
var newOwnerId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(newOwnerId);
@@ -33,7 +33,7 @@ internal class PlayerJoinHandler : IHandler
_listenerList = listenerList;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
var playerPeerId = reader.ReadUShort();
var playerId = reader.ReadString();
@@ -22,21 +22,18 @@ namespace Ragon.Client;
internal class PlayerLeftHandler : IHandler
{
private RagonPlayerCache _playerCache;
private RagonEntityCache _entityCache;
private RagonListenerList _listenerList;
public PlayerLeftHandler(
RagonEntityCache entityCache,
RagonPlayerCache playerCache,
RagonListenerList listenerList
)
{
_entityCache = entityCache;
_playerCache = playerCache;
_listenerList = listenerList;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
var playerId = reader.ReadString();
var player = _playerCache.GetPlayerById(playerId);
@@ -53,9 +50,9 @@ internal class PlayerLeftHandler : IHandler
toDeleteIds[i] = entityId;
}
var emptyPayload = new RagonPayload(0);
foreach (var id in toDeleteIds)
_entityCache.OnDestroy(id, emptyPayload);
// var emptyPayload = new RagonPayload(0);
// foreach (var id in toDeleteIds)
// _entityCache.OnDestroy(id, emptyPayload);
}
else
{
@@ -32,7 +32,7 @@ namespace Ragon.Client
_playerCache = playerCache;
_listenerList = listenerList;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
var playerPeerId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(playerPeerId);
@@ -32,9 +32,9 @@ internal class RoomDataHandler: IHandler
_listeners = listeners;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
var rawData = reader.RawData;
var rawData = reader.ReadBinary(reader.Lenght);
var peerId = (ushort)(rawData[1] + (rawData[2] << 8));
RagonPlayer player = null;
@@ -32,7 +32,7 @@ public class RoomEventHandler : IHandler
_playerCache = playerCache;
}
public void Handle(RagonBuffer buffer)
public void Handle(RagonStream buffer)
{
var eventCode = buffer.ReadUShort();
var peerId = buffer.ReadUShort();
@@ -13,7 +13,7 @@ internal class RoomListHandler: IHandler
_listenerList = list;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
var roomCount = reader.ReadUShort();
var roomList = new RagonRoomInformation[roomCount];
@@ -29,7 +29,7 @@ namespace Ragon.Client
_listenerList = listenerList;
}
public void Handle(RagonBuffer reader)
public void Handle(RagonStream reader)
{
var changes = _client.Room?.UserData.Read(reader);
_listenerList.OnRoomUserData(changes);
@@ -1,118 +0,0 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Diagnostics;
using Ragon.Protocol;
namespace Ragon.Client;
internal class SnapshotHandler : IHandler
{
private readonly IRagonEntityListener _entityListener;
private readonly RagonClient _client;
private readonly RagonListenerList _listenerList;
private readonly RagonEntityCache _entityCache;
private readonly RagonPlayerCache _playerCache;
public SnapshotHandler(
RagonClient ragonClient,
RagonListenerList listenerList,
RagonEntityCache entityCache,
RagonPlayerCache playerCache,
IRagonEntityListener entityListener
)
{
_client = ragonClient;
_entityListener = entityListener;
_listenerList = listenerList;
_entityCache = entityCache;
_playerCache = playerCache;
}
public void Handle(RagonBuffer buffer)
{
var entities = new List<RagonEntity>();
var dynamicEntities = buffer.ReadUShort();
RagonLog.Trace("Dynamic Entities: " + dynamicEntities);
for (var i = 0; i < dynamicEntities; i++)
{
var entityType = buffer.ReadUShort();
var entityId = buffer.ReadUShort();
var ownerPeerId = buffer.ReadUShort();
var payloadSize = buffer.ReadUShort();
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
if (player == null)
{
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
_playerCache.Dump();
return;
}
var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.TryGetEntity(0, entityType, 0, entityId, hasAuthority, out _);
var payload = RagonPayload.Empty;
if (payloadSize > 0)
{
payload = new RagonPayload(payloadSize);
payload.Read(buffer);
}
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
_entityListener.OnEntityCreated(entity);
entity.Read(buffer);
entities.Add(entity);
}
var staticEntities = buffer.ReadUShort();
RagonLog.Trace("Scene Entities: " + staticEntities);
for (var i = 0; i < staticEntities; i++)
{
var entityType = buffer.ReadUShort();
var entityId = buffer.ReadUShort();
var staticId = buffer.ReadUShort();
var ownerPeerId = buffer.ReadUShort();
var payloadSize = buffer.ReadUShort();
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
if (player == null)
{
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
_playerCache.Dump();
return;
}
var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _);
entity.Prepare(_client, entityId, entityType, hasAuthority, player, RagonPayload.Empty);
entity.Read(buffer);
entities.Add(entity);
}
foreach (var entity in entities)
entity.Attach();
_listenerList.OnSceneLoaded();
}
}
@@ -10,10 +10,10 @@ public class TimestampHandler: IHandler
_client = client;
}
public void Handle(RagonBuffer buffer)
public void Handle(RagonStream buffer)
{
var timestamp0 = buffer.Read(32);
var timestamp1 = buffer.Read(32);
var timestamp0 = (uint)buffer.ReadInt();
var timestamp1 = (uint)buffer.ReadInt();
var value = new DoubleToUInt { Int0 = timestamp0, Int1 = timestamp1 };
_client.SetTimestamp(value.Double);