PageRenderTime 65ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/server/framework/IO/Socket.js

https://github.com/aptana/Jaxer
JavaScript | 199 lines | 70 code | 27 blank | 102 comment | 9 complexity | bd94cd5f2f70ee5ce86b33124d6dd95e MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-3.0
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: GPL 3
  3. *
  4. * This program is Copyright (C) 2007-2008 Aptana, Inc. All Rights Reserved
  5. * This program is licensed under the GNU General Public license, version 3 (GPL).
  6. *
  7. * This program is distributed in the hope that it will be useful, but
  8. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  10. * NONINFRINGEMENT. Redistribution, except as permitted by the GPL,
  11. * is prohibited.
  12. *
  13. * You can redistribute and/or modify this program under the terms of the GPL,
  14. * as published by the Free Software Foundation. You should
  15. * have received a copy of the GNU General Public License, Version 3 along
  16. * with this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Aptana provides a special exception to allow redistribution of this file
  20. * with certain other code and certain additional terms
  21. * pursuant to Section 7 of the GPL. You may view the exception and these
  22. * terms on the web at http://www.aptana.com/legal/gpl/.
  23. *
  24. * You may view the GPL, and Aptana's exception and additional terms in the file
  25. * titled license-jaxer.html in the main distribution folder of this program.
  26. *
  27. * Any modifications to this file must keep this entire header intact.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. (function() {
  31. var log = Log.forModule("Socket");
  32. /**
  33. * @classDescription {Jaxer.Socket} Network socket utility object for simple
  34. * character-based (non-binary) socket access.
  35. */
  36. /**
  37. * The constructor of a network socket object used for character-based (non-binary) operations
  38. *
  39. * @alias Jaxer.Socket
  40. * @constructor
  41. * @return {Jaxer.Socket}
  42. * Returns an instance of Socket.
  43. */
  44. function Socket()
  45. {
  46. this._transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].
  47. getService(Components.interfaces.nsISocketTransportService);
  48. }
  49. /**
  50. * Open the socket for communication
  51. *
  52. * @alias Jaxer.Socket.prototype.open
  53. * @param {String} host
  54. * The host to connect to
  55. * @param {Number} port
  56. * The port on which to connect
  57. */
  58. Socket.prototype.open = function open(host, port)
  59. {
  60. this._transport = this._transportService.createTransport(null,0,host,port,null);
  61. if (!this._transport) {
  62. throw ("Cannot connect to server '" + host + ":" + port);
  63. }
  64. this._transport.setTimeout(Components.interfaces.nsISocketTransport.TIMEOUT_READ_WRITE, 5);
  65. // Set up output stream
  66. this._ostream = this._transport.openOutputStream(Components.interfaces.nsITransport.OPEN_BLOCKING,0,0);
  67. this._outcharstream = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
  68. createInstance(Components.interfaces.nsIConverterOutputStream);
  69. this._outcharstream.init(this._ostream, "UTF8", 80, 0x0);
  70. // Set up input stream
  71. this._istream = this._transport.openInputStream(Components.interfaces.nsITransport.OPEN_BLOCKING,0,0);
  72. this._incharstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
  73. createInstance(Components.interfaces.nsIConverterInputStream);
  74. this._incharstream.init(this._istream, "UTF8", 80, 0x0);
  75. };
  76. /**
  77. * Close the socket
  78. *
  79. * @alias Jaxer.Socket.prototype.close
  80. */
  81. Socket.prototype.close = function close()
  82. {
  83. try {
  84. this._incharstream.close();
  85. this._outcharstream.close();
  86. }
  87. catch(e) {
  88. // ignore this exception, we're just trying to close this socket down
  89. }
  90. this._transport.close(0);
  91. };
  92. /**
  93. * Write a string to the socket
  94. *
  95. * @alias Jaxer.Socket.prototype.writeString
  96. * @param {String} data
  97. * The text to write
  98. */
  99. Socket.prototype.writeString = function writeString(data)
  100. {
  101. this._outcharstream.writeString(data);
  102. };
  103. /**
  104. * Read a single line from the socket
  105. *
  106. * @alias Jaxer.Socket.prototype.readLine
  107. * @return {String}
  108. * The text read in
  109. */
  110. //Socket.prototype.readLine = function()
  111. //{
  112. // if (this._incharstream instanceof Components.interfaces.nsIUnicharLineInputStream)
  113. // {
  114. // var line = {};
  115. // this._incharstream.readLine(line);
  116. // return line.value;
  117. // }
  118. //};
  119. Socket.prototype.readLine = function readLine()
  120. { // this fake newline function is required to workaround the issue in jxr-140
  121. var buf = "";
  122. var str = {value : "0"};
  123. while (str.value != "")
  124. {
  125. this._incharstream.readString(1, str);
  126. if ( str.value != '\n' && str.value != '\r')
  127. {
  128. buf += str.value;
  129. }
  130. if ( str.value == '\n') {
  131. return buf;
  132. }
  133. }
  134. };
  135. /**
  136. * Read characters from the socket into a string
  137. *
  138. * @alias Jaxer.Socket.prototype.readString
  139. * @param {Number} count
  140. * How many characters to read
  141. * @return {String}
  142. * The text read in
  143. */
  144. Socket.prototype.readString = function readString(count)
  145. {
  146. var str = {}
  147. this._incharstream.readString(count, str);
  148. return str.value;
  149. };
  150. /**
  151. * How many bytes (not characters) are currently available on the stream?
  152. *
  153. * @alias Jaxer.Socket.prototype.available
  154. * @return {Number}
  155. * the number of bytes available
  156. */
  157. Socket.prototype.available = function available()
  158. {
  159. return this._istream.available();
  160. };
  161. /**
  162. * Flush the socket's output stream
  163. *
  164. * @alias Jaxer.Socket.prototype.flush
  165. */
  166. Socket.prototype.flush = function flush()
  167. {
  168. this._outcharstream.flush();
  169. };
  170. frameworkGlobal.Socket = Jaxer.Socket = Socket;
  171. Log.trace("*** Socket.js loaded");
  172. })();