feat: add KeepAlive packet

This commit is contained in:
2023-07-15 14:15:48 +02:00
parent 4ed86f2e88
commit bafa913506
3 changed files with 26 additions and 7 deletions
+18 -3
View File
@@ -7,7 +7,8 @@ import {
PublishResponse, PublishResponse,
DiscoverRequest, DiscoverRequest,
DiscoverResponse, DiscoverResponse,
DiscoverPeer DiscoverPeer,
KeepAlive
} from './Protocol.js' } from './Protocol.js'
import { EventEmitter } from 'node:events' import { EventEmitter } from 'node:events'
@@ -30,6 +31,7 @@ export class Peer extends EventEmitter {
this.#msgMap.set('Message', this.#onmessage) this.#msgMap.set('Message', this.#onmessage)
this.#msgMap.set('PublishRequest', this.#onpublishrequest) this.#msgMap.set('PublishRequest', this.#onpublishrequest)
this.#msgMap.set('DiscoverRequest', this.#ondiscoverrequest) this.#msgMap.set('DiscoverRequest', this.#ondiscoverrequest)
this.#msgMap.set('KeepAlive', this.#onkeepalive)
this.#setupListeners() this.#setupListeners()
} }
@@ -98,10 +100,9 @@ export class Peer extends EventEmitter {
const hash = createHash('sha256').update(this.#salt + seed).digest('hex') const hash = createHash('sha256').update(this.#salt + seed).digest('hex')
const address = BigInt('0x' + hash.substring(0, 16)) const address = BigInt('0x' + hash.substring(0, 16))
console.log('assigned address', address.toString(16).match(/.{1,4}/g).join(':'))
this.#address = address this.#address = address
console.log('assigned address', this.formattedAddress)
console.log(address.toString(10))
this.#send({ this.#send({
id: message.id, id: message.id,
message: AuthenticationResponse.create({ 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() { get address() {
return this.#address return this.#address
} }
get formattedAddress() {
return this.#address.toString(16).match(/.{1,4}/g).join(':')
}
} }
+7 -3
View File
@@ -13,11 +13,11 @@ export const [
export const [ export const [
AuthenticationRequest, AuthenticationRequest,
AuthenticationResponse AuthenticationResponse
] = await import_proto('2.0/Authentication.proto', ['AuthenticationRequest', 'AuthenticationResponse']) ] = await import_proto('2.0/Authentication.proto', ['AuthenticationRequest', 'AuthenticationResponse'])
export const [ export const [
Message Message
] = await import_proto('2.0/Message.proto', ['Message']) ] = await import_proto('2.0/Message.proto', ['Message'])
export const [ export const [
PublishRequest, PublishRequest,
@@ -25,7 +25,11 @@ export const [
DiscoverRequest, DiscoverRequest,
DiscoverResponse, DiscoverResponse,
DiscoverPeer 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 = [ export const messages = [
Container, Container,