Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configurable variation in lag #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ typedef struct _NODE {
char *packet;
UINT packetLen;
WINDIVERT_ADDRESS addr;
DWORD timestamp; // ! timestamp isn't filled when creating node since it's only needed for lag
DWORD sendTimestamp; // ! not filled when creating node since it's only needed for lag
struct _NODE *prev, *next;
} PacketNode;

Expand Down Expand Up @@ -121,7 +121,7 @@ typedef struct {
* Flags used during program excution. Need to be re initialized on each run
*/
short lastEnabled; // if it is enabled on last run
short processTriggered; // whether this module has been triggered in last step
short processTriggered; // whether this module has been triggered in last step
Ihandle *iconHandle; // store the icon to be updated
} Module;

Expand All @@ -135,7 +135,7 @@ extern Module resetModule;
extern Module capModule;
extern Module* modules[MODULE_CNT]; // all modules in a list

// status for sending packets,
// status for sending packets,
#define SEND_STATUS_NONE 0
#define SEND_STATUS_SEND 1
#define SEND_STATUS_FAIL -1
Expand Down
26 changes: 22 additions & 4 deletions src/lag.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
#define NAME "lag"
#define LAG_MIN "0"
#define LAG_MAX "3000"
#define VARIATION_MIN "0"
#define VARIATION_MAX "3000"
#define KEEP_AT_MOST 2000
// send FLUSH_WHEN_FULL packets when buffer is full
#define FLUSH_WHEN_FULL 800
#define LAG_DEFAULT 50
#define VARIATION_DEFAULT 0

// don't need a chance
static Ihandle *inboundCheckbox, *outboundCheckbox, *timeInput;
static Ihandle *inboundCheckbox, *outboundCheckbox, *timeInput, *variationInput;

static volatile short lagEnabled = 0,
lagInbound = 1,
lagOutbound = 1,
lagTime = LAG_DEFAULT; // default for 50ms
lagTime = LAG_DEFAULT, // default for 50ms
lagVariation = VARIATION_DEFAULT; // default for no variation

static PacketNode lagHeadNode = {0}, lagTailNode = {0};
static PacketNode *bufHead = &lagHeadNode, *bufTail = &lagTailNode;
Expand All @@ -33,6 +37,8 @@ static Ihandle *lagSetupUI() {
outboundCheckbox = IupToggle("Outbound", NULL),
IupLabel("Delay(ms):"),
timeInput = IupText(NULL),
IupLabel("Variation(ms):"),
variationInput = IupText(NULL),
NULL
);

Expand All @@ -42,8 +48,17 @@ static Ihandle *lagSetupUI() {
IupSetAttribute(timeInput, SYNCED_VALUE, (char*)&lagTime);
IupSetAttribute(timeInput, INTEGER_MAX, LAG_MAX);
IupSetAttribute(timeInput, INTEGER_MIN, LAG_MIN);

IupSetAttribute(variationInput, "VISIBLECOLUMNS", "4");
IupSetAttribute(variationInput, "VALUE", STR(VARIATION_DEFAULT));
IupSetCallback(variationInput, "VALUECHANGED_CB", uiSyncInteger);
IupSetAttribute(variationInput, SYNCED_VALUE, (char*)&lagVariation);
IupSetAttribute(variationInput, INTEGER_MAX, VARIATION_MAX);
IupSetAttribute(variationInput, INTEGER_MIN, VARIATION_MIN);

IupSetCallback(inboundCheckbox, "ACTION", (Icallback)uiSyncToggle);
IupSetAttribute(inboundCheckbox, SYNCED_VALUE, (char*)&lagInbound);

IupSetCallback(outboundCheckbox, "ACTION", (Icallback)uiSyncToggle);
IupSetAttribute(outboundCheckbox, SYNCED_VALUE, (char*)&lagOutbound);

Expand All @@ -55,6 +70,7 @@ static Ihandle *lagSetupUI() {
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
setFromParameter(timeInput, "VALUE", NAME"-time");
setFromParameter(variationInput, "VALUE", NAME"-variation");
}

return lagControlsBox;
Expand Down Expand Up @@ -89,7 +105,9 @@ static short lagProcess(PacketNode *head, PacketNode *tail) {
// pick up all packets and fill in the current time
while (bufSize < KEEP_AT_MOST && pac != head) {
if (checkDirection(pac->addr.Direction, lagInbound, lagOutbound)) {
insertAfter(popNode(pac), bufHead)->timestamp = timeGetTime();
// lag varies from lagTime - lagVariation <= lag <= lagTime + lagVariation
short lag = lagTime + (rand() % (1 + 2 * lagVariation)) - lagVariation;
insertAfter(popNode(pac), bufHead)->sendTimestamp = currentTime + lag;
++bufSize;
pac = tail->prev;
} else {
Expand All @@ -100,7 +118,7 @@ static short lagProcess(PacketNode *head, PacketNode *tail) {
// try sending overdue packets from buffer tail
while (!isBufEmpty()) {
PacketNode *pac = bufTail->prev;
if (currentTime > pac->timestamp + lagTime) {
if (currentTime >= pac->sendTimestamp) {
insertAfter(popNode(bufTail->prev), head); // sending queue is already empty by now
--bufSize;
LOG("Send lagged packets.");
Expand Down