gecko/dom/network/TCPServerSocket.js
Birunthan Mohanathas 882fe73908 Bug 1058101 - Flatten dom/network/src/ into parent directory. r=mccr8
--HG--
rename : dom/network/src/Connection.cpp => dom/network/Connection.cpp
rename : dom/network/src/Connection.h => dom/network/Connection.h
rename : dom/network/src/Constants.h => dom/network/Constants.h
rename : dom/network/src/NetUtils.cpp => dom/network/NetUtils.cpp
rename : dom/network/src/NetUtils.h => dom/network/NetUtils.h
rename : dom/network/src/NetworkStatsDB.jsm => dom/network/NetworkStatsDB.jsm
rename : dom/network/src/NetworkStatsManager.js => dom/network/NetworkStatsManager.js
rename : dom/network/src/NetworkStatsManager.manifest => dom/network/NetworkStatsManager.manifest
rename : dom/network/src/NetworkStatsService.jsm => dom/network/NetworkStatsService.jsm
rename : dom/network/src/NetworkStatsServiceProxy.js => dom/network/NetworkStatsServiceProxy.js
rename : dom/network/src/NetworkStatsServiceProxy.manifest => dom/network/NetworkStatsServiceProxy.manifest
rename : dom/network/src/PTCPServerSocket.ipdl => dom/network/PTCPServerSocket.ipdl
rename : dom/network/src/PTCPSocket.ipdl => dom/network/PTCPSocket.ipdl
rename : dom/network/src/PUDPSocket.ipdl => dom/network/PUDPSocket.ipdl
rename : dom/network/src/TCPServerSocket.js => dom/network/TCPServerSocket.js
rename : dom/network/src/TCPServerSocketChild.cpp => dom/network/TCPServerSocketChild.cpp
rename : dom/network/src/TCPServerSocketChild.h => dom/network/TCPServerSocketChild.h
rename : dom/network/src/TCPServerSocketParent.cpp => dom/network/TCPServerSocketParent.cpp
rename : dom/network/src/TCPServerSocketParent.h => dom/network/TCPServerSocketParent.h
rename : dom/network/src/TCPSocket.js => dom/network/TCPSocket.js
rename : dom/network/src/TCPSocket.manifest => dom/network/TCPSocket.manifest
rename : dom/network/src/TCPSocketChild.cpp => dom/network/TCPSocketChild.cpp
rename : dom/network/src/TCPSocketChild.h => dom/network/TCPSocketChild.h
rename : dom/network/src/TCPSocketParent.cpp => dom/network/TCPSocketParent.cpp
rename : dom/network/src/TCPSocketParent.h => dom/network/TCPSocketParent.h
rename : dom/network/src/TCPSocketParentIntermediary.js => dom/network/TCPSocketParentIntermediary.js
rename : dom/network/src/Types.h => dom/network/Types.h
rename : dom/network/src/UDPSocket.cpp => dom/network/UDPSocket.cpp
rename : dom/network/src/UDPSocket.h => dom/network/UDPSocket.h
rename : dom/network/src/UDPSocketChild.cpp => dom/network/UDPSocketChild.cpp
rename : dom/network/src/UDPSocketChild.h => dom/network/UDPSocketChild.h
rename : dom/network/src/UDPSocketParent.cpp => dom/network/UDPSocketParent.cpp
rename : dom/network/src/UDPSocketParent.h => dom/network/UDPSocketParent.h
2014-08-27 10:13:39 -07:00

