Commit ab6eb6a5 authored by 神楽坂玲奈's avatar 神楽坂玲奈

faster

parent 7c354c12
Pipeline #16108 passed with stages
in 2 minutes and 19 seconds
...@@ -73,7 +73,7 @@ interface Change { ...@@ -73,7 +73,7 @@ interface Change {
如果报文 ack 不为 0 而服务器 seq 为 0,立即发送 Change 如果报文 ack 不为 0 而服务器 seq 为 0,立即发送 Change
如果报文 ack 不为 change seq +1,忽略。 如果报文 ack 不为 change seq +1,忽略。
如果携带了 peers 信息,存下来。 如果携带了 peers 信息,存下来。
如果这个路由器正在变更,退出正在变更状态 如果这个路由器正在变更,退出正在变更状态,额外执行一次每秒更新
## 关于顺序号,断线、重启的一些解释: ## 关于顺序号,断线、重启的一些解释:
### 客户端跟客户端之间 ### 客户端跟客户端之间
......
...@@ -26,6 +26,10 @@ export class Router { ...@@ -26,6 +26,10 @@ export class Router {
constructor(public id: number) { constructor(public id: number) {
} }
static update(socket: Socket) {
for (const from of Router.all) from.update(socket);
}
reset() { reset() {
this.seq = 0; this.seq = 0;
this.peers = {}; this.peers = {};
...@@ -43,7 +47,10 @@ export class Router { ...@@ -43,7 +47,10 @@ export class Router {
if (data.ack == this.seq + 1) { if (data.ack == this.seq + 1) {
this.time = Date.now(); this.time = Date.now();
if (data.peers) this.peers = data.peers; if (data.peers) this.peers = data.peers;
if (Router.updating === this) Router.updating = undefined; if (Router.updating === this) {
Router.updating = undefined;
Router.update(socket);
}
} else if (data.ack === 0) { // 客户端重启 } else if (data.ack === 0) { // 客户端重启
this.reset(); this.reset();
this.time = Date.now(); this.time = Date.now();
......
...@@ -36,6 +36,4 @@ const socket = dgram ...@@ -36,6 +36,4 @@ const socket = dgram
}); });
socket.bind(config.port); socket.bind(config.port);
setInterval(() => { setInterval(() => Router.update(socket), config.interval);
for (const from of Router.all) from.update(socket);
}, config.interval);
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