Skip to content

Commit 40c42d6

Browse files
committed
add drop throttled toggle
1 parent 6a8ae99 commit 40c42d6

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ void init(int argc, char* argv[]) {
232232
);
233233

234234
IupSetAttribute(dialog, "TITLE", "clumsy " CLUMSY_VERSION);
235-
IupSetAttribute(dialog, "SIZE", "400x"); // add padding manually to width
235+
IupSetAttribute(dialog, "SIZE", "480x"); // add padding manually to width
236236
IupSetAttribute(dialog, "RESIZE", "NO");
237237
IupSetCallback(dialog, "SHOW_CB", (Icallback)uiOnDialogShow);
238238

src/throttle.c

+23-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
// threshold for how many packet to throttle at most
99
#define KEEP_AT_MOST 1000
1010

11-
static Ihandle *inboundCheckbox, *outboundCheckbox, *chanceInput, *frameInput;
11+
static Ihandle *inboundCheckbox, *outboundCheckbox, *chanceInput, *frameInput, *dropThrottledCheckbox;
1212

1313
static volatile short throttleEnabled = 0,
1414
throttleInbound = 1, throttleOutbound = 1,
1515
chance = 1000, // [0-10000]
1616
// time frame in ms, when a throttle start the packets within the time
1717
// will be queued and sent altogether when time is over
18-
throttleFrame = TIME_DEFAULT;
18+
throttleFrame = TIME_DEFAULT,
19+
dropThrottled = 0;
1920

2021
static PacketNode throttleHeadNode = {0}, throttleTailNode = {0};
2122
static PacketNode *bufHead = &throttleHeadNode, *bufTail = &throttleTailNode;
@@ -30,6 +31,7 @@ static INLINE_FUNCTION short isBufEmpty() {
3031

3132
static Ihandle *throttleSetupUI() {
3233
Ihandle *throttleControlsBox = IupHbox(
34+
dropThrottledCheckbox = IupToggle("Drop Throttled", NULL),
3335
IupLabel("Timeframe(ms):"),
3436
frameInput = IupText(NULL),
3537
inboundCheckbox = IupToggle("Inbound", NULL),
@@ -47,6 +49,9 @@ static Ihandle *throttleSetupUI() {
4749
IupSetAttribute(inboundCheckbox, SYNCED_VALUE, (char*)&throttleInbound);
4850
IupSetCallback(outboundCheckbox, "ACTION", (Icallback)uiSyncToggle);
4951
IupSetAttribute(outboundCheckbox, SYNCED_VALUE, (char*)&throttleOutbound);
52+
IupSetCallback(dropThrottledCheckbox, "ACTION", (Icallback)uiSyncToggle);
53+
IupSetAttribute(dropThrottledCheckbox, SYNCED_VALUE, (char*)&dropThrottled);
54+
5055
// sync throttle packet number
5156
IupSetAttribute(frameInput, "VISIBLECOLUMNS", "3");
5257
IupSetAttribute(frameInput, "VALUE", STR(TIME_DEFAULT));
@@ -91,6 +96,16 @@ static void clearBufPackets(PacketNode *tail) {
9196
throttleStartTick = 0;
9297
}
9398

99+
static void dropBufPackets() {
100+
LOG("Throttled end, drop all %d packets. Buffer at max: %s", bufSize, bufSize == KEEP_AT_MOST ? "YES" : "NO");
101+
while (!isBufEmpty()) {
102+
freeNode(popNode(bufTail->prev));
103+
--bufSize;
104+
}
105+
throttleStartTick = 0;
106+
}
107+
108+
94109
static void throttleCloseDown(PacketNode *head, PacketNode *tail) {
95110
UNREFERENCED_PARAMETER(tail);
96111
UNREFERENCED_PARAMETER(head);
@@ -127,7 +142,12 @@ static short throttleProcess(PacketNode *head, PacketNode *tail) {
127142

128143
// send all when throttled enough, including in current step
129144
if (bufSize >= KEEP_AT_MOST || (currentTick - throttleStartTick > (unsigned int)throttleFrame)) {
130-
clearBufPackets(tail);
145+
// drop throttled if dropThrottled is toggled
146+
if (dropThrottled) {
147+
dropBufPackets();
148+
} else {
149+
clearBufPackets(tail);
150+
}
131151
}
132152
}
133153
}

0 commit comments

Comments
 (0)