forked from jagt/clumsy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
throttle.c
168 lines (149 loc) · 5.73 KB
/
throttle.c
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
// throttling packets
#include "iup.h"
#include "common.h"
#define NAME "throttle"
#define TIME_MIN "0"
#define TIME_MAX "1000"
#define TIME_DEFAULT 30
// threshold for how many packet to throttle at most
#define KEEP_AT_MOST 1000
static Ihandle *inboundCheckbox, *outboundCheckbox, *chanceInput, *frameInput, *dropThrottledCheckbox;
static volatile short throttleEnabled = 0,
throttleInbound = 1, throttleOutbound = 1,
chance = 1000, // [0-10000]
// time frame in ms, when a throttle start the packets within the time
// will be queued and sent altogether when time is over
throttleFrame = TIME_DEFAULT,
dropThrottled = 0;
static PacketNode throttleHeadNode = {0}, throttleTailNode = {0};
static PacketNode *bufHead = &throttleHeadNode, *bufTail = &throttleTailNode;
static int bufSize = 0;
static DWORD throttleStartTick = 0;
static INLINE_FUNCTION short isBufEmpty() {
short ret = bufHead->next == bufTail;
if (ret) assert(bufSize == 0);
return ret;
}
static Ihandle *throttleSetupUI() {
Ihandle *throttleControlsBox = IupHbox(
dropThrottledCheckbox = IupToggle("Drop Throttled", NULL),
IupLabel("Timeframe(ms):"),
frameInput = IupText(NULL),
inboundCheckbox = IupToggle("Inbound", NULL),
outboundCheckbox = IupToggle("Outbound", NULL),
IupLabel("Chance(%):"),
chanceInput = IupText(NULL),
NULL
);
IupSetAttribute(chanceInput, "VISIBLECOLUMNS", "4");
IupSetAttribute(chanceInput, "VALUE", "10.0");
IupSetCallback(chanceInput, "VALUECHANGED_CB", uiSyncChance);
IupSetAttribute(chanceInput, SYNCED_VALUE, (char*)&chance);
IupSetCallback(inboundCheckbox, "ACTION", (Icallback)uiSyncToggle);
IupSetAttribute(inboundCheckbox, SYNCED_VALUE, (char*)&throttleInbound);
IupSetCallback(outboundCheckbox, "ACTION", (Icallback)uiSyncToggle);
IupSetAttribute(outboundCheckbox, SYNCED_VALUE, (char*)&throttleOutbound);
IupSetCallback(dropThrottledCheckbox, "ACTION", (Icallback)uiSyncToggle);
IupSetAttribute(dropThrottledCheckbox, SYNCED_VALUE, (char*)&dropThrottled);
// sync throttle packet number
IupSetAttribute(frameInput, "VISIBLECOLUMNS", "3");
IupSetAttribute(frameInput, "VALUE", STR(TIME_DEFAULT));
IupSetCallback(frameInput, "VALUECHANGED_CB", (Icallback)uiSyncInteger);
IupSetAttribute(frameInput, SYNCED_VALUE, (char*)&throttleFrame);
IupSetAttribute(frameInput, INTEGER_MAX, TIME_MAX);
IupSetAttribute(frameInput, INTEGER_MIN, TIME_MIN);
// enable by default to avoid confusing
IupSetAttribute(inboundCheckbox, "VALUE", "ON");
IupSetAttribute(outboundCheckbox, "VALUE", "ON");
if (parameterized) {
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
setFromParameter(chanceInput, "VALUE", NAME"-chance");
setFromParameter(frameInput, "VALUE", NAME"-frame");
}
return throttleControlsBox;
}
static void throttleStartUp() {
if (bufHead->next == NULL && bufTail->next == NULL) {
bufHead->next = bufTail;
bufTail->prev = bufHead;
bufSize = 0;
} else {
assert(isBufEmpty());
}
throttleStartTick = 0;
startTimePeriod();
}
static void clearBufPackets(PacketNode *tail) {
PacketNode *oldLast = tail->prev;
LOG("Throttled end, send all %d packets. Buffer at max: %s", bufSize, bufSize == KEEP_AT_MOST ? "YES" : "NO");
while (!isBufEmpty()) {
insertAfter(popNode(bufTail->prev), oldLast);
--bufSize;
}
throttleStartTick = 0;
}
static void dropBufPackets() {
LOG("Throttled end, drop all %d packets. Buffer at max: %s", bufSize, bufSize == KEEP_AT_MOST ? "YES" : "NO");
while (!isBufEmpty()) {
freeNode(popNode(bufTail->prev));
--bufSize;
}
throttleStartTick = 0;
}
static void throttleCloseDown(PacketNode *head, PacketNode *tail) {
UNREFERENCED_PARAMETER(tail);
UNREFERENCED_PARAMETER(head);
clearBufPackets(tail);
endTimePeriod();
}
static short throttleProcess(PacketNode *head, PacketNode *tail) {
short throttled = FALSE;
UNREFERENCED_PARAMETER(head);
if (!throttleStartTick) {
if (!isListEmpty() && calcChance(chance)) {
LOG("Start new throttling w/ chance %.1f, time frame: %d", chance/10.0, throttleFrame);
throttleStartTick = timeGetTime();
throttled = TRUE;
goto THROTTLE_START; // need this goto since maybe we'll start and stop at this single call
}
} else {
THROTTLE_START:
// start a block for declaring local variables
{
// already throttling, keep filling up
PacketNode *pac = tail->prev;
DWORD currentTick = timeGetTime();
while (bufSize < KEEP_AT_MOST && pac != head) {
if (checkDirection(pac->addr.Direction, throttleInbound, throttleOutbound)) {
insertAfter(popNode(pac), bufHead);
++bufSize;
pac = tail->prev;
} else {
pac = pac->prev;
}
}
// send all when throttled enough, including in current step
if (bufSize >= KEEP_AT_MOST || (currentTick - throttleStartTick > (unsigned int)throttleFrame)) {
// drop throttled if dropThrottled is toggled
if (dropThrottled) {
dropBufPackets();
} else {
clearBufPackets(tail);
}
}
}
}
return throttled;
}
Module throttleModule = {
"Throttle",
NAME,
(short*)&throttleEnabled,
throttleSetupUI,
throttleStartUp,
throttleCloseDown,
throttleProcess,
// runtime fields
0, 0, NULL
};