Skip to content

Commit

Permalink
add drop throttled toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
jagt committed Aug 2, 2015
1 parent 6a8ae99 commit 40c42d6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void init(int argc, char* argv[]) {
);

IupSetAttribute(dialog, "TITLE", "clumsy " CLUMSY_VERSION);
IupSetAttribute(dialog, "SIZE", "400x"); // add padding manually to width
IupSetAttribute(dialog, "SIZE", "480x"); // add padding manually to width
IupSetAttribute(dialog, "RESIZE", "NO");
IupSetCallback(dialog, "SHOW_CB", (Icallback)uiOnDialogShow);

Expand Down
26 changes: 23 additions & 3 deletions src/throttle.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
// threshold for how many packet to throttle at most
#define KEEP_AT_MOST 1000

static Ihandle *inboundCheckbox, *outboundCheckbox, *chanceInput, *frameInput;
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;
throttleFrame = TIME_DEFAULT,
dropThrottled = 0;

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

static Ihandle *throttleSetupUI() {
Ihandle *throttleControlsBox = IupHbox(
dropThrottledCheckbox = IupToggle("Drop Throttled", NULL),
IupLabel("Timeframe(ms):"),
frameInput = IupText(NULL),
inboundCheckbox = IupToggle("Inbound", NULL),
Expand All @@ -47,6 +49,9 @@ static Ihandle *throttleSetupUI() {
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));
Expand Down Expand Up @@ -91,6 +96,16 @@ static void clearBufPackets(PacketNode *tail) {
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);
Expand Down Expand Up @@ -127,7 +142,12 @@ static short throttleProcess(PacketNode *head, PacketNode *tail) {

// send all when throttled enough, including in current step
if (bufSize >= KEEP_AT_MOST || (currentTick - throttleStartTick > (unsigned int)throttleFrame)) {
clearBufPackets(tail);
// drop throttled if dropThrottled is toggled
if (dropThrottled) {
dropBufPackets();
} else {
clearBufPackets(tail);
}
}
}
}
Expand Down

0 comments on commit 40c42d6

Please sign in to comment.