1
- import { observable } from 'mobx' ;
1
+ import { action , observable } from 'mobx' ;
2
2
3
3
import { InputRawPassthrough , InputRawPassthroughData } from '../types' ;
4
4
import { HTKEventBase } from './events/event-base' ;
@@ -8,6 +8,8 @@ export interface RawTunnelMessage extends InputRawPassthroughData {
8
8
isBinary : true ;
9
9
}
10
10
11
+ const PACKET_COMBINE_LIMIT = 500_000 ;
12
+
11
13
export class RawTunnel extends HTKEventBase {
12
14
13
15
constructor (
@@ -35,9 +37,33 @@ export class RawTunnel extends HTKEventBase {
35
37
return true ;
36
38
}
37
39
40
+ @observable
38
41
readonly packets : StreamMessage [ ] = [ ] ;
39
42
43
+ @action
40
44
addChunk ( dataEvent : InputRawPassthroughData ) {
45
+ const lastPacket = this . packets [ this . packets . length - 1 ] as StreamMessage | undefined ;
46
+
47
+ // Combine together small sequential packets from the same client within 10ms, to
48
+ // simplify & clarify busy streams
49
+ if (
50
+ lastPacket ?. direction === dataEvent . direction &&
51
+ ( dataEvent . eventTimestamp - lastPacket . timestamp < 10 ) &&
52
+ lastPacket . content . byteLength + dataEvent . content . byteLength < PACKET_COMBINE_LIMIT
53
+ ) {
54
+ this . packets [ this . packets . length - 1 ] = new StreamMessage (
55
+ {
56
+ id : this . id ,
57
+ direction : lastPacket . direction ,
58
+ eventTimestamp : lastPacket . timestamp , // Use the first packet in the chunk's timestamp
59
+ content : Buffer . concat ( [ lastPacket . content , dataEvent . content ] ) ,
60
+ isBinary : true
61
+ } ,
62
+ this . packets . length
63
+ ) ;
64
+ return ;
65
+ }
66
+
41
67
this . packets . push (
42
68
new StreamMessage ( Object . assign ( dataEvent , { isBinary : true } as const ) , this . packets . length )
43
69
) ;
@@ -46,6 +72,7 @@ export class RawTunnel extends HTKEventBase {
46
72
@observable
47
73
private open = true ;
48
74
75
+ @action
49
76
markClosed ( closeEvent : InputRawPassthrough ) {
50
77
this . timingEvents . disconnectTimestamp = closeEvent . timingEvents . disconnectTimestamp ;
51
78
this . open = false ;
0 commit comments