From bafa913506a0b3af590c723d038e9696a3db27fb Mon Sep 17 00:00:00 2001 From: utf-4096 Date: Sat, 15 Jul 2023 14:15:48 +0200 Subject: [PATCH] feat: add KeepAlive packet --- src/Peer.js | 21 ++++++++++++++++++--- src/Protocol.js | 10 +++++++--- src/protocol | 2 +- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Peer.js b/src/Peer.js index f1f5500..16b8760 100644 --- a/src/Peer.js +++ b/src/Peer.js @@ -7,7 +7,8 @@ import { PublishResponse, DiscoverRequest, DiscoverResponse, - DiscoverPeer + DiscoverPeer, + KeepAlive } from './Protocol.js' import { EventEmitter } from 'node:events' @@ -30,6 +31,7 @@ export class Peer extends EventEmitter { this.#msgMap.set('Message', this.#onmessage) this.#msgMap.set('PublishRequest', this.#onpublishrequest) this.#msgMap.set('DiscoverRequest', this.#ondiscoverrequest) + this.#msgMap.set('KeepAlive', this.#onkeepalive) this.#setupListeners() } @@ -98,10 +100,9 @@ export class Peer extends EventEmitter { const hash = createHash('sha256').update(this.#salt + seed).digest('hex') const address = BigInt('0x' + hash.substring(0, 16)) - console.log('assigned address', address.toString(16).match(/.{1,4}/g).join(':')) this.#address = address + console.log('assigned address', this.formattedAddress) - console.log(address.toString(10)) this.#send({ id: message.id, message: AuthenticationResponse.create({ @@ -183,7 +184,21 @@ export class Peer extends EventEmitter { }) } + #onkeepalive(message) { + const keepAlive = KeepAlive.toObject(message.KeepAlive) + console.log(`keepalive from ${this.#address}: ${keepAlive.data}`) + + this.#send({ + id: message.id, + message: keepAlive + }) + } + get address() { return this.#address } + + get formattedAddress() { + return this.#address.toString(16).match(/.{1,4}/g).join(':') + } } diff --git a/src/Protocol.js b/src/Protocol.js index fa7ad99..9db2aca 100644 --- a/src/Protocol.js +++ b/src/Protocol.js @@ -13,11 +13,11 @@ export const [ export const [ AuthenticationRequest, AuthenticationResponse -] = await import_proto('2.0/Authentication.proto', ['AuthenticationRequest', 'AuthenticationResponse']) +] = await import_proto('2.0/Authentication.proto', ['AuthenticationRequest', 'AuthenticationResponse']) export const [ Message -] = await import_proto('2.0/Message.proto', ['Message']) +] = await import_proto('2.0/Message.proto', ['Message']) export const [ PublishRequest, @@ -25,7 +25,11 @@ export const [ DiscoverRequest, DiscoverResponse, DiscoverPeer -] = await import_proto('2.0/PublishDiscover.proto', ['PublishRequest', 'PublishResponse', 'DiscoverRequest', 'DiscoverResponse', 'DiscoverPeer']) +] = await import_proto('2.0/PublishDiscover.proto', ['PublishRequest', 'PublishResponse', 'DiscoverRequest', 'DiscoverResponse', 'DiscoverPeer']) + +export const [ + KeepAlive +] = await import_proto('2.0/KeepAlive.proto', ['KeepAlive']) export const messages = [ Container, diff --git a/src/protocol b/src/protocol index d0747fa..1fe4783 160000 --- a/src/protocol +++ b/src/protocol @@ -1 +1 @@ -Subproject commit d0747faf6860c825e81855e2b58cbc8298084af1 +Subproject commit 1fe4783d940cd8905b3f3cb03884890e887cd0c4