Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ internal struct NetworkBatchHeader : INetworkSerializeByMemcpy
/// </summary>
public ushort Magic;

/// <summary>
/// Total number of messages in the batch.
/// </summary>
public ushort BatchCount;

/// <summary>
/// Total number of bytes in the batch.
/// </summary>
Expand All @@ -22,9 +27,5 @@ internal struct NetworkBatchHeader : INetworkSerializeByMemcpy
/// </summary>
public ulong BatchHash;

/// <summary>
/// Total number of messages in the batch.
/// </summary>
public ushort BatchCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public InvalidMessageStructureException(string issue) : base(issue)

internal class NetworkMessageManager : IDisposable
{
private const double k_WordAlignBatchCalc = 1.0 / 8.0;
public bool StopProcessing = false;

private struct ReceiveQueueItem
Expand Down Expand Up @@ -829,11 +830,17 @@ internal unsafe void ProcessSendQueues()
// Skipping the Verify and sneaking the write mark in because we know it's fine.
queueItem.Writer.Handle->AllowedWriteMark = sizeof(NetworkBatchHeader);
#endif
queueItem.BatchHeader.BatchHash = XXHash.Hash64(queueItem.Writer.GetUnsafePtr() + sizeof(NetworkBatchHeader), queueItem.Writer.Length - sizeof(NetworkBatchHeader));

queueItem.BatchHeader.BatchSize = queueItem.Writer.Length;

var alignedLength = (int)(Math.Ceiling(queueItem.Writer.Length * k_WordAlignBatchCalc) * 8);
queueItem.Writer.TryBeginWrite(alignedLength);
Comment thread
SamuelBellomo marked this conversation as resolved.

queueItem.BatchHeader.BatchHash = XXHash.Hash64(queueItem.Writer.GetUnsafePtr() + sizeof(NetworkBatchHeader), alignedLength - sizeof(NetworkBatchHeader));

queueItem.BatchHeader.BatchSize = alignedLength;

queueItem.Writer.WriteValue(queueItem.BatchHeader);
queueItem.Writer.Seek(alignedLength);


try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public IEnumerator TrackTotalNumberOfBytesSent()
}

Assert.True(observer.Found);
Assert.AreEqual(FastBufferWriter.GetWriteSize(messageName) + k_MessageOverhead, observer.Value);
var expectedSize = (long)(Math.Ceiling((FastBufferWriter.GetWriteSize(messageName) + k_MessageOverhead) * (1.0f / 8.0f)) * 8.0f);
Assert.AreEqual(expectedSize, observer.Value);
}

[UnityTest]
Expand All @@ -61,8 +62,6 @@ public IEnumerator TrackTotalNumberOfBytesReceived()
writer.Dispose();
}



var nbFrames = 0;
while (!observer.Found || nbFrames < 10)
{
Expand All @@ -71,7 +70,8 @@ public IEnumerator TrackTotalNumberOfBytesReceived()
}

Assert.True(observer.Found);
Assert.AreEqual(FastBufferWriter.GetWriteSize(messageName) + k_MessageOverhead, observer.Value);
var expectedSize = (long)(Math.Ceiling((FastBufferWriter.GetWriteSize(messageName) + k_MessageOverhead) * (1.0f / 8.0f)) * 8.0f);
Assert.AreEqual(expectedSize, observer.Value);
}

private class TotalBytesObserver : IMetricObserver
Expand All @@ -89,12 +89,23 @@ public TotalBytesObserver(IMetricDispatcher dispatcher, DirectionalMetricInfo me

public long Value { get; private set; }

private int m_BytesFoundCounter;
private long m_TotalBytes;

public void Observe(MetricCollection collection)
{
if (collection.TryGetCounter(m_MetricInfo.Id, out var counter) && counter.Value > 0)
{
Found = true;
Value = counter.Value;
// Don't assign another observed value once one is already observed
if (!Found)
{
Found = true;
Value = counter.Value;
var alignedSize = (long)(Math.Ceiling(counter.Value * 1.0f / 8.0f) * 8.0f);
m_TotalBytes += alignedSize;
m_BytesFoundCounter++;
UnityEngine.Debug.Log($"[{m_BytesFoundCounter}] Bytes Observed {counter.Value} | Total Bytes Observed: {m_TotalBytes}");
}
}
}
}
Expand Down