PageRenderTime 64ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/xultris/content/Socket.js

https://bitbucket.org/activestate/komodo-3rdparty
JavaScript | 178 lines | 125 code | 28 blank | 25 comment | 16 complexity | 94fc0b580c371bb354da27d4b76f7bb7 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. *
  13. * Contributor(s): Jeremie Miller, jer@jabber.org
  14. * Rob Ginda, rginda@netscape.com
  15. * Eric Murphy, ericmurphy@jabber.org
  16. *
  17. * Date: 2001.01.02
  18. *
  19. */
  20. function Socket()
  21. {
  22. var sockServiceClass =
  23. Components.classesByID["{c07e81e0-ef12-11d2-92b6-00105a1b0d64}"];
  24. if (!sockServiceClass) {
  25. dump("Couldn't get socket service class.");
  26. throw ("Couldn't get socket service class.");
  27. }
  28. var sockService = sockServiceClass.getService();
  29. if (!sockService) {
  30. dump ("Couldn't get socket service.");
  31. throw ("Couldn't get socket service.");
  32. }
  33. this._sockService = sockService.QueryInterface
  34. (Components.interfaces.nsISocketTransportService);
  35. // to preserve ourselves within necko/async
  36. this.wrappedJSObject = this;
  37. }
  38. Socket.prototype.open = function(host, port)
  39. {
  40. this.host = host.toLowerCase();
  41. this.port = port;
  42. this._channel = this._sockService.createTransport (host, port, null, -1,
  43. 0, 0);
  44. if (!this._channel) {
  45. dump ("Error opening channel.");
  46. throw ("Error opening channel.");
  47. }
  48. this._outputStream = this._channel.openOutputStream(0);
  49. if (!this._outputStream) {
  50. dump ("Error getting output stream.");
  51. throw ("Error getting output stream.");
  52. }
  53. this.isConnected = true;
  54. return this.isConnected;
  55. }
  56. Socket.prototype.close = function()
  57. {
  58. if (this.isConnected) {
  59. this.isConnected = false;
  60. //this._inputStream.close();
  61. this._outputStream.close();
  62. }
  63. }
  64. Socket.prototype.write = function(str)
  65. {
  66. if (!this.isConnected)
  67. throw "Not Connected.";
  68. var rv = false;
  69. try
  70. {
  71. this._outputStream.write(str, str.length);
  72. rv = true;
  73. }
  74. catch (ex)
  75. {
  76. if (typeof ex != "undefined")
  77. {
  78. this.isConnected = false;
  79. throw (ex);
  80. }
  81. else
  82. rv = false;
  83. }
  84. return rv;
  85. }
  86. Socket.prototype.read = function(timeout)
  87. {
  88. if (!this.isConnected)
  89. throw "Not Connected.";
  90. if (!this._inputStream)
  91. {
  92. this._inputStream =
  93. this.toScriptableInputStream(this._channel.openInputStream (0, 0));
  94. if (!this._inputStream)
  95. throw ("Error getting input stream.");
  96. }
  97. var rv, av;
  98. try
  99. {
  100. av = this._inputStream.available();
  101. if (av)
  102. rv = this._inputStream.read (av);
  103. else
  104. rv = "";
  105. }
  106. catch (ex)
  107. {
  108. if (typeof ex != "undefined") {
  109. this.isConnected = false;
  110. throw (ex);
  111. } else {
  112. rv = "";
  113. }
  114. }
  115. return rv;
  116. }
  117. Socket.prototype.async = function(handler)
  118. {
  119. this.async = handler;
  120. this._channel.asyncRead (new SocketListener(), this);
  121. }
  122. // async callbacks
  123. function SocketListener(){}
  124. SocketListener.prototype.onStartRequest = function(channel, ctxt)
  125. {
  126. // dd ("onStartRequest: " + channel + ", " + ctxt);
  127. }
  128. SocketListener.prototype.onStopRequest = function(channel, ctxt, status, errorMsg)
  129. {
  130. // dd ("onStopRequest: " + channel + ", " + ctxt + ", " + status + ", " + errorMsg+"\n");
  131. ctxt = ctxt.wrappedJSObject;
  132. ctxt.async(""); // signal socket failed?
  133. }
  134. SocketListener.prototype.onDataAvailable = function(channel, ctxt, inStr, sourceOffset, count)
  135. {
  136. dump ("onDataAvailable: " + channel + ", " + ctxt + ", " + ctxt.wrappedJSObject + ", " + inStr + ", " + sourceOffset + ", " + count);
  137. ctxt = ctxt.wrappedJSObject;
  138. if (!ctxt._inputStream)
  139. ctxt._inputStream = ctxt.toScriptableInputStream(inStr);
  140. ctxt.async(ctxt.read());
  141. }
  142. // util
  143. Socket.prototype.toScriptableInputStream = function(i)
  144. {
  145. var si = Components.classes["@mozilla.org/scriptableinputstream;1"];
  146. si = si.createInstance();
  147. si = si.QueryInterface(Components.interfaces.nsIScriptableInputStream);
  148. si.init(i);
  149. return si;
  150. }