-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathZMQ.ts
62 lines (47 loc) · 1.48 KB
/
ZMQ.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as zmq from 'zeromq';
import {Mokka} from '../consensus/main';
class ZMQMokka extends Mokka {
private sockets: Map<string, zmq.Pair> = new Map<string, any>();
public async initialize() {
this.logger.info(`initializing reply socket on port ${this.address}`);
const socket = new zmq.Pair({receiveHighWaterMark: 10});
await socket.bind(this.address);
this.sockets.set(this.address, socket);
for await (const [msg] of this.sockets.get(this.address)) {
await this.emitPacket(msg);
}
}
/**
* The message to write.
*
* @param {string} address The peer address
* @param {Object} packet The packet to write to the connection.
* @api private
*/
public async write(address: string, packet: Buffer): Promise<void> {
if (!this.sockets.has(address)) {
const socket = new zmq.Pair({receiveHighWaterMark: 10});
socket.connect(address);
this.sockets.set(address, socket);
}
try {
await this.sockets.get(address).send(packet);
} catch (e) {
this.sockets.get(address).close();
const socket = new zmq.Pair({receiveHighWaterMark: 10});
socket.connect(address);
this.sockets.set(address, socket);
}
}
public async disconnect(): Promise<void> {
await super.disconnect();
for (const socket of this.sockets.values()) {
socket.close();
}
}
public async connect(): Promise<void> {
this.initialize();
await super.connect();
}
}
export default ZMQMokka;