Skip to content

Commit 39f0502

Browse files
WIP
Tracking down additional issues.
1 parent 755009c commit 39f0502

8 files changed

Lines changed: 61 additions & 20 deletions

File tree

com.unity.multiplayer.mlapi/Runtime/Core/NetworkingManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -737,14 +737,14 @@ public NetworkUpdateManager.NetworkUpdateLoopCallbackFunction RegisterUpdate(Net
737737
private void Awake()
738738
{
739739
//We always add the networking manager as the first entry
740-
NetworkLoopUpdateSystems.Insert(0, this);
740+
//NetworkLoopUpdateSystems.Insert(0, this);
741741

742742
RpcQueueManager = new RPCQueueManager(LoopbackEnabled);
743743
//Note: Since frame history is not being used, this is set to 0
744744
//To test frame history, increase the number to (n) where n > 0
745745
RpcQueueManager?.Initialize(0);
746746

747-
NetworkUpdateManager.HandleNetworkLoopRegistrations(NetworkLoopUpdateSystems);
747+
//NetworkUpdateManager.HandleNetworkLoopRegistrations(NetworkLoopUpdateSystems);
748748

749749
NetworkUpdateManager.RegisterNetworkUpdateAction(NetworkPreUpdate, NetworkUpdateManager.NetworkUpdateStages.PREUPDATE);
750750
NetworkUpdateManager.RegisterNetworkUpdateAction(NetworkFixedUpdate, NetworkUpdateManager.NetworkUpdateStages.FIXEDUPDATE);
@@ -1267,7 +1267,7 @@ internal void HandleIncomingData(ulong clientId, string channelName, ArraySegmen
12671267
public static void InvokeRpc(FrameQueueItem queueItem)
12681268
{
12691269
#if DEVELOPMENT_BUILD || UNITY_EDITOR
1270-
s_InvokeRPC.Begin();
1270+
//s_InvokeRPC.Begin();
12711271
#endif
12721272

12731273
var networkObjectId = queueItem.StreamReader.ReadUInt64Packed();
@@ -1296,7 +1296,7 @@ public static void InvokeRpc(FrameQueueItem queueItem)
12961296
}
12971297

12981298
#if DEVELOPMENT_BUILD || UNITY_EDITOR
1299-
s_InvokeRPC.End();
1299+
//s_InvokeRPC.End();
13001300
#endif
13011301
}
13021302

com.unity.multiplayer.mlapi/Runtime/Core/RPCQueue/RPCQueueHistoryFrame.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,32 @@ public FrameQueueItem GetFirstQueueItem()
199199

200200
if (queueFrameType == QueueFrameType.Inbound)
201201
{
202-
CurrentQueueItem.ItemStream = PooledBitStream.Get();
203-
CurrentQueueItem.StreamWriter = PooledBitWriter.Get(CurrentQueueItem.ItemStream);
204-
CurrentQueueItem.StreamReader = PooledBitReader.Get(CurrentQueueItem.ItemStream);
202+
if(CurrentQueueItem.ItemStream == null)
203+
{
204+
CurrentQueueItem.ItemStream = PooledBitStream.Get();
205+
}
206+
else
207+
{
208+
UnityEngine.Debug.LogError("Current queue item already has a stream!");
209+
}
210+
if(CurrentQueueItem.StreamWriter == null)
211+
{
212+
CurrentQueueItem.StreamWriter = PooledBitWriter.Get(CurrentQueueItem.ItemStream);
213+
}
214+
else
215+
{
216+
UnityEngine.Debug.LogError("Current queue writer already has an instance!");
217+
}
218+
219+
if(CurrentQueueItem.StreamReader == null)
220+
{
221+
CurrentQueueItem.StreamReader = PooledBitReader.Get(CurrentQueueItem.ItemStream);
222+
}
223+
else
224+
{
225+
UnityEngine.Debug.LogError("Current queue reader already has an instance!");
226+
}
227+
205228
}
206229

207230
return GetCurrentQueueItem();
@@ -235,7 +258,8 @@ public void CloseQueue()
235258

236259
if (CurrentQueueItem.ItemStream != null)
237260
{
238-
CurrentQueueItem.ItemStream.Dispose();
261+
CurrentQueueItem.ItemStream.Dispose();
262+
239263
CurrentQueueItem.ItemStream = null;
240264
}
241265
}

com.unity.multiplayer.mlapi/Runtime/Core/RPCQueue/RPCQueueManager.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,19 @@ private static void ResetQueueHistoryFrame(QueueHistoryFrame queueFrame)
256256
internal void AddQueueItemToInboundFrame(QueueItemType qItemType, float timeStamp, ulong sourceNetworkId, BitStream message)
257257
{
258258
long originalPosition = message.Position;
259-
BitReader BR = BitReaderPool.GetReader(message);
259+
PooledBitReader BR = PooledBitReader.Get(message);
260260
//var byteValue = BR.ReadByte(); // MLAPI Proto Header (temporary, we reset position just below)
261261
var longValue = BR.ReadUInt64Packed(); // NetworkObjectId (temporary, we reset position just below)
262262

263263
var shortValue = BR.ReadUInt16Packed(); // NetworkBehaviourId (temporary, we reset position just below)
264264
ushort updateStageValue = BR.ReadUInt16Packed();
265-
if(longValue > 1111111 || shortValue > 111111)
265+
if(longValue > 1111111 || shortValue > 11111)
266266
{
267267
originalPosition = originalPosition;
268268
}
269+
270+
BR.Dispose();
271+
BR = null;
269272
NetworkUpdateManager.NetworkUpdateStages updateStage = NetworkUpdateManager.NetworkUpdateStages.UPDATE;
270273
if(System.Enum.IsDefined(typeof(NetworkUpdateManager.NetworkUpdateStages),(int)updateStageValue))
271274
{

com.unity.multiplayer.mlapi/Runtime/Core/RPCQueue/RPCQueueProcessing.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,17 @@ public static void ProcessReceiveQueue(NetworkUpdateManager.NetworkUpdateStages
8080
/// </summary>
8181
public void ProcessSendQueue()
8282
{
83+
InternalMessagesSendAndFlush();
84+
8385
#if DEVELOPMENT_BUILD || UNITY_EDITOR
8486
s_MLAPIRPCQueueSend.Begin();
8587
#endif
86-
8788
RPCQueueSendAndFlush();
8889

8990
#if DEVELOPMENT_BUILD || UNITY_EDITOR
9091
s_MLAPIRPCQueueSend.End();
9192
#endif
92-
InternalMessagesSendAndFlush();
93+
9394
}
9495

9596
/// <summary>

com.unity.multiplayer.mlapi/Runtime/Serialization/Pooled/BitStreamPool.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ namespace MLAPI.Serialization.Pooled
99
/// </summary>
1010
public static class BitStreamPool
1111
{
12-
private static byte createdStreams = 0;
12+
private static uint createdStreams = 0;
1313
private static readonly Queue<WeakReference> overflowStreams = new Queue<WeakReference>();
1414
private static readonly Queue<PooledBitStream> streams = new Queue<PooledBitStream>();
1515

16+
private const uint MaxBitPoolStreams = 1024;
17+
private const uint MaxCreatedDelta = 768;
18+
19+
1620
/// <summary>
1721
/// Retrieves an expandable PooledBitStream from the pool
1822
/// </summary>
@@ -38,12 +42,12 @@ public static PooledBitStream GetStream()
3842
return strongStream;
3943
}
4044
}
41-
42-
if (createdStreams == 254)
45+
46+
if (createdStreams == MaxBitPoolStreams)
4347
{
44-
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) NetworkLog.LogWarning("255 streams have been created. Did you forget to dispose?");
48+
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) NetworkLog.LogWarning(MaxBitPoolStreams.ToString() + " streams have been created. Did you forget to dispose?");
4549
}
46-
else if (createdStreams < 255) createdStreams++;
50+
else if (createdStreams < MaxBitPoolStreams) createdStreams++;
4751

4852
return new PooledBitStream();
4953
}
@@ -62,7 +66,7 @@ public static PooledBitStream GetStream()
6266
/// <param name="stream">The stream to put in the pool</param>
6367
public static void PutBackInPool(PooledBitStream stream)
6468
{
65-
if (streams.Count > 16)
69+
if (streams.Count > MaxCreatedDelta)
6670
{
6771
// The user just created lots of streams without returning them in between.
6872
// Streams are essentially byte array wrappers. This is valuable memory.

com.unity.multiplayer.mlapi/Runtime/Serialization/Pooled/PooledBitReader.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace MLAPI.Serialization.Pooled
99
public sealed class PooledBitReader : BitReader, IDisposable
1010
{
1111
private bool isDisposed = false;
12-
12+
1313
internal PooledBitReader(Stream stream) : base(stream)
1414
{
1515
}
@@ -35,6 +35,10 @@ public void Dispose()
3535
isDisposed = true;
3636
BitReaderPool.PutBackInPool(this);
3737
}
38+
else
39+
{
40+
UnityEngine.Debug.LogWarning("Disposing reader that thinks it is already disposed!");
41+
}
3842
}
3943
}
40-
}
44+
}

com.unity.multiplayer.mlapi/Runtime/Serialization/Pooled/PooledBitWriter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public void Dispose()
3535
isDisposed = true;
3636
BitWriterPool.PutBackInPool(this);
3737
}
38+
else
39+
{
40+
UnityEngine.Debug.LogError("Writer is being disposed but thinks it is already disposed");
41+
}
3842
}
3943
}
4044
}

com.unity.multiplayer.mlapi/Runtime/Spawning/SpawnManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ internal static void OnDestroyObject(ulong networkId, bool destroyGameObject)
688688
ClientIds = NetworkingManager.Singleton.ConnectedClientsList.Select(c => c.ClientId).ToArray()
689689
};
690690
rpcQueueManager.AddToInternalMLAPISendQueue(QueueItem);
691+
writer.Dispose();
691692
}
692693
}
693694
}

0 commit comments

Comments
 (0)