added hybrid onchange compression to serializedobject helpers
parent
682c470d46
commit
08a1a72348
|
|
@ -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>
|
||||||
|
|
|
||||||
|
|
@ -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());
|
||||||
|
|
@ -18,9 +27,30 @@ namespace VelNet
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (IsMine)
|
try
|
||||||
{
|
{
|
||||||
SendBytes(SendState());
|
if (IsMine)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
@ -44,7 +68,7 @@ namespace VelNet
|
||||||
{
|
{
|
||||||
using MemoryStream mem = new MemoryStream(message);
|
using MemoryStream mem = new MemoryStream(message);
|
||||||
using BinaryReader reader = new BinaryReader(mem);
|
using BinaryReader reader = new BinaryReader(mem);
|
||||||
|
|
||||||
ReceiveState(reader);
|
ReceiveState(reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue