-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperPacket.cs
230 lines (193 loc) · 6.76 KB
/
SuperPacket.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Kaminari
{
public enum SuperPacketFlags
{
None = 0x00,
Handshake = 0x01,
Ping = 0x02,
Ack = 0x80,
All = 0xFF
}
public enum SuperPacketInternalFlags
{
None = 0x00,
WaitFirst = 0x01,
All = 0xFF
}
public class SuperPacket<PQ> where PQ : IProtocolQueues
{
public const ushort MAX_SIZE = 512;
public ushort _id;
private byte _flags;
private byte _internalFlags;
private ushort _ackBase;
private uint _pendingAcks;
private bool _mustAck;
private byte _counter;
private Dictionary<ushort, byte> _clearFlagsOnAck;
private Buffer _buffer;
private PQ _queues;
private bool _last_left_data;
public Buffer getBuffer()
{
return _buffer;
}
public ushort getID()
{
return _id;
}
public SuperPacket(PQ queues)
{
_ackBase = 0;
_pendingAcks = 0;
_mustAck = false;
_clearFlagsOnAck = new Dictionary<ushort, byte>();
_buffer = new Buffer(MAX_SIZE);
_queues = queues;
reset();
}
public void reset()
{
_id = 0;
_flags = 0;
_internalFlags = 0;
_ackBase = 0;
_pendingAcks = 0;
_mustAck = false;
_buffer.reset();
_queues.reset();
}
public void Ack(ushort id)
{
// Ack inside queues
getQueues().ack(id);
// Protocol level acks
if (_clearFlagsOnAck.ContainsKey(id))
{
_flags = (byte)(_flags & (byte)(~_clearFlagsOnAck[id]));
_clearFlagsOnAck.Remove(id);
}
}
public void SetFlag(SuperPacketFlags flag)
{
_flags = (byte)(_flags | (byte)flag);
if (_clearFlagsOnAck.ContainsKey(_id))
{
_clearFlagsOnAck[_id] = (byte)(_clearFlagsOnAck[_id] | (byte)flag);
}
else
{
_clearFlagsOnAck.Add(_id, (byte)flag);
}
}
public void SetInternalFlag(SuperPacketInternalFlags flag)
{
_internalFlags = (byte)(_internalFlags | (byte)flag);
}
public void ClearInternalFlag(SuperPacketInternalFlags flag)
{
_internalFlags = (byte)(_internalFlags & (~(byte)flag));
}
public bool HasFlag(SuperPacketFlags flag)
{
return (_flags & (byte)flag) != 0x00;
}
public bool HasInternalFlag(SuperPacketInternalFlags flag)
{
return (_internalFlags & (byte)flag) != 0x00;
}
public PQ getQueues()
{
return _queues;
}
public void scheduleAck(ushort blockId)
{
if (Overflow.ge(blockId, _ackBase))
{
ushort ackDiff = Overflow.sub(blockId, _ackBase);
_pendingAcks = (_pendingAcks << ackDiff) | 1; // | 1 because we are acking the base
_ackBase = blockId;
}
else
{
ushort ackDiff = Overflow.sub(_ackBase, blockId);
_pendingAcks = _pendingAcks | (ushort)(1 << ackDiff);
}
_mustAck = true;
}
public void prepare()
{
_counter = 0;
}
public bool finish(ushort tickId, bool isFirst)
{
_buffer.reset();
// First two bytes are tick id, next two id, finally 1 byte for flags
_buffer.write(tickId);
_buffer.write(_id);
_buffer.write(_flags);
// Reset if there is something we must ack
bool hasAcks = _mustAck;
_mustAck = false;
// Write acks and move for next id
// Moving acks bitset only happens if no doing handshake (ie. if incrementing id)
_buffer.write(_ackBase);
_buffer.write(_pendingAcks);
// -1 is to account for the number of blocks
ushort remaining = (ushort)(MAX_SIZE - _buffer.getPosition() - 1 - 1);
// During handshake/resync do not include any packets
bool hasData = false;
_last_left_data = false;
if (!HasFlag(SuperPacketFlags.Handshake))
{
// Organize packets that must be resent until ack'ed
SortedDictionary<uint, List<Packet>> by_block = new SortedDictionary<uint, List<Packet>>();
_queues.process(tickId, _id, ref remaining, ref _last_left_data, by_block);
// Write number of blocks
_buffer.write((byte)by_block.Count);
// Has there been any overflow? That can be detected by, for example
// checking max-min>thr
hasData = by_block.Count != 0;
if (hasData)
{
uint max = by_block.Keys.ToList()[by_block.Count - 1]; // TODO(gpascualg): Linq .Last()?
ushort threshold = ushort.MaxValue / 2;
foreach (var key in by_block.Keys.ToList())
{
if (max - key < threshold)
{
break;
}
// Overflows are masked higher, so that they get pushed to the end
// of the map
uint masked = (Convert.ToUInt32(1) << 16) | key;
by_block.Add(masked, by_block[key]);
by_block.Remove(key);
}
// Write in packets
foreach (var entry in by_block)
{
_buffer.write((ushort)Convert.ToUInt16(entry.Key & 0xffff));
_buffer.write((byte)entry.Value.Count);
UnityEngine.Assertions.Assert.AreNotEqual(entry.Value.Count, 0);
foreach (var packet in entry.Value)
{
if (entry.Key == _id)
{
packet.finish(_counter++);
}
_buffer.write(packet);
}
}
}
}
// Increment _id for next iter and acks
_id = Overflow.inc(_id);
return hasAcks || hasData || (isFirst && _flags != 0);
}
}
}