aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2011-10-21 17:58:04 -0700
committerRyan Dahl <ry@tinyclouds.org>2011-10-21 18:02:30 -0700
commit493d3b9f7c0fd4e09ed9289ae0968ef6c73be054 (patch)
tree0e5285450c12d659e9074ddff71e8d91d2bc11bc /lib
parent8498ee03bc271e4d886c02fab9af2d2d47d2ddb0 (diff)
parentff942c6b39d06e81d82c30bc91a159a0440b6d9b (diff)
downloadandroid-node-v8-493d3b9f7c0fd4e09ed9289ae0968ef6c73be054.tar.gz
android-node-v8-493d3b9f7c0fd4e09ed9289ae0968ef6c73be054.tar.bz2
android-node-v8-493d3b9f7c0fd4e09ed9289ae0968ef6c73be054.zip
Merge remote branch 'origin/v0.4'
Conflicts: ChangeLog Makefile deps/libev/wscript doc/index.html doc/template.html lib/net.js src/node_version.h src/platform_cygwin.cc test/pummel/test-net-write-callbacks.js test/simple/test-buffer.js
Diffstat (limited to 'lib')
-rw-r--r--lib/tls.js48
1 files changed, 41 insertions, 7 deletions
diff --git a/lib/tls.js b/lib/tls.js
index e84d501fc0..f2acd90fa8 100644
--- a/lib/tls.js
+++ b/lib/tls.js
@@ -77,6 +77,7 @@ function CryptoStream(pair) {
this.readable = this.writable = true;
this._paused = false;
+ this._needDrain = false;
this._pending = [];
this._pendingCallbacks = [];
this._pendingBytes = 0;
@@ -111,7 +112,7 @@ CryptoStream.prototype.write = function(data /* , encoding, cb */) {
data = new Buffer(data, encoding);
}
- debug('clearIn data');
+ debug((this === this.pair.cleartext ? 'clear' : 'encrypted') + 'In data');
this._pending.push(data);
this._pendingCallbacks.push(cb);
@@ -120,7 +121,26 @@ CryptoStream.prototype.write = function(data /* , encoding, cb */) {
this.pair._writeCalled = true;
this.pair.cycle();
- return this._pendingBytes < 128 * 1024;
+ // In the following cases, write() should return a false,
+ // then this stream should eventually emit 'drain' event.
+ //
+ // 1. There are pending data more than 128k bytes.
+ // 2. A forward stream shown below is paused.
+ // A) EncryptedStream for CleartextStream.write().
+ // B) CleartextStream for EncryptedStream.write().
+ //
+ if (!this._needDrain) {
+ if (this._pendingBytes >= 128 * 1024) {
+ this._needDrain = true;
+ } else {
+ if (this === this.pair.cleartext) {
+ this._needDrain = this.pair.encrypted._paused;
+ } else {
+ this._needDrain = this.pair.cleartext._paused;
+ }
+ }
+ }
+ return !this._needDrain;
};
@@ -420,11 +440,25 @@ CryptoStream.prototype._pull = function() {
assert(rv === tmp.length);
}
- // If we've cleared all of incoming encrypted data, emit drain.
- if (havePending && this._pending.length === 0) {
- debug('drain');
- this.emit('drain');
- if (this.__destroyOnDrain) this.end();
+ // If pending data has cleared, 'drain' event should be emitted
+ // after write() returns a false.
+ // Except when a forward stream shown below is paused.
+ // A) EncryptedStream for CleartextStream._pull().
+ // B) CleartextStream for EncryptedStream._pull().
+ //
+ if (this._needDrain && this._pending.length === 0) {
+ var paused;
+ if (this === this.pair.cleartext) {
+ paused = this.pair.encrypted._paused;
+ } else {
+ paused = this.pair.cleartext._paused;
+ }
+ if (!paused) {
+ debug('drain');
+ process.nextTick(this.emit.bind(this, 'drain'));
+ this._needDrain = false;
+ if (this.__destroyOnDrain) this.end();
+ }
}
};