1999-09-06 21:09:47 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-09-06 21:09:47 +04:00
|
|
|
*
|
|
|
|
* The Original Code is JSIRC Library
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is New Dimensions Consulting,
|
1999-11-06 06:43:54 +03:00
|
|
|
* Inc. Portions created by New Dimensions Consulting, Inc. are
|
|
|
|
* Copyright (C) 1999 New Dimenstions Consulting, Inc. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
1999-09-06 21:09:47 +04:00
|
|
|
* Contributor(s):
|
|
|
|
* Robert Ginda, rginda@ndcico.com, original author
|
2000-01-07 08:15:32 +03:00
|
|
|
* Peter Van der Beken, peter.vanderbeken@pandora.be, necko-only version
|
1999-09-06 21:09:47 +04:00
|
|
|
*
|
2001-06-07 04:48:45 +04:00
|
|
|
* depends on utils.js, and XPCOM/XPConnect in the JS environment
|
1999-09-06 21:09:47 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
const NS_ERROR_MODULE_NETWORK = 2152398848;
|
|
|
|
|
|
|
|
const NS_ERROR_UNKNOWN_HOST = NS_ERROR_MODULE_NETWORK + 30;
|
|
|
|
const NS_ERROR_CONNECTION_REFUSED = NS_ERROR_MODULE_NETWORK + 13;
|
|
|
|
const NS_ERROR_NET_TIMEOUT = NS_ERROR_MODULE_NETWORK + 14;
|
|
|
|
|
|
|
|
const NS_NET_STATUS_RESOLVING_HOST = NS_ERROR_MODULE_NETWORK + 3;
|
|
|
|
const NS_NET_STATUS_CONNECTED_TO = NS_ERROR_MODULE_NETWORK + 4;
|
|
|
|
const NS_NET_STATUS_SENDING_TO = NS_ERROR_MODULE_NETWORK + 5;
|
|
|
|
const NS_NET_STATUS_RECEIVING_FROM = NS_ERROR_MODULE_NETWORK + 6;
|
|
|
|
const NS_NET_STATUS_CONNECTING_TO = NS_ERROR_MODULE_NETWORK + 7;
|
|
|
|
|
2000-01-07 08:15:32 +03:00
|
|
|
function toScriptableInputStream (i)
|
|
|
|
{
|
2000-09-14 03:57:52 +04:00
|
|
|
var si = Components.classes["@mozilla.org/scriptableinputstream;1"];
|
2000-01-07 08:15:32 +03:00
|
|
|
|
|
|
|
si = si.createInstance();
|
|
|
|
si = si.QueryInterface(Components.interfaces.nsIScriptableInputStream);
|
|
|
|
si.init(i);
|
|
|
|
|
|
|
|
return si;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
1999-09-06 21:09:47 +04:00
|
|
|
function CBSConnection ()
|
|
|
|
{
|
2000-01-07 08:15:32 +03:00
|
|
|
var sockServiceClass =
|
|
|
|
Components.classesByID["{c07e81e0-ef12-11d2-92b6-00105a1b0d64}"];
|
|
|
|
|
|
|
|
if (!sockServiceClass)
|
|
|
|
throw ("Couldn't get socket service class.");
|
|
|
|
|
|
|
|
var sockService = sockServiceClass.getService();
|
|
|
|
if (!sockService)
|
|
|
|
throw ("Couldn't get socket service.");
|
|
|
|
|
|
|
|
this._sockService = sockService.QueryInterface
|
|
|
|
(Components.interfaces.nsISocketTransportService);
|
1999-09-06 21:09:47 +04:00
|
|
|
|
2000-08-23 03:58:02 +04:00
|
|
|
this.wrappedJSObject = this;
|
|
|
|
|
1999-09-06 21:09:47 +04:00
|
|
|
}
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
CBSConnection.prototype.connect =
|
|
|
|
function bc_connect(host, port, bind, tcp_flag, observer)
|
1999-09-06 21:09:47 +04:00
|
|
|
{
|
|
|
|
if (typeof tcp_flag == "undefined")
|
2000-01-07 08:15:32 +03:00
|
|
|
tcp_flag = false;
|
1999-09-06 21:09:47 +04:00
|
|
|
|
2000-06-01 09:31:04 +04:00
|
|
|
this.host = host.toLowerCase();
|
1999-09-06 21:09:47 +04:00
|
|
|
this.port = port;
|
|
|
|
this.bind = bind;
|
|
|
|
this.tcp_flag = tcp_flag;
|
|
|
|
|
2001-09-13 06:21:05 +04:00
|
|
|
// Lets get a transportInfo for this
|
|
|
|
var pps = Components.classes["@mozilla.org/network/protocol-proxy-service;1"].
|
|
|
|
getService().
|
|
|
|
QueryInterface(Components.interfaces.nsIProtocolProxyService);
|
|
|
|
|
|
|
|
if (!pps)
|
|
|
|
throw ("Couldn't get protocol proxy service");
|
|
|
|
|
|
|
|
var uri = Components.classes["@mozilla.org/network/simple-uri;1"].
|
|
|
|
createInstance(Components.interfaces.nsIURI);
|
|
|
|
uri.spec = "irc:" + host + ':' + port;
|
|
|
|
|
|
|
|
var info = pps.examineForProxy(uri);
|
|
|
|
|
|
|
|
this._transport = this._sockService.createTransport (host, port, info,
|
2001-06-07 04:48:45 +04:00
|
|
|
0, 0);
|
|
|
|
if (!this._transport)
|
|
|
|
throw ("Error creating transport.");
|
2000-01-07 08:15:32 +03:00
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
if (jsenv.HAS_NSPR_EVENTQ)
|
|
|
|
{ /* we've got an event queue, so start up an async write */
|
|
|
|
this._streamProvider = new StreamProvider (observer);
|
|
|
|
this._write_req =
|
|
|
|
this._transport.asyncWrite (this._streamProvider, this,
|
|
|
|
0, -1, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* no nspr event queues in this environment, we can't use async calls,
|
|
|
|
* so set up the streams. */
|
|
|
|
this._outputStream = this._transport.openOutputStream(0, -1, 0);
|
|
|
|
if (!this._outputStream)
|
|
|
|
throw "Error getting output stream.";
|
|
|
|
this._inputStream =
|
|
|
|
toScriptableInputStream(this._transport.openInputStream (0, -1, 0));
|
|
|
|
if (!this._inputStream)
|
|
|
|
throw "Error getting input stream.";
|
|
|
|
}
|
2000-01-07 08:15:32 +03:00
|
|
|
|
2001-03-14 05:35:29 +03:00
|
|
|
this.connectDate = new Date();
|
2000-01-07 08:15:32 +03:00
|
|
|
this.isConnected = true;
|
1999-09-06 21:09:47 +04:00
|
|
|
|
|
|
|
return this.isConnected;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
CBSConnection.prototype.disconnect =
|
|
|
|
function bc_disconnect()
|
|
|
|
{
|
2001-08-24 00:27:24 +04:00
|
|
|
this._inputStream.close();
|
|
|
|
/*
|
|
|
|
this._streamProvider.close();
|
|
|
|
if (this._streamProvider.isBlocked)
|
|
|
|
this._write_req.resume();
|
|
|
|
*/
|
1999-09-06 21:09:47 +04:00
|
|
|
}
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
CBSConnection.prototype.sendData =
|
|
|
|
function bc_senddata(str)
|
1999-09-06 21:09:47 +04:00
|
|
|
{
|
1999-09-15 06:04:24 +04:00
|
|
|
if (!this.isConnected)
|
|
|
|
throw "Not Connected.";
|
1999-09-06 21:09:47 +04:00
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
if (jsenv.HAS_NSPR_EVENTQ)
|
|
|
|
this.asyncWrite (str);
|
|
|
|
else
|
|
|
|
this.sendDataNow (str);
|
|
|
|
}
|
2000-01-07 08:15:32 +03:00
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
CBSConnection.prototype.readData =
|
|
|
|
function bc_readdata(timeout, count)
|
|
|
|
{
|
|
|
|
if (!this.isConnected)
|
|
|
|
throw "Not Connected.";
|
|
|
|
|
|
|
|
var rv;
|
|
|
|
|
1999-09-06 21:09:47 +04:00
|
|
|
try
|
|
|
|
{
|
2001-06-07 04:48:45 +04:00
|
|
|
rv = this._inputStream.read (count);
|
1999-09-06 21:09:47 +04:00
|
|
|
}
|
|
|
|
catch (ex)
|
|
|
|
{
|
2001-06-07 04:48:45 +04:00
|
|
|
dd ("*** Caught " + ex + " while reading.")
|
|
|
|
this.isConnected = false;
|
|
|
|
throw (ex);
|
1999-09-06 21:09:47 +04:00
|
|
|
}
|
2000-01-07 08:15:32 +03:00
|
|
|
|
|
|
|
return rv;
|
1999-09-06 21:09:47 +04:00
|
|
|
}
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
CBSConnection.prototype.startAsyncRead =
|
|
|
|
function bc_saread (observer)
|
1999-09-06 21:09:47 +04:00
|
|
|
{
|
2001-06-07 04:48:45 +04:00
|
|
|
this._transport.asyncRead (new StreamListener (observer), this, 0, -1, 0);
|
|
|
|
}
|
1999-09-15 06:04:24 +04:00
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
CBSConnection.prototype.asyncWrite =
|
|
|
|
function bc_awrite (str)
|
|
|
|
{
|
|
|
|
this._streamProvider.pendingData += str;
|
|
|
|
if (this._streamProvider.isBlocked)
|
2000-06-01 09:31:04 +04:00
|
|
|
{
|
2001-06-07 04:48:45 +04:00
|
|
|
this._write_req.resume();
|
|
|
|
this._streamProvider.isBlocked = false;
|
2000-06-01 09:31:04 +04:00
|
|
|
}
|
2001-06-07 04:48:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CBSConnection.prototype.hasPendingWrite =
|
|
|
|
function bc_haspwrite ()
|
|
|
|
{
|
|
|
|
return (this._streamProvider.pendingData != "");
|
|
|
|
}
|
2000-01-07 08:15:32 +03:00
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
CBSConnection.prototype.sendDataNow =
|
|
|
|
function bc_senddatanow(str)
|
|
|
|
{
|
|
|
|
var rv = false;
|
|
|
|
|
2000-06-01 09:31:04 +04:00
|
|
|
try
|
|
|
|
{
|
2001-06-07 04:48:45 +04:00
|
|
|
this._outputStream.write(str, str.length);
|
|
|
|
rv = true;
|
2000-06-01 09:31:04 +04:00
|
|
|
}
|
|
|
|
catch (ex)
|
|
|
|
{
|
2001-06-07 04:48:45 +04:00
|
|
|
dd ("*** Caught " + ex + " while sending.")
|
|
|
|
this.isConnected = false;
|
|
|
|
throw (ex);
|
1999-09-06 21:09:47 +04:00
|
|
|
}
|
2000-01-07 08:15:32 +03:00
|
|
|
|
1999-09-06 21:09:47 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2000-06-01 09:31:04 +04:00
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
function _notimpl ()
|
2000-06-01 09:31:04 +04:00
|
|
|
{
|
2001-06-07 04:48:45 +04:00
|
|
|
throw "Not Implemented.";
|
2000-06-01 09:31:04 +04:00
|
|
|
}
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
if (!jsenv.HAS_NSPR_EVENTQ)
|
|
|
|
{
|
|
|
|
CBSConnection.prototype.startAsyncRead = _notimpl;
|
|
|
|
CBSConnection.prototype.asyncWrite = _notimpl;
|
|
|
|
}
|
|
|
|
else
|
2000-06-01 09:31:04 +04:00
|
|
|
{
|
2001-06-07 04:48:45 +04:00
|
|
|
CBSConnection.prototype.sendDataNow = _notimpl;
|
2000-06-01 09:31:04 +04:00
|
|
|
}
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
delete _notimpl;
|
|
|
|
|
|
|
|
function StreamProvider(observer)
|
2000-06-01 09:31:04 +04:00
|
|
|
{
|
2001-06-07 04:48:45 +04:00
|
|
|
this._observer = observer;
|
2000-06-01 09:31:04 +04:00
|
|
|
}
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
StreamProvider.prototype.pendingData = "";
|
|
|
|
StreamProvider.prototype.isBlocked = true;
|
|
|
|
|
2001-08-24 00:27:24 +04:00
|
|
|
StreamProvider.prototype.close =
|
|
|
|
function sp_close ()
|
|
|
|
{
|
|
|
|
this.isClosed = true;
|
|
|
|
}
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
StreamProvider.prototype.onDataWritable =
|
|
|
|
function sp_datawrite (request, ctxt, ostream, offset, count)
|
2000-06-01 09:31:04 +04:00
|
|
|
{
|
2001-08-24 00:27:24 +04:00
|
|
|
//dd ("StreamProvider.prototype.onDataWritable");
|
|
|
|
|
|
|
|
if (this.isClosed)
|
|
|
|
return Components.results.NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
if (!this.pendingData)
|
2001-03-14 05:35:29 +03:00
|
|
|
{
|
2001-06-07 04:48:45 +04:00
|
|
|
this.isBlocked = true;
|
|
|
|
|
|
|
|
/* this is here to support pre-XPCDOM builds (0.9.0 era), which
|
|
|
|
* don't have this result code mapped. */
|
|
|
|
if (!Components.results.NS_BASE_STREAM_WOULD_BLOCK)
|
|
|
|
throw 2152136711;
|
|
|
|
|
|
|
|
throw Components.results.NS_BASE_STREAM_WOULD_BLOCK;
|
2001-03-14 05:35:29 +03:00
|
|
|
}
|
2001-06-07 04:48:45 +04:00
|
|
|
|
|
|
|
var len = ostream.write (this.pendingData, this.pendingData.length);
|
|
|
|
this.pendingData = this.pendingData.substr (len);
|
|
|
|
}
|
2001-03-14 05:35:29 +03:00
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
StreamProvider.prototype.onStartRequest =
|
|
|
|
function sp_startreq (request, ctxt)
|
|
|
|
{
|
|
|
|
//dd ("StreamProvider::onStartRequest: " + request + ", " + ctxt);
|
|
|
|
}
|
2001-03-14 05:35:29 +03:00
|
|
|
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
StreamProvider.prototype.onStopRequest =
|
|
|
|
function sp_stopreq (request, ctxt, status)
|
|
|
|
{
|
|
|
|
//dd ("StreamProvider::onStopRequest: " + request + ", " + ctxt + ", " +
|
|
|
|
// status);
|
2001-08-24 00:27:24 +04:00
|
|
|
if (this._observer)
|
|
|
|
this._observer.onStreamClose(status);
|
2001-06-07 04:48:45 +04:00
|
|
|
}
|
2001-03-14 05:35:29 +03:00
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
function StreamListener(observer)
|
|
|
|
{
|
|
|
|
this._observer = observer;
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamListener.prototype.onStartRequest =
|
|
|
|
function sl_startreq (request, ctxt)
|
|
|
|
{
|
|
|
|
//dd ("StreamListener::onStartRequest: " + request + ", " + ctxt);
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamListener.prototype.onStopRequest =
|
|
|
|
function sl_stopreq (request, ctxt, status)
|
|
|
|
{
|
|
|
|
//dd ("StreamListener::onStopRequest: " + request + ", " + ctxt + ", " +
|
|
|
|
//status);
|
|
|
|
if (this._observer)
|
|
|
|
this._observer.onStreamClose(status);
|
2000-06-01 09:31:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamListener.prototype.onDataAvailable =
|
2001-06-07 04:48:45 +04:00
|
|
|
function sl_dataavail (request, ctxt, inStr, sourceOffset, count)
|
2000-06-01 09:31:04 +04:00
|
|
|
{
|
2000-08-23 03:58:02 +04:00
|
|
|
ctxt = ctxt.wrappedJSObject;
|
|
|
|
if (!ctxt)
|
|
|
|
{
|
|
|
|
dd ("*** Can't get wrappedJSObject from ctxt in " +
|
|
|
|
"StreamListener.onDataAvailable ***");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!ctxt._inputStream)
|
2000-06-01 09:31:04 +04:00
|
|
|
ctxt._inputStream = toScriptableInputStream (inStr);
|
|
|
|
|
2001-06-07 04:48:45 +04:00
|
|
|
if (this._observer)
|
|
|
|
this._observer.onStreamDataAvailable(request, inStr, sourceOffset,
|
|
|
|
count);
|
2000-06-01 09:31:04 +04:00
|
|
|
}
|