Initial Commit
This commit is contained in:
parent
53eb92e9af
commit
270ab7d11f
15341 changed files with 700234 additions and 0 deletions
|
@ -0,0 +1,90 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MLAPI.Logging;
|
||||
|
||||
namespace MLAPI.Serialization.Pooled
|
||||
{
|
||||
/// <summary>
|
||||
/// Static class containing PooledNetworkBuffers
|
||||
/// </summary>
|
||||
public static class NetworkBufferPool
|
||||
{
|
||||
private static uint s_CreatedBuffers = 0;
|
||||
private static Queue<WeakReference> s_OverflowBuffers = new Queue<WeakReference>();
|
||||
private static Queue<PooledNetworkBuffer> s_Buffers = new Queue<PooledNetworkBuffer>();
|
||||
|
||||
private const uint k_MaxBitPoolBuffers = 1024;
|
||||
private const uint k_MaxCreatedDelta = 768;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves an expandable PooledNetworkBuffer from the pool
|
||||
/// </summary>
|
||||
/// <returns>An expandable PooledNetworkBuffer</returns>
|
||||
public static PooledNetworkBuffer GetBuffer()
|
||||
{
|
||||
if (s_Buffers.Count == 0)
|
||||
{
|
||||
if (s_OverflowBuffers.Count > 0)
|
||||
{
|
||||
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
|
||||
{
|
||||
NetworkLog.LogInfo($"Retrieving {nameof(PooledNetworkBuffer)} from overflow pool. Recent burst?");
|
||||
}
|
||||
|
||||
object weakBuffer = null;
|
||||
while (s_OverflowBuffers.Count > 0 && ((weakBuffer = s_OverflowBuffers.Dequeue().Target) == null)) ;
|
||||
|
||||
if (weakBuffer != null)
|
||||
{
|
||||
PooledNetworkBuffer strongBuffer = (PooledNetworkBuffer)weakBuffer;
|
||||
|
||||
strongBuffer.SetLength(0);
|
||||
strongBuffer.Position = 0;
|
||||
|
||||
return strongBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
if (s_CreatedBuffers == k_MaxBitPoolBuffers)
|
||||
{
|
||||
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) NetworkLog.LogWarning($"{k_MaxBitPoolBuffers} buffers have been created. Did you forget to dispose?");
|
||||
}
|
||||
else if (s_CreatedBuffers < k_MaxBitPoolBuffers) s_CreatedBuffers++;
|
||||
|
||||
return new PooledNetworkBuffer();
|
||||
}
|
||||
|
||||
PooledNetworkBuffer buffer = s_Buffers.Dequeue();
|
||||
buffer.SetLength(0);
|
||||
buffer.Position = 0;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts a PooledNetworkBuffer back into the pool
|
||||
/// </summary>
|
||||
/// <param name="buffer">The buffer to put in the pool</param>
|
||||
public static void PutBackInPool(PooledNetworkBuffer buffer)
|
||||
{
|
||||
if (s_Buffers.Count > k_MaxCreatedDelta)
|
||||
{
|
||||
// The user just created lots of buffers without returning them in between.
|
||||
// Buffers are essentially byte array wrappers. This is valuable memory.
|
||||
// Thus we put this buffer as a weak reference incase of another burst
|
||||
// But still leave it to GC
|
||||
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
|
||||
{
|
||||
NetworkLog.LogInfo($"Putting {nameof(PooledNetworkBuffer)} into overflow pool. Did you forget to dispose?");
|
||||
}
|
||||
|
||||
s_OverflowBuffers.Enqueue(new WeakReference(buffer));
|
||||
}
|
||||
else
|
||||
{
|
||||
s_Buffers.Enqueue(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b12cc234a5c700041bb1f08f8229f676
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,52 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using MLAPI.Logging;
|
||||
|
||||
namespace MLAPI.Serialization.Pooled
|
||||
{
|
||||
/// <summary>
|
||||
/// Static class containing PooledNetworkReaders
|
||||
/// </summary>
|
||||
public static class NetworkReaderPool
|
||||
{
|
||||
private static byte s_CreatedReaders = 0;
|
||||
private static Queue<PooledNetworkReader> s_Readers = new Queue<PooledNetworkReader>();
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a PooledNetworkReader
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream the reader should read from</param>
|
||||
/// <returns>A PooledNetworkReader</returns>
|
||||
public static PooledNetworkReader GetReader(Stream stream)
|
||||
{
|
||||
if (s_Readers.Count == 0)
|
||||
{
|
||||
if (s_CreatedReaders == 254)
|
||||
{
|
||||
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) NetworkLog.LogWarning("255 readers have been created. Did you forget to dispose?");
|
||||
}
|
||||
else if (s_CreatedReaders < 255) s_CreatedReaders++;
|
||||
|
||||
return new PooledNetworkReader(stream);
|
||||
}
|
||||
|
||||
PooledNetworkReader reader = s_Readers.Dequeue();
|
||||
reader.SetStream(stream);
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts a PooledNetworkReader back into the pool
|
||||
/// </summary>
|
||||
/// <param name="reader">The reader to put in the pool</param>
|
||||
public static void PutBackInPool(PooledNetworkReader reader)
|
||||
{
|
||||
if (s_Readers.Count < 64) s_Readers.Enqueue(reader);
|
||||
else if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
|
||||
{
|
||||
NetworkLog.LogInfo($"{nameof(NetworkReaderPool)} already has 64 queued. Throwing to GC. Did you forget to dispose?");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 14a958cf00e46084693623b5c90ba657
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,52 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using MLAPI.Logging;
|
||||
|
||||
namespace MLAPI.Serialization.Pooled
|
||||
{
|
||||
/// <summary>
|
||||
/// Static class containing PooledNetworkWriters
|
||||
/// </summary>
|
||||
public static class NetworkWriterPool
|
||||
{
|
||||
private static byte s_CreatedWriters = 0;
|
||||
private static Queue<PooledNetworkWriter> s_Writers = new Queue<PooledNetworkWriter>();
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a PooledNetworkWriter
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream the writer should write to</param>
|
||||
/// <returns>A PooledNetworkWriter</returns>
|
||||
public static PooledNetworkWriter GetWriter(Stream stream)
|
||||
{
|
||||
if (s_Writers.Count == 0)
|
||||
{
|
||||
if (s_CreatedWriters == 254)
|
||||
{
|
||||
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) NetworkLog.LogWarning("255 writers have been created. Did you forget to dispose?");
|
||||
}
|
||||
else if (s_CreatedWriters < 255) s_CreatedWriters++;
|
||||
|
||||
return new PooledNetworkWriter(stream);
|
||||
}
|
||||
|
||||
PooledNetworkWriter writer = s_Writers.Dequeue();
|
||||
writer.SetStream(stream);
|
||||
|
||||
return writer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts a PooledNetworkWriter back into the pool
|
||||
/// </summary>
|
||||
/// <param name="writer">The writer to put in the pool</param>
|
||||
public static void PutBackInPool(PooledNetworkWriter writer)
|
||||
{
|
||||
if (s_Writers.Count < 64) s_Writers.Enqueue(writer);
|
||||
else if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
|
||||
{
|
||||
NetworkLog.LogInfo($"{nameof(NetworkWriterPool)} already has 64 queued. Throwing to GC. Did you forget to dispose?");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2cec275be55b22c4ba0fe27f74fbe2a9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
|
||||
namespace MLAPI.Serialization.Pooled
|
||||
{
|
||||
/// <summary>
|
||||
/// Disposable NetworkBuffer that returns back to the NetworkBufferPool when disposed
|
||||
/// </summary>
|
||||
public sealed class PooledNetworkBuffer : NetworkBuffer, IDisposable
|
||||
{
|
||||
private bool isDisposed = false;
|
||||
|
||||
internal PooledNetworkBuffer() { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a PooledNetworkBuffer from the static NetworkBufferPool
|
||||
/// </summary>
|
||||
/// <returns>PooledNetworkBuffer</returns>
|
||||
public static PooledNetworkBuffer Get()
|
||||
{
|
||||
var buffer = NetworkBufferPool.GetBuffer();
|
||||
buffer.isDisposed = false;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the PooledNetworkBuffer into the static NetworkBufferPool
|
||||
/// </summary>
|
||||
public new void Dispose()
|
||||
{
|
||||
if (!isDisposed)
|
||||
{
|
||||
isDisposed = true;
|
||||
NetworkBufferPool.PutBackInPool(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4b9ca4cb2f1e219419c81cc68ad3b9b1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 31928243e74f75541b5e5c4ae87149db
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MLAPI.Serialization.Pooled
|
||||
{
|
||||
/// <summary>
|
||||
/// Disposable NetworkWriter that returns the Writer to the NetworkWriterPool when disposed
|
||||
/// </summary>
|
||||
public sealed class PooledNetworkWriter : NetworkWriter, IDisposable
|
||||
{
|
||||
private NetworkSerializer m_Serializer;
|
||||
public NetworkSerializer Serializer => m_Serializer ?? (m_Serializer = new NetworkSerializer(this));
|
||||
|
||||
private bool m_IsDisposed = false;
|
||||
|
||||
internal PooledNetworkWriter(Stream stream) : base(stream) { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a PooledNetworkWriter from the static NetworkWriterPool
|
||||
/// </summary>
|
||||
/// <returns>PooledNetworkWriter</returns>
|
||||
public static PooledNetworkWriter Get(Stream stream)
|
||||
{
|
||||
var writer = NetworkWriterPool.GetWriter(stream);
|
||||
writer.m_IsDisposed = false;
|
||||
return writer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the PooledNetworkWriter into the static NetworkWriterPool
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (!m_IsDisposed)
|
||||
{
|
||||
m_IsDisposed = true;
|
||||
NetworkWriterPool.PutBackInPool(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Writer is being disposed but thinks it is already disposed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fe89760ee0eccbe4dbf1a0e7cbb86a81
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue