Skip to content
This repository was archived by the owner on Aug 23, 2019. It is now read-only.

fix: correctly handle multiplex stream muxers #179

Merged
merged 1 commit into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@
"Sid Harder <[email protected]>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <[email protected]>"
]
}
}
59 changes: 30 additions & 29 deletions src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,44 +132,45 @@ module.exports = function dial (swarm) {
// - add the muxedConn to the list of muxedConns
// - add incomming new streams to connHandler

nextMuxer(muxers.shift())
const ms = new multistream.Dialer()
ms.handle(conn, (err) => {
if (err) {
return callback(new Error('multistream not supported'))
}

nextMuxer(muxers.shift())
})

function nextMuxer (key) {
const ms = new multistream.Dialer()
ms.handle(conn, (err) => {
log('selecting %s', key)
ms.select(key, (err, conn) => {
if (err) {
return callback(new Error('multistream not supported'))
}
log('selecting %s', key)
ms.select(key, (err, conn) => {
if (err) {
if (muxers.length === 0) {
cb(new Error('could not upgrade to stream muxing'))
} else {
nextMuxer(muxers.shift())
}
return
if (muxers.length === 0) {
cb(new Error('could not upgrade to stream muxing'))
} else {
nextMuxer(muxers.shift())
}
return
}

const muxedConn = swarm.muxers[key].dialer(conn)
swarm.muxedConns[b58Id] = {}
swarm.muxedConns[b58Id].muxer = muxedConn
// should not be needed anymore - swarm.muxedConns[b58Id].conn = conn

swarm.emit('peer-mux-established', pi)
const muxedConn = swarm.muxers[key].dialer(conn)
swarm.muxedConns[b58Id] = {}
swarm.muxedConns[b58Id].muxer = muxedConn
// should not be needed anymore - swarm.muxedConns[b58Id].conn = conn

muxedConn.once('close', () => {
delete swarm.muxedConns[pi.id.toB58String()]
swarm.emit('peer-mux-closed', pi)
})
swarm.emit('peer-mux-established', pi)

// For incoming streams, in case identify is on
muxedConn.on('stream', (conn) => {
protocolMuxer(swarm.protocols, conn)
})
muxedConn.once('close', () => {
delete swarm.muxedConns[pi.id.toB58String()]
swarm.emit('peer-mux-closed', pi)
})

cb(null, muxedConn)
// For incoming streams, in case identify is on
muxedConn.on('stream', (conn) => {
protocolMuxer(swarm.protocols, conn)
})

cb(null, muxedConn)
})
}
}
Expand Down