|
|
@ -23,8 +23,6 @@ var accept = binding.accept; |
|
|
|
var close = binding.close; |
|
|
|
var shutdown = binding.shutdown; |
|
|
|
var read = binding.read; |
|
|
|
var recvMsg = binding.recvMsg; |
|
|
|
var sendFD = binding.sendFD; |
|
|
|
var write = binding.write; |
|
|
|
var toRead = binding.toRead; |
|
|
|
var setNoDelay = binding.setNoDelay; |
|
|
@ -248,13 +246,11 @@ function _doFlush () { |
|
|
|
var socket = this.socket; |
|
|
|
// Stream becomes writeable on connect() but don't flush if there's
|
|
|
|
// nothing actually to write
|
|
|
|
if ((socket._writeQueueSize == 0) && (socket._writeMessageQueueSize == 0)) { |
|
|
|
if (socket._writeQueueSize == 0) { |
|
|
|
return; |
|
|
|
} |
|
|
|
if (socket.flush()) { |
|
|
|
assert(socket._writeQueueSize == 0); |
|
|
|
assert(socket._writeMessageQueueSize == 0); |
|
|
|
|
|
|
|
if (socket._events && socket._events['drain']) socket.emit("drain"); |
|
|
|
if (socket.ondrain) socket.ondrain(); // Optimization
|
|
|
|
} |
|
|
@ -280,21 +276,10 @@ function initStream (self) { |
|
|
|
var bytesRead; |
|
|
|
|
|
|
|
try { |
|
|
|
if (self.type == "unix") { |
|
|
|
bytesRead = recvMsg(self.fd, |
|
|
|
recvBuffer, |
|
|
|
recvBuffer.used, |
|
|
|
recvBuffer.length - recvBuffer.used); |
|
|
|
//debug('recvMsg.fd ' + recvMsg.fd);
|
|
|
|
if (recvMsg.fd) { |
|
|
|
self.emit('fd', recvMsg.fd); |
|
|
|
} |
|
|
|
} else { |
|
|
|
bytesRead = read(self.fd, |
|
|
|
recvBuffer, |
|
|
|
recvBuffer.used, |
|
|
|
recvBuffer.length - recvBuffer.used); |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
self.forceClose(e); |
|
|
|
return; |
|
|
@ -302,7 +287,7 @@ function initStream (self) { |
|
|
|
|
|
|
|
//debug('bytesRead ' + bytesRead + '\n');
|
|
|
|
|
|
|
|
if (!recvMsg.fd && bytesRead == 0) { |
|
|
|
if (bytesRead == 0) { |
|
|
|
self.readable = false; |
|
|
|
self._readWatcher.stop(); |
|
|
|
|
|
|
@ -351,7 +336,6 @@ function initStream (self) { |
|
|
|
self._writeQueue = []; // queue of buffers that need to be written to socket
|
|
|
|
// XXX use link list?
|
|
|
|
self._writeQueueSize = 0; // in bytes, not to be confused with _writeQueue.length!
|
|
|
|
self._writeMessageQueueSize = 0; // number of messages remaining to be sent
|
|
|
|
|
|
|
|
self._writeWatcher = ioWatchers.alloc(); |
|
|
|
self._writeWatcher.socket = self; |
|
|
@ -442,30 +426,17 @@ Stream.prototype._writeString = function (data, encoding) { |
|
|
|
var charsWritten; |
|
|
|
var bytesWritten; |
|
|
|
|
|
|
|
// The special encoding "fd" means that data is an integer FD and we want
|
|
|
|
// to pass the FD on the socket with sendmsg()
|
|
|
|
if (encoding == "fd") { |
|
|
|
buffer.isFd = true; |
|
|
|
// TODO is this OK -- does it guarantee that the fd is the only thing in the buffer?
|
|
|
|
charsWritten = buffer.asciiWrite(data, buffer.used, buffer.length - buffer.used); |
|
|
|
bytesWritten = charsWritten; |
|
|
|
} else if (encoding.toLowerCase() == 'utf8') { |
|
|
|
buffer.isFd = false; |
|
|
|
if (encoding.toLowerCase() == 'utf8') { |
|
|
|
charsWritten = buffer.utf8Write(data, buffer.used); |
|
|
|
bytesWritten = Buffer.utf8ByteLength(data.slice(0, charsWritten)); |
|
|
|
} else { |
|
|
|
// ascii
|
|
|
|
buffer.isFd = false; |
|
|
|
charsWritten = buffer.asciiWrite(data, buffer.used); |
|
|
|
bytesWritten = charsWritten; |
|
|
|
} |
|
|
|
|
|
|
|
buffer.used += bytesWritten; |
|
|
|
if (buffer.isFd) { |
|
|
|
self._writeMessageQueueSize += 1; |
|
|
|
} else { |
|
|
|
self._writeQueueSize += bytesWritten; |
|
|
|
} |
|
|
|
|
|
|
|
//debug('charsWritten ' + charsWritten);
|
|
|
|
//debug('buffer.used ' + buffer.used);
|
|
|
@ -596,28 +567,6 @@ Stream.prototype._writeQueued = function (data, encoding) { |
|
|
|
return this.flush(); |
|
|
|
}; |
|
|
|
|
|
|
|
// Sends a file descriptor over a unix socket
|
|
|
|
Stream.prototype.sendFD = function(socketToPass) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
if (!self.writable) throw new Error('Stream is not writable'); |
|
|
|
|
|
|
|
if (self.__writeQueueLast() == END_OF_FILE) { |
|
|
|
throw new Error('socket.close() called already; cannot write.'); |
|
|
|
} |
|
|
|
|
|
|
|
if (self.type != "unix") { |
|
|
|
throw new Error('FD passing only available on unix sockets'); |
|
|
|
} |
|
|
|
|
|
|
|
if (! socketToPass instanceof Stream) { |
|
|
|
throw new Error('Provided arg is not a socket'); |
|
|
|
} |
|
|
|
|
|
|
|
return self.write(socketToPass.fd.toString(), "fd"); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Flushes the write buffer out.
|
|
|
|
// Returns true if the entire buffer was flushed.
|
|
|
|
Stream.prototype.flush = function () { |
|
|
@ -644,15 +593,10 @@ Stream.prototype.flush = function () { |
|
|
|
var fdToSend = null; |
|
|
|
|
|
|
|
try { |
|
|
|
if (b.isFd) { |
|
|
|
fdToSend = parseInt(b.asciiSlice(b.sent, b.used - b.sent)); |
|
|
|
bytesWritten = writeFD(self.fd, fdToSend); |
|
|
|
} else { |
|
|
|
bytesWritten = write(self.fd, |
|
|
|
b, |
|
|
|
b.sent, |
|
|
|
b.used - b.sent); |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
self.forceClose(e); |
|
|
|
return false; |
|
|
@ -667,10 +611,6 @@ Stream.prototype.flush = function () { |
|
|
|
self._writeWatcher.start(); |
|
|
|
assert(self._writeQueueSize > 0); |
|
|
|
return false; |
|
|
|
} else if (b.isFd) { |
|
|
|
b.sent = b.used; |
|
|
|
self._writeMessageQueueSize -= 1; |
|
|
|
//debug('sent fd: ' + fdToSend);
|
|
|
|
} else { |
|
|
|
b.sent += bytesWritten; |
|
|
|
self._writeQueueSize -= bytesWritten; |
|
|
|