Skip to content

Commit

Permalink
feat(p2p): protocol check and support responce p2p connection
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Mar 2, 2018
1 parent 557776d commit b37bbb4
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/background/p2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class p2p {
this.send = send
this.tcpServer = net.createServer();
this.tcpServer.on('connection', (socket) => {
//console.log('p2p server connection', socket.remoteAddress)
// try to setup back connection
this.add(socket.address())

socket = new JsonSocket(socket);
socket.on('error', (err) => {})
socket.on('message', (message) => {
Expand All @@ -28,6 +30,15 @@ class p2p {
}
});
})
// check protocol
this.on('protocol', (data, callback) => {
if(!data || data.protocol != 'rats')
return

callback({
protocol: 'rats'
})
})
}

listen() {
Expand Down Expand Up @@ -64,13 +75,9 @@ class p2p {
connect(address)
{
this.peers.push(address)
const socket = new JsonSocket(new net.Socket()); //Decorate a standard net.Socket with JsonSocket
const rawSocket = new net.Socket();
const socket = new JsonSocket(rawSocket); //Decorate a standard net.Socket with JsonSocket
socket.on('connect', () => { //Don't send until we're connected
// add to peers
this.size++;
this.send('peer', this.size)
console.log('new peer', address)

const callbacks = {}
socket.on('message', (message) => {
if(message.id && callbacks[message.id])
Expand All @@ -90,7 +97,21 @@ class p2p {
data
});
}
address.emit = emit

// check protocol
const protocolTimeout = setTimeout(() => rawSocket.destroy(), 7000)
emit('protocol', {protocol: 'rats'}, (data) => {
if(!data || data.protocol != 'rats')
return

// success
clearTimeout(protocolTimeout)
// add to peers
address.emit = emit
this.size++;
this.send('peer', this.size)
console.log('new peer', address)
})
});

socket.on('close', () => {
Expand Down

0 comments on commit b37bbb4

Please sign in to comment.