@streamr/dht 100.2.5-beta.0 → 101.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package.json +7 -7
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionLockRpcLocal.js +8 -8
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.js +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +4 -6
- package/dist/src/connection/ConnectionManager.js +128 -103
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +15 -14
- package/dist/src/connection/ConnectorFacade.js +70 -52
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +9 -2
- package/dist/src/connection/Handshaker.js +117 -27
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +13 -38
- package/dist/src/connection/ManagedConnection.js +31 -252
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/OutputBuffer.d.ts +9 -0
- package/dist/src/connection/OutputBuffer.js +26 -0
- package/dist/src/connection/OutputBuffer.js.map +1 -0
- package/dist/src/connection/PendingConnection.d.ts +19 -0
- package/dist/src/connection/PendingConnection.js +59 -0
- package/dist/src/connection/PendingConnection.js.map +1 -0
- package/dist/src/connection/connectivityChecker.js +3 -3
- package/dist/src/connection/connectivityChecker.js.map +1 -1
- package/dist/src/connection/connectivityRequestHandler.js +2 -2
- package/dist/src/connection/connectivityRequestHandler.js.map +1 -1
- package/dist/src/connection/simulator/Simulator.d.ts +1 -3
- package/dist/src/connection/simulator/Simulator.js +1 -4
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +1 -2
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.d.ts +3 -3
- package/dist/src/connection/simulator/SimulatorConnector.js +28 -21
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.d.ts +1 -6
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +3 -20
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +11 -6
- package/dist/src/connection/webrtc/WebrtcConnector.js +57 -42
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +8 -10
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +21 -44
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js +8 -2
- package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +3 -3
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +8 -8
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/NodeWebsocketClientConnection.js +1 -1
- package/dist/src/connection/websocket/NodeWebsocketClientConnection.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketClientConnector.d.ts +26 -0
- package/dist/src/connection/websocket/WebsocketClientConnector.js +86 -0
- package/dist/src/connection/websocket/WebsocketClientConnector.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.d.ts +19 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js +23 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.d.ts +5 -0
- package/dist/src/connection/websocket/{WebsocketConnectorRpcRemote.js → WebsocketClientConnectorRpcRemote.js} +4 -4
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketServer.d.ts +8 -5
- package/dist/src/connection/websocket/WebsocketServer.js +11 -11
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/connection/websocket/{WebsocketConnector.d.ts → WebsocketServerConnector.d.ts} +16 -21
- package/dist/src/connection/websocket/{WebsocketConnector.js → WebsocketServerConnector.js} +112 -160
- package/dist/src/connection/websocket/WebsocketServerConnector.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +4 -4
- package/dist/src/dht/DhtNode.js +85 -84
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +3 -3
- package/dist/src/dht/DhtNodeRpcLocal.js +9 -9
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +3 -3
- package/dist/src/dht/ExternalApiRpcLocal.js +5 -5
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.js +2 -2
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +4 -4
- package/dist/src/dht/PeerManager.js +22 -22
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +3 -3
- package/dist/src/dht/contact/SortedContactList.js +9 -9
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +3 -3
- package/dist/src/dht/discovery/DiscoverySession.js +21 -21
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +3 -3
- package/dist/src/dht/discovery/PeerDiscovery.js +46 -44
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/discovery/RingDiscoverySession.d.ts +3 -3
- package/dist/src/dht/discovery/RingDiscoverySession.js +19 -19
- package/dist/src/dht/discovery/RingDiscoverySession.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +33 -33
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js +8 -8
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +4 -4
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +24 -24
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +4 -4
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +5 -5
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +3 -3
- package/dist/src/dht/routing/Router.js +20 -20
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +3 -3
- package/dist/src/dht/routing/RouterRpcLocal.js +16 -16
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -3
- package/dist/src/dht/routing/RoutingSession.js +24 -24
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +3 -3
- package/dist/src/dht/store/StoreManager.js +25 -25
- package/dist/src/dht/store/StoreManager.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.d.ts +3 -3
- package/dist/src/dht/store/StoreRpcLocal.js +12 -12
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/exports.d.ts +3 -0
- package/dist/src/exports.js +5 -1
- package/dist/src/exports.js.map +1 -1
- package/dist/src/proto/google/protobuf/any.d.ts +5 -8
- package/dist/src/proto/google/protobuf/any.js.map +1 -1
- package/dist/src/proto/google/protobuf/empty.d.ts +1 -0
- package/dist/src/proto/google/protobuf/empty.js.map +1 -1
- package/dist/src/proto/google/protobuf/timestamp.d.ts +1 -10
- package/dist/src/proto/google/protobuf/timestamp.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +4 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +8 -8
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +3 -3
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +4 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +2 -2
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.d.ts +2 -2
- package/dist/src/transport/ListeningRpcCommunicator.js +2 -2
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -2
- package/dist/src/transport/RoutingRpcCommunicator.js +2 -2
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/package.json +7 -7
- package/protos/DhtRpc.proto +1 -1
- package/src/connection/ConnectionLockRpcLocal.ts +9 -9
- package/src/connection/ConnectionLockRpcRemote.ts +1 -1
- package/src/connection/ConnectionManager.ts +153 -111
- package/src/connection/ConnectorFacade.ts +84 -61
- package/src/connection/Handshaker.ts +131 -27
- package/src/connection/ManagedConnection.ts +41 -304
- package/src/connection/OutputBuffer.ts +28 -0
- package/src/connection/PendingConnection.ts +68 -0
- package/src/connection/connectivityChecker.ts +2 -2
- package/src/connection/connectivityRequestHandler.ts +1 -1
- package/src/connection/simulator/Simulator.ts +1 -5
- package/src/connection/simulator/SimulatorConnection.ts +1 -2
- package/src/connection/simulator/SimulatorConnector.ts +34 -33
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -6
- package/src/connection/webrtc/NodeWebrtcConnection.ts +3 -24
- package/src/connection/webrtc/WebrtcConnector.ts +73 -62
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +26 -56
- package/src/connection/websocket/AbstractWebsocketClientConnection.ts +8 -2
- package/src/connection/websocket/AutoCertifierClientFacade.ts +11 -11
- package/src/connection/websocket/NodeWebsocketClientConnection.ts +1 -1
- package/src/connection/websocket/WebsocketClientConnector.ts +119 -0
- package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +39 -0
- package/src/connection/websocket/{WebsocketConnectorRpcRemote.ts → WebsocketClientConnectorRpcRemote.ts} +2 -2
- package/src/connection/websocket/WebsocketServer.ts +18 -14
- package/src/connection/websocket/{WebsocketConnector.ts → WebsocketServerConnector.ts} +128 -205
- package/src/dht/DhtNode.ts +90 -89
- package/src/dht/DhtNodeRpcLocal.ts +11 -11
- package/src/dht/ExternalApiRpcLocal.ts +6 -6
- package/src/dht/ExternalApiRpcRemote.ts +2 -2
- package/src/dht/PeerManager.ts +24 -24
- package/src/dht/contact/SortedContactList.ts +10 -10
- package/src/dht/discovery/DiscoverySession.ts +24 -24
- package/src/dht/discovery/PeerDiscovery.ts +47 -45
- package/src/dht/discovery/RingDiscoverySession.ts +23 -23
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +36 -36
- package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +9 -9
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +25 -25
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +7 -7
- package/src/dht/routing/Router.ts +21 -21
- package/src/dht/routing/RouterRpcLocal.ts +17 -17
- package/src/dht/routing/RoutingSession.ts +26 -26
- package/src/dht/store/StoreManager.ts +27 -27
- package/src/dht/store/StoreRpcLocal.ts +13 -13
- package/src/exports.ts +3 -0
- package/src/proto/google/protobuf/any.ts +6 -9
- package/src/proto/google/protobuf/empty.ts +2 -1
- package/src/proto/google/protobuf/timestamp.ts +2 -11
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +9 -9
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +3 -3
- package/src/proto/packages/dht/protos/DhtRpc.ts +4 -4
- package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +1 -1
- package/src/transport/ListeningRpcCommunicator.ts +3 -3
- package/src/transport/RoutingRpcCommunicator.ts +3 -3
- package/test/end-to-end/Layer0Webrtc.test.ts +0 -10
- package/test/integration/ConnectionManager.test.ts +3 -2
- package/test/integration/GeoIpConnectivityChecking.test.ts +1 -1
- package/test/integration/SimultaneousConnections.test.ts +2 -2
- package/test/integration/WebrtcConnectionManagement.test.ts +2 -10
- package/test/integration/{WebsocketConnectorRpc.test.ts → WebsocketClientConnectorRpc.test.ts} +9 -9
- package/test/integration/WebsocketConnectionManagement.test.ts +11 -29
- package/test/unit/ConnectionManager.test.ts +64 -0
- package/test/unit/DiscoverySession.test.ts +1 -1
- package/test/unit/Handshaker.test.ts +169 -0
- package/test/unit/ManagedConnection.test.ts +58 -0
- package/test/unit/PendingConnection.test.ts +57 -0
- package/test/unit/WebrtcConnector.test.ts +56 -0
- package/test/unit/{WebsocketConnector.test.ts → WebsocketClientConnector.test.ts} +56 -11
- package/test/unit/WebsocketServerConnector.test.ts +102 -0
- package/test/utils/FakeConnectorFacade.ts +41 -0
- package/test/utils/mock/MockConnection.ts +26 -0
- package/test/utils/utils.ts +2 -2
- package/dist/src/connection/IConnectionSource.d.ts +0 -4
- package/dist/src/connection/IConnectionSource.js +0 -3
- package/dist/src/connection/IConnectionSource.js.map +0 -1
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.d.ts +0 -7
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.js +0 -20
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnector.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +0 -19
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +0 -23
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +0 -5
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +0 -1
- package/src/connection/IConnectionSource.ts +0 -6
- package/src/connection/webrtc/ManagedWebrtcConnection.ts +0 -27
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +0 -39
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ConnectionID, IConnection } from './IConnection'
|
|
2
2
|
import * as Err from '../helpers/errors'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { Logger, runAndRaceEvents3, RunAndRaceEventsReturnType } from '@streamr/utils'
|
|
3
|
+
import { PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
|
|
4
|
+
import { Logger } from '@streamr/utils'
|
|
6
5
|
import EventEmitter from 'eventemitter3'
|
|
7
6
|
import { getNodeIdOrUnknownFromPeerDescriptor } from './ConnectionManager'
|
|
8
7
|
import { DhtAddress, getNodeIdFromPeerDescriptor } from '../identifiers'
|
|
@@ -10,342 +9,80 @@ import { createRandomConnectionId } from './Connection'
|
|
|
10
9
|
|
|
11
10
|
export interface ManagedConnectionEvents {
|
|
12
11
|
managedData: (bytes: Uint8Array, remotePeerDescriptor: PeerDescriptor) => void
|
|
13
|
-
|
|
14
|
-
handshakeCompleted: (peerDescriptor: PeerDescriptor) => void
|
|
15
|
-
handshakeFailed: () => void
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
interface OutputBufferEvents {
|
|
19
|
-
bufferSent: () => void
|
|
20
|
-
bufferSendingFailed: () => void
|
|
12
|
+
disconnected: (gracefulLeave: boolean) => void
|
|
21
13
|
}
|
|
22
14
|
|
|
23
15
|
const logger = new Logger(module)
|
|
24
16
|
|
|
25
|
-
export type Events = ManagedConnectionEvents
|
|
17
|
+
export type Events = ManagedConnectionEvents
|
|
26
18
|
|
|
27
|
-
|
|
19
|
+
// ManagedConnection is a component used as a wrapper for IConnection after they have been successfully handshaked.
|
|
20
|
+
// Should only be used in the ConnectionManager.
|
|
21
|
+
export class ManagedConnection extends EventEmitter<ManagedConnectionEvents> {
|
|
28
22
|
|
|
29
|
-
private
|
|
30
|
-
private outputBufferEmitter = new EventEmitter<OutputBufferEvents>()
|
|
31
|
-
private outputBuffer: Uint8Array[] = []
|
|
32
|
-
private inputBuffer: Uint8Array[] = []
|
|
23
|
+
private connection: IConnection
|
|
33
24
|
public connectionId: ConnectionID
|
|
34
|
-
private remotePeerDescriptor
|
|
35
|
-
public connectionType: ConnectionType
|
|
36
|
-
private handshaker?: Handshaker
|
|
37
|
-
private handshakeCompleted = false
|
|
25
|
+
private remotePeerDescriptor: PeerDescriptor
|
|
38
26
|
private lastUsedTimestamp: number = Date.now()
|
|
27
|
+
private replacedAsDuplicate = false
|
|
39
28
|
private stopped = false
|
|
40
|
-
private bufferSentbyOtherConnection = false
|
|
41
|
-
private closing = false
|
|
42
|
-
public replacedByOtherConnection = false
|
|
43
|
-
private localPeerDescriptor: PeerDescriptor
|
|
44
|
-
protected outgoingConnection?: IConnection
|
|
45
|
-
protected incomingConnection?: IConnection
|
|
46
|
-
// TODO: Temporary debug variable, should be removed in the future.
|
|
47
|
-
private created = Date.now()
|
|
48
29
|
|
|
49
|
-
constructor(
|
|
50
|
-
localPeerDescriptor: PeerDescriptor,
|
|
51
|
-
connectionType: ConnectionType,
|
|
52
|
-
outgoingConnection?: IConnection,
|
|
53
|
-
incomingConnection?: IConnection,
|
|
54
|
-
targetPeerDescriptor?: PeerDescriptor
|
|
55
|
-
) {
|
|
30
|
+
constructor(peerDescriptor: PeerDescriptor, connection: IConnection) {
|
|
56
31
|
super()
|
|
57
|
-
|
|
58
|
-
this.localPeerDescriptor = localPeerDescriptor
|
|
59
|
-
this.outgoingConnection = outgoingConnection
|
|
60
|
-
this.incomingConnection = incomingConnection
|
|
61
|
-
this.connectionType = connectionType
|
|
62
32
|
this.connectionId = createRandomConnectionId()
|
|
33
|
+
this.connection = connection
|
|
63
34
|
|
|
64
|
-
|
|
65
|
-
this.onDisconnected = this.onDisconnected.bind(this)
|
|
66
|
-
|
|
67
|
-
logger.trace('creating ManagedConnection of type: ' + connectionType)
|
|
68
|
-
if (incomingConnection && outgoingConnection) {
|
|
69
|
-
throw new Err.IllegalArguments('Managed connection constructor only accepts either an incoming connection OR a outgoing connection')
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (outgoingConnection) {
|
|
73
|
-
this.handshaker = new Handshaker(this.localPeerDescriptor, outgoingConnection)
|
|
74
|
-
|
|
75
|
-
this.handshaker.once('handshakeFailed', (error) => {
|
|
76
|
-
if (error === HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR || error === HandshakeError.UNSUPPORTED_VERSION) {
|
|
77
|
-
// TODO should we have some handling for this floating promise?
|
|
78
|
-
this.close(false)
|
|
79
|
-
} else {
|
|
80
|
-
logger.trace(getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor) + ' handshakeFailed: ' + error)
|
|
81
|
-
this.emit('handshakeFailed')
|
|
82
|
-
}
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
this.handshaker.on('handshakeCompleted', (peerDescriptor: PeerDescriptor) => {
|
|
86
|
-
logger.trace('handshake completed for outgoing connection '
|
|
87
|
-
+ ', ' + getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor)
|
|
88
|
-
+ ' outputBuffer.length: ' + this.outputBuffer.length)
|
|
89
|
-
this.attachImplementation(outgoingConnection)
|
|
90
|
-
this.onHandshakeCompleted(peerDescriptor)
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
outgoingConnection.once('connected', () => {
|
|
94
|
-
this.handshaker!.sendHandshakeRequest(targetPeerDescriptor)
|
|
95
|
-
this.emit('connected')
|
|
96
|
-
})
|
|
97
|
-
outgoingConnection.once('disconnected', this.onDisconnected)
|
|
98
|
-
|
|
99
|
-
} else if (incomingConnection) {
|
|
100
|
-
this.handshaker = new Handshaker(this.localPeerDescriptor, incomingConnection)
|
|
101
|
-
this.handshaker.on('handshakeRequest', (
|
|
102
|
-
sourcePeerDescriptor: PeerDescriptor,
|
|
103
|
-
version: string,
|
|
104
|
-
targetPeerDescriptor?: PeerDescriptor
|
|
105
|
-
) => {
|
|
106
|
-
this.setRemotePeerDescriptor(sourcePeerDescriptor)
|
|
107
|
-
this.emit('handshakeRequest', sourcePeerDescriptor, version, targetPeerDescriptor)
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
incomingConnection.on('disconnected', this.onDisconnected)
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// eventemitter3 does not implement the standard 'newListener' event, so we need to override
|
|
115
|
-
|
|
116
|
-
override on(
|
|
117
|
-
event: keyof Events,
|
|
118
|
-
fn: (...args: any) => void,
|
|
119
|
-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
120
|
-
context?: any
|
|
121
|
-
): this {
|
|
122
|
-
if (event === 'managedData' && this.listenerCount('managedData') === 0) {
|
|
123
|
-
while (this.inputBuffer.length > 0) {
|
|
124
|
-
logger.trace('emptying inputBuffer')
|
|
125
|
-
const data = this.inputBuffer.shift()!
|
|
126
|
-
fn(data, this.getPeerDescriptor())
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
super.on(event, fn, context)
|
|
130
|
-
return this
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
override once(
|
|
134
|
-
event: keyof Events,
|
|
135
|
-
fn: (...args: any) => void,
|
|
136
|
-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
137
|
-
context?: any
|
|
138
|
-
): this {
|
|
139
|
-
if (event === 'managedData' && this.listenerCount('managedData') === 0) {
|
|
140
|
-
if (this.inputBuffer.length > 0) {
|
|
141
|
-
while (this.inputBuffer.length > 0) {
|
|
142
|
-
logger.trace('emptying inputBuffer')
|
|
143
|
-
const data = this.inputBuffer.shift()!
|
|
144
|
-
fn(data, this.getPeerDescriptor())
|
|
145
|
-
}
|
|
146
|
-
} else {
|
|
147
|
-
super.once(event, fn, context)
|
|
148
|
-
}
|
|
149
|
-
} else {
|
|
150
|
-
super.once(event, fn, context)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return this
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
public getNodeId(): DhtAddress {
|
|
157
|
-
return getNodeIdFromPeerDescriptor(this.remotePeerDescriptor!)
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
public getLastUsedTimestamp(): number {
|
|
161
|
-
return this.lastUsedTimestamp
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
public setRemotePeerDescriptor(peerDescriptor: PeerDescriptor): void {
|
|
165
|
-
this.remotePeerDescriptor = peerDescriptor
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
public getPeerDescriptor(): PeerDescriptor | undefined {
|
|
169
|
-
return this.remotePeerDescriptor
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
private onHandshakeCompleted(peerDescriptor: PeerDescriptor) {
|
|
173
|
-
this.lastUsedTimestamp = Date.now()
|
|
174
|
-
|
|
175
|
-
this.setRemotePeerDescriptor(peerDescriptor)
|
|
176
|
-
this.handshakeCompleted = true
|
|
177
|
-
this.handshaker!.stop()
|
|
178
|
-
|
|
179
|
-
while (this.outputBuffer.length > 0) {
|
|
180
|
-
logger.trace('emptying outputBuffer')
|
|
181
|
-
this.implementation!.send(this.outputBuffer.shift()!)
|
|
182
|
-
}
|
|
183
|
-
this.outputBufferEmitter.emit('bufferSent')
|
|
184
|
-
logger.trace('emitting handshake_completed')
|
|
185
|
-
this.emit('handshakeCompleted', peerDescriptor)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
public attachImplementation(impl: IConnection): void {
|
|
189
|
-
logger.trace('attachImplementation()')
|
|
190
|
-
this.implementation = impl
|
|
191
|
-
|
|
192
|
-
impl.on('data', (bytes: Uint8Array) => {
|
|
35
|
+
connection.on('data', (bytes: Uint8Array) => {
|
|
193
36
|
this.lastUsedTimestamp = Date.now()
|
|
194
|
-
|
|
195
|
-
this.inputBuffer.push(bytes)
|
|
196
|
-
} else {
|
|
197
|
-
this.emit('managedData', bytes, this.getPeerDescriptor()!)
|
|
198
|
-
}
|
|
37
|
+
this.emit('managedData', bytes, this.getPeerDescriptor()!)
|
|
199
38
|
})
|
|
39
|
+
connection.on('disconnected', (gracefulLeave) => this.onDisconnected(gracefulLeave))
|
|
200
40
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
})
|
|
204
|
-
impl.on('connected', () => {
|
|
205
|
-
this.lastUsedTimestamp = Date.now()
|
|
206
|
-
logger.trace('connected emitted')
|
|
207
|
-
this.emit('connected')
|
|
208
|
-
})
|
|
209
|
-
|
|
210
|
-
//ensure that we have subscribed to the event only once
|
|
211
|
-
impl.off('disconnected', this.onDisconnected)
|
|
212
|
-
impl.on('disconnected', this.onDisconnected)
|
|
41
|
+
this.lastUsedTimestamp = Date.now()
|
|
42
|
+
this.remotePeerDescriptor = peerDescriptor
|
|
213
43
|
}
|
|
214
44
|
|
|
215
45
|
private onDisconnected(gracefulLeave: boolean): void {
|
|
216
46
|
logger.trace(getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor) + ' onDisconnected() ' + gracefulLeave)
|
|
217
|
-
if (this.
|
|
218
|
-
|
|
47
|
+
if (!this.replacedAsDuplicate) {
|
|
48
|
+
this.emit('disconnected', gracefulLeave)
|
|
219
49
|
}
|
|
220
|
-
this.
|
|
221
|
-
|
|
50
|
+
this.removeAllListeners()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// TODO: Can this be removed if ManagedConnections can never be duplicates?
|
|
54
|
+
// Handle duplicates in the ConncetorFacade and no longer have PendingConnections in ConnectionManager
|
|
55
|
+
replaceAsDuplicate(): void {
|
|
56
|
+
logger.trace(getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor) + ' replaceAsDuplicate')
|
|
57
|
+
this.replacedAsDuplicate = true
|
|
222
58
|
}
|
|
223
59
|
|
|
224
|
-
|
|
60
|
+
send(data: Uint8Array): void {
|
|
225
61
|
if (this.stopped) {
|
|
226
62
|
throw new Err.SendFailed('ManagedConnection is stopped')
|
|
227
63
|
}
|
|
228
|
-
if (this.closing) {
|
|
229
|
-
throw new Err.SendFailed('ManagedConnection is closing')
|
|
230
|
-
}
|
|
231
|
-
this.lastUsedTimestamp = Date.now()
|
|
232
|
-
|
|
233
|
-
if (!connect && !this.implementation) {
|
|
234
|
-
throw new Err.ConnectionNotOpen('Connection not open when calling send() with connect flag as false')
|
|
235
|
-
} else if (this.implementation) {
|
|
236
|
-
this.implementation.send(data)
|
|
237
|
-
} else {
|
|
238
|
-
logger.trace('adding data to outputBuffer')
|
|
239
|
-
|
|
240
|
-
let result: RunAndRaceEventsReturnType<OutputBufferEvents>
|
|
241
|
-
|
|
242
|
-
try {
|
|
243
|
-
result = await runAndRaceEvents3<OutputBufferEvents>([() => { this.outputBuffer.push(data) }],
|
|
244
|
-
this.outputBufferEmitter, ['bufferSent', 'bufferSendingFailed'], 15000) // TODO use config option or named constant?
|
|
245
|
-
} catch (e) {
|
|
246
|
-
logger.debug(`Connection to ${getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor)} timed out`, {
|
|
247
|
-
peerDescriptor: this.remotePeerDescriptor,
|
|
248
|
-
type: this.connectionType,
|
|
249
|
-
lifetime: Date.now() - this.created
|
|
250
|
-
})
|
|
251
|
-
await this.close(false)
|
|
252
|
-
throw new Err.SendFailed('Sending buffer timed out')
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if (result.winnerName === 'bufferSendingFailed') {
|
|
256
|
-
throw new Err.SendFailed('Sending buffer failed')
|
|
257
|
-
}
|
|
258
|
-
// buffer was sent successfully, return normally
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
public sendNoWait(data: Uint8Array): void {
|
|
263
64
|
this.lastUsedTimestamp = Date.now()
|
|
264
|
-
|
|
265
|
-
this.implementation.send(data)
|
|
266
|
-
} else {
|
|
267
|
-
logger.trace('adding data to outputBuffer')
|
|
268
|
-
this.outputBuffer.push(data)
|
|
269
|
-
}
|
|
65
|
+
this.connection.send(data)
|
|
270
66
|
}
|
|
271
67
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
if (this.handshaker) {
|
|
275
|
-
this.handshaker.removeAllListeners()
|
|
276
|
-
}
|
|
277
|
-
logger.trace('bufferSentByOtherConnection reported')
|
|
278
|
-
this.bufferSentbyOtherConnection = true
|
|
279
|
-
this.outputBufferEmitter.emit('bufferSent')
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
public acceptHandshake(): void {
|
|
283
|
-
// This happens when connectionRequest has been made and answered
|
|
284
|
-
if (this.implementation) {
|
|
285
|
-
if (!this.handshaker) {
|
|
286
|
-
this.handshaker = new Handshaker(this.localPeerDescriptor, this.implementation)
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
this.handshaker.sendHandshakeResponse()
|
|
290
|
-
|
|
291
|
-
} else { // This happens to when there is a regular incoming connection
|
|
292
|
-
this.handshaker!.sendHandshakeResponse()
|
|
293
|
-
this.attachImplementation(this.incomingConnection!)
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
this.onHandshakeCompleted(this.remotePeerDescriptor!)
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
public rejectHandshake(error: HandshakeError): void {
|
|
300
|
-
this.handshaker!.sendHandshakeResponse(error)
|
|
301
|
-
this.destroy()
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
public async close(gracefulLeave: boolean): Promise<void> {
|
|
305
|
-
if (this.closing) {
|
|
68
|
+
async close(gracefulLeave: boolean): Promise<void> {
|
|
69
|
+
if (this.stopped) {
|
|
306
70
|
return
|
|
307
71
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
311
|
-
this.closing = true
|
|
312
|
-
|
|
313
|
-
this.outputBufferEmitter.emit('bufferSendingFailed')
|
|
314
|
-
|
|
315
|
-
if (this.implementation) {
|
|
316
|
-
await this.implementation?.close(gracefulLeave)
|
|
317
|
-
} else if (this.outgoingConnection) {
|
|
318
|
-
await this.outgoingConnection?.close(gracefulLeave)
|
|
319
|
-
} else if (this.incomingConnection) {
|
|
320
|
-
await this.incomingConnection?.close(gracefulLeave)
|
|
321
|
-
} else {
|
|
322
|
-
this.emit('disconnected', gracefulLeave)
|
|
323
|
-
}
|
|
72
|
+
await this.connection.close(gracefulLeave)
|
|
73
|
+
this.removeAllListeners()
|
|
324
74
|
}
|
|
325
75
|
|
|
326
|
-
|
|
327
|
-
this.
|
|
328
|
-
if (!this.stopped) {
|
|
329
|
-
this.stopped = true
|
|
330
|
-
|
|
331
|
-
this.removeAllListeners()
|
|
332
|
-
if (this.implementation) {
|
|
333
|
-
this.implementation?.destroy()
|
|
334
|
-
} else if (this.outgoingConnection) {
|
|
335
|
-
this.outgoingConnection?.destroy()
|
|
336
|
-
} else if (this.incomingConnection) {
|
|
337
|
-
this.incomingConnection?.destroy()
|
|
338
|
-
}
|
|
339
|
-
}
|
|
76
|
+
getNodeId(): DhtAddress {
|
|
77
|
+
return getNodeIdFromPeerDescriptor(this.remotePeerDescriptor)
|
|
340
78
|
}
|
|
341
79
|
|
|
342
|
-
|
|
343
|
-
return this.
|
|
80
|
+
getLastUsedTimestamp(): number {
|
|
81
|
+
return this.lastUsedTimestamp
|
|
344
82
|
}
|
|
345
83
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
this.outputBuffer = []
|
|
349
|
-
return ret
|
|
84
|
+
getPeerDescriptor(): PeerDescriptor | undefined {
|
|
85
|
+
return this.remotePeerDescriptor
|
|
350
86
|
}
|
|
87
|
+
|
|
351
88
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Defer } from '@streamr/utils'
|
|
2
|
+
import { SendFailed } from '../helpers/errors'
|
|
3
|
+
|
|
4
|
+
export class OutputBuffer {
|
|
5
|
+
|
|
6
|
+
private readonly buffer: Uint8Array[] = []
|
|
7
|
+
private readonly deferredPromise: Defer<void> = new Defer<void>()
|
|
8
|
+
|
|
9
|
+
push(message: Uint8Array): Defer<void> {
|
|
10
|
+
this.buffer.push(message)
|
|
11
|
+
return this.deferredPromise
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
getBuffer(): Uint8Array[] {
|
|
15
|
+
return this.buffer
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
resolve(): void {
|
|
19
|
+
this.buffer.length = 0
|
|
20
|
+
this.deferredPromise.resolve()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
reject(): void {
|
|
24
|
+
this.buffer.length = 0
|
|
25
|
+
this.deferredPromise.reject(new SendFailed('Could not send buffered messages'))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import EventEmitter from 'eventemitter3'
|
|
2
|
+
import { PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
|
|
3
|
+
import { Logger, setAbortableTimeout } from '@streamr/utils'
|
|
4
|
+
import { getNodeIdOrUnknownFromPeerDescriptor } from './ConnectionManager'
|
|
5
|
+
import { IConnection } from './IConnection'
|
|
6
|
+
|
|
7
|
+
export interface PendingConnectionEvents {
|
|
8
|
+
connected: (peerDescriptor: PeerDescriptor, connection: IConnection) => void
|
|
9
|
+
disconnected: (gracefulLeave: boolean) => void
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const logger = new Logger(module)
|
|
13
|
+
|
|
14
|
+
// PendingConnection is used as a reference to a connection that should be opened and handshaked to a given PeerDescriptor
|
|
15
|
+
// It does not hold a connection internally. The public method onHandshakedCompleted should be called once a connection for the
|
|
16
|
+
// remotePeerDescriptor is opened and handshaked successfully.
|
|
17
|
+
// PendingConnections are created by the Connectors.
|
|
18
|
+
export class PendingConnection extends EventEmitter<PendingConnectionEvents> {
|
|
19
|
+
|
|
20
|
+
private readonly connectingAbortController: AbortController = new AbortController()
|
|
21
|
+
private remotePeerDescriptor: PeerDescriptor
|
|
22
|
+
private replacedAsDuplicate: boolean = false
|
|
23
|
+
private stopped: boolean = false
|
|
24
|
+
|
|
25
|
+
constructor(remotePeerDescriptor: PeerDescriptor, timeout = 15 * 1000) {
|
|
26
|
+
super()
|
|
27
|
+
this.remotePeerDescriptor = remotePeerDescriptor
|
|
28
|
+
setAbortableTimeout(() => {
|
|
29
|
+
this.close(false)
|
|
30
|
+
}, timeout, this.connectingAbortController.signal)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
replaceAsDuplicate(): void {
|
|
34
|
+
logger.trace(getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor) + ' replaceAsDuplicate')
|
|
35
|
+
this.replacedAsDuplicate = true
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
onHandshakeCompleted(connection: IConnection): void {
|
|
39
|
+
if (!this.replacedAsDuplicate) {
|
|
40
|
+
this.emit('connected', this.remotePeerDescriptor, connection)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
close(graceful: boolean): void {
|
|
45
|
+
if (this.stopped) {
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
this.stopped = true
|
|
49
|
+
this.connectingAbortController.abort()
|
|
50
|
+
if (!this.replacedAsDuplicate) {
|
|
51
|
+
this.emit('disconnected', graceful)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
destroy(): void {
|
|
56
|
+
if (this.stopped) {
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
this.stopped = true
|
|
60
|
+
this.connectingAbortController.abort()
|
|
61
|
+
this.removeAllListeners()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
getPeerDescriptor(): PeerDescriptor {
|
|
65
|
+
return this.remotePeerDescriptor
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
}
|
|
@@ -7,12 +7,12 @@ import {
|
|
|
7
7
|
} from '../proto/packages/dht/protos/DhtRpc'
|
|
8
8
|
import { ConnectionEvents, IConnection } from './IConnection'
|
|
9
9
|
import { WebsocketClientConnection } from './websocket/NodeWebsocketClientConnection'
|
|
10
|
-
import { connectivityMethodToWebsocketUrl } from './websocket/
|
|
10
|
+
import { connectivityMethodToWebsocketUrl } from './websocket/WebsocketClientConnector'
|
|
11
11
|
import { isMaybeSupportedVersion } from '../helpers/version'
|
|
12
12
|
|
|
13
13
|
const logger = new Logger(module)
|
|
14
14
|
|
|
15
|
-
// TODO use
|
|
15
|
+
// TODO use options option or named constant?
|
|
16
16
|
export const connectAsync = async ({ url, allowSelfSignedCertificate, timeoutMs = 1000 }:
|
|
17
17
|
{ url: string, allowSelfSignedCertificate: boolean, timeoutMs?: number }
|
|
18
18
|
): Promise<IConnection> => {
|
|
@@ -9,7 +9,7 @@ import { NatType } from './ConnectionManager'
|
|
|
9
9
|
import { CONNECTIVITY_CHECKER_SERVICE_ID, connectAsync } from './connectivityChecker'
|
|
10
10
|
import { IConnection } from './IConnection'
|
|
11
11
|
import { WebsocketServerConnection } from './websocket/WebsocketServerConnection'
|
|
12
|
-
import { connectivityMethodToWebsocketUrl } from './websocket/
|
|
12
|
+
import { connectivityMethodToWebsocketUrl } from './websocket/WebsocketClientConnector'
|
|
13
13
|
import { LOCAL_PROTOCOL_VERSION } from '../helpers/version'
|
|
14
14
|
import { GeoIpLocator } from '@streamr/geoip-location'
|
|
15
15
|
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/parameter-properties */
|
|
2
|
-
import EventEmitter from 'eventemitter3'
|
|
3
2
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
4
|
-
import { ConnectionSourceEvents } from '../IConnectionSource'
|
|
5
3
|
import { SimulatorConnector } from './SimulatorConnector'
|
|
6
4
|
import { SimulatorConnection } from './SimulatorConnection'
|
|
7
5
|
import { ConnectionID } from '../IConnection'
|
|
@@ -89,7 +87,7 @@ class SendOperation extends SimulatorOperation {
|
|
|
89
87
|
class CloseOperation extends SimulatorOperation {
|
|
90
88
|
}
|
|
91
89
|
|
|
92
|
-
export class Simulator
|
|
90
|
+
export class Simulator {
|
|
93
91
|
private stopped = false
|
|
94
92
|
private connectors: Map<DhtAddress, SimulatorConnector> = new Map()
|
|
95
93
|
private latencyTable?: Array<Array<number>>
|
|
@@ -112,8 +110,6 @@ export class Simulator extends EventEmitter<ConnectionSourceEvents> {
|
|
|
112
110
|
private simulatorTimeout?: NodeJS.Timeout
|
|
113
111
|
|
|
114
112
|
constructor(latencyType: LatencyType = LatencyType.NONE, fixedLatency?: number) {
|
|
115
|
-
super()
|
|
116
|
-
|
|
117
113
|
this.latencyType = latencyType
|
|
118
114
|
this.fixedLatency = fixedLatency
|
|
119
115
|
|
|
@@ -95,8 +95,7 @@ export class SimulatorConnection extends Connection implements IConnection {
|
|
|
95
95
|
|
|
96
96
|
public handleIncomingData(data: Uint8Array): void {
|
|
97
97
|
if (!this.stopped) {
|
|
98
|
-
logger.trace('handleIncomingData()')
|
|
99
|
-
logger.trace(protoToString(Message.fromBinary(data), Message))
|
|
98
|
+
logger.trace('handleIncomingData() ' + protoToString(Message.fromBinary(data), Message))
|
|
100
99
|
this.emit('data', data)
|
|
101
100
|
} else {
|
|
102
101
|
logger.trace('tried to call handleIncomingData() a stopped connection')
|
|
@@ -5,32 +5,33 @@ import {
|
|
|
5
5
|
PeerDescriptor,
|
|
6
6
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
7
7
|
import { Logger } from '@streamr/utils'
|
|
8
|
-
import { ManagedConnection } from '../ManagedConnection'
|
|
9
8
|
import { Simulator } from './Simulator'
|
|
10
9
|
import { SimulatorConnection } from './SimulatorConnection'
|
|
11
10
|
import { DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
11
|
+
import { acceptHandshake, createIncomingHandshaker, createOutgoingHandshaker, rejectHandshake } from '../Handshaker'
|
|
12
|
+
import { PendingConnection } from '../PendingConnection'
|
|
12
13
|
|
|
13
14
|
const logger = new Logger(module)
|
|
14
15
|
|
|
15
16
|
export class SimulatorConnector {
|
|
16
17
|
|
|
17
|
-
private connectingConnections: Map<DhtAddress,
|
|
18
|
+
private connectingConnections: Map<DhtAddress, PendingConnection> = new Map()
|
|
18
19
|
private stopped = false
|
|
19
20
|
private localPeerDescriptor: PeerDescriptor
|
|
20
21
|
private simulator: Simulator
|
|
21
|
-
private onNewConnection: (connection:
|
|
22
|
+
private onNewConnection: (connection: PendingConnection) => boolean
|
|
22
23
|
|
|
23
24
|
constructor(
|
|
24
25
|
localPeerDescriptor: PeerDescriptor,
|
|
25
26
|
simulator: Simulator,
|
|
26
|
-
onNewConnection: (connection:
|
|
27
|
+
onNewConnection: (connection: PendingConnection) => boolean,
|
|
27
28
|
) {
|
|
28
29
|
this.localPeerDescriptor = localPeerDescriptor
|
|
29
30
|
this.simulator = simulator
|
|
30
31
|
this.onNewConnection = onNewConnection
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
public connect(targetPeerDescriptor: PeerDescriptor):
|
|
34
|
+
public connect(targetPeerDescriptor: PeerDescriptor): PendingConnection {
|
|
34
35
|
logger.trace('connect() ' + getNodeIdFromPeerDescriptor(targetPeerDescriptor))
|
|
35
36
|
const nodeId = getNodeIdFromPeerDescriptor(targetPeerDescriptor)
|
|
36
37
|
const existingConnection = this.connectingConnections.get(nodeId)
|
|
@@ -40,20 +41,21 @@ export class SimulatorConnector {
|
|
|
40
41
|
|
|
41
42
|
const connection = new SimulatorConnection(this.localPeerDescriptor, targetPeerDescriptor, ConnectionType.SIMULATOR_CLIENT, this.simulator)
|
|
42
43
|
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
connection.once('disconnected', () => {
|
|
48
|
-
this.connectingConnections.delete(nodeId)
|
|
49
|
-
})
|
|
50
|
-
connection.once('connected', () => {
|
|
44
|
+
const pendingConnection = new PendingConnection(targetPeerDescriptor)
|
|
45
|
+
createOutgoingHandshaker(this.localPeerDescriptor, pendingConnection, connection, targetPeerDescriptor)
|
|
46
|
+
this.connectingConnections.set(nodeId, pendingConnection)
|
|
47
|
+
const delFunc = () => {
|
|
51
48
|
this.connectingConnections.delete(nodeId)
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
connection.off('disconnected', delFunc)
|
|
50
|
+
pendingConnection.off('connected', delFunc)
|
|
51
|
+
pendingConnection.off('disconnected', delFunc)
|
|
52
|
+
}
|
|
53
|
+
connection.once('disconnected', delFunc)
|
|
54
|
+
pendingConnection.once('connected', delFunc)
|
|
55
|
+
pendingConnection.once('disconnected', delFunc)
|
|
56
|
+
|
|
54
57
|
connection.connect()
|
|
55
|
-
|
|
56
|
-
return managedConnection
|
|
58
|
+
return pendingConnection
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
public getPeerDescriptor(): PeerDescriptor {
|
|
@@ -61,26 +63,27 @@ export class SimulatorConnector {
|
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
public handleIncomingConnection(sourceConnection: SimulatorConnection): void {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
// connection is incoming, so remotePeerDescriptor is localPeerDescriptor
|
|
67
|
+
const remotePeerDescriptor = sourceConnection.localPeerDescriptor
|
|
68
|
+
const remoteNodeId = getNodeIdFromPeerDescriptor(sourceConnection.localPeerDescriptor)
|
|
69
|
+
logger.trace(remoteNodeId + ' incoming connection, stopped: ' + this.stopped)
|
|
66
70
|
if (this.stopped) {
|
|
67
71
|
return
|
|
68
72
|
}
|
|
69
|
-
const connection = new SimulatorConnection(this.localPeerDescriptor,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
73
|
+
const connection = new SimulatorConnection(this.localPeerDescriptor, remotePeerDescriptor, ConnectionType.SIMULATOR_SERVER, this.simulator)
|
|
74
|
+
|
|
75
|
+
const pendingConnection = new PendingConnection(remotePeerDescriptor)
|
|
76
|
+
const handshaker = createIncomingHandshaker(this.localPeerDescriptor, pendingConnection, connection)
|
|
74
77
|
logger.trace('connected')
|
|
75
78
|
|
|
76
|
-
|
|
77
|
-
logger.trace(
|
|
79
|
+
handshaker.once('handshakeRequest', () => {
|
|
80
|
+
logger.trace(remoteNodeId + ' incoming handshake request')
|
|
78
81
|
|
|
79
|
-
if (this.onNewConnection(
|
|
80
|
-
logger.trace(
|
|
81
|
-
|
|
82
|
+
if (this.onNewConnection(pendingConnection)) {
|
|
83
|
+
logger.trace(remoteNodeId + ' calling acceptHandshake')
|
|
84
|
+
acceptHandshake(handshaker, pendingConnection, connection)
|
|
82
85
|
} else {
|
|
83
|
-
|
|
86
|
+
rejectHandshake(pendingConnection, connection, handshaker, HandshakeError.DUPLICATE_CONNECTION)
|
|
84
87
|
}
|
|
85
88
|
})
|
|
86
89
|
|
|
@@ -90,8 +93,6 @@ export class SimulatorConnector {
|
|
|
90
93
|
public async stop(): Promise<void> {
|
|
91
94
|
this.stopped = true
|
|
92
95
|
const conns = Array.from(this.connectingConnections.values())
|
|
93
|
-
await Promise.allSettled(conns.map((conn) =>
|
|
94
|
-
conn.close(false)
|
|
95
|
-
))
|
|
96
|
+
await Promise.allSettled(conns.map(async (conn) => conn.close(false)))
|
|
96
97
|
}
|
|
97
98
|
}
|