| 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359 |
1
1
1
1
1
1
1
1
1
162
162
162
162
162
162
162
1091
162
162
162
162
162
1
1
162
162
162
162
162
1
1952
1
1
162
162
1
1
1
162
162
2044
162
162
162
162
162
162
162
1
4
1
1
1
2031
1
778
1
742
1
36
1
4
4
4
4
1
1
1
2031
2031
2031
2031
1
2031
1
742
742
742
742
641
101
101
742
742
5
5
5
742
1
742
742
742
1
36
36
36
1
35
35
1
34
1
162
162
87
87
75
1
75
1
75
1
75
75
75
1
75
75
15
15
60
75
75
1
263
263
123
140
140
140
1
1
1
1
1
779
779
779
779
779
1
778
778
778
217
222
778
1
| // Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var assert = require('assert');
var bufrw = require('bufrw');
var ReadMachine = require('bufrw/stream/read_machine');
var inherits = require('util').inherits;
var v2 = require('./v2');
var errors = require('./errors');
var States = require('./reqres_states');
var TChannelConnectionBase = require('./connection_base');
function TChannelConnection(channel, socket, direction, remoteAddr) {
assert(remoteAddr !== channel.hostPort, 'refusing to create self connection');
var self = this;
TChannelConnectionBase.call(self, channel, direction, remoteAddr);
self.identifiedEvent = self.defineEvent('identified');
self.socket = socket;
var opts = {
logger: self.channel.logger,
random: self.channel.random,
timers: self.channel.timers,
hostPort: self.channel.hostPort,
tracer: self.tracer,
connection: self
};
// jshint forin:false
for (var prop in self.options) {
opts[prop] = self.options[prop];
}
// jshint forin:true
self.handler = new v2.Handler(opts);
self.mach = ReadMachine(bufrw.UInt16BE, v2.Frame.RW);
self.setupSocket();
self.setupHandler();
self.start();
}
inherits(TChannelConnection, TChannelConnectionBase);
TChannelConnection.prototype.setupSocket = function setupSocket() {
var self = this;
self.socket.setNoDelay(true);
self.socket.on('data', onSocketChunk);
self.socket.on('close', onSocketClose);
self.socket.on('error', onSocketError);
function onSocketChunk(chunk) {
self.mach.handleChunk(chunk, chunkHandled);
}
function chunkHandled(err) {
if (err) {
self.resetAll(errors.TChannelReadProtocolError(err, {
remoteName: self.remoteName,
localName: self.channel.hostPort
}));
self.socket.destroy();
}
}
function onSocketClose() {
self.resetAll(errors.SocketClosedError({reason: 'remote clossed'}));
Iif (self.remoteName === '0.0.0.0:0') {
self.channel.peers.delete(self.remoteAddr);
}
}
function onSocketError(err) {
self.onSocketError(err);
}
};
TChannelConnection.prototype.setupHandler = function setupHandler() {
var self = this;
self.handler.write = function write(buf, done) {
self.socket.write(buf, null, done);
};
self.mach.emit = handleReadFrame;
self.handler.writeErrorEvent.on(onWriteError);
self.handler.errorEvent.on(onHandlerError);
self.handler.callIncomingRequestEvent.on(onCallRequest);
self.handler.callIncomingResponseEvent.on(onCallResponse);
self.handler.callIncomingErrorEvent.on(onCallError);
self.timedOutEvent.on(onTimedOut);
// TODO: restore dumping from old:
// var stream = self.socket;
// if (dumpEnabled) {
// stream = stream.pipe(Spy(process.stdout, {
// prefix: '>>> ' + self.remoteAddr + ' '
// }));
// }
// stream = stream
// .pipe(self.reader)
// .pipe(self.handler)
// ;
// if (dumpEnabled) {
// stream = stream.pipe(Spy(process.stdout, {
// prefix: '<<< ' + self.remoteAddr + ' '
// }));
// }
// stream = stream
// .pipe(self.socket)
// ;
function onTimedOut(err) {
self.onTimedOut(err);
}
function onWriteError(err) {
self.onWriteError(err);
}
function onHandlerError(err) {
self.onHandlerError(err);
}
function handleReadFrame(frame) {
self.handleReadFrame(frame);
}
function onCallRequest(req) {
self.handleCallRequest(req);
}
function onCallResponse(res) {
self.onCallResponse(res);
}
function onCallError(err) {
self.onCallError(err);
}
};
TChannelConnection.prototype.onTimedOut = function onTimedOut(err) {
var self = this;
self.logger.warn('destroying socket from timeouts', {
hostPort: self.channel.hostPort
});
self.resetAll(err);
self.socket.destroy();
};
TChannelConnection.prototype.onWriteError = function onWriteError(err) {
var self = this;
self.resetAll(errors.TChannelWriteProtocolError(err, {
remoteName: self.remoteName,
localName: self.channel.hostPort
}));
self.socket.destroy();
};
TChannelConnection.prototype.onHandlerError = function onHandlerError(err) {
var self = this;
self.resetAll(err);
// resetAll() does not close the socket
self.socket.destroy();
};
TChannelConnection.prototype.handleReadFrame = function handleReadFrame(frame) {
var self = this;
Eif (!self.closing) {
self.lastTimeoutTime = 0;
}
self.handler.handleFrame(frame, handledFrame);
function handledFrame(err) {
Iif (err) self.onHandlerError(err);
}
};
TChannelConnection.prototype.onCallResponse = function onCallResponse(res) {
var self = this;
var called = false;
var req = self.ops.getOutReq(res.id);
if (res.state === States.Done || res.state === States.Error) {
popOutReq();
} else {
res.errorEvent.on(popOutReq);
res.finishEvent.on(popOutReq);
}
Iif (!req) {
return;
}
if (self.tracer) {
// TODO: better annotations
req.span.annotate('cr');
self.tracer.report(req.span);
res.span = req.span;
}
req.responseEvent.emit(req, res);
function popOutReq() {
Iif (called) {
return;
}
called = true;
self.ops.popOutReq(res.id, {
responseId: res.id,
code: res.code,
arg1: Buffer.isBuffer(res.arg1) ?
String(res.arg1) : 'streamed-arg1',
info: 'got call response for unknown id'
});
}
};
TChannelConnection.prototype.onCallError = function onCallError(err) {
var self = this;
var req = self.ops.getOutReq(err.originalId);
if (req && req.res) {
req.res.errorEvent.emit(req.res, err);
} else {
// Only popOutReq if there is no call response object yet
req = self.ops.popOutReq(err.originalId, {
err: err,
id: err.originalId,
info: 'got error frame for unknown id'
});
if (!req) {
return;
}
req.errorEvent.emit(req, err);
}
};
TChannelConnection.prototype.start = function start() {
var self = this;
if (self.direction === 'out') {
self.handler.sendInitRequest();
self.handler.initResponseEvent.on(onOutIdentified);
} else {
self.handler.initRequestEvent.on(onInIdentified);
}
function onOutIdentified(init) {
self.onOutIdentified(init);
}
function onInIdentified(init) {
self.onInIdentified(init);
}
};
TChannelConnection.prototype.onOutIdentified = function onOutIdentified(init) {
var self = this;
self.remoteName = init.hostPort;
self.identifiedEvent.emit(self, {
hostPort: init.hostPort,
processName: init.processName
});
};
TChannelConnection.prototype.onInIdentified = function onInIdentified(init) {
var self = this;
if (init.hostPort === '0.0.0.0:0') {
self.remoteName = '' + self.socket.remoteAddress + ':' + self.socket.remotePort;
assert(self.remoteName !== self.channel.hostPort,
'should not be able to receive ephemeral connection from self');
} else {
self.remoteName = init.hostPort;
}
self.channel.peers.add(self.remoteName).addConnection(self);
self.identifiedEvent.emit(self, {
hostPort: self.remoteName,
processName: init.processName
});
};
TChannelConnection.prototype.close = function close(callback) {
var self = this;
if (self.socket.destroyed) {
callback();
} else {
self.socket.once('close', callback);
self.resetAll(errors.SocketClosedError({reason: 'local close'}));
self.socket.destroy();
}
};
TChannelConnection.prototype.onSocketError = function onSocketError(err) {
var self = this;
Eif (!self.closing) {
self.resetAll(errors.SocketError(err, {
hostPort: self.channel.hostPort,
direction: self.direction,
remoteAddr: self.remoteAddr
}));
}
};
TChannelConnection.prototype.buildOutRequest = function buildOutRequest(options) {
var self = this;
options.logger = self.logger;
options.random = self.random;
options.timers = self.timers;
return self.handler.buildOutRequest(options);
};
TChannelConnection.prototype.buildOutResponse = function buildOutResponse(req, options) {
var self = this;
var opts = {
logger: self.logger,
random: self.random,
timers: self.timers
};
if (options) {
// jshint forin:false
for (var prop in options) {
opts[prop] = options[prop];
}
// jshint forin:true
}
return self.handler.buildOutResponse(req, opts);
};
module.exports = TChannelConnection;
|