CBSConnection Prototype

Wraps up various mechanics of sockets for easy consumption by other code.

Syntax

var object = new CBSConnection(binary);

Arguments

ArgumentSummary
binary Provide true or false here to override the automatic selection of binary or text streams. This should only ever be specified as true or omitted, otherwise you will be shooting yourself in the foot on some versions - let the code handle the choice unless you know you need binary.

Remarks

Members

MemberSummary
accept
asyncWrite
close
connect
disconnect
getCertificate
getSecurityState Gets an array containing information about the security of the connection.
hasPendingWrite
listen
readData
sendData
sendDataNow
startAsyncRead

See Also

Source Code

function CBSConnection (binary)
{
/* Since 2003-01-17 18:14, Mozilla has had this contract ID for the STS.
* Prior to that it didn't have one, so we also include the CID for the
* STS back then - DO NOT UPDATE THE ID if it changes in Mozilla.
*/
const sockClassByName =
Components.classes["@mozilla.org/network/socket-transport-service;1"];
const sockClassByID =
Components.classesByID["{c07e81e0-ef12-11d2-92b6-00105a1b0d64}"];
var sockServiceClass = (sockClassByName || sockClassByID);
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);
/* Note: as part of the mess from bug 315288 and bug 316178, ChatZilla now
* uses the *binary* stream interfaces for all network
* communications.
*
* However, these interfaces do not exist prior to 1999-11-05. To
* make matters worse, an incompatible change to the "readBytes"
* method of this interface was made on 2003-03-13; luckly, this
* change also added a "readByteArray" method, which we will check
* for below, to determine if we can use the binary streams.
*/
// We want to check for working binary streams only the first time.
if (CBSConnection.prototype.workingBinaryStreams == -1)
{
CBSConnection.prototype.workingBinaryStreams = false;
if (typeof nsIBinaryInputStream != "undefined")
{
var isCls = Components.classes["@mozilla.org/binaryinputstream;1"];
var inputStream = isCls.createInstance(nsIBinaryInputStream);
if ("readByteArray" in inputStream)
CBSConnection.prototype.workingBinaryStreams = true;
}
}
/*
* As part of the changes in Gecko 1.9, invalid SSL certificates now
* produce a horrible error message. We must look up the toolkit version
* to see if we need to catch these errors cleanly - see bug 454966.
*/
if (!("strictSSL" in CBSConnection.prototype))
{
CBSConnection.prototype.strictSSL = false;
var app = getService("@mozilla.org/xre/app-info;1", "nsIXULAppInfo");
if (app && ("platformVersion" in app) &&
compareVersions("1.9", app.platformVersion) >= 0)
{
CBSConnection.prototype.strictSSL = true;
}
}
this.wrappedJSObject = this;
if (typeof binary != "undefined")
this.binaryMode = binary;
else
this.binaryMode = this.workingBinaryStreams;
if (!ASSERT(!this.binaryMode || this.workingBinaryStreams,
"Unable to use binary streams in this build."))
{
throw ("Unable to use binary streams in this build.");
}
}