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,11 +365,15 @@ 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))
{
if (DeserializeDelta(reader, out byte baselineTick, out Vector3 position, out Quaternion rotation, out Vector3 scale))
{
OnClientToServerDeltaSync(baselineTick, position, rotation);//, scale);
//For client authority, immediately pass on the client snapshot to all other //For client authority, immediately pass on the client snapshot to all other
//clients instead of waiting for server to send its snapshots. //clients instead of waiting for server to send its snapshots.
if (syncDirection == SyncDirection.ClientToServer && if (syncDirection == SyncDirection.ClientToServer &&
@ -392,9 +396,11 @@ void CmdClientToServerSync(Vector3? position, Quaternion? rotation)//, Vector3?
} }
} }
} }
}
}
// 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;
} }