fix: fix broadcasting to find peers on topic correctly, and use the discovery object to refresh if required
This commit is contained in:
parent
fc083fa72c
commit
1da4a4967e
59
src/index.ts
59
src/index.ts
|
@ -28,6 +28,7 @@ export default class DHTFlood extends EventEmitter {
|
||||||
private topic: Buffer;
|
private topic: Buffer;
|
||||||
private symbol: Symbol;
|
private symbol: Symbol;
|
||||||
private socketMap: Set<Function> = new Set<Function>();
|
private socketMap: Set<Function> = new Set<Function>();
|
||||||
|
private discovery: any;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
lruSize = LRU_SIZE,
|
lruSize = LRU_SIZE,
|
||||||
|
@ -60,7 +61,7 @@ export default class DHTFlood extends EventEmitter {
|
||||||
|
|
||||||
this.topic = topicHash as Buffer;
|
this.topic = topicHash as Buffer;
|
||||||
|
|
||||||
this.swarm.join(topicHash);
|
this.discovery = this.swarm.join(topicHash);
|
||||||
|
|
||||||
this.symbol = Symbol.for(this.protocol);
|
this.symbol = Symbol.for(this.protocol);
|
||||||
}
|
}
|
||||||
|
@ -155,30 +156,54 @@ export default class DHTFlood extends EventEmitter {
|
||||||
return chan.messages[0];
|
return chan.messages[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcast(data: any, ttl = this.ttl) {
|
async broadcast(data: any, ttl = this.ttl) {
|
||||||
this.messageNumber++;
|
let failed: Buffer[] = [];
|
||||||
const { id, messageNumber } = this;
|
|
||||||
|
|
||||||
let topicString = this.topic.toString("hex");
|
for (const peer of this.getAllPeers()) {
|
||||||
|
if (!this.trySendDataToPubkey(peer, data, ttl)) {
|
||||||
|
failed.push(peer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let peers: Buffer[] = [...this.swarm.peers.values()]
|
if (failed.length) {
|
||||||
.filter((peerInfo: any) => peerInfo._seenTopics.has(topicString))
|
await this.discovery.refresh();
|
||||||
|
|
||||||
|
for (const peer of failed) {
|
||||||
|
this.trySendDataToPubkey(peer, data, ttl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private getTopicPeers(): Buffer[] {
|
||||||
|
return [...this.swarm.peers.values()]
|
||||||
|
.filter((peerInfo: any) =>
|
||||||
|
peerInfo._seenTopics.has(this.topic.toString("hex"))
|
||||||
|
)
|
||||||
.map((peerInfo) => peerInfo.publicKey);
|
.map((peerInfo) => peerInfo.publicKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
private getAllPeers(): Buffer[] {
|
||||||
|
return [...this.swarm._allConnections[Symbol.iterator]()].map(
|
||||||
|
(peer: any) => peer.remotePublicKey
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private trySendDataToPubkey(peer: any, data: any, ttl: number) {
|
||||||
|
const topicPeers = this.getTopicPeers();
|
||||||
|
|
||||||
|
const found = topicPeers.filter((item) => b4a.equals(item, peer));
|
||||||
|
|
||||||
|
if (!found.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
for (const peer of peers) {
|
|
||||||
const conn = this.swarm._allConnections.get(peer);
|
const conn = this.swarm._allConnections.get(peer);
|
||||||
if (!conn) {
|
if (!conn) {
|
||||||
continue;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const message = this.setupPeer(conn);
|
this.send(conn, data, ttl);
|
||||||
message.send({
|
return true;
|
||||||
originId: id,
|
|
||||||
messageNumber,
|
|
||||||
ttl,
|
|
||||||
data: b4a.from(data),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
send(peer: any, data: any, ttl = this.ttl) {
|
send(peer: any, data: any, ttl = this.ttl) {
|
||||||
|
|
Loading…
Reference in New Issue