Skip to content

Commit 18d965d

Browse files
committed
Changed BitWriter class to take a pre allocated buffer
1 parent d5cf96d commit 18d965d

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

MLAPI/MLAPI.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
<Compile Include="GlobalSuppressions.cs" />
7979
<Compile Include="MonoBehaviours\Prototyping\NetworkedAnimator.cs" />
8080
<Compile Include="MonoBehaviours\Prototyping\NetworkedNavMeshAgent.cs" />
81-
<Compile Include="NetworkingManagerComponents\Binary\BinaryCollector.cs" />
82-
<Compile Include="NetworkingManagerComponents\Binary\BinaryDistributor.cs" />
81+
<Compile Include="NetworkingManagerComponents\Binary\BitWriter.cs" />
82+
<Compile Include="NetworkingManagerComponents\Binary\BitReader.cs" />
8383
<Compile Include="NetworkingManagerComponents\Binary\BinaryHelpers.cs" />
8484
<Compile Include="NetworkingManagerComponents\Binary\BinarySerializer.cs" />
8585
<Compile Include="NetworkingManagerComponents\Cryptography\CryptographyHelper.cs" />

MLAPI/NetworkingManagerComponents/Binary/BinaryDistributor.cs renamed to MLAPI/NetworkingManagerComponents/Binary/BitReader.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
using MLAPI.NetworkingManagerComponents.Binary;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
1+
using System;
52
using System.Runtime.InteropServices;
63
using System.Text;
74

8-
namespace Tofvesson.Common
5+
namespace MLAPI.NetworkingManagerComponents.Binary
96
{
10-
public class BinaryDistributor
7+
public class BitReader
118
{
129
private delegate T Getter<T>();
1310
private static readonly float[] holder_f = new float[1];
@@ -17,7 +14,7 @@ public class BinaryDistributor
1714

1815
private readonly byte[] readFrom;
1916
private long bitCount = 0;
20-
public BinaryDistributor(byte[] readFrom) => this.readFrom = readFrom;
17+
public BitReader(byte[] readFrom) => this.readFrom = readFrom;
2118

2219
public bool ReadBool()
2320
{

MLAPI/NetworkingManagerComponents/Binary/BinaryCollector.cs renamed to MLAPI/NetworkingManagerComponents/Binary/BitWriter.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
using MLAPI.NetworkingManagerComponents.Binary;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
4-
using System.IO;
5-
using System.Linq;
63
using System.Reflection;
7-
using System.Runtime.InteropServices;
84
using System.Text;
5+
using UnityEngine;
96

10-
namespace Tofvesson.Common
7+
namespace MLAPI.NetworkingManagerComponents.Binary
118
{
12-
public sealed class BinaryCollector : IDisposable
9+
public sealed class BitWiter : IDisposable
1310
{
1411
// Collects reusable
1512
private static readonly List<WeakReference> expired = new List<WeakReference>();
@@ -41,7 +38,7 @@ private static readonly FieldInfo
4138
dec_hi,
4239
dec_flags;
4340

44-
static BinaryCollector()
41+
static BitWiter()
4542
{
4643
dec_lo = typeof(decimal).GetField("lo", BindingFlags.NonPublic);
4744
dec_mid = typeof(decimal).GetField("mid", BindingFlags.NonPublic);
@@ -56,7 +53,7 @@ static BinaryCollector()
5653
/// <summary>
5754
/// Allocates a new binary collector.
5855
/// </summary>
59-
public BinaryCollector(int bufferSize)
56+
public BitWiter(int bufferSize)
6057
{
6158
this.bufferSize = bufferSize;
6259
for (int i = expired.Count - 1; i >= 0; --i)
@@ -106,17 +103,33 @@ private void Push<T>(T b)
106103
public void WriteLongArray(long[] l) => Push(l);
107104
public void WriteString(string s) => Push(s);
108105

109-
public byte[] ToArray()
106+
public long Finalize(ref byte[] buffer)
110107
{
108+
if(buffer == null)
109+
{
110+
Debug.LogWarning("MLAPI: no buffer provided");
111+
return 0;
112+
}
111113
long bitCount = 0;
112114
for (int i = 0; i < collectCount; ++i) bitCount += GetBitCount(collect[i]);
113115

114-
byte[] alloc = new byte[(bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1)];
116+
if(buffer.Length < ((bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1)))
117+
{
118+
Debug.LogWarning("MLAPI: The buffer size is not large enough");
119+
return 0;
120+
}
115121
long bitOffset = 0;
116122
foreach (var item in collect)
117-
Serialize(item, alloc, ref bitOffset);
123+
Serialize(item, buffer, ref bitOffset);
118124

119-
return alloc;
125+
return (bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1));
126+
}
127+
128+
public long GetFinalizeSize()
129+
{
130+
long bitCount = 0;
131+
for (int i = 0; i < collectCount; ++i) bitCount += GetBitCount(collect[i]);
132+
return ((bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1));
120133
}
121134

122135
private static void Serialize<T>(T t, byte[] writeTo, ref long bitOffset)

0 commit comments

Comments
 (0)