/
index.js
1357 lines (1063 loc) · 29 KB
/
index.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright 2011 Hydna AB. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY HYDNA AB ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL HYDNA AB OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are
// those of the authors and should not be interpreted as representing
// official policies, either expressed or implied, of Hydna AB.
//
var Buffer = require("buffer").Buffer;
var inherits = require("util").inherits;
var Stream = require("stream").Stream;
var VERSION = require("./package.json").version;
var READ = 0x01;
var WRITE = 0x02;
var READWRITE = 0x03;
var EMIT = 0x04;
// Packet related sizes
var PAYLOAD_MAX_SIZE = 0xFFF8;
var ALL_CHANNELS = 0;
var VALID_ENCODINGS_RE = /^(ascii|utf8|base64|json)/i;
var MODE_RE = /^(r|read){0,1}(w|write){0,1}(?:\+){0,1}(e|emit){0,1}$/i;
exports.PAYLOAD_MAX_SIZE = PAYLOAD_MAX_SIZE;
// Follow 302 redirects. Adds a `X-Accept-Redirects: no` to the
// headers of the handshake request.
exports.followRedirects = true;
// Set the origin in handshakes. Set to `null` to disable
exports.origin = require("os").hostname();
// Set the agent header in handshakes. Set to `null` to disable
exports.agent = "node-winsock-client/" + VERSION;
exports.createChannel = function(url, mode, C) {
var chan = new Channel();
chan.connect(url, mode);
if (typeof C == "function") {
chan.once("connect", C);
}
return chan;
};
function Channel() {
this.id = null;
this._connecting = false;
this._opening = false;
this._closing = false;
this._connection = null;
this._request = null;
this._mode = null;
this._writeQueue = null;
this._encoding = null;
this._url = null;
this.readable = false;
this.writable = false;
this.emitable = false;
}
exports.Channel = Channel;
inherits(Channel, Stream);
Object.defineProperty(Channel.prototype, 'readyState', {
get: function () {
var state;
if (this._connecting) {
return "opening";
} else if (this._closing) {
return "closing";
} else if (!this.id) {
return 'closed';
} else if (this.readable && this.writable) {
state = "readwrite";
} else if (this.readable && !this.writable){
state = "read";
} else if (!this.readable && this.writable){
state = "write";
}
if (this.emitable) {
state += "+emit";
}
return state;
}
});
Object.defineProperty(Channel.prototype, 'url', {
get: function () {
if (!this.id || !this._connection) {
return null;
}
return this._url;
}
});
Channel.prototype.connect = function(url, mode) {
var parse;
var self = this;
var messagesize;
var request;
var uri;
var id;
var host;
var mode;
var token;
if (this._connecting) {
throw new Error("Already connecting");
}
if (typeof url !== "string") {
throw new Error("bad argument, `url`, expected String");
}
if (/^http:\/\/|^https:\/\//.test(url) == false) {
url = "http://" + url;
}
url = require("url").parse(url);
if (url.protocol !== "https:" && url.protocol !== "http:") {
throw new Error("bad protocol, expected `http` or `https`");
}
if (url.pathname && url.pathname.length != 1) {
if (url.pathname.substr(0, 2) == "/x") {
id = parseInt("0" + url.pathname.substr(1));
} else {
id = parseInt(url.pathname.substr(1));
}
if (isNaN(id)) {
throw new Error("Invalid channel");
}
} else {
id = 1;
}
if (id > 0xFFFFFFFF) {
throw new Error("Invalid channel expected no between x0 and xFFFFFFFF");
}
mode = getBinMode(mode);
if (typeof mode !== "number") {
throw new Error("Invalid mode");
}
if (url.query) {
token = new Buffer(decodeURIComponent(uri.query), "utf8");
}
this.id = id;
this._mode = mode;
this._connecting = true;
this._url = url.href;
this.readable = ((this._mode & READ) == READ);
this.writable = ((this._mode & WRITE) == WRITE);
this.emitable = ((this._mode & EMIT) == EMIT);
this._connection = Connection.getConnection(url, false);
this._request = this._connection.open(this, id, mode, token);
};
Channel.prototype.setEncoding = function(encoding) {
if (encoding && !VALID_ENCODINGS_RE.test(encoding)) {
throw new Error("Encoding method not supported");
}
this._encoding = encoding;
};
Channel.prototype.write = function(data, enc, prio) {
var encoding = (typeof enc == "string" && enc);
var flag = ((encoding && prio) || prio || 1) - 1;
var id = this.id;
var frame;
var payload;
if (!this.writable) {
throw new Error("Channel is not writable");
}
if (flag < 0 || flag > 3 || isNaN(flag)) {
throw new Error("Bad priority, expected Number between 1-4");
}
if (!data) {
throw new Error("Expected `data`");
}
if (Buffer.isBuffer(data)) {
flag = flag << 1 | 0; // Set datatype to BINARY
payload = data;
} else {
flag = flag << 1 | 1; // Set datatype to UTF8
if (encoding && !VALID_ENCODINGS_RE.test(encoding)) {
throw new Error("Encoding method is not supported");
}
if (encoding == "json") {
payload = new Buffer(JSON.stringify(data), "utf8");
} else {
payload = new Buffer(data.toString(), encoding);
}
}
if (payload.length > PAYLOAD_MAX_SIZE) {
throw new Error("Payload overflow");
}
frame = new DataFrame(this.id, flag, payload);
try {
flushed = this._writeOut(frame);
} catch (writeException) {
this.destroy(writeException);
return false;
}
return flushed;
};
Channel.prototype.dispatch = function(message) {
var frame;
var payload;
var flushed;
if (!this.emitable) {
throw new Error("Channel is not emitable.");
}
if (typeof message !== "undefined" && typeof message !== "string") {
throw new Error("Expected 'message' as String");
}
if (message) {
payload = new Buffer(message, "utf8");
if (payload.length > PAYLOAD_MAX_SIZE) {
throw new Error("Payload overflow");
}
}
frame = new SignalFrame(this.id, SignalFrame.FLAG_EMIT, payload);
try {
flushed = this._writeOut(frame);
} catch (writeException) {
this.destroy(writeException);
return false;
}
return flushed;
};
Channel.prototype.end = function(message) {
var payload;
if (this.destroyed || this._closing) {
return;
}
if (typeof message !== "undefined" && typeof message !== "string") {
throw new Error("Expected 'message' as String");
}
if (message) {
payload = new Buffer(message, "utf8");
if (payload.length > PAYLOAD_MAX_SIZE) {
throw new Error("Payload overflow");
}
}
this._endsig = new SignalFrame(this.id, SignalFrame.FLAG_END, payload);
this.destroy();
};
Channel.prototype.destroy = function(err) {
var sig;
if (this.destroyed || this._closing || !this.id) {
return;
}
if (!this._connection) {
finalizeDestroyChannel(this);
}
this.readable = false;
this.writable = false;
this.emitable = false;
this._closing = true;
if (this._request && !this._endsig &&
this._request.cancel()) {
this._request = null;
finalizeDestroyChannel(this, err);
return;
}
sig = this._endsig || new SignalFrame(this.id, SignalFrame.FLAG_END);
if (this._request) {
// Do not send ENDSIG if _request is present. We need to wait for
// the OPENSIG before we can close it.
this._endsig = sig;
} else {
// Channel is open and we can therefor send ENDSIG immideitnly. This
// can fail, if TCP connection is dead. If so, we can
// destroy channel with good conscience.
try {
this._writeOut(sig);
} catch (err) {
// ignore
}
}
};
function finalizeDestroyChannel(chan, err, message) {
var id = chan.id;
var conn;
if (chan.destroyed) {
return;
}
if ((conn = chan._connection) && chan.id) {
if (conn.channels[id] == chan) {
delete conn.channels[id];
conn.chanRefCount--;
if (conn.chanRefCount == 0 &&
conn.reqRefCount == 0) {
conn.setDisposed(true);
}
}
}
chan.id = null;
chan.readable = false;
chan.writable = false;
chan.emitable = false;
chan.destroyed = true;
chan._request = null;
chan._writequeue = null;
chan._connection = null;
err && chan.emit("error", err);
chan.emit("close", !(!err), message);
};
Channel.prototype.ondata = function(data, start, end, flag) {
var encoding = this._encoding;
var message = data.slice(start, end);
if (encoding || (flag & 1 == 1)) {
if (encoding == "json") {
try {
message = JSON.parse(message.toString("utf8"));
} catch (exception) {
this.destroy(exception);
return;
}
} else {
try {
message = message.toString(encoding);
} catch (exception) {
this.destroy(exception);
return;
}
}
}
if (this._events && this._events["data"]) {
this.emit("data", message, (flag >> 1) + 1);
}
};
Channel.prototype.onsignal = function(data, start, end) {
var message = null;
if (end - start) {
message = data.toString("utf8", start, end);
}
if (this._events && this._events["signal"]) {
this.emit("signal", message);
}
};
// Internal write method to write raw packets.
Channel.prototype._writeOut = function(packet) {
var written;
if (this._writeQueue) {
this._writeQueue.push(packet);
return false;
}
if (this._connecting) {
this._writeQueue = [packet];
return false;
} else if (this._connection) {
return this._connection.write(packet);
} else {
this.destroy(new Error("Channel is not writable"));
return false;
}
};
Channel.prototype._open = function(newid, message) {
var flushed = false;
var queue = this._writeQueue;
var id = this.id;
var packet;
this.id = newid;
this._connecting = false;
this._writeQueue = null;
this._request = null;
this._connection.channels[this.id] = this;
this._connection.chanRefCount++;
if (queue && queue.length) {
for (var i = 0, l = queue.length; i < l; i++) {
packet = queue[i];
packet.id = newid;
try {
flushed = this._writeOut(packet);
} catch(writeException) {
this.destroy(writeException);
return;
}
}
}
if (this._closing) {
if ((packet = self._endsig)) {
self._endsig = null;
packet.id = newid;
try {
this._writeOut(packet);
} catch (err) {
// Ignore
}
return;
}
}
this.emit("connect", message);
if (flushed) {
this.emit("drain");
}
};
// Represents a server connection.
function Connection(id) {
this.id = id;
this.chanRefCount = 0;
this.reqRefCount = 0;
this.channels = {};
this.requests = {};
this.sock = null;
Connection.all[id] = this;
}
Connection.all = {};
Connection.disposed = {};
Connection.getConnection = function(url) {
var id;
var connection;
var datacache = "";
var lastException;
id = url.protocol + url.host + (url.auth && (":" + url.auth) || "");
if ((connection = Connection.all[id])) {
return connection;
}
if ((connection = Connection.disposed[id])) {
connection.setDisposed(false);
return connection;
}
// rewrite url if initial token is present.
url = require("url").parse([
url.protocol,
"//",
url.hostname,
url.port ? ":" + url.port : "",
"/",
url.auth
].join(""));
connection = new Connection(id);
connection.connect(url);
return connection;
}
Connection.prototype.connect = function(url) {
var self = this;
if (this.sock) {
throw new Error("Socket already connected");
}
process.nextTick(function() {
getSock(url, function(err, sock) {
var requests = self.requests;
if (err) {
return self.destroy(err);
}
sock.setNoDelay(true);
sock.setKeepAlive && sock.setKeepAlive(true);
sock.on("drain", function() {
var channels = self.channels;
var chan;
for (var id in channels) {
chan = channels[id];
if (chan._events && chan._events["drain"]) {
chan.emit("drain");
}
}
});
sock.on("error", function(err) {
self.sock = null;
self.destroy(err);
});
sock.on("close", function(hadError) {
if (hadError == false) {
self.sock = null;
self.destroy(new Error("Connection reseted by server"));
}
});
self.sock = sock;
parserImplementation(self)
if (self.reqRefCount == 0) {
// All requests was cancelled before we got a
// handshake from server. Dispose us.
self.setDisposed(true);
}
try {
for (var id in requests) {
self.write(requests[id]);
requests[id].sent = true;
}
} catch (writeException) {
self.destroy(writeException);
}
});
});
};
function getSock(url, C) {
var parse = require("url").parse;
var STATUS_CODES = require("http").STATUS_CODES;
var MAX_REDIRECTS = 5;
var redirections = 1;
function dorequest(url) {
var request;
var opts;
var req;
var port;
var host;
var path;
if (url.protocol !== "http:" && url.protocol !== "https:") {
return C(new Error("Redirect, bad protocol `" + url.protocol + "`"));
}
request = require(url.protocol == "http:" ? "http" : "https").request;
host = url.hostname;
port = url.port || (url.protocol == "http:" ? 80 : 443);
path = url.pathname;
opts = {
port: port,
host: host,
path: path,
headers: {
"Connection": "Upgrade",
"Upgrade": "winksock/1",
}
}
if (!exports.followRedirects) {
opts.headers["X-Accept-Redirects"] = "no";
}
if (exports.agent) {
opts.headers["User-Agent"] = exports.agent;
}
if (exports.origin) {
opts.headers["Origin"] = exports.origin;
}
req = request(opts, function(res) {
var msg;
res.setEncoding("utf8");
res.on("data", function(chunk) {
msg = msg ? msg + chunk : chunk;
});
res.on("end", function() {
var code = res.statusCode;
var url;
var err;
switch (code) {
case 301:
case 302:
case 307:
if (exports.followRedirects) {
if (redirections++ == MAX_REDIRECTS) {
return C(new Error("Max HTTP redirections reached"));
}
try {
url = parse(res.headers["location"]);
} catch (err) {
return C(err);
}
return dorequest(url)
} else {
err = new Error("Redirected by host, followRedirects=false");
return C(err);
}
break;
default:
if (msg) {
err = new Error(STATUS_CODES[code] + " (" + msg + ")");
} else {
err = new Error(STATUS_CODES[code]);
}
break;
}
return C(err);
});
});
req.on("error", function(err) {
return C(err);
});
req.on("upgrade", function(res, sock) {
sock.setTimeout(0);
sock.removeAllListeners("error");
sock.removeAllListeners("close");
sock.resume();
if (res.headers["upgrade"] != "winksock/1") {
sock.destroy(new Error("Bad protocol version " + res.headers["upgrade"]));
}
return C(null, sock);
});
req.end();
}
dorequest(url);
}
Connection.prototype.open = function(chan, id, mode, token) {
var self = this;
var channels = this.channels;
var oldchan;
var request;
if ((oldchan = channels[id]) && !oldchan._closing) {
process.nextTick(function() {
finalizeDestroyChannel(chan, new Error("Channel is already open"));
});
return null;
}
request = new OpenRequest(this, id, mode, token);
request.onresponse = function(newid, message) {
chan._open(newid, message);
};
request.onclose = function(err) {
if (err) { finalizeDestroyChannel(chan, err); }
};
if (this.sock && !oldchan) {
// Do not send request if socket isnt handshaked yet, or
// if a channel is open and waiting for an ENDSIG.
request.send();
}
return request;
};
Connection.prototype.setDisposed = function(state) {
var id = this.id;
var sock = this.sock;
var self = this;
if (!this.id || !sock) return;
if (state) {
if (sock) {
sock.setTimeout(200);
sock.once("timeout", function() {
self.destroy();
});
}
Connection.disposed[id] = this;
Connection.all[id] = undefined;
} else {
delete Connection.disposed[id];
Connection.all[id] = this;
if (sock) {
sock.setTimeout(0);
sock.removeAllListeners("timeout");
}
}
};
// Write a `Packet` to the underlying socket.
Connection.prototype.write = function(frame) {
if (this.sock) {
return this.sock.write(frame.toBuffer());
} else {
return false;
}
};
Connection.prototype.processOpen = function(id, flag, data, start, end) {
var request;
if (!(request = this.requests[id])) {
sock.destroy(new Error("Server sent an open response to unknown"));
return;
}
request.processResponse(flag, data, start, end);
};
Connection.prototype.processData = function(id, flag, data, start, end) {
var channels = this.channels;
var chan;
if (id === ALL_CHANNELS) {
for (var chanid in channels) {
chan = channels[chanid];
if (chan.readable) {
chan.ondata && chan.ondata(data, start, end, flag);
}
}
} else if ((chan = channels[id])) {
if (chan.readable) {
chan.ondata && chan.ondata(data, start, end, flag);
}
}
};
Connection.prototype.processSignal = function(id, flag, data, start, end) {
var channels = this.channels;
var requests = this.requests;
var chan;
var message;
switch (flag) {
case SignalFrame.FLAG_EMIT:
if (id === ALL_CHANNELS) {
for (var chanid in channels) {
chan = channels[chanid];
if (chan._closing == false) {
chan.onsignal && chan.onsignal(data, start, end);
}
}
} else if ((chan = channels[id])) {
if (chan._closing == false) {
chan.onsignal && chan.onsignal(data, start, end);
}
}
break;
case SignalFrame.FLAG_END:
case SignalFrame.FLAG_ERROR:
if (end - start) {
message = data.toString("utf8", start, end);
}
if (id === ALL_CHANNELS) {
if (flag != SignalFrame.FLAG_END) {
this.destroy(new Error(message || "ERR_UNKNOWN"));
} else {
this.destroy(null, message);
}
return;
}
if (!(chan = channels[id])) {
// Protocol violation. Channel does not exists in client. Ignore
// for now.
return;
}
if (chan._closing) {
// User requested to close this channel. This ENDSIG is a
// response to that request. It is now safe to destroy
// channel. Note: We are intentionally not sending the message
// to the function, because channel is closed according
// to client.
finalizeDestroyChannel(chan);
if (requests[id]) {
// Send pending open request if exists.
requests[id].send();
}
} else {
// Server closed this channel. We need to respond with a
// ENDSIG in order to let server now that we received this
// signal.
try {
this.write(new SignalFrame(id, SignalFrame.FLAG_END));
} catch (writeException) {
this.destroy(writeException);
}
if (flag != SignalFrame.FLAG_END) {
finalizeDestroyChannel(chan, new Error(message || "ERR_UNKNOWN"));
} else {
finalizeDestroyChannel(chan, null, message);
}
}
break;
default:
this.destroy(new Error("Server sent an unknown SIGFLAG"));
return;
}
};
// Destroy connection with optional Error
Connection.prototype.destroy = function(err, message) {
var id = this.id;
var channels = this.channels;
var requests = this.requests;
var chan;
var request;
var queued;
if (!id) {
return;
}
this.id = null;
for (var chanid in channels) {
if ((chan = channels[chanid])) {
finalizeDestroyChannel(chan, err, message);
}
}
for (var reqid in requests) {
if ((request = requests[reqid])) {
request.destroyAndNext(err);
}
}
this.channels = {};
this.requests = {};
this.chanRefCount = 0;
this.reqRefCount = 0;
delete Connection.all[id];
delete Connection.disposed[id];
if (this.sock) {
this.sock.destroy();
this.sock = null;
}
};
// OpenRequest constructor.
function OpenRequest(conn, id, flag, data) {
var requests = conn.requests;
var next;
this.conn = conn;
this.id = id;
this.flag = flag;
this.data = data;
this.present = false;
this.sent = false;
this.destroyed = false;
this.prev = null;
this.next = null;
if ((next = requests[id])) {
while (next.next && (next = next.next)) {};
next.next = this;
} else {
requests[id] = this;
}
conn.reqRefCount++;
}