Client LuaCsForBarotrauma
VoipQueue.cs
1 using System;
2 
3 namespace Barotrauma.Networking
4 {
5  class VoipQueue : IDisposable
6  {
7  public const int BUFFER_COUNT = 8;
8  protected int[] bufferLengths;
9  protected byte[][] buffers;
10  protected int newestBufferInd;
11  protected bool firstRead;
12 
14  {
15  get
16  {
17  int enqueuedTotalLength = 0;
18  for (int i = 0; i < BUFFER_COUNT; i++)
19  {
20  enqueuedTotalLength += bufferLengths[i];
21  }
22  return enqueuedTotalLength;
23  }
24  }
25 
26  public byte[] BufferToQueue
27  {
28  get;
29  protected set;
30  }
31 
32  public virtual byte QueueID
33  {
34  get;
35  protected set;
36  }
37 
38  public UInt16 LatestBufferID
39  {
40  get;
41  protected set;
42  }
43 
44  public bool CanSend
45  {
46  get;
47  protected set;
48  }
49 
50  public bool CanReceive
51  {
52  get;
53  protected set;
54  }
55 
56  public bool ForceLocal
57  {
58  get;
59  set;
60  }
61 
62  public DateTime LastReadTime
63  {
64  get;
65  private set;
66  }
67 
68  public VoipQueue(byte id, bool canSend, bool canReceive)
69  {
70  BufferToQueue = new byte[VoipConfig.MAX_COMPRESSED_SIZE];
72  bufferLengths = new int[BUFFER_COUNT];
73  buffers = new byte[BUFFER_COUNT][];
74  for (int i = 0; i < BUFFER_COUNT; i++)
75  {
76  buffers[i] = new byte[VoipConfig.MAX_COMPRESSED_SIZE];
77  }
78  QueueID = id;
79  CanSend = canSend;
80  CanReceive = canReceive;
82  firstRead = true;
83 
84  LastReadTime = DateTime.Now;
85  }
86 
87  public void EnqueueBuffer(int length)
88  {
89  if (length > byte.MaxValue) { return; }
90 
92 
93  int enqueuedTotalLength = EnqueuedTotalLength;
94 
97 
98  if ((enqueuedTotalLength + length) > 0) { LatestBufferID++; }
99  }
100 
101  public void RetrieveBuffer(int id, out int outSize, out byte[] outBuf)
102  {
103  lock (buffers)
104  {
105  if (id >= LatestBufferID - (BUFFER_COUNT - 1) && id <= LatestBufferID)
106  {
107  int index = newestBufferInd - (LatestBufferID - id);
108  if (index < 0) { index += BUFFER_COUNT; }
109  outSize = bufferLengths[index];
110  outBuf = buffers[index];
111  return;
112  }
113  }
114  outSize = -1;
115  outBuf = null;
116  }
117 
118  public virtual void Write(IWriteMessage msg)
119  {
120  if (!CanSend) { throw new Exception("Called Write on a VoipQueue not set up for sending"); }
121 
122  msg.WriteUInt16((UInt16)LatestBufferID);
124  lock (buffers)
125  {
126  for (int i = 0; i < BUFFER_COUNT; i++)
127  {
128  int index = (newestBufferInd + i + 1) % BUFFER_COUNT;
129 
130  msg.WriteByte((byte)bufferLengths[index]);
131  msg.WriteBytes(buffers[index], 0, bufferLengths[index]);
132  }
133  }
134  }
135 
136  public virtual bool Read(IReadMessage msg, bool discardData = false)
137  {
138  if (!CanReceive) { throw new Exception("Called Read on a VoipQueue not set up for receiving"); }
139 
140  UInt16 incLatestBufferID = msg.ReadUInt16();
141  if ((firstRead || NetIdUtils.IdMoreRecent(incLatestBufferID, LatestBufferID)) && !discardData)
142  {
143  ForceLocal = msg.ReadBoolean(); msg.ReadPadBits();
144 
145  firstRead = false;
146  lock (buffers)
147  {
148  for (int i = 0; i < BUFFER_COUNT; i++)
149  {
150  bufferLengths[i] = msg.ReadByte();
151  buffers[i] = msg.ReadBytes(bufferLengths[i]);
152  }
153  }
155  LatestBufferID = incLatestBufferID;
156  LastReadTime = DateTime.Now;
157  return true;
158  }
159  else
160  {
161  msg.ReadBoolean(); msg.ReadPadBits();
162  for (int i = 0; i < BUFFER_COUNT; i++)
163  {
164  byte len = msg.ReadByte();
165  msg.BitPosition += len * 8;
166  }
167  return false;
168  }
169  }
170 
171  public virtual void Dispose() { }
172  }
173 }
virtual bool Read(IReadMessage msg, bool discardData=false)
Definition: VoipQueue.cs:136
void RetrieveBuffer(int id, out int outSize, out byte[] outBuf)
Definition: VoipQueue.cs:101
void EnqueueBuffer(int length)
Definition: VoipQueue.cs:87
virtual void Write(IWriteMessage msg)
Definition: VoipQueue.cs:118
VoipQueue(byte id, bool canSend, bool canReceive)
Definition: VoipQueue.cs:68
byte[] ReadBytes(int numberOfBytes)
void WriteBytes(byte[] val, int startIndex, int length)