Using ctypes function in firefox ChromeWorker

340 views Asked by At

I am trying to use a ChromeWorker to launch a background process, like so:

var worker = new ChromeWorker(data.url("stun-manager.js"));
worker.addEventListener('message', function(e) {
    console.log(e.data);
}, false);

worker.postMessage({'cmd': 'start', 'msg': 'Hi'});

But where exactly do I declare all my ctypes and such? Interestingly, in stun-manager.js, if I have the following:

dump ("Message 1");
var {Cu} = require("chrome");
dump ("Message 2");

/*import js-ctypes */
var {ctypes} = Cu.import("resource://gre/modules/ctypes.jsm")
var stun_driver = ctypes.open("C:\\Users\\derek_000\\Documents\\Visual Studio 2012\\Projects\\stunnel507\\stunnel507\\bin\\win32\\stun_driver.dll");

const launch_stun = stun_driver.declare("launch_stun", ctypes.default_abi, ctypes.int32_t,  ctypes.int32_t, ctypes.char.ptr.ptr);

let argv_t = ctypes.ArrayType(ctypes.char.ptr);
let argc = 2;
let argv = new argv_t(argc);
var conf_path = "C:\\Users\\derek_000\\Documents\\Visual Studio 2012\\Projects\\stunnel507\\stunnel507\\stunnel.conf";
argv[0] = ctypes.char.array()(conf_path);
argv[1] = ctypes.char.array()(conf_path);

self.addEventListener('message', function (e) {
    var data = e.data;
    switch (data.cmd) {
        case 'start':
            self.postMessage("Value of launch_stun " + self.launch_stun);
            self.postMessage('WORKER STARTED: ' + data.msg);
            self.postMessage("debug" + self.argv_t);
            self.postMessage("test: " + self.argv_t);
            self.postMessage(self.argv[0].readString());
            launch_stun(argc, argv );
            break;
        case 'stop':
            self.postMessage('WORKER STOPPED: ' + data.msg +
                             '. (buttons will no longer work)');
            self.close(); // Terminates the worker.
            break;
        default:
            self.postMessage('Unknown command: ' + data.msg);
    };
}, false);

"Message 2" is never printed to the screen, and "Message 1" is. It's almost like I am getting a silent fail from one of those other lines, but this is the exact code I have used to launch this from main.js, before I tried to use ChromeWorker.

Any ideas? I guess it seems like stun-manager.js is failing silently, and on a related note, I can't even find it in the Browser Toolbox to debug, but I do see my main.js file there.

1

There are 1 answers

10
Noitidart On BEST ANSWER

Two simple examples of using ChromeWorker (and also an advancement of ChromeWorker, PromiseWorker, which rocks by the way):

You can't do var {ctypes} = Cu.import("resource://gre/modules/ctypes.jsm") in the worker (your worker is stun-manager.js). That's causing an error, remove that. You can just use ctypes without importing anything, ChromeWorker's autoamtically have it. That should fix it.

I'll look more over your code, but definitely check out the ChromeWorker repo it will help a lot, its a very basic messaging thing between worker and main.