forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitWriterPool.cs
More file actions
46 lines (40 loc) · 1.65 KB
/
Copy pathBitWriterPool.cs
File metadata and controls
46 lines (40 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Collections.Generic;
using System.IO;
using MLAPI.Logging;
namespace MLAPI.Serialization.Pooled
{
/// <summary>
/// Static class containing PooledBitWriters
/// </summary>
public static class BitWriterPool
{
private static byte createdWriters = 0;
private static readonly Queue<PooledBitWriter> writers = new Queue<PooledBitWriter>();
/// <summary>
/// Retrieves a PooledBitWriter
/// </summary>
/// <param name="stream">The stream the writer should write to</param>
/// <returns>A PooledBitWriter</returns>
public static PooledBitWriter GetWriter(Stream stream)
{
if (writers.Count == 0)
{
if (createdWriters == 254) if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("255 writers have been created. Did you forget to dispose?");
else if (createdWriters < 255) createdWriters++;
return new PooledBitWriter(stream);
}
PooledBitWriter writer = writers.Dequeue();
writer.SetStream(stream);
return writer;
}
/// <summary>
/// Puts a PooledBitWriter back into the pool
/// </summary>
/// <param name="writer">The writer to put in the pool</param>
public static void PutBackInPool(PooledBitWriter writer)
{
if (writers.Count < 64) writers.Enqueue(writer);
else if (LogHelper.CurrentLogLevel <= LogLevel.Developer) LogHelper.LogInfo("BitWriterPool already has 64 queued. Throwing to GC. Did you forget to dispose?");
}
}
}