added hybrid onchange compression to serializedobject helpers
parent
682c470d46
commit
08a1a72348
|
|
@ -65,6 +65,16 @@ namespace VelNet
|
|||
#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>
|
||||
/// Compresses the list of bools into bytes using a bitmask
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace VelNet
|
||||
{
|
||||
|
|
@ -9,6 +9,15 @@ namespace VelNet
|
|||
[Tooltip("Send rate of this object. This caps out at the framerate of the game.")]
|
||||
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()
|
||||
{
|
||||
StartCoroutine(SendMessageUpdate());
|
||||
|
|
@ -17,10 +26,31 @@ namespace VelNet
|
|||
private IEnumerator SendMessageUpdate()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
using System.Collections;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace VelNet
|
||||
{
|
||||
|
|
@ -11,6 +10,16 @@ namespace VelNet
|
|||
[Tooltip("Send rate of this object. This caps out at the framerate of the game.")]
|
||||
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()
|
||||
{
|
||||
StartCoroutine(SendMessageUpdate());
|
||||
|
|
@ -27,7 +36,22 @@ namespace VelNet
|
|||
using MemoryStream mem = new MemoryStream();
|
||||
using BinaryWriter writer = new BinaryWriter(mem);
|
||||
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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue