-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdgram.ts
140 lines (126 loc) · 3.01 KB
/
dgram.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { EventEmitter } from 'events';
import {
DgramSocketWrap
} from './addon';
type FnRecv = (err: undefined | Error, buf: Buffer) => void;
export type SendCb = (err: undefined | Error) => void;
/**
* DgramSocket is used to create a SOCK_DGRAM unix domain socket.
* Currently DgramSocket doesn't work with `cluster` module.
*
* DgramSocket is also an `EventEmitter` and will emit events including:
*
* ### Event: `'data'`
* - buffer `Buffer`
* - path `string`
*
* Emitted when data is received. `path` indicates remote address information.
*
* ### Event: `'error'`
* - error `Error`
*
* Emitted when an error occurs.
*
* ### Event: `'close'`
* The 'close' event is emitted after a socket is closed with close().
*/
export class DgramSocket extends EventEmitter {
private closed: boolean = false;
private wrap: DgramSocketWrap;
constructor() {
super();
this.emit = this.emit.bind(this);
this.wrap = new DgramSocketWrap(this);
// TODO currently we can't get this object in rust side
this.wrap.init(this.wrap);
this.wrap.startRecv();
this.on('_data', this.onData);
this.on('_error', this.onError);
}
private onData = (buf: Buffer, filepath: string) => {
process.nextTick(() => {
this.emit('data', buf, filepath);
});
};
private onError = (err: Error) => {
this.close();
this.emit('error', err);
};
private checkClosed() {
if (this.closed) {
throw new Error('DgramSocket has been closed');
}
}
/**
* Listen for datagram messages on a path.
* @param socketPath
*/
bind(socketPath: string) {
this.checkClosed();
this.wrap.bind(socketPath);
}
/**
* Send messages to the destination path.
* @param buf
* @param offset
* @param length
* @param destPath
* @param onWrite
*/
sendTo(
buf: Buffer,
offset: number,
length: number,
destPath: string,
onWrite?: SendCb
) {
this.checkClosed();
this.wrap.sendTo(buf, offset, length, destPath, onWrite);
}
/**
* @returns the SO_RCVBUF socket receive buffer size in bytes.
*/
getRecvBufferSize() {
return this.wrap.getRecvBufferSize();
}
/**
* Sets the SO_RCVBUF socket option. Sets the maximum socket receive buffer in bytes.
* @param size
* @returns
*/
setRecvBufferSize(size: number) {
return this.wrap.setRecvBufferSize(size);
}
/**
* @returns the SO_SNDBUF socket send buffer size in bytes.
*/
getSendBufferSize() {
return this.wrap.getSendBufferSize();
}
/**
* Sets the SO_SNDBUF socket option. Sets the maximum socket send buffer in bytes.
* @param size
* @returns
*/
setSendBufferSize(size: number) {
return this.wrap.setSendBufferSize(size);
}
/**
* Returns the bound address.
* @returns
*/
address(): string {
return this.wrap.address();
}
/**
* Close the underlying socket and stop listening for data on it.
* @returns
*/
close() {
if (this.closed) {
return;
}
this.closed = true;
this.wrap.close();
}
}