Problem: NetworkConnection.SetMaxDelay(float) has no effect because ChannelBuffer.m_LastFlushTime is never set. This means that the buffer is flushed every update instead of every maxDelay seconds.
Solution: Set m_LastFlushTime to Time.time inside the ChannelBuffer.SendInternalBuffer() method.
Code showing the issue (decompiled from UnityEngine.Networking.dll)
ChannelBuffer.cs
//This is the only place that m_LastFlushTime is referenced; it is never set besides in the constructor
public void CheckInternalBuffer()
{
if (((Time.time - this.m_LastFlushTime) > this.maxDelay) && !this.m_CurrentPacket.IsEmpty())
{
this.SendInternalBuffer();
}
if ((Time.time - this.m_LastBufferedMessageCountTimer) > 1f)
{
this.lastBufferedPerSecond = this.numBufferedPerSecond;
this.numBufferedPerSecond = 0;
this.m_LastBufferedMessageCountTimer = Time.time;
}
}
//This function should be setting m_LastFlushTime but it does not.
public bool SendInternalBuffer()
{
NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Outgoing, 0x1d, "msg", 1);
if (!this.m_isReliable || (this.m_PendingPackets.Count <= 0))
{
return this.m_CurrentPacket.SendToTransport(this.m_hostId, this.m_connectionId, this.m_channelId);
}
while (this.m_PendingPackets.Count > 0)
{
ChannelPacket packet = this.m_PendingPackets[0];
if (!packet.SendToTransport(this.m_hostId, this.m_connectionId, this.m_channelId))
{
break;
}
s_PendingPacketCount--;
this.m_PendingPackets.RemoveAt(0);
FreePacket(packet);
if (this.m_isBroken && (this.m_PendingPackets.Count < (this.m_maxPendingPacketCount / 2)))
{
if (LogFilter.logWarn)
{
Debug.LogWarning("ChannelBuffer recovered from overflow but data was lost.");
}
this.m_isBroken = false;
}
}
return true;
}
NetworkConnection.cs
internal void FlushInternalBuffer()
{
if (this.m_Channels != null)
{
foreach (ChannelBuffer buffer in this.m_Channels)
{
buffer.CheckInternalBuffer();
}
}
}
NetworkConnection.FlushInternalBuffer() is called every update on both client and server for each connection present.
I have verified this bug with the debugger. For all connections, on client or server, the m_LastFlushTime field is always 0f.