client->server delta

This commit is contained in:
mischa 2024-10-18 13:53:27 +02:00
parent 47ef1b94ce
commit 2e4978c097

View File

@ -287,7 +287,7 @@ void DeserializeBaseline(NetworkReader reader)
// } // }
} }
void SerializeServerDelta(NetworkWriter writer, byte baselineTick, Vector3 position, Quaternion rotation)//, Vector3 scale) void SerializeDelta(NetworkWriter writer, byte baselineTick, Vector3 position, Quaternion rotation)//, Vector3 scale)
{ {
writer.WriteByte(baselineTick); writer.WriteByte(baselineTick);
@ -313,7 +313,7 @@ void SerializeServerDelta(NetworkWriter writer, byte baselineTick, Vector3 posit
// } // }
} }
bool DeserializeServerDelta(NetworkReader reader, out byte baselineTick, out Vector3 position, out Quaternion rotation, out Vector3 scale) bool DeserializeDelta(NetworkReader reader, out byte baselineTick, out Vector3 position, out Quaternion rotation, out Vector3 scale)
{ {
position = default; position = default;
rotation = default; rotation = default;
@ -355,7 +355,7 @@ bool DeserializeServerDelta(NetworkReader reader, out byte baselineTick, out Vec
} }
// cmd ///////////////////////////////////////////////////////////////// // cmd /////////////////////////////////////////////////////////////////
[Command(channel = Channels.Reliable)] [Command(channel = Channels.Reliable)] // reliable baseline
void CmdClientToServerBaselineSync(ArraySegment<byte> message) void CmdClientToServerBaselineSync(ArraySegment<byte> message)
{ {
using (NetworkReaderPooled reader = NetworkReaderPool.Get(message)) using (NetworkReaderPooled reader = NetworkReaderPool.Get(message))
@ -365,36 +365,42 @@ void CmdClientToServerBaselineSync(ArraySegment<byte> message)
} }
} }
// only unreliable. see comment above of this file. [Command(channel = Channels.Unreliable)] // unreliable delta
[Command(channel = Channels.Unreliable)] void CmdClientToServerDeltaSync(ArraySegment<byte> message)
void CmdClientToServerSync(Vector3? position, Quaternion? rotation)//, Vector3? scale)
{ {
OnClientToServerSync(position, rotation);//, scale); using (NetworkReaderPooled reader = NetworkReaderPool.Get(message))
//For client authority, immediately pass on the client snapshot to all other
//clients instead of waiting for server to send its snapshots.
if (syncDirection == SyncDirection.ClientToServer &&
connectionToClient != null && // CUSTOM CHANGE: for the drop thing..
!disableSendingThisToClients) // CUSTOM CHANGE: see comment at definition
{ {
// reuse cached writer for performance if (DeserializeDelta(reader, out byte baselineTick, out Vector3 position, out Quaternion rotation, out Vector3 scale))
// using (NetworkWriterPooled writer = NetworkWriterPool.Get())
writer.Position = 0;
{ {
// TODO directly forward again later OnClientToServerDeltaSync(baselineTick, position, rotation);//, scale);
Debug.LogWarning($"[{name}] CmdClientToServerSync: TODO which baseline to pass in Rpc?");
// SerializeServerDelta(writer, //For client authority, immediately pass on the client snapshot to all other
// 0xFF, //clients instead of waiting for server to send its snapshots.
// position.HasValue ? position.Value : default, if (syncDirection == SyncDirection.ClientToServer &&
// rotation.HasValue ? rotation.Value : default//, connectionToClient != null && // CUSTOM CHANGE: for the drop thing..
// // scale.HasValue ? scale.Value : default !disableSendingThisToClients) // CUSTOM CHANGE: see comment at definition
// ); {
// RpcServerToClientDeltaSync(writer); // reuse cached writer for performance
// using (NetworkWriterPooled writer = NetworkWriterPool.Get())
writer.Position = 0;
{
// TODO directly forward again later
Debug.LogWarning($"[{name}] CmdClientToServerSync: TODO which baseline to pass in Rpc?");
// SerializeServerDelta(writer,
// 0xFF,
// position.HasValue ? position.Value : default,
// rotation.HasValue ? rotation.Value : default//,
// // scale.HasValue ? scale.Value : default
// );
// RpcServerToClientDeltaSync(writer);
}
}
} }
} }
} }
// local authority client sends sync message to server for broadcasting // local authority client sends sync message to server for broadcasting
protected virtual void OnClientToServerSync(Vector3? position, Quaternion? rotation)//, Vector3? scale) protected virtual void OnClientToServerDeltaSync(byte baselineTick, Vector3? position, Quaternion? rotation)//, Vector3? scale)
{ {
// only apply if in client authority mode // only apply if in client authority mode
if (syncDirection != SyncDirection.ClientToServer) return; if (syncDirection != SyncDirection.ClientToServer) return;
@ -448,7 +454,7 @@ void RpcServerToClientDeltaSync(ArraySegment<byte> message)
{ {
using (NetworkReaderPooled reader = NetworkReaderPool.Get(message)) using (NetworkReaderPooled reader = NetworkReaderPool.Get(message))
{ {
if (DeserializeServerDelta(reader, out byte baselineTick, out Vector3 position, out Quaternion rotation, out Vector3 scale)) if (DeserializeDelta(reader, out byte baselineTick, out Vector3 position, out Quaternion rotation, out Vector3 scale))
{ {
OnServerToClientDeltaSync(baselineTick, position, rotation, scale); OnServerToClientDeltaSync(baselineTick, position, rotation, scale);
} }
@ -586,7 +592,7 @@ void UpdateServerDelta(double localTime)
// using (NetworkWriterPooled writer = NetworkWriterPool.Get()) // using (NetworkWriterPooled writer = NetworkWriterPool.Get())
writer.Position = 0; writer.Position = 0;
{ {
SerializeServerDelta( SerializeDelta(
writer, writer,
// include the last reliable baseline tick#. // include the last reliable baseline tick#.
// the unreliable delta is meant to go on top of it that, and no older one. // the unreliable delta is meant to go on top of it that, and no older one.
@ -718,12 +724,23 @@ void UpdateClientDelta(double localTime)
// TransformSnapshot snapshot = ConstructSnapshot(); // TransformSnapshot snapshot = ConstructSnapshot();
target.GetLocalPositionAndRotation(out Vector3 position, out Quaternion rotation); target.GetLocalPositionAndRotation(out Vector3 position, out Quaternion rotation);
CmdClientToServerSync( // reuse cached writer for performance
// only sync what the user wants to sync // using (NetworkWriterPooled writer = NetworkWriterPool.Get())
syncPosition ? position : default(Vector3?), writer.Position = 0;
syncRotation ? rotation : default(Quaternion?)//, {
// syncScale ? snapshot.scale : default(Vector3?) SerializeDelta(
); writer,
// include the last reliable baseline tick#.
// the unreliable delta is meant to go on top of it that, and no older one.
lastSerializedBaselineTick,
position,
rotation//,
// snapshot.scale
);
// send snapshot without timestamp.
// receiver gets it from batch timestamp to save bandwidth.
CmdClientToServerDeltaSync(writer);
}
lastClientSendTime = localTime; lastClientSendTime = localTime;
} }