generated from napi-rs/package-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
181 lines (144 loc) · 3.9 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { EventEmitter } from 'events'
import { VsockSocket as VsockSocketAddon } from './addon'
export type Callback = () => void
export class VsockServer extends EventEmitter {
listening = false
private closed = false
private readonly socket: VsockSocketAddon
constructor() {
super()
const emit = (this.emit = this.emit.bind(this))
this.socket = new VsockSocketAddon(function (err: Error, eventName: string, ...args) {
if (err) {
err.message += `(server socket event: ${eventName})`
emit('error', err)
} else {
emit(eventName, ...args)
}
})
this.on('_connection', this.onConnection)
this.on('_error', this.onError)
}
close() {
if (this.closed) {
return
}
this.listening = false
this.closed = true
this.socket.close()
}
listen(port: number) {
if (this.closed) {
throw new Error('Socket has been closed')
}
this.socket.listen(port)
this.listening = true
}
private readonly onError = (err: Error) => {
process.nextTick(() => {
// unhandled error emitted from emitter will cause stopping process.
// server have to listen on 'error' event to bypass this problem now.
this.emit('error', err)
})
}
private readonly onConnection = (fd: number) => {
const socket = new VsockSocket(fd)
this.emit('connection', socket)
}
}
export class VsockSocket extends EventEmitter {
destroyed = false
connecting = false
private readonly socket: VsockSocketAddon
private connectCallback?: Callback
private shutdownCallback?: Callback
private isShutdown = false
private isEnd = false
constructor(fd?: number) {
super()
const emit = (this.emit = this.emit.bind(this))
this.socket = new VsockSocketAddon(function (err: Error, eventName: string, ...args) {
if (err) {
err.message += `(socket event: ${eventName})`
emit('error', err)
} else {
emit(eventName, ...args)
}
}, fd)
if (fd) {
this.socket.startRecv()
}
this.on('_data', this.onData)
this.on('_connect', this.onConnect)
this.on('_error', this.onError)
this.on('_shutdown', this.onShutdown)
this.on('end', this.onEnd)
}
setMaxConnectionAttempts(maxConnectionAttempts: number) {
this.socket.setMaxConnectionAttempts(maxConnectionAttempts)
}
connect(cid: number, port: number, connectCallback?: Callback) {
this.checkDestroyed()
this.connecting = true
this.connectCallback = connectCallback
this.socket.connect(cid, port)
}
writeSync(buf: Buffer) {
this.checkDestroyed()
this.socket.writeBuffer(buf)
}
writeTextSync(data: string) {
this.checkDestroyed()
this.socket.writeText(data)
}
end(callback?: Callback) {
this.shutdownCallback = callback
this.socket.end()
}
destroy() {
if (this.destroyed) {
return
}
this.destroyed = true
this.socket.close()
}
private checkDestroyed() {
if (this.destroyed) {
throw new Error('Socket has been destroyed')
}
}
private tryClose() {
if (this.isEnd && this.isShutdown) {
this.destroy()
}
}
private readonly onData = (buf: Buffer) => {
this.emit('data', buf)
}
private readonly onError = (err: Error) => {
process.nextTick(() => {
// unhandled error emitted from emitter will cause stopping process.
// incoming socket have to listen on 'error' event to bypass this problem now.
this.emit('error', err)
})
}
private readonly onEnd = () => {
this.isEnd = true
this.tryClose()
}
private readonly onShutdown = () => {
this.isShutdown = true
this.tryClose()
if (this.shutdownCallback) {
this.shutdownCallback()
}
}
private readonly onConnect = () => {
this.connecting = false
this.emit('connect')
this.socket.startRecv()
if (this.connectCallback) {
this.connectCallback()
}
}
}