forked from wadey/node-thrift
/
connection.js
144 lines (122 loc) · 3.85 KB
/
connection.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
134
135
136
137
138
139
140
141
142
143
144
var sys = require('sys'),
EventEmitter = require("events").EventEmitter,
net = require('net'),
TMemoryBuffer = require('./transport').TMemoryBuffer,
TBinaryProtocol = require('./protocol').TBinaryProtocol;
var BinaryParser = require('./binary_parser').BinaryParser;
BinaryParser.bigEndian = true;
var int32FramedReceiver = exports.int32FramedReceiver = function (callback) {
var frameLeft = 0,
framePos = 0,
frame = null;
var residual = null;
return function(data) {
// Prepend any residual data from our previous read
if (residual) {
var dat = new Buffer(data.length + residual.length);
residual.copy(dat, 0, 0);
data.copy(dat, residual.length, 0);
residual = null;
}
// var buf = new Buffer(data, 'binary');
// console.log(buf);
// framed transport
while (data.length) {
if (frameLeft === 0) {
// TODO assumes we have all 4 bytes
if (data.length < 4) {
console.log("Expecting > 4 bytes, found only " + data.length);
residual = data;
break;
//throw Error("Expecting > 4 bytes, found only " + data.length);
}
frameLeft = BinaryParser.toInt(data.slice(0,4));
frame = new Buffer(frameLeft);
framePos = 0;
data = data.slice(4, data.length);
}
if (data.length >= frameLeft) {
data.copy(frame, framePos, 0, frameLeft);
data = data.slice(frameLeft, data.length);
frameLeft = 0;
callback(frame);
} else if (data.length) {
data.copy(frame, framePos, 0, data.length);
frameLeft -= data.length;
framePos += data.length;
data = data.slice(data.length, data.length);
}
}
};
};
var Connection = exports.Connection = function(stream, options) {
var self = this;
EventEmitter.call(this);
this.connection = stream;
this.offline_queue = [];
this.options = options || {};
this.connected = false;
this.connection.addListener("connect", function() {
self.connected = true;
this.setTimeout(self.options.timeout || 0);
this.setNoDelay();
this.frameLeft = 0;
this.framePos = 0;
this.frame = null;
self.offline_queue.forEach(function(data) {
self.connection.write(data);
});
self.emit("connect");
});
this.connection.addListener("error", function(err) {
self.emit("error", err);
});
// Add a close listener
this.connection.addListener("close", function() {
self.emit("close");
});
this.connection.addListener("timeout", function() {
self.emit("timeout");
});
this.connection.addListener("data", int32FramedReceiver(function(data) {
// console.log(typeof(data));
var input = new TBinaryProtocol(new TMemoryBuffer(data));
var r = input.readMessageBegin();
// console.log(r);
self.client['recv_' + r.fname](input, r.mtype, r.rseqid);
// self.emit("data", data);
}));
};
sys.inherits(Connection, EventEmitter);
exports.createConnection = function(host, port, options) {
var stream = net.createConnection(port, host);
var connection = new Connection(stream, options);
connection.host = host;
connection.port = port;
return connection;
}
exports.createClient = function(cls, connection) {
if (cls.Client) {
cls = cls.Client;
}
var client = new cls(new TMemoryBuffer(undefined, function(buf) {
connection.write(buf);
}), TBinaryProtocol);
// TODO clean this up
connection.client = client;
return client;
}
Connection.prototype.end = function() {
this.connection.end();
}
Connection.prototype.write = function(buf) {
// TODO: optimize this better, allocate one buffer instead of both:
var msg = new Buffer(buf.length + 4);
BinaryParser.fromInt(buf.length).copy(msg, 0, 0, 4);
buf.copy(msg, 4, 0, buf.length);
if (!this.connected) {
this.offline_queue.push(msg);
} else {
this.connection.write(msg);
}
}