added hybrid onchange compression to serializedobject helpers

BinaryServer
Anton Franzluebbers 2022-01-24 21:32:09 -05:00
parent 80a16c4ce2
commit 7d110bb8ad
4 changed files with 72 additions and 7 deletions

View File

@ -161,6 +161,7 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
networkObject: {fileID: 3951900052977689805} networkObject: {fileID: 3951900052977689805}
serializationRateHz: 60 serializationRateHz: 60
hybridOnChangeCompression: 1
useLocalTransform: 0 useLocalTransform: 0
teleportDistance: 0 teleportDistance: 0
teleportAngle: 0 teleportAngle: 0

View File

@ -65,6 +65,16 @@ namespace VelNet
#endregion #endregion
public static bool SameAs(this byte[] bytes, byte[] otherBytes)
{
if (bytes.Length != otherBytes.Length)
{
return false;
}
return !bytes.Where((t, i) => t != otherBytes[i]).Any();
}
/// <summary> /// <summary>
/// Compresses the list of bools into bytes using a bitmask /// Compresses the list of bools into bytes using a bitmask
/// </summary> /// </summary>

View File

@ -1,6 +1,6 @@
using System.Collections; using System;
using System.Collections;
using UnityEngine; using UnityEngine;
using UnityEngine.Serialization;
namespace VelNet namespace VelNet
{ {
@ -9,6 +9,15 @@ namespace VelNet
[Tooltip("Send rate of this object. This caps out at the framerate of the game.")] [Tooltip("Send rate of this object. This caps out at the framerate of the game.")]
public float serializationRateHz = 30; public float serializationRateHz = 30;
/// <summary>
/// If the data hasn't changed, only sends updates across the network at 1Hz
/// </summary>
public bool hybridOnChangeCompression = true;
private byte[] lastSentBytes;
private double lastSendTime;
private const double slowSendInterval = 2;
protected virtual void Awake() protected virtual void Awake()
{ {
StartCoroutine(SendMessageUpdate()); StartCoroutine(SendMessageUpdate());
@ -17,10 +26,31 @@ namespace VelNet
private IEnumerator SendMessageUpdate() private IEnumerator SendMessageUpdate()
{ {
while (true) while (true)
{
try
{ {
if (IsMine) if (IsMine)
{ {
SendBytes(SendState()); byte[] newBytes = SendState();
if (hybridOnChangeCompression)
{
if (Time.timeAsDouble - lastSendTime > slowSendInterval || !lastSentBytes.SameAs(newBytes))
{
SendBytes(newBytes);
}
}
else
{
SendBytes(newBytes);
}
lastSendTime = Time.timeAsDouble;
lastSentBytes = newBytes;
}
}
catch (Exception e)
{
Debug.LogError(e);
} }
yield return new WaitForSeconds(1f / serializationRateHz); yield return new WaitForSeconds(1f / serializationRateHz);

View File

@ -2,7 +2,6 @@
using System.Collections; using System.Collections;
using System.IO; using System.IO;
using UnityEngine; using UnityEngine;
using UnityEngine.Serialization;
namespace VelNet namespace VelNet
{ {
@ -11,6 +10,16 @@ namespace VelNet
[Tooltip("Send rate of this object. This caps out at the framerate of the game.")] [Tooltip("Send rate of this object. This caps out at the framerate of the game.")]
public float serializationRateHz = 30; public float serializationRateHz = 30;
/// <summary>
/// If the data hasn't changed, only sends updates across the network at 1Hz
/// </summary>
public bool hybridOnChangeCompression = true;
private byte[] lastSentBytes;
private double lastSendTime;
private const double slowSendInterval = 2;
protected virtual void Awake() protected virtual void Awake()
{ {
StartCoroutine(SendMessageUpdate()); StartCoroutine(SendMessageUpdate());
@ -27,7 +36,22 @@ namespace VelNet
using MemoryStream mem = new MemoryStream(); using MemoryStream mem = new MemoryStream();
using BinaryWriter writer = new BinaryWriter(mem); using BinaryWriter writer = new BinaryWriter(mem);
SendState(writer); SendState(writer);
SendBytes(mem.ToArray());
byte[] newBytes = mem.ToArray();
if (hybridOnChangeCompression)
{
if (Time.timeAsDouble - lastSendTime > slowSendInterval || !lastSentBytes.SameAs(newBytes))
{
SendBytes(newBytes);
}
}
else
{
SendBytes(newBytes);
}
lastSendTime = Time.timeAsDouble;
lastSentBytes = newBytes;
} }
} }
catch (Exception e) catch (Exception e)