Skip to content

Commit

Permalink
Add congestion control
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgrammerIn-wonderland committed Jul 30, 2024
1 parent 2ef2914 commit 9a86639
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/browser/wisp_network.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@ let wispws;
let lastStream = 1;

const connections = {};

const congestedBuffer = [];
let congestion = 0;
let congested = true;
function sendPacket(data, type) {
if (congestion > 0) {
if (type === "DATA")
congestion--;
wispws.send(data);
} else {
congested = true;
congestedBuffer.push({data: data, type: type});
}
}

function processIncomingWispFrame(frame) {
// console.log(frame);
let view;
Expand All @@ -29,7 +43,13 @@ function processIncomingWispFrame(frame) {
case 3: // CONTINUE
view = new DataView(frame.buffer);
congestion = view.getUint32(0, true);

if (congested) {
for (const packet of congestedBuffer) {
sendPacket(packet.data, packet.type);
}
congested = false;
}

break;
case 4: // CLOSE
// Call some closer here
Expand Down Expand Up @@ -66,8 +86,6 @@ function processIncomingWispFrame(frame) {
//
//

const congestionBuffer = [];
let congested = false;
function sendWispFrame(frameObj) {

let fullPacket;
Expand Down Expand Up @@ -113,7 +131,7 @@ function sendWispFrame(frameObj) {

}
// console.log("Congestion:" + congestion);
wispws.send(fullPacket);
wispws.send(fullPacket, frameObj.type);
// if (congestion > 0) {
// if (frameObj.type === "DATA")
// congestion--;
Expand Down

0 comments on commit 9a86639

Please sign in to comment.