Commit 373556dd authored by 神楽坂玲奈's avatar 神楽坂玲奈

fix

parent 14bffc61
Pipeline #16114 passed with stages
in 43 seconds
......@@ -57,7 +57,7 @@ interface Change {
每秒:
检查客户端是否超时,若超时标记为下线
检查客户端是否超时,若超时标记为下线。若这个路由器正在变更,退出正在变更状态
- 非 正在变更 状态:
为每个路由器检查,目标到每个路由器,下一跳哪个路由器最优。
......@@ -65,8 +65,7 @@ interface Change {
如果发现有比现在更优的,就为这个路由器 seq++ 并 下发 Change 报文。之后进入正在变更状态,跳过其他路由器的检查
- 正在变更 状态:
向路由器发送 Change 报文
若正在变更的客户端被标记为下线,退出正在变更状态
向路由器重新发送的发送 Change 报文
### 收到客户端 Report 报文时
如果报文 ack 为 0,重置状态并立即发送 Change
......
......@@ -13,7 +13,10 @@ const connections: Record<number, Record<number, { metric: number, protocol: str
export class Router {
static all: Router[] = routers.map(s => new Router(s.id));
static updating?: Router;
static updating?: {
router: Router,
message: Change
};
seq = 0;
peers: Record<number, PeerQuality> = {};
......@@ -36,7 +39,7 @@ export class Router {
for (const router of Router.all.filter(r => r.id !== this.id)) {
this.via.set(router, router);
}
if (Router.updating == this) Router.updating = undefined;
if (Router.updating?.router == this) Router.updating = undefined;
// for (const plan of plans.filter(plan => !plan.routers.includes(this.id))) {
// this.plan[plan.id] = this.id;
// }
......@@ -47,17 +50,17 @@ export class Router {
if (data.ack == this.seq + 1) {
this.time = Date.now();
if (data.peers) this.peers = data.peers;
if (Router.updating === this) {
if (Router.updating?.router === this) {
Router.updating = undefined;
Router.update(socket);
}
} else if (data.ack === 0) { // 客户端重启
this.reset();
this.time = Date.now();
this.send(socket, {}, {});
this.send(socket, { seq: this.seq, via: {} });
} else if (this.seq == 0) { // 服务器重启或客户端下线
this.time = Date.now();
this.send(socket, {}, {});
this.send(socket, { seq: this.seq, via: {} });
} else {
console.log(`ignoring packet from ${data.id}, packet ack=${data.ack}, server seq=${this.seq}`);
}
......@@ -71,7 +74,10 @@ export class Router {
this.reset();
return;
}
if (Router.updating) return;
if (Router.updating) {
if (Router.updating.router == this) this.send(socket, Router.updating.message);
return;
}
const changedVia: Record<number, number> = {};
const metric: Record<number, number> = {};
......@@ -113,13 +119,13 @@ export class Router {
if (!_.isEmpty(changedVia) || !_.isEmpty(changedPlan)) {
this.seq++;
Router.updating = this;
this.send(socket, changedVia, changedPlan);
const message: Change = { seq: this.seq, via: changedVia };
Router.updating = { router: this, message };
this.send(socket, message);
}
}
send(socket: Socket, via: Record<number, number>, plan: Record<number, number>) {
const message: Change = { seq: this.seq, via };
send(socket: Socket, message: Change) {
console.log(`send: ${this.id} ${JSON.stringify(message)}`);
return socket.send(JSON.stringify(message), this.rinfo!.port, this.rinfo!.address);
}
......
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