@@ -14,8 +14,8 @@ import type {
1414} from './types' ;
1515import type KeyRing from '../keys/KeyRing' ;
1616import type { Key , CertificatePEM } from '../keys/types' ;
17- import type { ConnectionData , Host , Hostname , Port } from '../network/types' ;
18- import type { TLSConfig } from '../network /types' ;
17+ import type { ConnectionData , Host , Hostname , Port , TLSConfig } from '../network/types' ;
18+ import type { ServerManifest } from '../rpc /types' ;
1919import type { HolePunchRelayMessage } from './agent/types' ;
2020import Logger from '@matrixai/logger' ;
2121import { withF } from '@matrixai/resources' ;
@@ -37,6 +37,8 @@ import manifestClientAgent from './agent/callers';
3737import * as utils from '../utils' ;
3838import config from '../config' ;
3939import { running , status } from "@matrixai/async-init" ;
40+ import RPCServer from '../rpc/RPCServer' ;
41+ import * as rpcUtilsMiddleware from '../rpc/utils/middleware' ;
4042
4143type ManifestClientAgent = typeof manifestClientAgent ;
4244
@@ -109,6 +111,16 @@ class NodeConnectionManager {
109111 */
110112 public readonly connectionHolePunchIntervalTime : number ;
111113
114+ /**
115+ * Max parse buffer size before RPC parser throws an parse error.
116+ */
117+ public readonly rpcParserBufferSize : number ;
118+
119+ /**
120+ * default timeout for RPC handlers
121+ */
122+ public readonly rpcCallTimeoutTime : number ;
123+
112124 protected logger : Logger ;
113125 protected keyRing : KeyRing ;
114126 protected nodeGraph : NodeGraph ;
@@ -134,6 +146,8 @@ class NodeConnectionManager {
134146
135147 protected connectionLocks : LockBox < Lock > = new LockBox ( ) ;
136148
149+ protected rpcServer ?: RPCServer ;
150+
137151 /**
138152 * Dispatches a `EventNodeConnectionManagerClose` in response to any `NodeConnectionManager`
139153 * error event. Will trigger stop of the `NodeConnectionManager` via the
@@ -157,6 +171,11 @@ class NodeConnectionManager {
157171 }
158172 }
159173
174+ protected handleEventNodeConnectionStream = async ( e : nodesEvents . EventNodeConnectionStream ) => {
175+ const stream = e . detail ;
176+ this . rpcServer ! . handleStream ( stream ) ;
177+ }
178+
160179 /**
161180 * redispatches `QUICSOcket` or `QUICServer` error events as `NodeConnectionManager` error events.
162181 * This should trigger the destruction of the `NodeConnection` through the
@@ -245,6 +264,8 @@ class NodeConnectionManager {
245264 . clientKeepAliveIntervalTime ,
246265 connectionHolePunchIntervalTime : config . defaultsSystem
247266 . nodesConnectionHolePunchIntervalTime ,
267+ rpcParserBufferSize : config . defaultsSystem . rpcParserBufferSize ,
268+ rpcCallTimeoutTime : config . defaultsSystem . rpcCallTimeoutTime ,
248269 } )
249270 this . logger = logger ?? new Logger ( this . constructor . name ) ;
250271 this . keyRing = keyRing ;
@@ -261,6 +282,8 @@ class NodeConnectionManager {
261282 this . connectionKeepAliveTimeoutTime = optionsDefaulted . connectionKeepAliveTimeoutTime ;
262283 this . connectionKeepAliveIntervalTime = optionsDefaulted . connectionKeepAliveIntervalTime ;
263284 this . connectionHolePunchIntervalTime = optionsDefaulted . connectionHolePunchIntervalTime ;
285+ this . rpcParserBufferSize = optionsDefaulted . rpcParserBufferSize ;
286+ this . rpcCallTimeoutTime = optionsDefaulted . rpcCallTimeoutTime ;
264287 // Note that all buffers allocated for crypto operations is using
265288 // `allocUnsafeSlow`. Which ensures that the underlying `ArrayBuffer`
266289 // is not shared. Also, all node buffers satisfy the `ArrayBuffer` interface.
@@ -343,18 +366,34 @@ class NodeConnectionManager {
343366 port = 0 as Port ,
344367 reuseAddr = false ,
345368 ipv6Only = false ,
369+ manifest = { } ,
346370 } : {
347371 host ?: Host ;
348372 port ?: Port ;
349373 reuseAddr ?: boolean ;
350374 ipv6Only ?: boolean ;
375+ manifest ?: ServerManifest ;
351376 } ) {
352377 const address = networkUtils . buildAddress ( host , port ) ;
353378 this . logger . info ( `Start ${ this . constructor . name } on ${ address } ` ) ;
354379
355380 // We should expect that seed nodes are already in the node manager
356381 // It should not be managed here!
357382
383+ // setting up RPCServer
384+ this . rpcServer = await RPCServer . createRPCServer ( {
385+ manifest,
386+ middlewareFactory : rpcUtilsMiddleware . defaultServerMiddlewareWrapper (
387+ undefined ,
388+ this . rpcParserBufferSize ,
389+ ) ,
390+ sensitive : true ,
391+ handlerTimeoutTime : this . rpcCallTimeoutTime ,
392+ handlerTimeoutGraceTime : this . rpcCallTimeoutTime + 2000 ,
393+ logger : this . logger . getChild ( RPCServer . name ) ,
394+ } ) ;
395+
396+ // Setting up QUICSocket
358397 await this . quicSocket . start ( {
359398 host,
360399 port,
@@ -397,7 +436,6 @@ class NodeConnectionManager {
397436 EventAll . name ,
398437 this . handleEventAll ,
399438 ) ;
400-
401439 this . logger . info ( `Started ${ this . constructor . name } ` ) ;
402440 }
403441
@@ -453,6 +491,7 @@ class NodeConnectionManager {
453491 await Promise . all ( destroyProms ) ;
454492 await this . quicServer . stop ( { force : true } ) ;
455493 await this . quicSocket . stop ( { force : true } ) ;
494+ await this . rpcServer ?. destroy ( { force : true , reason : Error ( 'TMP NCM stopping' ) } ) ; // TODO
456495 this . logger . info ( `Stopped ${ this . constructor . name } ` ) ;
457496 }
458497
@@ -928,6 +967,10 @@ class NodeConnectionManager {
928967 // Check if exists in map, this should never happen but better safe than sorry.
929968 if ( this . connections . has ( nodeIdString ) ) utils . never ( ) ;
930969 // Setting up events
970+ nodeConnection . addEventListener (
971+ nodesEvents . EventNodeConnectionStream . name ,
972+ this . handleEventNodeConnectionStream ,
973+ )
931974 nodeConnection . addEventListener (
932975 EventAll . name ,
933976 this . handleEventAll ,
@@ -941,6 +984,10 @@ class NodeConnectionManager {
941984 // Already locked so already destroying
942985 if ( this . connectionLocks . isLocked ( nodeIdString ) ) return ;
943986 await this . destroyConnection ( nodeId ) ;
987+ nodeConnection . removeEventListener (
988+ nodesEvents . EventNodeConnectionStream . name ,
989+ this . handleEventNodeConnectionStream ,
990+ )
944991 nodeConnection . removeEventListener (
945992 EventAll . name ,
946993 this . handleEventAll ,
0 commit comments