-
Notifications
You must be signed in to change notification settings - Fork 2
/
CircularBuffer.cs
125 lines (107 loc) · 3.73 KB
/
CircularBuffer.cs
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
namespace HoloToolkit.Unity
{
/// <summary>
/// Helper class for transmitting data over network.
/// </summary>
public class CircularBuffer
{
public CircularBuffer(int size, bool allowOverwrite = false, int padding = 4)
{
data = new byte[size];
readWritePadding = padding;
this.allowOverwrite = allowOverwrite;
}
public int TotalCapacity
{
get
{
return data.Length - readWritePadding;
}
}
public int UsedCapacity
{
get
{
if (writeOffset >= readOffset)
{
return writeOffset - readOffset;
}
int firstChunk = data.Length - readOffset;
int secondChunk = writeOffset;
return firstChunk + secondChunk;
}
}
public void Reset()
{
readOffset = 0;
writeOffset = 0;
}
public int Write(Array src, int srcReadPosBytes, int byteCount)
{
int maxWritePos;
bool wrappedAround = writeOffset < readOffset;
if (!wrappedAround)
{
maxWritePos = (readOffset != 0 || allowOverwrite) ? data.Length : data.Length - readWritePadding;
}
else
{
maxWritePos = allowOverwrite ? data.Length : readOffset - readWritePadding;
}
int chunkSize = Math.Min(byteCount, maxWritePos - writeOffset);
int writeEnd = writeOffset + chunkSize;
bool needToMoveReadOffset = wrappedAround ? writeEnd >= readOffset : (writeEnd == data.Length && readOffset == 0);
if (needToMoveReadOffset)
{
if (!allowOverwrite)
{
throw new Exception("Circular buffer logic error. Overwriting data.");
}
readOffset = (writeEnd + readWritePadding) % data.Length;
}
Buffer.BlockCopy(src, srcReadPosBytes, data, writeOffset, chunkSize);
writeOffset = (writeOffset + chunkSize) % data.Length;
int bytesWritten = chunkSize;
int remaining = byteCount - bytesWritten;
if (bytesWritten > 0 && remaining > 0)
{
bytesWritten += Write(src, srcReadPosBytes + chunkSize, remaining);
}
return bytesWritten;
}
public int Read(Array dst, int dstWritePosBytes, int byteCount)
{
if (readOffset == writeOffset)
{
return 0;
}
int maxReadPos;
if (readOffset > writeOffset)
{
maxReadPos = data.Length;
}
else
{
maxReadPos = writeOffset;
}
int chunkSize = Math.Min(byteCount, maxReadPos - readOffset);
Buffer.BlockCopy(data, readOffset, dst, dstWritePosBytes, chunkSize);
readOffset = (readOffset + chunkSize) % data.Length;
int bytesRead = chunkSize;
int remaining = byteCount - bytesRead;
if (bytesRead > 0 && remaining > 0)
{
bytesRead += Read(dst, dstWritePosBytes + bytesRead, remaining);
}
return bytesRead;
}
private byte[] data;
private int writeOffset;
private int readOffset;
private readonly int readWritePadding;
private readonly bool allowOverwrite;
}
}