Compare commits

...

5 Commits

Author SHA1 Message Date
miwarnec
6d6d31b202 fix: server doesn't overwrite client authority sync points 2024-11-04 11:59:45 +01:00
miwarnec
6ece0c7d71 nthybrid: OnClientToServer check ordering 2024-11-04 11:25:52 +01:00
miwarnec
93cc81707a nthybrid: OnServerToClient checks for host mode first to avoid noise! 2024-11-04 11:17:06 +01:00
miwarnec
c8c632ce6c debug draw: drops 2024-11-04 10:41:48 +01:00
miwarnec
707bb87fb6 nthybrid: debug draw data points 2024-11-04 10:40:31 +01:00

View File

@ -102,6 +102,7 @@ public class NetworkTransformHybrid2022 : NetworkBehaviour
// debugging /////////////////////////////////////////////////////////// // debugging ///////////////////////////////////////////////////////////
[Header("Debug")] [Header("Debug")]
public bool debugDraw;
public bool showGizmos; public bool showGizmos;
public bool showOverlay; public bool showOverlay;
public Color overlayColor = new Color(0, 0, 0, 0.5f); public Color overlayColor = new Color(0, 0, 0, 0.5f);
@ -213,6 +214,9 @@ void CmdClientToServerBaseline_PositionRotation(byte baselineTick, Vector3 posit
lastDeserializedBaselinePosition = position; lastDeserializedBaselinePosition = position;
lastDeserializedBaselineRotation = rotation; lastDeserializedBaselineRotation = rotation;
// debug draw: baseline
if (debugDraw) Debug.DrawLine(position, position + Vector3.up, Color.yellow, 10f);
// if baseline counts as delta, insert it into snapshot buffer too // if baseline counts as delta, insert it into snapshot buffer too
if (baselineIsDelta) if (baselineIsDelta)
OnClientToServerDeltaSync(baselineTick, position, rotation);//, scale); OnClientToServerDeltaSync(baselineTick, position, rotation);//, scale);
@ -224,6 +228,9 @@ void CmdClientToServerBaseline_Position(byte baselineTick, Vector3 position)
lastDeserializedBaselineTick = baselineTick; lastDeserializedBaselineTick = baselineTick;
lastDeserializedBaselinePosition = position; lastDeserializedBaselinePosition = position;
// debug draw: baseline
if (debugDraw) Debug.DrawLine(position, position + Vector3.up, Color.yellow, 10f);
// if baseline counts as delta, insert it into snapshot buffer too // if baseline counts as delta, insert it into snapshot buffer too
if (baselineIsDelta) if (baselineIsDelta)
OnClientToServerDeltaSync(baselineTick, position, Quaternion.identity);//, scale); OnClientToServerDeltaSync(baselineTick, position, Quaternion.identity);//, scale);
@ -244,6 +251,9 @@ void CmdClientToServerBaseline_Rotation(byte baselineTick, Quaternion rotation)
[Command(channel = Channels.Unreliable)] // unreliable delta [Command(channel = Channels.Unreliable)] // unreliable delta
void CmdClientToServerDelta_Position(byte baselineTick, Vector3 position) void CmdClientToServerDelta_Position(byte baselineTick, Vector3 position)
{ {
// debug draw: delta
if (debugDraw) Debug.DrawLine(position, position + Vector3.up, Color.white, 10f);
// Debug.Log($"[{name}] server received delta for baseline #{lastDeserializedBaselineTick}"); // Debug.Log($"[{name}] server received delta for baseline #{lastDeserializedBaselineTick}");
OnClientToServerDeltaSync(baselineTick, position, Quaternion.identity);//, scale); OnClientToServerDeltaSync(baselineTick, position, Quaternion.identity);//, scale);
} }
@ -258,6 +268,9 @@ void CmdClientToServerDelta_Rotation(byte baselineTick, Quaternion rotation)
[Command(channel = Channels.Unreliable)] // unreliable delta [Command(channel = Channels.Unreliable)] // unreliable delta
void CmdClientToServerDelta_PositionRotation(byte baselineTick, Vector3 position, Quaternion rotation) void CmdClientToServerDelta_PositionRotation(byte baselineTick, Vector3 position, Quaternion rotation)
{ {
// debug draw: delta
if (debugDraw) Debug.DrawLine(position, position + Vector3.up, Color.white, 10f);
// Debug.Log($"[{name}] server received delta for baseline #{lastDeserializedBaselineTick}"); // Debug.Log($"[{name}] server received delta for baseline #{lastDeserializedBaselineTick}");
OnClientToServerDeltaSync(baselineTick, position, rotation);//, scale); OnClientToServerDeltaSync(baselineTick, position, rotation);//, scale);
} }
@ -265,19 +278,25 @@ void CmdClientToServerDelta_PositionRotation(byte baselineTick, Vector3 position
// local authority client sends sync message to server for broadcasting // local authority client sends sync message to server for broadcasting
protected virtual void OnClientToServerDeltaSync(byte baselineTick, 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
if (syncDirection != SyncDirection.ClientToServer) return;
// ensure this delta is for our last known baseline. // ensure this delta is for our last known baseline.
// we should never apply a delta on top of a wrong baseline. // we should never apply a delta on top of a wrong baseline.
if (baselineTick != lastDeserializedBaselineTick) if (baselineTick != lastDeserializedBaselineTick)
{ {
// debug draw: drop
if (debugDraw)
{
if (position.HasValue) Debug.DrawLine(position.Value, position.Value + Vector3.up, Color.red, 10f);
}
// this can happen if unreliable arrives before reliable etc. // this can happen if unreliable arrives before reliable etc.
// no need to log this except when debugging. // no need to log this except when debugging.
// Debug.Log($"[{name}] Server: received delta for wrong baseline #{baselineTick} from: {connectionToClient}. Last was {lastDeserializedBaselineTick}. Ignoring."); // Debug.Log($"[{name}] Server: received delta for wrong baseline #{baselineTick} from: {connectionToClient}. Last was {lastDeserializedBaselineTick}. Ignoring.");
return; return;
} }
// only apply if in client authority mode
if (syncDirection != SyncDirection.ClientToServer) return;
// protect against ever-growing buffer size attacks // protect against ever-growing buffer size attacks
if (serverSnapshots.Count >= connectionToClient.snapshotBufferSizeLimit) return; if (serverSnapshots.Count >= connectionToClient.snapshotBufferSizeLimit) return;
@ -324,6 +343,9 @@ void RpcServerToClientBaseline_PositionRotation(byte baselineTick, Vector3 posit
lastDeserializedBaselinePosition = position; lastDeserializedBaselinePosition = position;
lastDeserializedBaselineRotation = rotation; lastDeserializedBaselineRotation = rotation;
// debug draw: baseline
if (debugDraw) Debug.DrawLine(position, position + Vector3.up, Color.yellow, 10f);
// if baseline counts as delta, insert it into snapshot buffer too // if baseline counts as delta, insert it into snapshot buffer too
if (baselineIsDelta) if (baselineIsDelta)
OnServerToClientDeltaSync(baselineTick, position, rotation);//, Vector3.zero);//, scale); OnServerToClientDeltaSync(baselineTick, position, rotation);//, Vector3.zero);//, scale);
@ -340,6 +362,9 @@ void RpcServerToClientBaseline_Position(byte baselineTick, Vector3 position)
lastDeserializedBaselineTick = baselineTick; lastDeserializedBaselineTick = baselineTick;
lastDeserializedBaselinePosition = position; lastDeserializedBaselinePosition = position;
// debug draw: baseline
if (debugDraw) Debug.DrawLine(position, position + Vector3.up, Color.yellow, 10f);
// if baseline counts as delta, insert it into snapshot buffer too // if baseline counts as delta, insert it into snapshot buffer too
if (baselineIsDelta) if (baselineIsDelta)
OnServerToClientDeltaSync(baselineTick, position, Quaternion.identity);//, Vector3.zero);//, scale); OnServerToClientDeltaSync(baselineTick, position, Quaternion.identity);//, Vector3.zero);//, scale);
@ -369,6 +394,9 @@ void RpcServerToClientDelta_PositionRotation(byte baselineTick, Vector3 position
// ignore if this object is owned by this client. // ignore if this object is owned by this client.
if (IsClientWithAuthority) return; if (IsClientWithAuthority) return;
// debug draw: delta
if (debugDraw) Debug.DrawLine(position, position + Vector3.up, Color.white, 10f);
OnServerToClientDeltaSync(baselineTick, position, rotation);//, scale); OnServerToClientDeltaSync(baselineTick, position, rotation);//, scale);
} }
@ -380,6 +408,9 @@ void RpcServerToClientDelta_Position(byte baselineTick, Vector3 position)
// ignore if this object is owned by this client. // ignore if this object is owned by this client.
if (IsClientWithAuthority) return; if (IsClientWithAuthority) return;
// debug draw: delta
if (debugDraw) Debug.DrawLine(position, position + Vector3.up, Color.white, 10f);
OnServerToClientDeltaSync(baselineTick, position, Quaternion.identity);//, scale); OnServerToClientDeltaSync(baselineTick, position, Quaternion.identity);//, scale);
} }
@ -397,16 +428,6 @@ void RpcServerToClientDelta_Rotation(byte baselineTick, Quaternion rotation)
// server broadcasts sync message to all clients // server broadcasts sync message to all clients
protected virtual void OnServerToClientDeltaSync(byte baselineTick, Vector3 position, Quaternion rotation)//, Vector3 scale) protected virtual void OnServerToClientDeltaSync(byte baselineTick, Vector3 position, Quaternion rotation)//, Vector3 scale)
{ {
// ensure this delta is for our last known baseline.
// we should never apply a delta on top of a wrong baseline.
if (baselineTick != lastDeserializedBaselineTick)
{
// this can happen if unreliable arrives before reliable etc.
// no need to log this except when debugging.
// Debug.Log($"[{name}] Client: received delta for wrong baseline #{baselineTick}. Last was {lastDeserializedBaselineTick}. Ignoring.");
return;
}
// in host mode, the server sends rpcs to all clients. // in host mode, the server sends rpcs to all clients.
// the host client itself will receive them too. // the host client itself will receive them too.
// -> host server is always the source of truth // -> host server is always the source of truth
@ -418,6 +439,22 @@ protected virtual void OnServerToClientDeltaSync(byte baselineTick, Vector3 posi
// don't apply for local player with authority // don't apply for local player with authority
if (IsClientWithAuthority) return; if (IsClientWithAuthority) return;
// ensure this delta is for our last known baseline.
// we should never apply a delta on top of a wrong baseline.
if (baselineTick != lastDeserializedBaselineTick)
{
// debug draw: drop
if (debugDraw)
{
Debug.DrawLine(position, position + Vector3.up, Color.red, 10f);
}
// this can happen if unreliable arrives before reliable etc.
// no need to log this except when debugging.
// Debug.Log($"[{name}] Client: received delta for wrong baseline #{baselineTick}. Last was {lastDeserializedBaselineTick}. Ignoring.");
return;
}
// Debug.Log($"[{name}] Client: received delta for baseline #{baselineTick}"); // Debug.Log($"[{name}] Client: received delta for baseline #{baselineTick}");
// on the client, we receive rpcs for all entities. // on the client, we receive rpcs for all entities.
@ -647,8 +684,13 @@ void UpdateServer()
// should we broadcast at all? // should we broadcast at all?
if (!disableSendingThisToClients) // CUSTOM CHANGE: see comment at definition if (!disableSendingThisToClients) // CUSTOM CHANGE: see comment at definition
{ {
UpdateServerBaseline(localTime); // only broadcast for server owned objects.
UpdateServerDelta(localTime); // otherwise server would overwrite ClientToServer object's baselines.
if (syncDirection == SyncDirection.ServerToClient || IsClientWithAuthority)
{
UpdateServerBaseline(localTime);
UpdateServerDelta(localTime);
}
} }
// interpolate remote clients // interpolate remote clients