ChannelBuffer: removed static message caching to simplify code and reduce state

This commit is contained in:
vis2k 2018-06-10 16:59:14 +02:00
parent d1e53f5811
commit 798afca9ec

View File

@ -19,11 +19,9 @@ class ChannelBuffer : IDisposable
bool m_IsBroken; bool m_IsBroken;
int m_MaxPendingPacketCount; int m_MaxPendingPacketCount;
const int k_MaxFreePacketCount = 512; // this is for all connections. maybe make this configurable (this is the pooling size!)
public const int MaxBufferedPackets = 512; // this is per connection. each is around 1400 bytes (MTU) public const int MaxBufferedPackets = 512; // this is per connection. each is around 1400 bytes (MTU)
Queue<ChannelPacket> m_PendingPackets; Queue<ChannelPacket> m_PendingPackets;
static List<ChannelPacket> s_FreePackets;
static internal int pendingPacketCount; // this is across all connections. only used for profiler metrics. static internal int pendingPacketCount; // this is across all connections. only used for profiler metrics.
// config // config
@ -61,10 +59,6 @@ public ChannelBuffer(NetworkConnection conn, int bufferSize, byte cid, bool isRe
if (isReliable) if (isReliable)
{ {
m_PendingPackets = new Queue<ChannelPacket>(); m_PendingPackets = new Queue<ChannelPacket>();
if (s_FreePackets == null)
{
s_FreePackets = new List<ChannelPacket>();
}
} }
} }
@ -89,16 +83,7 @@ protected virtual void Dispose(bool disposing)
{ {
if (m_PendingPackets != null) if (m_PendingPackets != null)
{ {
while (m_PendingPackets.Count > 0) pendingPacketCount = 0;
{
pendingPacketCount -= 1;
ChannelPacket packet = m_PendingPackets.Dequeue();
if (s_FreePackets.Count < k_MaxFreePacketCount)
{
s_FreePackets.Add(packet);
}
}
m_PendingPackets.Clear(); m_PendingPackets.Clear();
} }
} }
@ -335,41 +320,10 @@ void QueuePacket()
{ {
pendingPacketCount += 1; pendingPacketCount += 1;
m_PendingPackets.Enqueue(m_CurrentPacket); m_PendingPackets.Enqueue(m_CurrentPacket);
m_CurrentPacket = AllocPacket();
}
ChannelPacket AllocPacket() // create new m_currentPacket so that the one in the queue isn't touched anymore
{ // (calling .Reset would reset it in the queue too)
#if UNITY_EDITOR m_CurrentPacket = new ChannelPacket(m_MaxPacketSize, m_IsReliable);
UnityEditor.NetworkDetailStats.SetStat(
UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
MsgType.HLAPIPending, "msg", pendingPacketCount);
#endif
if (s_FreePackets.Count == 0)
{
return new ChannelPacket(m_MaxPacketSize, m_IsReliable);
}
var packet = s_FreePackets[s_FreePackets.Count - 1];
s_FreePackets.RemoveAt(s_FreePackets.Count - 1);
packet.Reset();
return packet;
}
static void FreePacket(ChannelPacket packet)
{
#if UNITY_EDITOR
UnityEditor.NetworkDetailStats.SetStat(
UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
MsgType.HLAPIPending, "msg", pendingPacketCount);
#endif
if (s_FreePackets.Count >= k_MaxFreePacketCount)
{
// just discard this packet, already tracking too many free packets
return;
}
s_FreePackets.Add(packet);
} }
public bool SendInternalBuffer() public bool SendInternalBuffer()
@ -391,7 +345,6 @@ public bool SendInternalBuffer()
break; break;
} }
pendingPacketCount -= 1; pendingPacketCount -= 1;
FreePacket(packet);
if (m_IsBroken && m_PendingPackets.Count < (m_MaxPendingPacketCount / 2)) if (m_IsBroken && m_PendingPackets.Count < (m_MaxPendingPacketCount / 2))
{ {