Skip to content

Commit 2ceb7eb

Browse files
Jeffrey Rainyjeffreyrainy0xFA11
authored
fix: Dispose of PooledBitStream used in the MessageBatcher (#436)
* fix: Dispose of PooledBitStream used in the MessageBatcher, also stores only the FrameQueueItem's channel, as opposed to the whole FrameQueueItem MTT-193 MTT-250 * perf: avoid transport issues by dropping message size * style: using 'using' instead of manually 'dispose'ing Co-authored-by: Jeffrey Rainy <jeffrey.rainy@unity3d.com> Co-authored-by: M. Fatih MAR <mfatihmar@gmail.com>
1 parent 210e5fc commit 2ceb7eb

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MessageBatcher
1313
{
1414
public class SendStream
1515
{
16-
public FrameQueueItem Item;
16+
public string channel;
1717
public PooledBitStream Stream = PooledBitStream.Get();
1818
public bool Empty = true;
1919
}
@@ -114,7 +114,7 @@ public void QueueItem(in FrameQueueItem item)
114114
if (SendDict[clientId].Empty)
115115
{
116116
SendDict[clientId].Empty = false;
117-
SendDict[clientId].Item = item;
117+
SendDict[clientId].channel = item.Channel;
118118
Writer.SetStream(SendDict[clientId].Stream);
119119

120120
Writer.WriteBit(false); // Encrypted
@@ -187,6 +187,7 @@ public void SendItems(int threshold, SendCallbackType sendCallback)
187187
/// <param name="receiveTime"> the packet receive time to pass back to callback</param>
188188
public int ReceiveItems(in BitStream messageStream, ReceiveCallbackType receiveCallback, MLAPI.RPCQueueManager.QueueItemType messageType, ulong clientId, float receiveTime)
189189
{
190+
using PooledBitStream copy = PooledBitStream.Get();
190191
do
191192
{
192193
// read the length of the next RPC
@@ -200,7 +201,6 @@ public int ReceiveItems(in BitStream messageStream, ReceiveCallbackType receiveC
200201

201202
// copy what comes after current stream position
202203
long pos = messageStream.Position;
203-
BitStream copy = PooledBitStream.Get();
204204
copy.SetLength(rpcSize);
205205
copy.Position = 0;
206206
Buffer.BlockCopy(messageStream.GetBuffer(), (int)pos, copy.GetBuffer(), 0, rpcSize);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal class RPCQueueProcessing
2525
#endif
2626
// Batcher object used to manage the RPC batching on the send side
2727
private MessageBatcher batcher = new MessageBatcher();
28-
private int BatchThreshold = 1000;
28+
private int BatchThreshold = 512;
2929

3030
//NSS-TODO: Need to determine how we want to handle all other MLAPI send types
3131
//Temporary place to keep internal MLAPI messages
@@ -208,7 +208,7 @@ private static void SendCallback(ulong clientId, MLAPI.MessageBatcher.SendStream
208208
ArraySegment<byte> sendBuffer = new ArraySegment<byte>(bytes, 0, length);
209209

210210
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(clientId, sendBuffer,
211-
string.IsNullOrEmpty(sendStream.Item.Channel) ? "MLAPI_DEFAULT_MESSAGE" : sendStream.Item.Channel);
211+
string.IsNullOrEmpty(sendStream.channel) ? "MLAPI_DEFAULT_MESSAGE" : sendStream.channel);
212212
}
213213
}
214214
}

0 commit comments

Comments
 (0)