Repository navigation
Expand file tree
/
Copy pathSendBuffer.cs
More file actions
116 lines (102 loc) · 2.03 KB
/
Copy pathSendBuffer.cs
File metadata and controls
116 lines (102 loc) · 2.03 KB
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
// Copyright 2017 Tim Oster. All rights reserved.
// Use of this source code is governed by the MIT license.
// More information can be found in the LICENSE file.
using System;
namespace rmnp
{
class SendBuffer
{
internal enum Operation
{
DELETE,
CANCEL,
CONTINUE
}
internal class SendPacket
{
public Packet packet;
public long sendTime;
public bool noRTT;
}
internal class SendBufferElement
{
public SendBufferElement next;
public SendBufferElement prev;
public SendPacket data;
}
private SendBufferElement head;
private SendBufferElement tail;
private readonly object mutex = new object();
internal void Reset()
{
lock (this.mutex)
{
this.head = null;
this.tail = null;
}
}
internal void Add(Packet packet, bool noRTT)
{
lock (this.mutex)
{
SendBufferElement e = new SendBufferElement();
e.data = new SendPacket();
e.data.packet = packet;
e.data.sendTime = Util.CurrentTime();
e.data.noRTT = noRTT;
if (this.head == null)
{
this.head = this.tail = e;
}
else
{
e.prev = this.tail;
this.tail.next = e;
this.tail = e;
}
}
}
private void Remove(SendBufferElement e)
{
if (e.prev == null) this.head = e.next;
else e.prev.next = e.next;
if (e.next == null) this.tail = e.prev;
else e.next.prev = e.prev;
}
internal SendPacket Retrieve(ushort sequence)
{
lock (this.mutex)
{
for (SendBufferElement e = this.head; e != null; e = e.next)
{
if (e.data.packet.Sequence == sequence)
{
this.Remove(e);
return e.data;
}
}
return null;
}
}
internal void Iterate(Func<int, SendPacket, Operation> iterator)
{
lock (this.mutex)
{
int i = 0;
for (SendBufferElement e = this.head; e != null; e = e.next)
{
switch (iterator(i++, e.data))
{
case Operation.DELETE:
this.Remove(e);
break;
case Operation.CANCEL:
return;
case Operation.CONTINUE:
break;
}
}
}
}
}
}