@streamr/dht 100.0.0-pretestnet.3 → 100.0.0-pretestnet.6
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/src/connection/ConnectionLockRpcLocal.js +1 -1
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.js +3 -3
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +4 -8
- package/dist/src/connection/ConnectionManager.js +30 -31
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectivityChecker.d.ts +0 -4
- package/dist/src/connection/ConnectivityChecker.js +13 -18
- package/dist/src/connection/ConnectivityChecker.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +6 -3
- package/dist/src/connection/ConnectorFacade.js +33 -23
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +6 -6
- package/dist/src/connection/Handshaker.js +10 -13
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +7 -10
- package/dist/src/connection/ManagedConnection.js +43 -79
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/ManagedWebrtcConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +16 -15
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.d.ts +2 -2
- package/dist/src/connection/simulator/SimulatorConnector.js +10 -11
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +8 -9
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js +6 -6
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +4 -4
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/ClientWebsocket.js +3 -0
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/ServerWebsocket.js +2 -0
- package/dist/src/connection/websocket/ServerWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +5 -5
- package/dist/src/connection/websocket/WebsocketConnector.js +37 -35
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
- package/dist/src/dht/DhtNode.js +18 -15
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.js +2 -2
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.js +4 -4
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.js +1 -1
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.js +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/find/FindRpcLocal.js +2 -2
- package/dist/src/dht/find/FindRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/FindRpcRemote.js +3 -3
- package/dist/src/dht/routing/FindRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/Router.js +3 -3
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.js +4 -4
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcRemote.js +6 -6
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.js +1 -1
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.js +1 -1
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.js +3 -3
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +1 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +7 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +21 -12
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +20 -7
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/package.json +5 -5
- package/protos/DhtRpc.proto +9 -5
- package/src/connection/ConnectionLockRpcLocal.ts +2 -2
- package/src/connection/ConnectionLockRpcRemote.ts +4 -5
- package/src/connection/ConnectionManager.ts +30 -36
- package/src/connection/ConnectivityChecker.ts +13 -15
- package/src/connection/ConnectorFacade.ts +36 -25
- package/src/connection/Handshaker.ts +15 -18
- package/src/connection/ManagedConnection.ts +63 -87
- package/src/connection/ManagedWebrtcConnection.ts +4 -2
- package/src/connection/simulator/SimulatorConnection.ts +17 -16
- package/src/connection/simulator/SimulatorConnector.ts +12 -13
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +2 -2
- package/src/connection/webrtc/NodeWebrtcConnection.ts +9 -10
- package/src/connection/webrtc/WebrtcConnector.ts +7 -6
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +5 -5
- package/src/connection/websocket/ClientWebsocket.ts +3 -0
- package/src/connection/websocket/ServerWebsocket.ts +2 -0
- package/src/connection/websocket/WebsocketConnector.ts +63 -39
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +2 -2
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +2 -2
- package/src/dht/DhtNode.ts +24 -16
- package/src/dht/DhtNodeRpcLocal.ts +3 -3
- package/src/dht/DhtNodeRpcRemote.ts +5 -5
- package/src/dht/discovery/DiscoverySession.ts +2 -2
- package/src/dht/discovery/PeerDiscovery.ts +2 -2
- package/src/dht/find/FindRpcLocal.ts +3 -3
- package/src/dht/routing/FindRpcRemote.ts +4 -4
- package/src/dht/routing/Router.ts +10 -5
- package/src/dht/routing/RouterRpcLocal.ts +5 -5
- package/src/dht/routing/RouterRpcRemote.ts +7 -8
- package/src/dht/routing/RoutingSession.ts +2 -2
- package/src/dht/store/StoreRpcLocal.ts +2 -2
- package/src/dht/store/StoreRpcRemote.ts +4 -4
- package/src/helpers/peerIdFromPeerDescriptor.ts +6 -0
- package/src/proto/packages/dht/protos/DhtRpc.ts +25 -17
- package/test/benchmark/Find.test.ts +2 -2
- package/test/benchmark/KademliaCorrectness.test.ts +2 -2
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +4 -3
- package/test/integration/ConnectionLocking.test.ts +7 -4
- package/test/integration/ConnectionManager.test.ts +72 -4
- package/test/integration/Find.test.ts +3 -6
- package/test/integration/MigrateData.test.ts +4 -4
- package/test/integration/MultipleEntryPointJoining.test.ts +2 -2
- package/test/integration/ScaleDownDht.test.ts +4 -4
- package/test/integration/Store.test.ts +3 -6
- package/test/integration/StoreAndDelete.test.ts +3 -6
- package/test/integration/WebrtcConnectionManagement.test.ts +1 -1
- package/test/utils/utils.ts +8 -2
|
@@ -1,45 +1,55 @@
|
|
|
1
1
|
import { ConnectionEvents, ConnectionID, ConnectionType, IConnection } from './IConnection'
|
|
2
2
|
import * as Err from '../helpers/errors'
|
|
3
3
|
import { Handshaker } from './Handshaker'
|
|
4
|
-
import { PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
|
|
5
|
-
import { Logger,
|
|
4
|
+
import { HandshakeError, PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
|
|
5
|
+
import { Logger, runAndRaceEvents3, RunAndRaceEventsReturnType } from '@streamr/utils'
|
|
6
6
|
import EventEmitter from 'eventemitter3'
|
|
7
7
|
import { PeerIDKey } from '../helpers/PeerID'
|
|
8
8
|
import { keyFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
9
|
-
import {
|
|
9
|
+
import { getNodeIdOrUnknownFromPeerDescriptor } from './ConnectionManager'
|
|
10
10
|
|
|
11
11
|
export interface ManagedConnectionEvents {
|
|
12
12
|
managedData: (bytes: Uint8Array, remotePeerDescriptor: PeerDescriptor) => void
|
|
13
|
-
handshakeRequest: (
|
|
13
|
+
handshakeRequest: (source: PeerDescriptor, target?: PeerDescriptor) => void
|
|
14
14
|
handshakeCompleted: (peerDescriptor: PeerDescriptor) => void
|
|
15
15
|
handshakeFailed: () => void
|
|
16
16
|
bufferSentByOtherConnection: () => void
|
|
17
17
|
closing: () => void
|
|
18
|
-
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface OutpuBufferEvents {
|
|
21
|
+
bufferSent: () => void
|
|
22
|
+
bufferSendingFailed: () => void
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface OutpuBufferEvents {
|
|
26
|
+
bufferSent: () => void
|
|
27
|
+
bufferSendingFailed: () => void
|
|
19
28
|
}
|
|
20
29
|
|
|
21
30
|
const logger = new Logger(module)
|
|
22
31
|
|
|
23
32
|
export type Events = ManagedConnectionEvents & ConnectionEvents
|
|
33
|
+
|
|
24
34
|
export class ManagedConnection extends EventEmitter<Events> {
|
|
25
35
|
|
|
26
36
|
private implementation?: IConnection
|
|
27
37
|
|
|
38
|
+
private outputBufferEmitter = new EventEmitter<OutpuBufferEvents>()
|
|
28
39
|
private outputBuffer: Uint8Array[] = []
|
|
40
|
+
|
|
29
41
|
private inputBuffer: Uint8Array[] = []
|
|
30
42
|
|
|
31
43
|
public connectionId: ConnectionID
|
|
32
|
-
private
|
|
44
|
+
private remotePeerDescriptor?: PeerDescriptor
|
|
33
45
|
public connectionType: ConnectionType
|
|
34
46
|
|
|
35
47
|
private handshaker?: Handshaker
|
|
36
48
|
private handshakeCompleted = false
|
|
37
|
-
private doNotEmitDisconnected = false
|
|
38
49
|
|
|
39
50
|
private lastUsed: number = Date.now()
|
|
40
51
|
private stopped = false
|
|
41
52
|
public offeredAsIncoming = false
|
|
42
|
-
public rejectedAsIncoming = false
|
|
43
53
|
private bufferSentbyOtherConnection = false
|
|
44
54
|
private closing = false
|
|
45
55
|
public replacedByOtherConnection = false
|
|
@@ -52,17 +62,17 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
52
62
|
connectionType: ConnectionType,
|
|
53
63
|
outgoingConnection?: IConnection,
|
|
54
64
|
incomingConnection?: IConnection,
|
|
65
|
+
targetPeerDescriptor?: PeerDescriptor
|
|
55
66
|
) {
|
|
56
67
|
super()
|
|
57
68
|
|
|
58
|
-
this.send = this.send.bind(this)
|
|
59
|
-
|
|
60
69
|
this.localPeerDescriptor = localPeerDescriptor
|
|
61
70
|
this.outgoingConnection = outgoingConnection
|
|
62
71
|
this.incomingConnection = incomingConnection
|
|
63
72
|
this.connectionType = connectionType
|
|
64
73
|
this.connectionId = new ConnectionID()
|
|
65
74
|
|
|
75
|
+
this.send = this.send.bind(this)
|
|
66
76
|
this.onDisconnected = this.onDisconnected.bind(this)
|
|
67
77
|
|
|
68
78
|
logger.trace('creating ManagedConnection of type: ' + connectionType)
|
|
@@ -73,21 +83,25 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
73
83
|
if (outgoingConnection) {
|
|
74
84
|
this.handshaker = new Handshaker(this.localPeerDescriptor, outgoingConnection)
|
|
75
85
|
|
|
76
|
-
this.handshaker.once('handshakeFailed', (
|
|
77
|
-
|
|
78
|
-
|
|
86
|
+
this.handshaker.once('handshakeFailed', (error) => {
|
|
87
|
+
if (error === HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR) {
|
|
88
|
+
this.close(false)
|
|
89
|
+
} else {
|
|
90
|
+
logger.trace(getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor) + ' handshakeFailed: ' + error)
|
|
91
|
+
this.emit('handshakeFailed')
|
|
92
|
+
}
|
|
79
93
|
})
|
|
80
94
|
|
|
81
95
|
this.handshaker.on('handshakeCompleted', (peerDescriptor: PeerDescriptor) => {
|
|
82
96
|
logger.trace('handshake completed for outgoing connection '
|
|
83
|
-
+ ', ' +
|
|
97
|
+
+ ', ' + getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor)
|
|
84
98
|
+ ' outputBuffer.length: ' + this.outputBuffer.length)
|
|
85
99
|
this.attachImplementation(outgoingConnection)
|
|
86
100
|
this.onHandshakeCompleted(peerDescriptor)
|
|
87
101
|
})
|
|
88
102
|
|
|
89
103
|
outgoingConnection.once('connected', () => {
|
|
90
|
-
this.handshaker!.sendHandshakeRequest()
|
|
104
|
+
this.handshaker!.sendHandshakeRequest(targetPeerDescriptor)
|
|
91
105
|
this.emit('connected')
|
|
92
106
|
})
|
|
93
107
|
outgoingConnection.once('disconnected', this.onDisconnected)
|
|
@@ -95,9 +109,9 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
95
109
|
} else {
|
|
96
110
|
if (incomingConnection) {
|
|
97
111
|
this.handshaker = new Handshaker(this.localPeerDescriptor, incomingConnection)
|
|
98
|
-
this.handshaker.on('handshakeRequest', (
|
|
99
|
-
this.
|
|
100
|
-
this.emit('handshakeRequest',
|
|
112
|
+
this.handshaker.on('handshakeRequest', (sourcePeerDescriptor: PeerDescriptor, targetPeerDescriptor?: PeerDescriptor) => {
|
|
113
|
+
this.setRemotePeerDescriptor(sourcePeerDescriptor)
|
|
114
|
+
this.emit('handshakeRequest', sourcePeerDescriptor, targetPeerDescriptor)
|
|
101
115
|
})
|
|
102
116
|
|
|
103
117
|
incomingConnection.on('disconnected', this.onDisconnected)
|
|
@@ -149,32 +163,32 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
149
163
|
}
|
|
150
164
|
|
|
151
165
|
public get peerIdKey(): PeerIDKey {
|
|
152
|
-
return keyFromPeerDescriptor(this.
|
|
166
|
+
return keyFromPeerDescriptor(this.remotePeerDescriptor!)
|
|
153
167
|
}
|
|
154
168
|
|
|
155
169
|
public getLastUsed(): number {
|
|
156
170
|
return this.lastUsed
|
|
157
171
|
}
|
|
158
172
|
|
|
159
|
-
public
|
|
160
|
-
this.
|
|
173
|
+
public setRemotePeerDescriptor(peerDescriptor: PeerDescriptor): void {
|
|
174
|
+
this.remotePeerDescriptor = peerDescriptor
|
|
161
175
|
}
|
|
162
176
|
|
|
163
177
|
public getPeerDescriptor(): PeerDescriptor | undefined {
|
|
164
|
-
return this.
|
|
178
|
+
return this.remotePeerDescriptor
|
|
165
179
|
}
|
|
166
180
|
|
|
167
181
|
private onHandshakeCompleted(peerDescriptor: PeerDescriptor) {
|
|
168
182
|
this.lastUsed = Date.now()
|
|
169
183
|
|
|
170
|
-
this.
|
|
184
|
+
this.setRemotePeerDescriptor(peerDescriptor)
|
|
171
185
|
this.handshakeCompleted = true
|
|
172
186
|
|
|
173
187
|
while (this.outputBuffer.length > 0) {
|
|
174
188
|
logger.trace('emptying outputBuffer')
|
|
175
189
|
this.implementation!.send(this.outputBuffer.shift()!)
|
|
176
190
|
}
|
|
177
|
-
|
|
191
|
+
this.outputBufferEmitter.emit('bufferSent')
|
|
178
192
|
logger.trace('emitting handshake_completed')
|
|
179
193
|
this.emit('handshakeCompleted', peerDescriptor)
|
|
180
194
|
}
|
|
@@ -186,7 +200,6 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
186
200
|
impl.on('data', (bytes: Uint8Array) => {
|
|
187
201
|
this.lastUsed = Date.now()
|
|
188
202
|
if (this.listenerCount('managedData') === 0) {
|
|
189
|
-
|
|
190
203
|
this.inputBuffer.push(bytes)
|
|
191
204
|
} else {
|
|
192
205
|
this.emit('managedData', bytes, this.getPeerDescriptor()!)
|
|
@@ -202,25 +215,26 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
202
215
|
this.emit('connected')
|
|
203
216
|
})
|
|
204
217
|
|
|
218
|
+
//ensure that we have subscribed to the event only once
|
|
205
219
|
impl.off('disconnected', this.onDisconnected)
|
|
206
220
|
impl.on('disconnected', this.onDisconnected)
|
|
207
221
|
}
|
|
208
222
|
|
|
209
223
|
private onDisconnected(gracefulLeave: boolean): void {
|
|
210
|
-
logger.trace(
|
|
224
|
+
logger.trace(getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor) + ' onDisconnected() ' + gracefulLeave)
|
|
211
225
|
if (this.bufferSentbyOtherConnection) {
|
|
212
226
|
return
|
|
213
227
|
}
|
|
214
|
-
this.emit('
|
|
215
|
-
this.
|
|
228
|
+
this.outputBufferEmitter.emit('bufferSendingFailed')
|
|
229
|
+
this.emit('disconnected', gracefulLeave)
|
|
216
230
|
}
|
|
217
231
|
|
|
218
232
|
async send(data: Uint8Array, doNotConnect = false): Promise<void> {
|
|
219
233
|
if (this.stopped) {
|
|
220
|
-
|
|
234
|
+
throw new Err.SendFailed('ManagedConnection is stopped')
|
|
221
235
|
}
|
|
222
236
|
if (this.closing) {
|
|
223
|
-
|
|
237
|
+
throw new Err.SendFailed('ManagedConnection is closing')
|
|
224
238
|
}
|
|
225
239
|
this.lastUsed = Date.now()
|
|
226
240
|
|
|
@@ -231,52 +245,20 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
231
245
|
} else {
|
|
232
246
|
logger.trace('adding data to outputBuffer')
|
|
233
247
|
|
|
234
|
-
let result: RunAndRaceEventsReturnType<
|
|
235
|
-
|
|
236
|
-
this.doNotEmitDisconnected = true
|
|
248
|
+
let result: RunAndRaceEventsReturnType<OutpuBufferEvents>
|
|
237
249
|
|
|
238
250
|
try {
|
|
239
|
-
result = await runAndRaceEvents3<
|
|
240
|
-
|
|
251
|
+
result = await runAndRaceEvents3<OutpuBufferEvents>([() => { this.outputBuffer.push(data) }],
|
|
252
|
+
this.outputBufferEmitter, ['bufferSent', 'bufferSendingFailed'], 15000)
|
|
241
253
|
} catch (e) {
|
|
242
|
-
logger.debug(`Connection to ${
|
|
243
|
-
throw
|
|
254
|
+
logger.debug(`Connection to ${getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor)} timed out`)
|
|
255
|
+
throw new Err.SendFailed('Sending buffer timed out')
|
|
244
256
|
}
|
|
245
257
|
|
|
246
|
-
if (result.winnerName === '
|
|
247
|
-
|
|
248
|
-
this.doDisconnect(false)
|
|
249
|
-
} else if (result.winnerName === 'handshakeFailed') {
|
|
250
|
-
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor) + ' handshakeFailed received')
|
|
251
|
-
|
|
252
|
-
if (this.bufferSentbyOtherConnection) {
|
|
253
|
-
logger.trace('bufferSentByOtherConnection already true')
|
|
254
|
-
this.doNotEmitDisconnected = false
|
|
255
|
-
this.doDisconnect(false)
|
|
256
|
-
} else {
|
|
257
|
-
let result2: RunAndRaceEventsReturnType<Events>
|
|
258
|
-
|
|
259
|
-
try {
|
|
260
|
-
result2 = await raceEvents3<Events>(this,
|
|
261
|
-
['bufferSentByOtherConnection', 'closing', 'disconnected'], 15000)
|
|
262
|
-
} catch (ex) {
|
|
263
|
-
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor)
|
|
264
|
-
+ ' Exception from raceEvents3 while waiting bufferSentByOtherConnection or closing ' + ex)
|
|
265
|
-
throw ex
|
|
266
|
-
}
|
|
267
|
-
if (result2.winnerName === 'bufferSentByOtherConnection') {
|
|
268
|
-
logger.trace('bufferSentByOtherConnection received')
|
|
269
|
-
this.doNotEmitDisconnected = false
|
|
270
|
-
this.doDisconnect(false)
|
|
271
|
-
} else if (result2.winnerName === 'closing') {
|
|
272
|
-
logger.trace('bufferSentByOtherConnection not received, instead received a closing event')
|
|
273
|
-
} else if (result2.winnerName === 'disconnected') {
|
|
274
|
-
logger.trace('disconnected while in raceEvents3')
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
} else {
|
|
278
|
-
this.doNotEmitDisconnected = false
|
|
258
|
+
if (result.winnerName === 'bufferSendingFailed') {
|
|
259
|
+
throw new Err.SendFailed('Sending buffer failed')
|
|
279
260
|
}
|
|
261
|
+
// buffer was sent successfully, return normally
|
|
280
262
|
}
|
|
281
263
|
}
|
|
282
264
|
|
|
@@ -291,12 +273,13 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
291
273
|
}
|
|
292
274
|
|
|
293
275
|
public reportBufferSentByOtherConnection(): void {
|
|
294
|
-
logger.trace(
|
|
276
|
+
logger.trace(getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor) + ' reportBufferSentByOtherConnection')
|
|
295
277
|
if (this.handshaker) {
|
|
296
278
|
this.handshaker.removeAllListeners()
|
|
297
279
|
}
|
|
298
280
|
logger.trace('bufferSentByOtherConnection reported')
|
|
299
281
|
this.bufferSentbyOtherConnection = true
|
|
282
|
+
this.outputBufferEmitter.emit('bufferSent')
|
|
300
283
|
this.emit('bufferSentByOtherConnection')
|
|
301
284
|
}
|
|
302
285
|
|
|
@@ -314,22 +297,12 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
314
297
|
this.attachImplementation(this.incomingConnection!)
|
|
315
298
|
}
|
|
316
299
|
|
|
317
|
-
this.onHandshakeCompleted(this.
|
|
300
|
+
this.onHandshakeCompleted(this.remotePeerDescriptor!)
|
|
318
301
|
}
|
|
319
302
|
|
|
320
|
-
public rejectHandshake(
|
|
321
|
-
this.handshaker!.sendHandshakeResponse(
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
private doDisconnect(gracefulLeave: boolean) {
|
|
325
|
-
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor) + ' doDisconnect() emitting')
|
|
326
|
-
|
|
327
|
-
if (!this.doNotEmitDisconnected) {
|
|
328
|
-
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor) + ' emitting disconnected')
|
|
329
|
-
this.emit('disconnected', gracefulLeave)
|
|
330
|
-
} else {
|
|
331
|
-
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor) + ' not emitting disconnected because doNotEmitDisconnected flag is set')
|
|
332
|
-
}
|
|
303
|
+
public rejectHandshake(error: HandshakeError): void {
|
|
304
|
+
this.handshaker!.sendHandshakeResponse(error)
|
|
305
|
+
this.destroy()
|
|
333
306
|
}
|
|
334
307
|
|
|
335
308
|
public async close(gracefulLeave: boolean): Promise<void> {
|
|
@@ -337,8 +310,10 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
337
310
|
logger.trace('close() called on replaced connection')
|
|
338
311
|
}
|
|
339
312
|
this.closing = true
|
|
313
|
+
|
|
314
|
+
this.outputBufferEmitter.emit('bufferSendingFailed')
|
|
340
315
|
this.emit('closing')
|
|
341
|
-
|
|
316
|
+
|
|
342
317
|
if (this.implementation) {
|
|
343
318
|
await this.implementation?.close(gracefulLeave)
|
|
344
319
|
} else if (this.outgoingConnection) {
|
|
@@ -346,12 +321,13 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
346
321
|
} else if (this.incomingConnection) {
|
|
347
322
|
await this.incomingConnection?.close(gracefulLeave)
|
|
348
323
|
} else {
|
|
349
|
-
this.
|
|
324
|
+
this.emit('disconnected', gracefulLeave)
|
|
350
325
|
}
|
|
351
326
|
}
|
|
352
327
|
|
|
353
328
|
public destroy(): void {
|
|
354
329
|
this.closing = true
|
|
330
|
+
|
|
355
331
|
this.emit('closing')
|
|
356
332
|
if (!this.stopped) {
|
|
357
333
|
this.stopped = true
|
|
@@ -7,12 +7,14 @@ export class ManagedWebrtcConnection extends ManagedConnection {
|
|
|
7
7
|
|
|
8
8
|
constructor(localPeerDescriptor: PeerDescriptor,
|
|
9
9
|
connectingConnection?: NodeWebrtcConnection,
|
|
10
|
-
connectedConnection?: NodeWebrtcConnection
|
|
10
|
+
connectedConnection?: NodeWebrtcConnection
|
|
11
|
+
) {
|
|
11
12
|
super(
|
|
12
13
|
localPeerDescriptor,
|
|
13
14
|
ConnectionType.WEBRTC,
|
|
14
15
|
connectingConnection,
|
|
15
|
-
connectedConnection
|
|
16
|
+
connectedConnection
|
|
17
|
+
)
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
public getWebrtcConnection(): NodeWebrtcConnection {
|
|
@@ -4,7 +4,7 @@ import { Message, PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
|
4
4
|
import { Connection } from '../Connection'
|
|
5
5
|
import { Logger } from '@streamr/utils'
|
|
6
6
|
import { protoToString } from '../../helpers/protoToString'
|
|
7
|
-
import {
|
|
7
|
+
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
8
8
|
|
|
9
9
|
const logger = new Logger(module)
|
|
10
10
|
|
|
@@ -45,36 +45,37 @@ export class SimulatorConnection extends Connection implements IConnection {
|
|
|
45
45
|
this.simulator.send(this, data)
|
|
46
46
|
|
|
47
47
|
} else {
|
|
48
|
-
logger.error(
|
|
48
|
+
logger.error(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ', ' + getNodeIdFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
49
49
|
'tried to send() on a stopped connection')
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
public async close(gracefulLeave: boolean): Promise<void> {
|
|
54
|
-
logger.trace(
|
|
54
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor)
|
|
55
|
+
+ ', ' + getNodeIdFromPeerDescriptor(this.targetPeerDescriptor) + ' close()')
|
|
55
56
|
|
|
56
57
|
if (!this.stopped) {
|
|
57
|
-
logger.trace(
|
|
58
|
-
+
|
|
58
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ', '
|
|
59
|
+
+ getNodeIdFromPeerDescriptor(this.targetPeerDescriptor) + ' close() not stopped')
|
|
59
60
|
this.stopped = true
|
|
60
61
|
|
|
61
62
|
try {
|
|
62
|
-
logger.trace(
|
|
63
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ', ' + getNodeIdFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
63
64
|
' close() calling simulator.disconnect()')
|
|
64
65
|
this.simulator.close(this)
|
|
65
|
-
logger.trace(
|
|
66
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ', ' + getNodeIdFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
66
67
|
' close() simulator.disconnect returned')
|
|
67
68
|
} catch (e) {
|
|
68
|
-
logger.trace(
|
|
69
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ', ' + getNodeIdFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
69
70
|
'close aborted' + e)
|
|
70
71
|
} finally {
|
|
71
|
-
logger.trace(
|
|
72
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ', ' + getNodeIdFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
72
73
|
' calling this.doDisconnect')
|
|
73
74
|
this.doDisconnect(gracefulLeave)
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
} else {
|
|
77
|
-
logger.trace(
|
|
78
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ', ' + getNodeIdFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
78
79
|
' close() tried to close a stopped connection')
|
|
79
80
|
}
|
|
80
81
|
}
|
|
@@ -108,7 +109,7 @@ export class SimulatorConnection extends Connection implements IConnection {
|
|
|
108
109
|
|
|
109
110
|
public handleIncomingDisconnection(): void {
|
|
110
111
|
if (!this.stopped) {
|
|
111
|
-
logger.trace(
|
|
112
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ' handleIncomingDisconnection()')
|
|
112
113
|
this.stopped = true
|
|
113
114
|
this.doDisconnect(false)
|
|
114
115
|
} else {
|
|
@@ -118,20 +119,20 @@ export class SimulatorConnection extends Connection implements IConnection {
|
|
|
118
119
|
|
|
119
120
|
public destroy(): void {
|
|
120
121
|
if (!this.stopped) {
|
|
121
|
-
logger.trace(
|
|
122
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ' destroy()')
|
|
122
123
|
this.removeAllListeners()
|
|
123
124
|
this.close(false).catch((_e) => { })
|
|
124
125
|
} else {
|
|
125
|
-
logger.trace(
|
|
126
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ' tried to call destroy() a stopped connection')
|
|
126
127
|
}
|
|
127
128
|
}
|
|
128
129
|
|
|
129
130
|
private doDisconnect(gracefulLeave: boolean) {
|
|
130
|
-
logger.trace(
|
|
131
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ' doDisconnect()')
|
|
131
132
|
this.stopped = true
|
|
132
133
|
|
|
133
|
-
logger.trace(
|
|
134
|
-
+
|
|
134
|
+
logger.trace(getNodeIdFromPeerDescriptor(this.localPeerDescriptor) + ', '
|
|
135
|
+
+ getNodeIdFromPeerDescriptor(this.targetPeerDescriptor) + ' doDisconnect emitting')
|
|
135
136
|
|
|
136
137
|
this.emit('disconnected', gracefulLeave)
|
|
137
138
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ConnectionType } from '../IConnection'
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
+
HandshakeError,
|
|
4
5
|
PeerDescriptor,
|
|
5
6
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
6
7
|
import { Logger } from '@streamr/utils'
|
|
@@ -8,7 +9,7 @@ import { ManagedConnection } from '../ManagedConnection'
|
|
|
8
9
|
import { PeerIDKey } from '../../helpers/PeerID'
|
|
9
10
|
import { Simulator } from './Simulator'
|
|
10
11
|
import { SimulatorConnection } from './SimulatorConnection'
|
|
11
|
-
import { keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
12
|
+
import { getNodeIdFromPeerDescriptor, keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
12
13
|
|
|
13
14
|
const logger = new Logger(module)
|
|
14
15
|
|
|
@@ -18,20 +19,20 @@ export class SimulatorConnector {
|
|
|
18
19
|
private stopped = false
|
|
19
20
|
private localPeerDescriptor: PeerDescriptor
|
|
20
21
|
private simulator: Simulator
|
|
21
|
-
private
|
|
22
|
+
private onNewConnection: (connection: ManagedConnection) => boolean
|
|
22
23
|
|
|
23
24
|
constructor(
|
|
24
25
|
localPeerDescriptor: PeerDescriptor,
|
|
25
26
|
simulator: Simulator,
|
|
26
|
-
|
|
27
|
+
onNewConnection: (connection: ManagedConnection) => boolean
|
|
27
28
|
) {
|
|
28
29
|
this.localPeerDescriptor = localPeerDescriptor
|
|
29
30
|
this.simulator = simulator
|
|
30
|
-
this.
|
|
31
|
+
this.onNewConnection = onNewConnection
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
public connect(targetPeerDescriptor: PeerDescriptor): ManagedConnection {
|
|
34
|
-
logger.trace('connect() ' +
|
|
35
|
+
logger.trace('connect() ' + getNodeIdFromPeerDescriptor(targetPeerDescriptor))
|
|
35
36
|
const peerKey = keyFromPeerDescriptor(targetPeerDescriptor)
|
|
36
37
|
const existingConnection = this.connectingConnections.get(peerKey)
|
|
37
38
|
if (existingConnection) {
|
|
@@ -41,7 +42,7 @@ export class SimulatorConnector {
|
|
|
41
42
|
const connection = new SimulatorConnection(this.localPeerDescriptor, targetPeerDescriptor, ConnectionType.SIMULATOR_CLIENT, this.simulator)
|
|
42
43
|
|
|
43
44
|
const managedConnection = new ManagedConnection(this.localPeerDescriptor, ConnectionType.SIMULATOR_CLIENT, connection, undefined)
|
|
44
|
-
managedConnection.
|
|
45
|
+
managedConnection.setRemotePeerDescriptor(targetPeerDescriptor)
|
|
45
46
|
|
|
46
47
|
this.connectingConnections.set(peerKey, managedConnection)
|
|
47
48
|
connection.once('disconnected', () => {
|
|
@@ -61,7 +62,7 @@ export class SimulatorConnector {
|
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
public handleIncomingConnection(sourceConnection: SimulatorConnection): void {
|
|
64
|
-
logger.trace(
|
|
65
|
+
logger.trace(getNodeIdFromPeerDescriptor(sourceConnection.localPeerDescriptor) + ' incoming connection, stopped: ' + this.stopped)
|
|
65
66
|
if (this.stopped) {
|
|
66
67
|
return
|
|
67
68
|
}
|
|
@@ -73,15 +74,13 @@ export class SimulatorConnector {
|
|
|
73
74
|
logger.trace('connected')
|
|
74
75
|
|
|
75
76
|
managedConnection.once('handshakeRequest', () => {
|
|
76
|
-
logger.trace(
|
|
77
|
-
logger.trace('incoming handshake request')
|
|
77
|
+
logger.trace(getNodeIdFromPeerDescriptor(sourceConnection.localPeerDescriptor) + ' incoming handshake request')
|
|
78
78
|
|
|
79
|
-
if (this.
|
|
80
|
-
logger.trace(
|
|
79
|
+
if (this.onNewConnection(managedConnection)) {
|
|
80
|
+
logger.trace(getNodeIdFromPeerDescriptor(sourceConnection.localPeerDescriptor) + ' calling acceptHandshake')
|
|
81
81
|
managedConnection.acceptHandshake()
|
|
82
82
|
} else {
|
|
83
|
-
managedConnection.rejectHandshake(
|
|
84
|
-
managedConnection.destroy()
|
|
83
|
+
managedConnection.rejectHandshake(HandshakeError.DUPLICATE_CONNECTION)
|
|
85
84
|
}
|
|
86
85
|
})
|
|
87
86
|
|
|
@@ -2,7 +2,7 @@ import EventEmitter from 'eventemitter3'
|
|
|
2
2
|
import { WebrtcConnectionEvents, IWebrtcConnection, RtcDescription } from './IWebrtcConnection'
|
|
3
3
|
import { IConnection, ConnectionID, ConnectionEvents, ConnectionType } from '../IConnection'
|
|
4
4
|
import { Logger } from '@streamr/utils'
|
|
5
|
-
import { IceServer } from './
|
|
5
|
+
import { IceServer } from './WebrtcConnector'
|
|
6
6
|
|
|
7
7
|
const logger = new Logger(module)
|
|
8
8
|
|
|
@@ -20,7 +20,7 @@ interface Params {
|
|
|
20
20
|
|
|
21
21
|
export class NodeWebrtcConnection extends EventEmitter<Events> implements IWebrtcConnection, IConnection {
|
|
22
22
|
|
|
23
|
-
public
|
|
23
|
+
public connectionId: ConnectionID
|
|
24
24
|
public readonly connectionType: ConnectionType = ConnectionType.WEBRTC
|
|
25
25
|
|
|
26
26
|
// We need to keep track of connection state ourselves because
|
|
@@ -5,7 +5,7 @@ import EventEmitter from 'eventemitter3'
|
|
|
5
5
|
import nodeDatachannel, { DataChannel, DescriptionType, PeerConnection } from 'node-datachannel'
|
|
6
6
|
import { Logger } from '@streamr/utils'
|
|
7
7
|
import { IllegalRtcPeerConnectionState } from '../../helpers/errors'
|
|
8
|
-
import { keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
8
|
+
import { getNodeIdFromPeerDescriptor, keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
9
9
|
import { iceServerAsString } from './iceServerAsString'
|
|
10
10
|
import { IceServer } from './WebrtcConnector'
|
|
11
11
|
import { PortRange } from '../ConnectionManager'
|
|
@@ -80,9 +80,8 @@ export class NodeWebrtcConnection extends EventEmitter<Events> implements IConne
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
public start(isOffering: boolean): void {
|
|
83
|
-
logger.trace(`Staring new connection for peer: ${keyFromPeerDescriptor(this.remotePeerDescriptor)}`)
|
|
84
83
|
const peerIdKey = keyFromPeerDescriptor(this.remotePeerDescriptor)
|
|
85
|
-
logger.trace(`
|
|
84
|
+
logger.trace(`Starting new connection for peer ${getNodeIdFromPeerDescriptor(this.remotePeerDescriptor)}`, { isOffering })
|
|
86
85
|
this.connection = new PeerConnection(peerIdKey, {
|
|
87
86
|
iceServers: this.iceServers.map(iceServerAsString),
|
|
88
87
|
maxMessageSize: this.maxMessageSize,
|
|
@@ -115,11 +114,11 @@ export class NodeWebrtcConnection extends EventEmitter<Events> implements IConne
|
|
|
115
114
|
public async setRemoteDescription(description: string, type: string): Promise<void> {
|
|
116
115
|
if (this.connection) {
|
|
117
116
|
try {
|
|
118
|
-
logger.trace(`Setting remote descriptor for peer: ${
|
|
117
|
+
logger.trace(`Setting remote descriptor for peer: ${getNodeIdFromPeerDescriptor(this.remotePeerDescriptor)}`)
|
|
119
118
|
this.connection.setRemoteDescription(description, type as DescriptionType)
|
|
120
119
|
this.remoteDescriptionSet = true
|
|
121
120
|
} catch (err) {
|
|
122
|
-
logger.debug(`Failed to set remote descriptor for peer ${
|
|
121
|
+
logger.debug(`Failed to set remote descriptor for peer ${getNodeIdFromPeerDescriptor(this.remotePeerDescriptor)}`)
|
|
123
122
|
}
|
|
124
123
|
} else {
|
|
125
124
|
this.doClose(false, `Tried to set description for non-existent connection`)
|
|
@@ -130,10 +129,10 @@ export class NodeWebrtcConnection extends EventEmitter<Events> implements IConne
|
|
|
130
129
|
if (this.connection) {
|
|
131
130
|
if (this.remoteDescriptionSet) {
|
|
132
131
|
try {
|
|
133
|
-
logger.trace(`Setting remote candidate for peer: ${
|
|
132
|
+
logger.trace(`Setting remote candidate for peer: ${getNodeIdFromPeerDescriptor(this.remotePeerDescriptor)}`)
|
|
134
133
|
this.connection.addRemoteCandidate(candidate, mid)
|
|
135
134
|
} catch (err) {
|
|
136
|
-
logger.debug(`Failed to set remote candidate for peer ${
|
|
135
|
+
logger.debug(`Failed to set remote candidate for peer ${getNodeIdFromPeerDescriptor(this.remotePeerDescriptor)}`)
|
|
137
136
|
}
|
|
138
137
|
} else {
|
|
139
138
|
// TODO: should queue candidates until remote description is set?
|
|
@@ -149,7 +148,7 @@ export class NodeWebrtcConnection extends EventEmitter<Events> implements IConne
|
|
|
149
148
|
try {
|
|
150
149
|
this.dataChannel!.sendMessageBinary(data as Buffer)
|
|
151
150
|
} catch (err) {
|
|
152
|
-
logger.debug('Failed to send binary message to ' +
|
|
151
|
+
logger.debug('Failed to send binary message to ' + getNodeIdFromPeerDescriptor(this.remotePeerDescriptor) + err)
|
|
153
152
|
}
|
|
154
153
|
}
|
|
155
154
|
}
|
|
@@ -161,7 +160,7 @@ export class NodeWebrtcConnection extends EventEmitter<Events> implements IConne
|
|
|
161
160
|
private doClose(gracefulLeave: boolean, reason?: string): void {
|
|
162
161
|
if (!this.closed) {
|
|
163
162
|
logger.trace(
|
|
164
|
-
`Closing Node WebRTC Connection to ${
|
|
163
|
+
`Closing Node WebRTC Connection to ${getNodeIdFromPeerDescriptor(this.remotePeerDescriptor)}`
|
|
165
164
|
+ `${reason ? `, reason: ${reason}` : ''}`
|
|
166
165
|
)
|
|
167
166
|
|
|
@@ -232,7 +231,7 @@ export class NodeWebrtcConnection extends EventEmitter<Events> implements IConne
|
|
|
232
231
|
clearTimeout(this.connectingTimeoutRef)
|
|
233
232
|
}
|
|
234
233
|
this.dataChannel = dataChannel
|
|
235
|
-
logger.trace(`DataChannel opened for peer ${
|
|
234
|
+
logger.trace(`DataChannel opened for peer ${getNodeIdFromPeerDescriptor(this.remotePeerDescriptor)}`)
|
|
236
235
|
this.emit('connected')
|
|
237
236
|
}
|
|
238
237
|
|