Skip to content

Commit

Permalink
fix(isFirst): dont send isFirst twice, ping only members
Browse files Browse the repository at this point in the history
  • Loading branch information
kalitine committed Sep 14, 2017
1 parent de3dd4b commit 3570e8f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 35 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"node": ">=8.1.4"
},
"scripts": {
"dev": "npm run pbjs && npm run build && node server.js --host 0.0.0.0 --port 8181 | bunyan",
"dev": "npm run proto && npm run build && node server.js --host 0.0.0.0 --port 8010 | bunyan",
"lint": "eslint ./",
"build": "rollup -c",
"proto": "pbjs -t static-module --es6 -w es6 --no-verify --no-delimited --no-comments --no-convert -o src/Protobuf.js src/message.proto",
Expand Down Expand Up @@ -47,7 +47,7 @@
"commitizen": "^2.5.0",
"cz-conventional-changelog": "^2.0.0",
"eslint": "^4.6.1",
"eslint-config-conaclos": "^2.0.0",
"eslint-config-conaclos": "^2.1.0",
"eslint-config-standard": "^10.0.0",
"eslint-plugin-extra-rules": "0.8.1",
"eslint-plugin-import": "^2.3.0",
Expand Down
22 changes: 7 additions & 15 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2823,7 +2823,7 @@ class SigverError extends Error {
}
}

const PING_INTERVAL = 4000;
const PING_TIMEOUT = 5000;
const ID_MAX_VALUE = 4294967295;

class Peer extends require('rxjs/Rx').ReplaySubject {
Expand All @@ -2832,31 +2832,28 @@ class Peer extends require('rxjs/Rx').ReplaySubject {
this.key = key;
this.id = 1 + Math.ceil(Math.random() * ID_MAX_VALUE);
this.network = undefined;
this.pongTimeout = undefined;
this.pingTimer = undefined;
this.pongReceived = false;
}

clean () {
if (this.pongTimeout !== undefined) {
clearTimeout(this.pongTimeout);
}
clearTimeout(this.pingTimer);
if (this.network !== undefined) {
this.network.removeMember(this);
}
this.close(SigverError.PONG_TIMEOUT_ERROR, `Pong not received in ${PING_INTERVAL} seconds`);
this.close(SigverError.PONG_TIMEOUT_ERROR, `Signaling: pong not received in ${PING_TIMEOUT} milliseconds`);
}

startPing () {
this.pongReceived = false;
this.send({ ping: true });
this.pongTimeout = setTimeout(() => {
this.pingTimer = setTimeout(() => {
if (!this.pongReceived) {
this.pongTimeout = undefined;
this.error(new SigverError(SigverError.PONG_TIMEOUT_ERROR));
} else {
this.startPing();
}
}, PING_INTERVAL);
}, PING_TIMEOUT);
}

connect (member) {
Expand Down Expand Up @@ -3045,9 +3042,6 @@ class ServerCore {
},
() => peer.clean()
);

// Start ping
peer.startPing();
}

becomeMember (peer) {
Expand All @@ -3060,9 +3054,7 @@ class ServerCore {
const net = new Network(peer.key, peer);
networks.add(peer.key, net);
}
peer.send({ isFirst: true });
} else {
peer.send({ isFirst: false });
peer.startPing();
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/Peer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SigverError from './SigverError'

const PING_INTERVAL = 4000
const PING_TIMEOUT = 5000
const ID_MAX_VALUE = 4294967295

export default class Peer extends require('rxjs/Rx').ReplaySubject {
Expand All @@ -9,31 +9,28 @@ export default class Peer extends require('rxjs/Rx').ReplaySubject {
this.key = key
this.id = 1 + Math.ceil(Math.random() * ID_MAX_VALUE)
this.network = undefined
this.pongTimeout = undefined
this.pingTimer = undefined
this.pongReceived = false
}

clean () {
if (this.pongTimeout !== undefined) {
clearTimeout(this.pongTimeout)
}
clearTimeout(this.pingTimer)
if (this.network !== undefined) {
this.network.removeMember(this)
}
this.close(SigverError.PONG_TIMEOUT_ERROR, `Pong not received in ${PING_INTERVAL} seconds`)
this.close(SigverError.PONG_TIMEOUT_ERROR, `Signaling: pong not received in ${PING_TIMEOUT} milliseconds`)
}

startPing () {
this.pongReceived = false
this.send({ ping: true })
this.pongTimeout = setTimeout(() => {
this.pingTimer = setTimeout(() => {
if (!this.pongReceived) {
this.pongTimeout = undefined
this.error(new SigverError(SigverError.PONG_TIMEOUT_ERROR))
} else {
this.startPing()
}
}, PING_INTERVAL)
}, PING_TIMEOUT)
}

connect (member) {
Expand Down
7 changes: 1 addition & 6 deletions src/ServerCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ export default class ServerCore {
},
() => peer.clean()
)

// Start ping
peer.startPing()
}

becomeMember (peer) {
Expand All @@ -53,9 +50,7 @@ export default class ServerCore {
const net = new Network(peer.key, peer)
networks.add(peer.key, net)
}
peer.send({ isFirst: true })
} else {
peer.send({ isFirst: false })
peer.startPing()
}
}
}
Expand Down

0 comments on commit 3570e8f

Please sign in to comment.