Changed BitWriter class to take a pre allocated buffer

This commit is contained in:
Albin Corén 2018-04-11 09:43:45 +01:00
parent d5cf96d563
commit 18d965d562
3 changed files with 32 additions and 22 deletions

View File

@ -78,8 +78,8 @@
<Compile Include="GlobalSuppressions.cs" /> <Compile Include="GlobalSuppressions.cs" />
<Compile Include="MonoBehaviours\Prototyping\NetworkedAnimator.cs" /> <Compile Include="MonoBehaviours\Prototyping\NetworkedAnimator.cs" />
<Compile Include="MonoBehaviours\Prototyping\NetworkedNavMeshAgent.cs" /> <Compile Include="MonoBehaviours\Prototyping\NetworkedNavMeshAgent.cs" />
<Compile Include="NetworkingManagerComponents\Binary\BinaryCollector.cs" /> <Compile Include="NetworkingManagerComponents\Binary\BitWriter.cs" />
<Compile Include="NetworkingManagerComponents\Binary\BinaryDistributor.cs" /> <Compile Include="NetworkingManagerComponents\Binary\BitReader.cs" />
<Compile Include="NetworkingManagerComponents\Binary\BinaryHelpers.cs" /> <Compile Include="NetworkingManagerComponents\Binary\BinaryHelpers.cs" />
<Compile Include="NetworkingManagerComponents\Binary\BinarySerializer.cs" /> <Compile Include="NetworkingManagerComponents\Binary\BinarySerializer.cs" />
<Compile Include="NetworkingManagerComponents\Cryptography\CryptographyHelper.cs" /> <Compile Include="NetworkingManagerComponents\Cryptography\CryptographyHelper.cs" />

View File

@ -1,13 +1,10 @@
using MLAPI.NetworkingManagerComponents.Binary; using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
namespace Tofvesson.Common namespace MLAPI.NetworkingManagerComponents.Binary
{ {
public class BinaryDistributor public class BitReader
{ {
private delegate T Getter<T>(); private delegate T Getter<T>();
private static readonly float[] holder_f = new float[1]; private static readonly float[] holder_f = new float[1];
@ -17,7 +14,7 @@ namespace Tofvesson.Common
private readonly byte[] readFrom; private readonly byte[] readFrom;
private long bitCount = 0; private long bitCount = 0;
public BinaryDistributor(byte[] readFrom) => this.readFrom = readFrom; public BitReader(byte[] readFrom) => this.readFrom = readFrom;
public bool ReadBool() public bool ReadBool()
{ {

View File

@ -1,15 +1,12 @@
using MLAPI.NetworkingManagerComponents.Binary; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using UnityEngine;
namespace Tofvesson.Common namespace MLAPI.NetworkingManagerComponents.Binary
{ {
public sealed class BinaryCollector : IDisposable public sealed class BitWiter : IDisposable
{ {
// Collects reusable // Collects reusable
private static readonly List<WeakReference> expired = new List<WeakReference>(); private static readonly List<WeakReference> expired = new List<WeakReference>();
@ -41,7 +38,7 @@ namespace Tofvesson.Common
dec_hi, dec_hi,
dec_flags; dec_flags;
static BinaryCollector() static BitWiter()
{ {
dec_lo = typeof(decimal).GetField("lo", BindingFlags.NonPublic); dec_lo = typeof(decimal).GetField("lo", BindingFlags.NonPublic);
dec_mid = typeof(decimal).GetField("mid", BindingFlags.NonPublic); dec_mid = typeof(decimal).GetField("mid", BindingFlags.NonPublic);
@ -56,7 +53,7 @@ namespace Tofvesson.Common
/// <summary> /// <summary>
/// Allocates a new binary collector. /// Allocates a new binary collector.
/// </summary> /// </summary>
public BinaryCollector(int bufferSize) public BitWiter(int bufferSize)
{ {
this.bufferSize = bufferSize; this.bufferSize = bufferSize;
for (int i = expired.Count - 1; i >= 0; --i) for (int i = expired.Count - 1; i >= 0; --i)
@ -106,17 +103,33 @@ namespace Tofvesson.Common
public void WriteLongArray(long[] l) => Push(l); public void WriteLongArray(long[] l) => Push(l);
public void WriteString(string s) => Push(s); public void WriteString(string s) => Push(s);
public byte[] ToArray() public long Finalize(ref byte[] buffer)
{ {
if(buffer == null)
{
Debug.LogWarning("MLAPI: no buffer provided");
return 0;
}
long bitCount = 0; long bitCount = 0;
for (int i = 0; i < collectCount; ++i) bitCount += GetBitCount(collect[i]); for (int i = 0; i < collectCount; ++i) bitCount += GetBitCount(collect[i]);
byte[] alloc = new byte[(bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1)]; if(buffer.Length < ((bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1)))
{
Debug.LogWarning("MLAPI: The buffer size is not large enough");
return 0;
}
long bitOffset = 0; long bitOffset = 0;
foreach (var item in collect) foreach (var item in collect)
Serialize(item, alloc, ref bitOffset); Serialize(item, buffer, ref bitOffset);
return alloc; return (bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1));
}
public long GetFinalizeSize()
{
long bitCount = 0;
for (int i = 0; i < collectCount; ++i) bitCount += GetBitCount(collect[i]);
return ((bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1));
} }
private static void Serialize<T>(T t, byte[] writeTo, ref long bitOffset) private static void Serialize<T>(T t, byte[] writeTo, ref long bitOffset)