No more seg faults!

This commit is contained in:
John Shaver 2018-04-20 10:29:09 -07:00
parent f8bbfbb2d2
commit 38bdc76ab2
2 changed files with 31 additions and 21 deletions

28
main.c
View file

@ -1,5 +1,8 @@
#include <node_api.h> #include <node/node_api.h>
#include <stdio.h> #include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <unistd.h>
#include <pulse/pulseaudio.h> #include <pulse/pulseaudio.h>
struct PACBData { struct PACBData {
@ -25,15 +28,18 @@ napi_value connectToPulse(napi_env env, napi_callback_info info) {
napi_value args[1]; napi_value args[1];
struct PACBData *paCBData; struct PACBData *paCBData;
napi_status status; napi_status status;
struct NapiCBData *napiCBData = malloc(sizeof(*napiCBData)); struct NapiCBData *napiCBData = malloc(sizeof(struct NapiCBData));
napiCBData->cbRef = malloc(sizeof(napi_ref));
status = napi_get_cb_info(env, info, &argc, args, NULL, (void**)&paCBData); status = napi_get_cb_info(env, info, &argc, args, NULL, (void**)&paCBData);
assert(argc == 1);
assert(status == napi_ok); assert(status == napi_ok);
printf("finished getting calback info... \n");
napiCBData->env = &env; napiCBData->env = &env;
status = napi_create_reference(env, args[0], 1, napiCBData->cbRef); status = napi_create_reference(env, *args, 1, napiCBData->cbRef);
assert(status == napi_ok); assert(status == napi_ok);
int ret = 1; int ret = 1;
@ -50,6 +56,7 @@ napi_value connectToPulse(napi_env env, napi_callback_info info) {
} }
void connectToPulseCB(pa_context *context, void *userdata) { void connectToPulseCB(pa_context *context, void *userdata) {
printf("Callback called... \n");
napi_status status; napi_status status;
napi_value argv[1]; napi_value argv[1];
struct NapiCBData *napiCBData = (struct NapiCBData*)userdata; struct NapiCBData *napiCBData = (struct NapiCBData*)userdata;
@ -69,6 +76,7 @@ void connectToPulseCB(pa_context *context, void *userdata) {
status = napi_create_error(*(napiCBData->env), NULL, errorString, &argv[0]); status = napi_create_error(*(napiCBData->env), NULL, errorString, &argv[0]);
assert(status == napi_ok); assert(status == napi_ok);
} else { } else {
return;
//eventually need to look into reconnecting on disconnections and handling //eventually need to look into reconnecting on disconnections and handling
//connection failures. //connection failures.
} }
@ -80,6 +88,7 @@ void connectToPulseCB(pa_context *context, void *userdata) {
status = napi_get_reference_value(*(napiCBData->env), *(napiCBData->cbRef), &cb); status = napi_get_reference_value(*(napiCBData->env), *(napiCBData->cbRef), &cb);
assert(status == napi_ok); assert(status == napi_ok);
printf("Calling callback from native... \n");
status = napi_call_function(*(napiCBData->env), global, cb, 1, argv, &result); status = napi_call_function(*(napiCBData->env), global, cb, 1, argv, &result);
assert(status == napi_ok); assert(status == napi_ok);
} }
@ -109,7 +118,18 @@ void connectToPulseCB(pa_context *context, void *userdata) {
// return; // return;
//} //}
void handler(int sig) {
void *array[10];
size_t size;
size = backtrace(array, 10);
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
napi_value init(napi_env env, napi_value exports) { napi_value init(napi_env env, napi_value exports) {
signal(SIGSEGV, handler);
pa_mainloop *mainloop = pa_mainloop_new(); pa_mainloop *mainloop = pa_mainloop_new();
pa_mainloop_api *mainloop_api = pa_mainloop_get_api(mainloop); pa_mainloop_api *mainloop_api = pa_mainloop_get_api(mainloop);
pa_context *context = pa_context_new(mainloop_api, NULL); pa_context *context = pa_context_new(mainloop_api, NULL);

24
main.js
View file

@ -1,18 +1,22 @@
const nativeCode = require('bindings')('miccontrol.node'); const nativeCode = require('bindings')('miccontrol.node');
const {promisify} = require("util");
let connectionStarted = false; let connectionStarted = false;
let connected = false; let connected = false;
let queuedRequests = []; let queuedRequests = [];
let aAddon = [ let aAddon = [
"connectToPulse", "connectToPulse",
"getSources" //"getSources"
].reduce((obj, fn) => { ].reduce((obj, fn) => {
obj[fn] = asyncify(nativeCode[fn]); obj[fn] = promisify(nativeCode[fn]);
return obj; return obj;
}, {}); }, {});
module.exports = () => { module.exports = () => {
console.log("Starting connection");
if(!connectionStarted) { if(!connectionStarted) {
aAddon.connectToPulse().then((e) => { nativeCode.connectToPulse((e) => {
console.log("CONNECTED!");
if(e) { if(e) {
connectionStarted = false; connectionStarted = false;
} else { } else {
@ -64,17 +68,3 @@ module.exports = () => {
return miccontrol; return miccontrol;
} }
function asyncify(fn) {
return async function(...args) {
return new Promise((res, rej) => {
fn(...args, (e, value) => {
if(e) {
return rej(e);
}
return res(value);
});;
});
};
}