@@ -10,9 +10,11 @@ function StreamWrap(stream) { |
10 | 10 |
|
11 | 11 | this.stream = stream; |
12 | 12 |
|
| 13 | + this.queue = null; |
| 14 | + |
13 | 15 | var self = this; |
14 | 16 | handle.close = function(cb) { |
15 | | - cb(); |
| 17 | + self.close(cb); |
16 | 18 | }; |
17 | 19 | handle.isAlive = function() { |
18 | 20 | return self.isAlive(); |
@@ -35,10 +37,12 @@ function StreamWrap(stream) { |
35 | 37 |
|
36 | 38 | this.stream.pause(); |
37 | 39 | this.stream.on('data', function(chunk) { |
38 | | - self._handle.readBuffer(chunk); |
| 40 | + if (self._handle) |
| 41 | + self._handle.readBuffer(chunk); |
39 | 42 | }); |
40 | 43 | this.stream.once('end', function() { |
41 | | - self._handle.emitEOF(); |
| 44 | + if (self._handle) |
| 45 | + self._handle.emitEOF(); |
42 | 46 | }); |
43 | 47 | this.stream.on('error', function(err) { |
44 | 48 | self.emit('error', err); |
@@ -88,6 +92,9 @@ StreamWrap.prototype.write = function write(req, bufs) { |
88 | 92 | var pending = bufs.length; |
89 | 93 | var self = this; |
90 | 94 |
|
| 95 | + // Queue the request to be able to cancel it |
| 96 | + self.enqueue(req); |
| 97 | + |
91 | 98 | self.stream.cork(); |
92 | 99 | bufs.forEach(function(buf) { |
93 | 100 | self.stream.write(buf, done); |
@@ -103,6 +110,10 @@ StreamWrap.prototype.write = function write(req, bufs) { |
103 | 110 |
|
104 | 111 | // Ensure that write was dispatched |
105 | 112 | setImmediate(function() { |
| 113 | + // Do not invoke callback twice |
| 114 | + if (!self.dequeue(req)) |
| 115 | + return; |
| 116 | + |
106 | 117 | var errCode = 0; |
107 | 118 | if (err) { |
108 | 119 | if (err.code && uv['UV_' + err.code]) |
@@ -118,3 +129,58 @@ StreamWrap.prototype.write = function write(req, bufs) { |
118 | 129 |
|
119 | 130 | return 0; |
120 | 131 | }; |
| 132 | + |
| 133 | +StreamWrap.prototype.enqueue = function enqueue(req) { |
| 134 | + if (this.queue === null) { |
| 135 | + this.queue = req; |
| 136 | + req._prev = req; |
| 137 | + req._next = req; |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + req._next = this.queue._next; |
| 142 | + this.queue._next._prev = req; |
| 143 | + req._prev = this.queue; |
| 144 | + this.queue._next = req; |
| 145 | +}; |
| 146 | + |
| 147 | +StreamWrap.prototype.dequeue = function dequeue(req) { |
| 148 | + var next = req._next; |
| 149 | + var prev = req._prev; |
| 150 | + |
| 151 | + if (next === null && prev === null) |
| 152 | + return false; |
| 153 | + |
| 154 | + req._next = null; |
| 155 | + req._prev = null; |
| 156 | + |
| 157 | + if (next === prev) { |
| 158 | + this.queue = null; |
| 159 | + } else { |
| 160 | + prev._next = next; |
| 161 | + next._prev = prev; |
| 162 | + |
| 163 | + if (this.queue === req) |
| 164 | + this.queue = next; |
| 165 | + } |
| 166 | + |
| 167 | + return true; |
| 168 | +}; |
| 169 | + |
| 170 | +StreamWrap.prototype.close = function close(cb) { |
| 171 | + var self = this; |
| 172 | + |
| 173 | + setImmediate(function() { |
| 174 | + while (self.queue !== null) { |
| 175 | + var req = self.queue; |
| 176 | + self.dequeue(req); |
| 177 | + |
| 178 | + var errCode = uv.UV_ECANCELED; |
| 179 | + self._handle.doAfterWrite(req); |
| 180 | + self._handle.finishWrite(req, errCode); |
| 181 | + } |
| 182 | + |
| 183 | + self._handle = null; |
| 184 | + cb(); |
| 185 | + }); |
| 186 | +}; |