183 lines
5.0 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cr = Components.results;
const CC = Components.Constructor;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
const ServerSocket = CC(
'@mozilla.org/network/server-socket;1', 'nsIServerSocket', 'init'),
TCPSocketInternal = Cc[
'@mozilla.org/tcp-socket;1'].createInstance(Ci.nsITCPSocketInternal);
/*
* Debug logging function
*/
let debug = true;
function LOG(msg) {
if (debug) {
dump("TCPServerSocket: " + msg + "\n");
}
}
/*
* nsIDOMTCPServerSocket object
*/
function TCPServerSocket() {
this._localPort = 0;
this._binaryType = null;
this._onconnect = null;
this._onerror = null;
this._inChild = false;
this._neckoTCPServerSocket = null;
this._serverBridge = null;
this.useWin = null;
}
TCPServerSocket.prototype = {
__exposedProps__: {
port: 'r',
onconnect: 'rw',
onerror: 'rw'
},
get localPort() {
return this._localPort;
},
get onconnect() {
return this._onconnect;
},
set onconnect(f) {
this._onconnect = f;
},
get onerror() {
return this._onerror;
},
set onerror(f) {
this._onerror = f;
},
_callListenerAcceptCommon: function tss_callListenerAcceptCommon(socket) {
if (this._onconnect) {
try {
this["onconnect"].call(null, socket);
} catch (e) {
socket.close();
}
}
else {
socket.close();
dump("Received unexpected connection!");
}
},
init: function tss_init(aWindowObj) {
this.useWin = aWindowObj;
},
/* nsITCPServerSocketInternal method */
listen: function tss_listen(localPort, options, backlog) {
this._inChild = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
this._binaryType = options.binaryType;
if (this._inChild) {
if (this._serverBridge == null) {
this._serverBridge = Cc["@mozilla.org/tcp-server-socket-child;1"]
.createInstance(Ci.nsITCPServerSocketChild);
this._serverBridge.listen(this, localPort, backlog, options.binaryType);
}
else {
throw new Error("Child TCPServerSocket has already listening. \n");
}
}
else {
if (this._neckoTCPServerSocket == null) {
this._neckoTCPServerSocket = new ServerSocket(localPort, false, backlog);
this._localPort = this._neckoTCPServerSocket.port;
this._neckoTCPServerSocket.asyncListen(this);
}
else {
throw new Error("Parent TCPServerSocket has already listening. \n");
}
}
},
callListenerAccept: function tss_callListenerSocket(socketChild) {
// this method is called at child process when the socket is accepted at parent process.
let socket = TCPSocketInternal.createAcceptedChild(socketChild, this._binaryType, this.useWin);
this._callListenerAcceptCommon(socket);
},
callListenerError: function tss_callListenerError(message, filename, lineNumber, columnNumber) {
if (this._onerror) {
var type = "error";
var error = new Error(message, filename, lineNumber, columnNumber);
this["onerror"].call(null, new TCPSocketEvent(type, this, error));
}
},
/* end nsITCPServerSocketInternal method */
close: function tss_close() {
if (this._inChild) {
this._serverBridge.close();
return;
}
/* Close ServerSocket */
if (this._neckoTCPServerSocket) {
this._neckoTCPServerSocket.close();
}
},
// nsIServerSocketListener (Triggered by _neckoTCPServerSocket.asyncListen)
onSocketAccepted: function tss_onSocketAccepted(server, trans) {
// precondition: this._inChild == false
try {
let that = TCPSocketInternal.createAcceptedParent(trans, this._binaryType);
this._callListenerAcceptCommon(that);
}
catch(e) {
trans.close(Cr.NS_BINDING_ABORTED);
}
},
// nsIServerSocketListener (Triggered by _neckoTCPServerSocket.asyncListen)
onStopListening: function tss_onStopListening(server, status) {
if (status != Cr.NS_BINDING_ABORTED) {
throw new Error("Server socket was closed by unexpected reason.");
}
this._neckoTCPServerSocket = null;
},
classID: Components.ID("{73065eae-27dc-11e2-895a-000c29987aa2}"),
classInfo: XPCOMUtils.generateCI({
classID: Components.ID("{73065eae-27dc-11e2-895a-000c29987aa2}"),
classDescription: "Server TCP Socket",
interfaces: [
Ci.nsIDOMTCPServerSocket,
Ci.nsISupportsWeakReference
],
flags: Ci.nsIClassInfo.DOM_OBJECT,
}),
QueryInterface: XPCOMUtils.generateQI([
Ci.nsIDOMTCPServerSocket,
Ci.nsITCPServerSocketInternal,
Ci.nsISupportsWeakReference
])
}
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TCPServerSocket]);