-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpair.js
More file actions
61 lines (35 loc) · 1.01 KB
/
pair.js
File metadata and controls
61 lines (35 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require('./promise_emitter')
var net = require('net')
var fs = require('fs')
var q = require('q')
var debug = require('debug')('msgpack-socket:pair')
var util = require('util')
var co = require('co')
function mkTempPath(){
var uid = Math.floor(Math.random()*0x100000000).toString(36)
return util.format('/tmp/%s-%s.sock', process.pid, uid)
}
function *mkSocketPair(useTcp){
var S = new net.Server()
if(useTcp){
var sockpath = Math.floor((Math.random()+1)*32000)
} else {
var sockpath = mkTempPath()
}
console.log('sockpath', sockpath)
S.on('close', function(){
console.log('S.on.close', arguments)
})
console.log('S.listen(sockpath)')
S.listen(sockpath)
yield S.await('listening')
console.log('listening')
var C = new net.Socket()
C.connect(sockpath)
var conn = yield S.await('connection')
console.log('S.on.connection', conn._handle.fd)
S.close()
return [C, conn]
}
module.exports.mkSocketPair = mkSocketPair
module.exports.mkTempPath = mkTempPath