FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mediasoup/node/src/Channel.ts at v3 · NFhook/mediasoup · GitHub
NFhook
mediasoup
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mediasoup
/
node
/
src
/
Channel.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
494 lines (395 loc) · 10.9 KB
Breadcrumbs
mediasoup
/
node
/
src
/
Channel.ts
Copy path
File metadata and controls
494 lines (395 loc) · 10.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import
*
as
os
from
'node:os'
;
import
{
Duplex
}
from
'node:stream'
;
import
{
info
,
warn
}
from
'node:console'
;
import
*
as
flatbuffers
from
'flatbuffers'
;
import
{
Logger
}
from
'./Logger'
;
import
{
EnhancedEventEmitter
}
from
'./enhancedEvents'
;
import
{
InvalidStateError
}
from
'./errors'
;
import
{
Body
as
RequestBody
,
Method
,
Request
}
from
'./fbs/request'
;
import
{
Response
}
from
'./fbs/response'
;
import
{
Message
,
Body
as
MessageBody
}
from
'./fbs/message'
;
import
{
Notification
,
Body
as
NotificationBody
,
Event
,
}
from
'./fbs/notification'
;
import
{
Log
}
from
'./fbs/log'
;
const
IS_LITTLE_ENDIAN
=
os
.
endianness
(
)
===
'LE'
;
const
logger
=
new
Logger
(
'Channel'
)
;
type
Sent
=
{
id
:
number
;
method
:
string
;
resolve
:
(
data
?:
any
)
=>
void
;
reject
:
(
error
:
Error
)
=>
void
;
close
:
(
)
=>
void
;
}
;
// Binary length for a 4194304 bytes payload.
const
MESSAGE_MAX_LEN
=
4194308
;
const
PAYLOAD_MAX_LEN
=
4194304
;
export
class
Channel
extends
EnhancedEventEmitter
{
// Closed flag.
#closed
=
false
;
// Unix Socket instance for sending messages to the worker process.
readonly
#producerSocket:
Duplex
;
// Unix Socket instance for receiving messages to the worker process.
readonly
#consumerSocket:
Duplex
;
// Next id for messages sent to the worker process.
#nextId
=
0
;
// Map of pending sent requests.
readonly
#sents:
Map
<
number
,
Sent
>
=
new
Map
(
)
;
// Buffer for reading messages from the worker.
#recvBuffer
=
Buffer
.
alloc
(
0
)
;
// flatbuffers builder.
#bufferBuilder:
flatbuffers
.
Builder
=
new
flatbuffers
.
Builder
(
1024
)
;
/**
*
@private
*/
constructor
(
{
producerSocket
,
consumerSocket
,
pid
,
}
:
{
producerSocket
:
any
;
consumerSocket
:
any
;
pid
:
number
;
}
)
{
super
(
)
;
logger
.
debug
(
'constructor()'
)
;
this
.
#producerSocket
=
producerSocket
as
Duplex
;
this
.
#consumerSocket
=
consumerSocket
as
Duplex
;
// Read Channel responses/notifications from the worker.
this
.
#consumerSocket
.
on
(
'data'
,
(
buffer
:
Buffer
)
=>
{
if
(
!
this
.
#recvBuffer
.
length
)
{
this
.
#recvBuffer
=
buffer
;
}
else
{
this
.
#recvBuffer
=
Buffer
.
concat
(
[
this
.
#recvBuffer
,
buffer
]
,
this
.
#recvBuffer
.
length
+
buffer
.
length
)
;
}
if
(
this
.
#recvBuffer
.
length
>
PAYLOAD_MAX_LEN
)
{
logger
.
error
(
'receiving buffer is full, discarding all data in it'
)
;
// Reset the buffer and exit.
this
.
#recvBuffer
=
Buffer
.
alloc
(
0
)
;
return
;
}
let
msgStart
=
0
;
while
(
true
)
{
const
readLen
=
this
.
#recvBuffer
.
length
-
msgStart
;
if
(
readLen
<
4
)
{
// Incomplete data.
break
;
}
const
dataView
=
new
DataView
(
this
.
#recvBuffer
.
buffer
,
this
.
#recvBuffer
.
byteOffset
+
msgStart
)
;
const
msgLen
=
dataView
.
getUint32
(
0
,
IS_LITTLE_ENDIAN
)
;
if
(
readLen
<
4
+
msgLen
)
{
// Incomplete data.
break
;
}
const
payload
=
this
.
#recvBuffer
.
subarray
(
msgStart
+
4
,
msgStart
+
4
+
msgLen
)
;
msgStart
+=
4
+
msgLen
;
const
buf
=
new
flatbuffers
.
ByteBuffer
(
new
Uint8Array
(
payload
)
)
;
const
message
=
Message
.
getRootAsMessage
(
buf
)
;
try
{
switch
(
message
.
dataType
(
)
)
{
case
MessageBody
.
Response
:
{
const
response
=
new
Response
(
)
;
message
.
data
(
response
)
;
this
.
processResponse
(
response
)
;
break
;
}
case
MessageBody
.
Notification
:
{
const
notification
=
new
Notification
(
)
;
message
.
data
(
notification
)
;
this
.
processNotification
(
notification
)
;
break
;
}
case
MessageBody
.
Log
:
{
const
log
=
new
Log
(
)
;
message
.
data
(
log
)
;
this
.
processLog
(
pid
,
log
)
;
break
;
}
default
:
{
warn
(
`worker[pid:
${
pid
}
] unexpected data:
${
payload
.
toString
(
'utf8'
,
1
)
}
`
)
;
}
}
}
catch
(
error
)
{
logger
.
error
(
`received invalid message from the worker process:
${
error
}
`
)
;
}
}
if
(
msgStart
!=
0
)
{
this
.
#recvBuffer
=
this
.
#recvBuffer
.
slice
(
msgStart
)
;
}
}
)
;
this
.
#consumerSocket
.
on
(
'end'
,
(
)
=>
logger
.
debug
(
'Consumer Channel ended by the worker process'
)
)
;
this
.
#consumerSocket
.
on
(
'error'
,
error
=>
logger
.
error
(
`Consumer Channel error:
${
error
}
`
)
)
;
this
.
#producerSocket
.
on
(
'end'
,
(
)
=>
logger
.
debug
(
'Producer Channel ended by the worker process'
)
)
;
this
.
#producerSocket
.
on
(
'error'
,
error
=>
logger
.
error
(
`Producer Channel error:
${
error
}
`
)
)
;
}
/**
* flatbuffer builder.
*/
get
bufferBuilder
(
)
:
flatbuffers
.
Builder
{
return
this
.
#bufferBuilder
;
}
/**
*
@private
*/
close
(
)
:
void
{
if
(
this
.
#closed
)
{
return
;
}
logger
.
debug
(
'close()'
)
;
this
.
#closed
=
true
;
// Close every pending sent.
for
(
const
sent
of
this
.
#sents
.
values
(
)
)
{
sent
.
close
(
)
;
}
// Remove event listeners but leave a fake 'error' hander to avoid
// propagation.
this
.
#consumerSocket
.
removeAllListeners
(
'end'
)
;
this
.
#consumerSocket
.
removeAllListeners
(
'error'
)
;
this
.
#consumerSocket
.
on
(
'error'
,
(
)
=>
{
}
)
;
this
.
#producerSocket
.
removeAllListeners
(
'end'
)
;
this
.
#producerSocket
.
removeAllListeners
(
'error'
)
;
this
.
#producerSocket
.
on
(
'error'
,
(
)
=>
{
}
)
;
// Destroy the sockets.
try
{
this
.
#producerSocket
.
destroy
(
)
;
}
catch
(
error
)
{
}
try
{
this
.
#consumerSocket
.
destroy
(
)
;
}
catch
(
error
)
{
}
}
/**
*
@private
*/
notify
(
event
:
Event
,
bodyType
?:
NotificationBody
,
bodyOffset
?:
number
,
handlerId
?:
string
)
:
void
{
logger
.
debug
(
`notify() [event:
${
Event
[
event
]
}
]`
)
;
if
(
this
.
#closed
)
{
throw
new
InvalidStateError
(
`Channel closed, cannot send notification [event:
${
Event
[
event
]
}
]`
)
;
}
const
handlerIdOffset
=
this
.
#bufferBuilder
.
createString
(
handlerId
)
;
let
notificationOffset
:
number
;
if
(
bodyType
&&
bodyOffset
)
{
notificationOffset
=
Notification
.
createNotification
(
this
.
#bufferBuilder
,
handlerIdOffset
,
event
,
bodyType
,
bodyOffset
)
;
}
else
{
notificationOffset
=
Notification
.
createNotification
(
this
.
#bufferBuilder
,
handlerIdOffset
,
event
,
NotificationBody
.
NONE
,
0
)
;
}
const
messageOffset
=
Message
.
createMessage
(
this
.
#bufferBuilder
,
MessageBody
.
Notification
,
notificationOffset
)
;
// Finalizes the buffer and adds a 4 byte prefix with the size of the buffer.
this
.
#bufferBuilder
.
finishSizePrefixed
(
messageOffset
)
;
// Create a new buffer with this data so multiple contiguous flatbuffers
// do not point to the builder buffer overriding others info.
const
buffer
=
new
Uint8Array
(
this
.
#bufferBuilder
.
asUint8Array
(
)
)
;
// Clear the buffer builder so it's reused for the next request.
this
.
#bufferBuilder
.
clear
(
)
;
if
(
buffer
.
byteLength
>
MESSAGE_MAX_LEN
)
{
throw
new
Error
(
`notification too big [event:
${
Event
[
event
]
}
]`
)
;
}
try
{
// This may throw if closed or remote side ended.
this
.
#producerSocket
.
write
(
buffer
,
'binary'
)
;
}
catch
(
error
)
{
logger
.
warn
(
`notify() | sending notification failed:
${
error
}
`
)
;
return
;
}
}
async
request
(
method
:
Method
,
bodyType
?:
RequestBody
,
bodyOffset
?:
number
,
handlerId
?:
string
)
:
Promise
<
Response
>
{
logger
.
debug
(
`request() [method:
${
Method
[
method
]
}
]`
)
;
if
(
this
.
#closed
)
{
throw
new
InvalidStateError
(
`Channel closed, cannot send request [method:
${
Method
[
method
]
}
]`
)
;
}
if
(
this
.
#nextId
<
4294967295
)
{
++
this
.
#nextId
;
}
else
{
this
.
#nextId
=
1
;
}
const
id
=
this
.
#nextId
;
const
handlerIdOffset
=
this
.
#bufferBuilder
.
createString
(
handlerId
??
''
)
;
let
requestOffset
:
number
;
if
(
bodyType
&&
bodyOffset
)
{
requestOffset
=
Request
.
createRequest
(
this
.
#bufferBuilder
,
id
,
method
,
handlerIdOffset
,
bodyType
,
bodyOffset
)
;
}
else
{
requestOffset
=
Request
.
createRequest
(
this
.
#bufferBuilder
,
id
,
method
,
handlerIdOffset
,
RequestBody
.
NONE
,
0
)
;
}
const
messageOffset
=
Message
.
createMessage
(
this
.
#bufferBuilder
,
MessageBody
.
Request
,
requestOffset
)
;
// Finalizes the buffer and adds a 4 byte prefix with the size of the buffer.
this
.
#bufferBuilder
.
finishSizePrefixed
(
messageOffset
)
;
// Create a new buffer with this data so multiple contiguous flatbuffers
// do not point to the builder buffer overriding others info.
const
buffer
=
new
Uint8Array
(
this
.
#bufferBuilder
.
asUint8Array
(
)
)
;
// Clear the buffer builder so it's reused for the next request.
this
.
#bufferBuilder
.
clear
(
)
;
if
(
buffer
.
byteLength
>
MESSAGE_MAX_LEN
)
{
throw
new
Error
(
`request too big [method:
${
Method
[
method
]
}
]`
)
;
}
// This may throw if closed or remote side ended.
this
.
#producerSocket
.
write
(
buffer
,
'binary'
)
;
return
new
Promise
(
(
pResolve
,
pReject
)
=>
{
const
sent
:
Sent
=
{
id
:
id
,
method
:
Method
[
method
]
,
resolve
:
data2
=>
{
if
(
!
this
.
#sents
.
delete
(
id
)
)
{
return
;
}
pResolve
(
data2
)
;
}
,
reject
:
error
=>
{
if
(
!
this
.
#sents
.
delete
(
id
)
)
{
return
;
}
pReject
(
error
)
;
}
,
close
:
(
)
=>
{
pReject
(
new
InvalidStateError
(
`Channel closed, pending request aborted [method:
${
Method
[
method
]
}
, id:
${
id
}
]`
)
)
;
}
,
}
;
// Add sent stuff to the map.
this
.
#sents
.
set
(
id
,
sent
)
;
}
)
;
}
private
processResponse
(
response
:
Response
)
:
void
{
const
sent
=
this
.
#sents
.
get
(
response
.
id
(
)
)
;
if
(
!
sent
)
{
logger
.
error
(
`received response does not match any sent request [id:
${
response
.
id
(
)
}
]`
)
;
return
;
}
if
(
response
.
accepted
(
)
)
{
logger
.
debug
(
`request succeeded [method:
${
sent
.
method
}
, id:
${
sent
.
id
}
]`
)
;
sent
.
resolve
(
response
)
;
}
else
if
(
response
.
error
(
)
)
{
logger
.
warn
(
`request failed [method:
${
sent
.
method
}
, id:
${
sent
.
id
}
]:
${
response
.
reason
(
)
}
`
)
;
switch
(
response
.
error
(
)
!
)
{
case
'TypeError'
:
{
sent
.
reject
(
new
TypeError
(
response
.
reason
(
)
!
)
)
;
break
;
}
default
:
{
sent
.
reject
(
new
Error
(
response
.
reason
(
)
!
)
)
;
}
}
}
else
{
logger
.
error
(
`received response is not accepted nor rejected [method:
${
sent
.
method
}
, id:
${
sent
.
id
}
]`
)
;
}
}
private
processNotification
(
notification
:
Notification
)
:
void
{
// Due to how Promises work, it may happen that we receive a response
// from the worker followed by a notification from the worker. If we
// emit the notification immediately it may reach its target **before**
// the response, destroying the ordered delivery. So we must wait a bit
// here.
// See https://github.com/versatica/mediasoup/issues/510
setImmediate
(
(
)
=>
this
.
emit
(
notification
.
handlerId
(
)
!
,
notification
.
event
(
)
,
notification
)
)
;
}
private
processLog
(
pid
:
number
,
log
:
Log
)
:
void
{
const
logData
=
log
.
data
(
)
!
;
switch
(
logData
[
0
]
)
{
// 'D' (a debug log).
case
'D'
:
{
logger
.
debug
(
`[pid:
${
pid
}
]
${
logData
.
slice
(
1
)
}
`
)
;
break
;
}
// 'W' (a warn log).
case
'W'
:
{
logger
.
warn
(
`[pid:
${
pid
}
]
${
logData
.
slice
(
1
)
}
`
)
;
break
;
}
// 'E' (a error log).
case
'E'
:
{
logger
.
error
(
`[pid:
${
pid
}
]
${
logData
.
slice
(
1
)
}
`
)
;
break
;
}
// 'X' (a dump log).
case
'X'
:
{
info
(
logData
.
slice
(
1
)
)
;
break
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL