Commit 1f6fcc19 authored by 神楽坂玲奈's avatar 神楽坂玲奈

fix

parent dbb5e3c2
...@@ -50,14 +50,21 @@ export class Peer implements PeerMessage, PeerQuality, RouterConfig { ...@@ -50,14 +50,21 @@ export class Peer implements PeerMessage, PeerQuality, RouterConfig {
this.time = data.time; this.time = data.time;
} }
update(time: number) { update(): PeerQuality {
if (this.reliability === 0) { if (this.reliability === 0) {
return; return;
} }
// 有几个包没到 // 有几个包没到
const step = Math.floor((time - this.time + this.delay - config.interval) / config.interval); const step = Math.floor((Date.now() - this.time + this.delay - config.interval) / config.interval);
if (step > config.timeout) { if (step > config.timeout) {
this.reset(); this.reset();
} }
return {
delay: Math.round(this.delay),
jitter: 0,
reliability: this.reliability
};
} }
} }
...@@ -4,7 +4,7 @@ import config from '../config/config.json'; ...@@ -4,7 +4,7 @@ import config from '../config/config.json';
import { RouteWriter } from './RouteWriter'; import { RouteWriter } from './RouteWriter';
import { Peer } from './Peer'; import { Peer } from './Peer';
import { DownloadMessage, PeerMessage, PeerQuality, UploadMessage } from '../protocol'; import { DownloadMessage, PeerMessage, UploadMessage } from '../protocol';
export class Server { export class Server {
ack = 0; ack = 0;
...@@ -13,7 +13,7 @@ export class Server { ...@@ -13,7 +13,7 @@ export class Server {
console.log(message); console.log(message);
if (message.seq && this.ack !== message.seq) { if (message.seq && this.ack !== message.seq) {
console.log("seq mismatch rejected"); console.log('seq mismatch rejected');
return; return;
} }
...@@ -39,26 +39,11 @@ export class Server { ...@@ -39,26 +39,11 @@ export class Server {
} }
update(socket: Socket, self: PeerMessage, peers: Peer[]) { update(socket: Socket, self: PeerMessage, peers: Peer[]) {
const p: Record<number, PeerQuality> = {}; const message: UploadMessage = {
id: self.id,
for (const peer of peers) { ack: this.ack,
if (peer.reliability === 0) { peers: Object.fromEntries(peers.map(peer => [peer.id, peer.update()]))
continue; };
}
// 有几个包没到
const step = Math.max(0, Math.floor((self.time - peer.time + peer.delay - config.interval / 2) / config.interval));
if (step >= config.timeout) {
peer.reset();
continue;
}
const { id, delay } = peer;
const reliability = (peer.reliability * (config.timeout - step)) / config.timeout;
// TODO: jitter 没算
p[id] = { delay: Math.round(delay), jitter: 0, reliability };
}
const message: UploadMessage = { id: self.id, ack: this.ack, peers: p };
// console.log(message); // console.log(message);
socket.send(JSON.stringify(message), config.server_port, config.server_address); socket.send(JSON.stringify(message), config.server_port, config.server_address);
} }
......
...@@ -38,10 +38,8 @@ const socket = dgram ...@@ -38,10 +38,8 @@ const socket = dgram
socket.bind(config.port); socket.bind(config.port);
setInterval(() => { setInterval(() => {
self.time = Date.now();
const message = JSON.stringify(self); const message = JSON.stringify(self);
for (const peer of peers) { for (const peer of peers) {
peer.update(self.time);
socket.send(message, config.port, `10.200.${peer.id}.${self.id}`); socket.send(message, config.port, `10.200.${peer.id}.${self.id}`);
} }
server.update(socket, self, peers); server.update(socket, self, peers);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment