forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPooledNetworkReader.cs
More file actions
46 lines (41 loc) · 1.39 KB
/
PooledNetworkReader.cs
File metadata and controls
46 lines (41 loc) · 1.39 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;
using System.IO;
using UnityEngine;
namespace MLAPI.Serialization.Pooled
{
/// <summary>
/// Disposable NetworkReader that returns the Reader to the NetworkReaderPool when disposed
/// </summary>
public sealed class PooledNetworkReader : NetworkReader, IDisposable
{
private NetworkSerializer m_Serializer;
public NetworkSerializer Serializer => m_Serializer ?? (m_Serializer = new NetworkSerializer(this));
private bool m_IsDisposed = false;
internal PooledNetworkReader(Stream stream) : base(stream) { }
/// <summary>
/// Gets a PooledNetworkReader from the static NetworkReaderPool
/// </summary>
/// <returns>PooledNetworkReader</returns>
public static PooledNetworkReader Get(Stream stream)
{
var reader = NetworkReaderPool.GetReader(stream);
reader.m_IsDisposed = false;
return reader;
}
/// <summary>
/// Returns the PooledNetworkReader into the static NetworkReaderPool
/// </summary>
public void Dispose()
{
if (!m_IsDisposed)
{
m_IsDisposed = true;
NetworkReaderPool.PutBackInPool(this);
}
else
{
Debug.LogWarning("Disposing reader that thinks it is already disposed!");
}
}
}
}