/
inHub.js
135 lines (116 loc) · 3.27 KB
/
inHub.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Created with JetBrains WebStorm.
* User: kaven276
* Date: 13-5-16
* Time: 下午2:47
*/
console.log(process.execPath);
var net = require('net')
, C = require('./constant.js')
, debug = require('debug')('noradle:inhub')
, gseq = 0
;
// for accept front end request
// may accept from different front nodejs connection request
var server = net.createServer({allowHalfOpen : true}, function(c){
var seq = ++gseq;
debug('node(%d) connected', seq);
var freeSlotID = freeList.shift()
, marker = new Buffer(4)
;
if (!freeSlotID) {
// no available free slot, disconnect client
marker.writeInt32BE(197610262, 0);
c.end(marker);
c.destroy();
debug('node(%d) connected but no free', seq);
return;
}
var slot = oraPool[freeSlotID]
;
c.write(slot.head);
c.pipe(slot.oraSock, {end : false}).pipe(c);
debug('node(%d) use free slot %s, %j', seq, freeSlotID, freeList);
c.on('end', function(){
debug('node(%d) disconnected', seq);
// c.end();
if (oraPool[freeSlotID]) {
freeList.unshift(freeSlotID);
debug('node(%d) return back slot %s, %j', seq, freeSlotID, freeList);
}
});
});
var oraPool = new Array(1000)
, freeList = []
, connSeq = 0
;
// for oracle reverse connection
// check magic number only
var pool = net.createServer(function(c){
debug('oracle connected');
connSeq++;
var slotID;
c.on('readable', onHandshake);
var head, chunks = [], chunkSeq = 0;
function onHandshake(){
var data = c.read();
chunkSeq++;
if (data === null) {
debug('onHandshake(%d,%d): data === null', connSeq, chunkSeq);
return;
} else {
chunks.push(data);
}
if (chunks.length === 1) {
try {
var ptoken = data.readInt32BE(0);
} catch (e) {
ptoken = -1;
}
if (ptoken !== 197610261) {
console.warn('none oracle connection attempt found', data);
c.end();
c.destroy();
return;
}
}
if (data.length < 7) {
debug('onHandshake(%d,%d): data.length < 7', connSeq, chunkSeq);
debug(chunks, data);
return;
}
if (data.slice(-7).toString('ascii') !== '/080526') {
debug('onHandshake(%d,%d): data.slice(-7).toString("ascii"") !== "/080526"', connSeq, chunkSeq);
debug('partial oracle connect data', data, data.slice(36), data.slice(36).toString());
return;
}
head = Buffer.concat(chunks);
c.removeListener('readable', onHandshake);
slotID = head.readInt32BE(16);
oraPool[slotID] = {oraSock : c, head : head};
freeList.push(slotID);
debug('add new slot(%s), freeList=%j', slotID, freeList);
};
c.on('end', function(){
debug('oracle disconnected');
// find free list and remove from free list
delete oraPool[slotID];
var pos = freeList.indexOf(slotID);
if (pos >= 0) {
freeList.splice(pos, 1);
debug('removed slot(%s), freeList=%j', slotID, freeList);
} else {
debug('not found on end slotID(%s) in freeList(%j)', slotID, freeList);
}
});
});
exports.listenOracle = function(port){
pool.listen(port, function(){
debug('listening to oracle at port:%d', port);
});
};
exports.listenClient = function(port){
server.listen(port, function(){
debug('listening to client at port:%d ', port);
});
};