PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/net.js

https://github.com/kevinxx/node_doc_zh_CN
JavaScript | 1219 lines | 850 code | 220 blank | 149 comment | 222 complexity | 3faa68f12c163262f3166d24c0343b76 MD5 | raw file
Possible License(s): JSON, BSD-2-Clause, BSD-3-Clause
  1. var sys = require("sys");
  2. var fs = require("fs");
  3. var events = require("events");
  4. var dns = require('dns');
  5. var kMinPoolSpace = 128;
  6. var kPoolSize = 40*1024;
  7. var debugLevel = parseInt(process.env.NODE_DEBUG, 16);
  8. function debug () {
  9. if (debugLevel & 0x2) sys.error.apply(this, arguments);
  10. }
  11. var binding = process.binding('net');
  12. // Note about Buffer interface:
  13. // I'm attempting to do the simplest possible interface to abstracting raw
  14. // memory allocation. This might turn out to be too simple - it seems that
  15. // I always use a buffer.used member to keep track of how much I've filled.
  16. // Perhaps giving the Buffer a file-like interface with a head (which would
  17. // represent buffer.used) that can be seeked around would be easier. I'm not
  18. // yet convinced that every use-case can be fit into that abstraction, so
  19. // waiting to implement it until I get more experience with this.
  20. var Buffer = require('buffer').Buffer;
  21. var FreeList = require('freelist').FreeList;
  22. var IOWatcher = process.IOWatcher;
  23. var assert = process.assert;
  24. var socket = binding.socket;
  25. var bind = binding.bind;
  26. var connect = binding.connect;
  27. var listen = binding.listen;
  28. var accept = binding.accept;
  29. var close = binding.close;
  30. var shutdown = binding.shutdown;
  31. var read = binding.read;
  32. var write = binding.write;
  33. var toRead = binding.toRead;
  34. var setNoDelay = binding.setNoDelay;
  35. var setKeepAlive= binding.setKeepAlive;
  36. var socketError = binding.socketError;
  37. var getsockname = binding.getsockname;
  38. var errnoException = binding.errnoException;
  39. var sendMsg = binding.sendMsg;
  40. var recvMsg = binding.recvMsg;
  41. var EINPROGRESS = binding.EINPROGRESS;
  42. var ENOENT = binding.ENOENT;
  43. var EMFILE = binding.EMFILE;
  44. var END_OF_FILE = 42;
  45. // Do we have openssl crypto?
  46. try {
  47. var SecureContext = process.binding('crypto').SecureContext;
  48. var SecureStream = process.binding('crypto').SecureStream;
  49. var have_crypto = true;
  50. } catch (e) {
  51. var have_crypto = false;
  52. }
  53. // IDLE TIMEOUTS
  54. //
  55. // Because often many sockets will have the same idle timeout we will not
  56. // use one timeout watcher per socket. It is too much overhead. Instead
  57. // we'll use a single watcher for all sockets with the same timeout value
  58. // and a linked list. This technique is described in the libev manual:
  59. // http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Be_smart_about_timeouts
  60. var timeout = new (function () {
  61. // Object containing all lists, timers
  62. // key = time in milliseconds
  63. // value = list
  64. var lists = {};
  65. // show the most idle socket
  66. function peek (list) {
  67. if (list._idlePrev == list) return null;
  68. return list._idlePrev;
  69. }
  70. // remove the most idle socket from the list
  71. function shift (list) {
  72. var first = list._idlePrev;
  73. remove(first);
  74. return first;
  75. }
  76. // remove a socket from its list
  77. function remove (socket) {
  78. socket._idleNext._idlePrev = socket._idlePrev;
  79. socket._idlePrev._idleNext = socket._idleNext;
  80. }
  81. // remove a socket from its list and place at the end.
  82. function append (list, socket) {
  83. remove(socket);
  84. socket._idleNext = list._idleNext;
  85. socket._idleNext._idlePrev = socket;
  86. socket._idlePrev = list
  87. list._idleNext = socket;
  88. }
  89. function normalize (msecs) {
  90. if (!msecs || msecs <= 0) return 0;
  91. // round up to one sec
  92. if (msecs < 1000) return 1000;
  93. // round down to nearest second.
  94. return msecs - (msecs % 1000);
  95. }
  96. // the main function - creates lists on demand and the watchers associated
  97. // with them.
  98. function insert (socket, msecs) {
  99. socket._idleStart = new Date();
  100. socket._idleTimeout = msecs;
  101. if (!msecs) return;
  102. var list;
  103. if (lists[msecs]) {
  104. list = lists[msecs];
  105. } else {
  106. list = new process.Timer();
  107. list._idleNext = list;
  108. list._idlePrev = list;
  109. lists[msecs] = list;
  110. list.callback = function () {
  111. debug('timeout callback ' + msecs);
  112. // TODO - don't stop and start the watcher all the time.
  113. // just set its repeat
  114. var now = new Date();
  115. debug("now: " + now);
  116. var first;
  117. while (first = peek(list)) {
  118. var diff = now - first._idleStart;
  119. if (diff < msecs) {
  120. list.again(msecs - diff);
  121. debug(msecs + ' list wait because diff is ' + diff);
  122. return;
  123. } else {
  124. remove(first);
  125. assert(first != peek(list));
  126. first.emit('timeout');
  127. }
  128. }
  129. debug(msecs + ' list empty');
  130. assert(list._idleNext == list); // list is empty
  131. list.stop();
  132. };
  133. }
  134. if (list._idleNext == list) {
  135. // if empty (re)start the timer
  136. list.again(msecs);
  137. }
  138. append(list, socket);
  139. assert(list._idleNext != list); // list is not empty
  140. }
  141. var unenroll = this.unenroll = function (socket) {
  142. if (socket._idleNext) {
  143. socket._idleNext._idlePrev = socket._idlePrev;
  144. socket._idlePrev._idleNext = socket._idleNext;
  145. var list = lists[socket._idleTimeout];
  146. // if empty then stop the watcher
  147. //debug('unenroll');
  148. if (list && list._idlePrev == list) {
  149. //debug('unenroll: list empty');
  150. list.stop();
  151. }
  152. }
  153. };
  154. // Does not start the time, just sets up the members needed.
  155. this.enroll = function (socket, msecs) {
  156. // if this socket was already in a list somewhere
  157. // then we should unenroll it from that
  158. if (socket._idleNext) unenroll(socket);
  159. socket._idleTimeout = msecs;
  160. socket._idleNext = socket;
  161. socket._idlePrev = socket;
  162. };
  163. // call this whenever the socket is active (not idle)
  164. // it will reset its timeout.
  165. this.active = function (socket) {
  166. var msecs = socket._idleTimeout;
  167. if (msecs) {
  168. var list = lists[msecs];
  169. if (socket._idleNext == socket) {
  170. insert(socket, msecs);
  171. } else {
  172. // inline append
  173. socket._idleStart = new Date();
  174. socket._idleNext._idlePrev = socket._idlePrev;
  175. socket._idlePrev._idleNext = socket._idleNext;
  176. socket._idleNext = list._idleNext;
  177. socket._idleNext._idlePrev = socket;
  178. socket._idlePrev = list
  179. list._idleNext = socket;
  180. }
  181. }
  182. };
  183. })();
  184. var ioWatchers = new FreeList("iowatcher", 100, function () {
  185. return new IOWatcher();
  186. });
  187. // waitingForFDs stores servers which have experienced EMFILE.
  188. // When a file descriptor becomes available through closeFD()
  189. // a server from waitingForFDs is started.
  190. var waitingForFDs = [];
  191. function closeFD(fd) {
  192. close(fd);
  193. // Try to recover from EMFILE
  194. var server, serverFD;
  195. while (server = waitingForFDs.shift()) {
  196. serverFD = parseInt(server.fd);
  197. if (serverFD && serverFD > 0) {
  198. server.watcher.set(serverFD, true, false);
  199. server.watcher.start();
  200. return;
  201. }
  202. }
  203. }
  204. // Allocated on demand.
  205. var pool = null;
  206. function allocNewPool () {
  207. pool = new Buffer(kPoolSize);
  208. pool.used = 0;
  209. }
  210. var securePool = null;
  211. function allocNewSecurePool () {
  212. securePool = new Buffer(40*1024);
  213. }
  214. var emptyBuffer = null;
  215. function allocEmptyBuffer () {
  216. emptyBuffer = new Buffer(1);
  217. emptyBuffer.sent = 0;
  218. emptyBuffer.length = 0;
  219. }
  220. function _doFlush () {
  221. var socket = this.socket;
  222. // Stream becomes writeable on connect() but don't flush if there's
  223. // nothing actually to write
  224. if (socket.flush()) {
  225. if (socket._events && socket._events['drain']) socket.emit("drain");
  226. if (socket.ondrain) socket.ondrain(); // Optimization
  227. }
  228. }
  229. function setImplmentationMethods (self) {
  230. function noData(buf, off, len) {
  231. return !buf ||
  232. (off != undefined && off >= buf.length) ||
  233. (len == 0);
  234. };
  235. if (self.type == 'unix') {
  236. self._writeImpl = function(buf, off, len, fd, flags) {
  237. // Detect and disallow zero-byte writes wth an attached file
  238. // descriptor. This is an implementation limitation of sendmsg(2).
  239. if (fd && noData(buf, off, len)) {
  240. throw new Error('File descriptors can only be written with data');
  241. }
  242. return sendMsg(self.fd, buf, off, len, fd, flags);
  243. };
  244. self._readImpl = function(buf, off, len, calledByIOWatcher) {
  245. var bytesRead = recvMsg(self.fd, buf, off, len);
  246. // Do not emit this in the same stack, otherwise we risk corrupting our
  247. // buffer pool which is full of read data, but has not had had its
  248. // pointers updated just yet.
  249. //
  250. // Save off recvMsg.fd in a closure so that, when we emit it later, we're
  251. // emitting the same value that we see now. Otherwise, we can end up
  252. // calling emit() after recvMsg() has been called again and end up
  253. // emitting null (or another FD).
  254. if (recvMsg.fd !== null) {
  255. (function () {
  256. var fd = recvMsg.fd;
  257. process.nextTick(function() {
  258. self.emit('fd', fd);
  259. });
  260. })();
  261. }
  262. return bytesRead;
  263. };
  264. } else {
  265. self._writeImpl = function(buf, off, len, fd, flags) {
  266. // XXX: TLS support requires that 0-byte writes get processed
  267. // by the kernel for some reason. Otherwise, we'd just
  268. // fast-path return here.
  269. // Drop 'fd' and 'flags' as these are not supported by the write(2)
  270. // system call
  271. return write(self.fd, buf, off, len);
  272. };
  273. self._readImpl = function(buf, off, len, calledByIOWatcher) {
  274. return read(self.fd, buf, off, len);
  275. };
  276. }
  277. self._shutdownImpl = function() {
  278. shutdown(self.fd, 'write')
  279. };
  280. if (self.secure) {
  281. var oldWrite = self._writeImpl;
  282. self._writeImpl = function(buf, off, len, fd, flags) {
  283. assert(buf);
  284. assert(self.secure);
  285. var bytesWritten = self.secureStream.writeInject(buf, off, len);
  286. if (!securePool) {
  287. allocNewSecurePool();
  288. }
  289. var secureLen = self.secureStream.writeExtract(
  290. securePool, 0, securePool.length
  291. );
  292. if (secureLen == -1) {
  293. // Check our read again for secure handshake
  294. self._readWatcher.callback();
  295. } else {
  296. oldWrite(securePool, 0, secureLen, fd, flags);
  297. }
  298. if (!self.secureEstablished && self.secureStream.isInitFinished()) {
  299. self.secureEstablished = true;
  300. if (self._events && self._events['secure']) {
  301. self.emit('secure');
  302. }
  303. }
  304. return bytesWritten;
  305. };
  306. var oldRead = self._readImpl;
  307. self._readImpl = function(buf, off, len, calledByIOWatcher) {
  308. assert(self.secure);
  309. var bytesRead = 0;
  310. var secureBytesRead = null;
  311. if (!securePool) {
  312. allocNewSecurePool();
  313. }
  314. if (calledByIOWatcher) {
  315. secureBytesRead = oldRead(securePool, 0, securePool.length);
  316. self.secureStream.readInject(securePool, 0, secureBytesRead);
  317. }
  318. var chunkBytes;
  319. do {
  320. chunkBytes = self.secureStream.readExtract(
  321. pool,
  322. pool.used + bytesRead,
  323. pool.length - pool.used - bytesRead
  324. );
  325. bytesRead += chunkBytes;
  326. } while ((chunkBytes > 0) && (pool.used + bytesRead < pool.length));
  327. if (bytesRead == 0 && !calledByIOWatcher) {
  328. return -1;
  329. }
  330. if (self.secureStream.readPending()) {
  331. process.nextTick(function () {
  332. if(self._readWatcher)
  333. self._readWatcher.callback();
  334. });
  335. }
  336. if (!self.secureEstablished) {
  337. if (self.secureStream.isInitFinished()) {
  338. self.secureEstablished = true;
  339. if (self._events && self._events['secure']) {
  340. self.emit('secure');
  341. }
  342. }
  343. }
  344. if (calledByIOWatcher && secureBytesRead === null && !self.server) {
  345. // Client needs to write as part of handshake
  346. self._writeWatcher.start();
  347. return -1;
  348. }
  349. if (bytesRead == 0 && secureBytesRead > 0) {
  350. // Deal with SSL handshake
  351. if (self.server) {
  352. self._checkForSecureHandshake();
  353. } else {
  354. if (self.secureEstablised) {
  355. self.flush();
  356. } else {
  357. self._checkForSecureHandshake();
  358. }
  359. }
  360. return -1;
  361. }
  362. return bytesRead;
  363. };
  364. var oldShutdown = self._shutdownImpl;
  365. self._shutdownImpl = function() {
  366. self.secureStream.shutdown();
  367. if (!securePool) {
  368. allocNewSecurePool();
  369. }
  370. var secureLen = self.secureStream.writeExtract(
  371. securePool, 0, securePool.length
  372. );
  373. try {
  374. oldWrite(securePool, 0, secureLen);
  375. } catch (e) { }
  376. oldShutdown();
  377. };
  378. }
  379. };
  380. function initStream (self) {
  381. self._readWatcher = ioWatchers.alloc();
  382. self._readWatcher.callback = function () {
  383. // If this is the first recv (pool doesn't exist) or we've used up
  384. // most of the pool, allocate a new one.
  385. if (!pool || pool.length - pool.used < kMinPoolSpace) {
  386. // discard the old pool. Can't add to the free list because
  387. // users might have refernces to slices on it.
  388. pool = null;
  389. allocNewPool();
  390. }
  391. //debug('pool.used ' + pool.used);
  392. var bytesRead;
  393. try {
  394. bytesRead = self._readImpl(pool, pool.used, pool.length - pool.used, (arguments.length > 0));
  395. } catch (e) {
  396. self.destroy(e);
  397. return;
  398. }
  399. // Note that some _readImpl() implementations return -1 bytes
  400. // read as an indication not to do any processing on the result
  401. // (but not an error).
  402. if (bytesRead === 0) {
  403. self.readable = false;
  404. self._readWatcher.stop();
  405. if (!self.writable) self.destroy();
  406. // Note: 'close' not emitted until nextTick.
  407. if (self._events && self._events['end']) self.emit('end');
  408. if (self.onend) self.onend();
  409. } else if (bytesRead > 0) {
  410. timeout.active(self);
  411. var start = pool.used;
  412. var end = pool.used + bytesRead;
  413. pool.used += bytesRead;
  414. if (self._decoder) {
  415. // emit String
  416. var string = self._decoder.write(pool.slice(start, end));
  417. if (string.length) self.emit('data', string);
  418. } else {
  419. // emit buffer
  420. if (self._events && self._events['data']) {
  421. // emit a slice
  422. self.emit('data', pool.slice(start, end));
  423. }
  424. }
  425. // Optimization: emit the original buffer with end points
  426. if (self.ondata) self.ondata(pool, start, end);
  427. }
  428. };
  429. self.readable = false;
  430. // Queue of buffers and string that need to be written to socket.
  431. self._writeQueue = [];
  432. self._writeQueueEncoding = [];
  433. self._writeQueueFD = [];
  434. self._writeWatcher = ioWatchers.alloc();
  435. self._writeWatcher.socket = self;
  436. self._writeWatcher.callback = _doFlush;
  437. self.writable = false;
  438. }
  439. function Stream (fd, type) {
  440. events.EventEmitter.call(this);
  441. this.fd = null;
  442. this.type = null;
  443. this.secure = false;
  444. if (parseInt(fd) >= 0) {
  445. this.open(fd, type);
  446. } else {
  447. setImplmentationMethods(this);
  448. }
  449. };
  450. sys.inherits(Stream, events.EventEmitter);
  451. exports.Stream = Stream;
  452. Stream.prototype.setSecure = function(credentials) {
  453. if (!have_crypto) {
  454. throw new Error('node.js not compiled with openssl crypto support.');
  455. }
  456. var crypto= require("crypto");
  457. this.secure = true;
  458. this.secureEstablished = false;
  459. // If no credentials given, create a new one for just this Stream
  460. if (!credentials) {
  461. this.credentials = crypto.createCredentials();
  462. } else {
  463. this.credentials = credentials;
  464. }
  465. if (!this.server) {
  466. // For clients, we will always have either a given ca list or the default one;
  467. this.credentials.shouldVerify = true;
  468. }
  469. this.secureStream = new SecureStream(this.credentials.context, this.server ? 1 : 0, this.credentials.shouldVerify ? 1 : 0);
  470. setImplmentationMethods(this);
  471. if (!this.server) {
  472. // If client, trigger handshake
  473. this._checkForSecureHandshake();
  474. }
  475. }
  476. Stream.prototype.verifyPeer = function() {
  477. if (!this.secure) {
  478. throw new Error('Stream is not a secure stream.');
  479. }
  480. return this.secureStream.verifyPeer(this.credentials.context);
  481. }
  482. Stream.prototype._checkForSecureHandshake = function() {
  483. if (!this.writable) {
  484. return;
  485. }
  486. // Do an empty write to see if we need to write out as part of handshake
  487. if (!emptyBuffer) allocEmptyBuffer();
  488. this.write(emptyBuffer);
  489. }
  490. Stream.prototype.getPeerCertificate = function(credentials) {
  491. if (!this.secure) {
  492. throw new Error('Stream is not a secure stream.');
  493. }
  494. return this.secureStream.getPeerCertificate();
  495. }
  496. Stream.prototype.getCipher = function() {
  497. if (!this.secure) {
  498. throw new Error('Stream is not a secure stream.');
  499. }
  500. return this.secureStream.getCurrentCipher();
  501. }
  502. Stream.prototype.open = function (fd, type) {
  503. initStream(this);
  504. this.fd = fd;
  505. this.type = type || null;
  506. this.readable = true;
  507. setImplmentationMethods(this);
  508. this._writeWatcher.set(this.fd, false, true);
  509. this.writable = true;
  510. }
  511. exports.createConnection = function (port, host) {
  512. var s = new Stream();
  513. s.connect(port, host);
  514. return s;
  515. };
  516. Object.defineProperty(Stream.prototype, 'readyState', {
  517. get: function () {
  518. if (this._connecting) {
  519. return 'opening';
  520. } else if (this.readable && this.writable) {
  521. assert(typeof this.fd == 'number');
  522. return 'open';
  523. } else if (this.readable && !this.writable){
  524. assert(typeof this.fd == 'number');
  525. return 'readOnly';
  526. } else if (!this.readable && this.writable){
  527. assert(typeof this.fd == 'number');
  528. return 'writeOnly';
  529. } else {
  530. assert(typeof this.fd != 'number');
  531. return 'closed';
  532. }
  533. }
  534. });
  535. // Returns true if all the data was flushed to socket. Returns false if
  536. // something was queued. If data was queued, then the "drain" event will
  537. // signal when it has been finally flushed to socket.
  538. Stream.prototype.write = function (data, encoding, fd) {
  539. if (this._writeQueue && this._writeQueue.length) {
  540. // Slow. There is already a write queue, so let's append to it.
  541. if (this._writeQueueLast() === END_OF_FILE) {
  542. throw new Error('Stream.end() called already; cannot write.');
  543. }
  544. if (typeof data == 'string' &&
  545. this._writeQueueEncoding[this._writeQueueEncoding.length-1] === encoding) {
  546. // optimization - concat onto last
  547. this._writeQueue[this._writeQueue.length-1] += data;
  548. } else {
  549. this._writeQueue.push(data);
  550. this._writeQueueEncoding.push(encoding);
  551. }
  552. if (fd != undefined) {
  553. this._writeQueueFD.push(fd);
  554. }
  555. return false;
  556. } else {
  557. // Fast.
  558. // The most common case. There is no write queue. Just push the data
  559. // directly to the socket.
  560. return this._writeOut(data, encoding, fd);
  561. }
  562. };
  563. // Directly writes the data to socket.
  564. //
  565. // Steps:
  566. // 1. If it's a string, write it to the `pool`. (If not space remains
  567. // on the pool make a new one.)
  568. // 2. Write data to socket. Return true if flushed.
  569. // 3. Slice out remaining
  570. // 4. Unshift remaining onto _writeQueue. Return false.
  571. Stream.prototype._writeOut = function (data, encoding, fd) {
  572. if (!this.writable) {
  573. throw new Error('Stream is not writable');
  574. }
  575. var buffer, off, len;
  576. var bytesWritten, charsWritten;
  577. var queuedData = false;
  578. if (typeof data != 'string') {
  579. // 'data' is a buffer, ignore 'encoding'
  580. buffer = data;
  581. off = 0;
  582. len = data.length;
  583. } else {
  584. assert(typeof data == 'string')
  585. if (!pool || pool.length - pool.used < kMinPoolSpace) {
  586. pool = null;
  587. allocNewPool();
  588. }
  589. if (!encoding || encoding == 'utf8' || encoding == 'utf-8') {
  590. // default to utf8
  591. bytesWritten = pool.write(data, 'utf8', pool.used);
  592. charsWritten = Buffer._charsWritten;
  593. } else {
  594. bytesWritten = pool.write(data, encoding, pool.used);
  595. charsWritten = bytesWritten;
  596. }
  597. if (encoding && data.length > 0) {
  598. assert(bytesWritten > 0);
  599. }
  600. buffer = pool;
  601. len = bytesWritten;
  602. off = pool.used;
  603. pool.used += bytesWritten;
  604. debug('wrote ' + bytesWritten + ' bytes to pool');
  605. if (charsWritten != data.length) {
  606. //debug("couldn't fit " + (data.length - charsWritten) + " bytes into the pool\n");
  607. // Unshift whatever didn't fit onto the buffer
  608. this._writeQueue.unshift(data.slice(charsWritten));
  609. this._writeQueueEncoding.unshift(encoding);
  610. this._writeWatcher.start();
  611. queuedData = true;
  612. }
  613. }
  614. try {
  615. bytesWritten = this._writeImpl(buffer, off, len, fd, 0);
  616. } catch (e) {
  617. this.destroy(e);
  618. return false;
  619. }
  620. debug('wrote ' + bytesWritten + ' to socket. [fd, off, len] = ' + JSON.stringify([this.fd, off, len]) + "\n");
  621. timeout.active(this);
  622. if (bytesWritten == len) {
  623. // awesome. sent to buffer.
  624. if (buffer === pool) {
  625. // If we're just writing from the pool then we can make a little
  626. // optimization and save the space.
  627. buffer.used -= len;
  628. }
  629. if (queuedData) {
  630. return false;
  631. } else {
  632. return true;
  633. }
  634. }
  635. // Didn't write the entire thing to buffer.
  636. // Need to wait for the socket to become available before trying again.
  637. this._writeWatcher.start();
  638. // Slice out the data left.
  639. var leftOver = buffer.slice(off + bytesWritten, off + len);
  640. leftOver.used = leftOver.length; // used the whole thing...
  641. // sys.error('data.used = ' + data.used);
  642. //if (!this._writeQueue) initWriteStream(this);
  643. // data should be the next thing to write.
  644. this._writeQueue.unshift(leftOver);
  645. this._writeQueueEncoding.unshift(null);
  646. // If didn't successfully write any bytes, enqueue our fd and try again
  647. if (!bytesWritten) {
  648. this._writeQueueFD.unshift(fd);
  649. }
  650. return false;
  651. }
  652. // Flushes the write buffer out.
  653. // Returns true if the entire buffer was flushed.
  654. Stream.prototype.flush = function () {
  655. while (this._writeQueue && this._writeQueue.length) {
  656. var data = this._writeQueue.shift();
  657. var encoding = this._writeQueueEncoding.shift();
  658. var fd = this._writeQueueFD.shift();
  659. if (data === END_OF_FILE) {
  660. this._shutdown();
  661. return true;
  662. }
  663. var flushed = this._writeOut(data,encoding,fd);
  664. if (!flushed) return false;
  665. }
  666. if (this._writeWatcher) this._writeWatcher.stop();
  667. return true;
  668. };
  669. Stream.prototype.send = function () {
  670. throw new Error('send renamed to write');
  671. };
  672. Stream.prototype._writeQueueLast = function () {
  673. return this._writeQueue.length > 0 ? this._writeQueue[this._writeQueue.length-1]
  674. : null;
  675. };
  676. Stream.prototype.setEncoding = function (encoding) {
  677. var StringDecoder = require("string_decoder").StringDecoder; // lazy load
  678. this._decoder = new StringDecoder(encoding);
  679. };
  680. function doConnect (socket, port, host) {
  681. try {
  682. connect(socket.fd, port, host);
  683. } catch (e) {
  684. socket.destroy(e);
  685. return;
  686. }
  687. debug('connecting to ' + host + ' : ' + port);
  688. // Don't start the read watcher until connection is established
  689. socket._readWatcher.set(socket.fd, true, false);
  690. // How to connect on POSIX: Wait for fd to become writable, then call
  691. // socketError() if there isn't an error, we're connected. AFAIK this a
  692. // platform independent way determining when a non-blocking connection
  693. // is established, but I have only seen it documented in the Linux
  694. // Manual Page connect(2) under the error code EINPROGRESS.
  695. socket._writeWatcher.set(socket.fd, false, true);
  696. socket._writeWatcher.start();
  697. socket._writeWatcher.callback = function () {
  698. var errno = socketError(socket.fd);
  699. if (errno == 0) {
  700. // connection established
  701. socket._connecting = false;
  702. socket.resume();
  703. socket.readable = socket.writable = true;
  704. socket._writeWatcher.callback = _doFlush;
  705. try {
  706. socket.emit('connect');
  707. } catch (e) {
  708. socket.destroy(e);
  709. return;
  710. }
  711. } else if (errno != EINPROGRESS) {
  712. socket.destroy(errnoException(errno, 'connect'));
  713. }
  714. };
  715. }
  716. function isPort (x) { return parseInt(x) >= 0; }
  717. // var stream = new Stream();
  718. // stream.connect(80) - TCP connect to port 80 on the localhost
  719. // stream.connect(80, 'nodejs.org') - TCP connect to port 80 on nodejs.org
  720. // stream.connect('/tmp/socket') - UNIX connect to socket specified by path
  721. Stream.prototype.connect = function () {
  722. var self = this;
  723. initStream(self);
  724. if (self.fd) throw new Error('Stream already opened');
  725. if (!self._readWatcher) throw new Error('No readWatcher');
  726. timeout.active(socket);
  727. self._connecting = true; // set false in doConnect
  728. if (isPort(arguments[0])) {
  729. // TCP
  730. var port = arguments[0];
  731. dns.lookup(arguments[1], function (err, ip, addressType) {
  732. if (err) {
  733. self.emit('error', err);
  734. } else {
  735. self.type = addressType == 4 ? 'tcp4' : 'tcp6';
  736. self.fd = socket(self.type);
  737. doConnect(self, port, ip);
  738. }
  739. });
  740. } else {
  741. // UNIX
  742. self.fd = socket('unix');
  743. self.type = 'unix';
  744. setImplmentationMethods(this);
  745. doConnect(self, arguments[0]);
  746. }
  747. };
  748. Stream.prototype.address = function () {
  749. return getsockname(this.fd);
  750. };
  751. Stream.prototype.setNoDelay = function (v) {
  752. if ((this.type == 'tcp4')||(this.type == 'tcp6')) {
  753. setNoDelay(this.fd, v);
  754. }
  755. };
  756. Stream.prototype.setKeepAlive = function (enable, time) {
  757. if ((this.type == 'tcp4')||(this.type == 'tcp6')) {
  758. var secondDelay = Math.ceil(time/1000);
  759. setKeepAlive(this.fd, enable, secondDelay);
  760. }
  761. };
  762. Stream.prototype.setTimeout = function (msecs) {
  763. if (msecs > 0) {
  764. timeout.enroll(this, msecs);
  765. if (this.fd) { timeout.active(this); }
  766. } else if (msecs === 0) {
  767. timeout.unenroll(this);
  768. }
  769. };
  770. Stream.prototype.pause = function () {
  771. this._readWatcher.stop();
  772. };
  773. Stream.prototype.resume = function () {
  774. if (this.fd === null) throw new Error('Cannot resume() closed Stream.');
  775. this._readWatcher.set(this.fd, true, false);
  776. this._readWatcher.start();
  777. };
  778. var forceCloseWarning;
  779. Stream.prototype.forceClose = function (e) {
  780. if (!forceCloseWarning) {
  781. forceCloseWarning = "forceClose() has been renamed to destroy()";
  782. sys.error(forceCloseWarning);
  783. }
  784. return this.destroy(e);
  785. };
  786. Stream.prototype.destroy = function (exception) {
  787. // pool is shared between sockets, so don't need to free it here.
  788. var self = this;
  789. // TODO would like to set _writeQueue to null to avoid extra object alloc,
  790. // but lots of code assumes this._writeQueue is always an array.
  791. this._writeQueue = [];
  792. this.readable = this.writable = false;
  793. if (this._writeWatcher) {
  794. this._writeWatcher.stop();
  795. ioWatchers.free(this._writeWatcher);
  796. this._writeWatcher = null;
  797. }
  798. if (this._readWatcher) {
  799. this._readWatcher.stop();
  800. ioWatchers.free(this._readWatcher);
  801. this._readWatcher = null;
  802. }
  803. timeout.unenroll(this);
  804. if (this.secure) {
  805. this.secureStream.close();
  806. }
  807. // FIXME Bug when this.fd == 0
  808. if (typeof this.fd == 'number') {
  809. closeFD(this.fd);
  810. this.fd = null;
  811. process.nextTick(function () {
  812. if (exception) self.emit('error', exception);
  813. self.emit('close', exception ? true : false);
  814. });
  815. }
  816. };
  817. Stream.prototype._shutdown = function () {
  818. if (!this.writable) {
  819. throw new Error('The connection is not writable');
  820. } else {
  821. if (this.readable) {
  822. // readable and writable
  823. this.writable = false;
  824. try {
  825. this._shutdownImpl();
  826. } catch (e) {
  827. this.destroy(e);
  828. }
  829. } else {
  830. // writable but not readable
  831. this.destroy();
  832. }
  833. }
  834. };
  835. var closeDepricationWarning;
  836. Stream.prototype.close = function (data, encoding) {
  837. if (!closeDepricationWarning) {
  838. closeDepricationWarning = "Notification: Stream.prototype.close has been renamed to end()";
  839. sys.error(closeDepricationWarning);
  840. }
  841. return this.end(data, encoding);
  842. };
  843. Stream.prototype.end = function (data, encoding) {
  844. if (this.writable) {
  845. if (data) this.write(data, encoding);
  846. if (this._writeQueueLast() !== END_OF_FILE) {
  847. this._writeQueue.push(END_OF_FILE);
  848. this.flush();
  849. }
  850. }
  851. };
  852. function Server (listener) {
  853. events.EventEmitter.call(this);
  854. var self = this;
  855. if (listener) {
  856. self.addListener('connection', listener);
  857. }
  858. self.watcher = new IOWatcher();
  859. self.watcher.host = self;
  860. self.watcher.callback = function () {
  861. while (self.fd) {
  862. try {
  863. var peerInfo = accept(self.fd);
  864. } catch (e) {
  865. if (e.errno == EMFILE) {
  866. waitingForFDs.push(self);
  867. self.watcher.stop();
  868. return;
  869. }
  870. throw e;
  871. }
  872. if (!peerInfo) return;
  873. var s = new Stream(peerInfo.fd, self.type);
  874. s.remoteAddress = peerInfo.address;
  875. s.remotePort = peerInfo.port;
  876. s.type = self.type;
  877. s.server = self;
  878. s.resume();
  879. self.emit('connection', s);
  880. // The 'connect' event probably should be removed for server-side
  881. // sockets. It's redundant.
  882. try {
  883. s.emit('connect');
  884. } catch (e) {
  885. s.destroy(e);
  886. return;
  887. }
  888. }
  889. };
  890. }
  891. sys.inherits(Server, events.EventEmitter);
  892. exports.Server = Server;
  893. exports.createServer = function (listener) {
  894. return new Server(listener);
  895. };
  896. // Listen on a UNIX socket
  897. // server.listen("/tmp/socket");
  898. //
  899. // Listen on port 8000, accept connections from INADDR_ANY.
  900. // server.listen(8000);
  901. //
  902. // Listen on port 8000, accept connections to "192.168.1.2"
  903. // server.listen(8000, "192.168.1.2");
  904. Server.prototype.listen = function () {
  905. var self = this;
  906. if (self.fd) throw new Error('Server already opened');
  907. var lastArg = arguments[arguments.length - 1];
  908. if (typeof lastArg == 'function') {
  909. self.addListener('listening', lastArg);
  910. }
  911. if (!isPort(arguments[0])) {
  912. // the first argument specifies a path
  913. self.fd = socket('unix');
  914. self.type = 'unix';
  915. var path = arguments[0];
  916. self.path = path;
  917. // unlink sockfile if it exists
  918. fs.stat(path, function (err, r) {
  919. if (err) {
  920. if (err.errno == ENOENT) {
  921. bind(self.fd, path);
  922. self._doListen();
  923. } else {
  924. throw r;
  925. }
  926. } else {
  927. if (!r.isSocket()) {
  928. throw new Error("Non-socket exists at " + path);
  929. } else {
  930. fs.unlink(path, function (err) {
  931. if (err) {
  932. throw err;
  933. } else {
  934. bind(self.fd, path);
  935. self._doListen();
  936. }
  937. });
  938. }
  939. }
  940. });
  941. } else if (!arguments[1]) {
  942. // Don't bind(). OS will assign a port with INADDR_ANY.
  943. // The port can be found with server.address()
  944. self.type = 'tcp4';
  945. self.fd = socket(self.type);
  946. bind(self.fd, arguments[0]);
  947. process.nextTick(function () {
  948. self._doListen();
  949. });
  950. } else {
  951. // the first argument is the port, the second an IP
  952. var port = arguments[0];
  953. dns.lookup(arguments[1], function (err, ip, addressType) {
  954. if (err) {
  955. self.emit('error', err);
  956. } else {
  957. self.type = addressType == 4 ? 'tcp4' : 'tcp6';
  958. self.fd = socket(self.type);
  959. bind(self.fd, port, ip);
  960. self._doListen();
  961. }
  962. });
  963. }
  964. };
  965. Server.prototype.listenFD = function (fd, type) {
  966. if (this.fd) {
  967. throw new Error('Server already opened');
  968. }
  969. this.fd = fd;
  970. this.type = type || null;
  971. this._startWatcher();
  972. };
  973. Server.prototype._startWatcher = function () {
  974. this.watcher.set(this.fd, true, false);
  975. this.watcher.start();
  976. this.emit("listening");
  977. };
  978. Server.prototype._doListen = function () {
  979. listen(this.fd, 128);
  980. this._startWatcher();
  981. }
  982. Server.prototype.address = function () {
  983. return getsockname(this.fd);
  984. };
  985. Server.prototype.close = function () {
  986. var self = this;
  987. if (!self.fd) throw new Error('Not running');
  988. self.watcher.stop();
  989. closeFD(self.fd);
  990. self.fd = null;
  991. if (self.type === "unix") {
  992. fs.unlink(self.path, function () {
  993. self.emit("close");
  994. });
  995. } else {
  996. self.emit("close");
  997. }
  998. };
  999. // vim:ts=2 sw=2