static void make_js_conn(struct v7_val *obj, struct ns_connection *nc) { v7_set_num(s_v7, obj, "nc", (unsigned long) nc); v7_set_func(s_v7, obj, "discard", js_discard); v7_set_func(s_v7, obj, "close", js_close); v7_set_func(s_v7, obj, "send", js_send); v7_set_str(s_v7, obj, "data", nc->recv_iobuf.buf, nc->recv_iobuf.len); }
static void ev_handler(struct ns_connection *nc, enum ns_event ev, void *p) { struct v7 *v7 = (struct v7 *) nc->server->server_data; struct v7_val *jsconn; printf("C handler: %p %d\n", nc, ev); // Call javascript event handler v7_exec(v7, "ev_handler"); jsconn = v7_push(v7, V7_OBJ); v7_push(v7, V7_NUM)->v.num = ev; v7_push(v7, V7_NUM)->v.num = (unsigned long) p; v7_set_num(jsconn, "nc", (unsigned long) nc); v7_set_str(jsconn, "recv_buf", nc->recv_iobuf.buf, nc->recv_iobuf.len); v7_set_str(jsconn, "send_buf", nc->send_iobuf.buf, nc->send_iobuf.len); v7_set_func(jsconn, "write", js_write); v7_set_func(jsconn, "close", js_close); v7_call(v7, v7_top(v7) - 4); // Exit if we receive string 'exit' if (ev == NS_RECV && (* (int *) p) == 4 && memcmp(nc->recv_iobuf.buf + nc->recv_iobuf.len - 4, "exit", 4) == 0) { s_received_signal = 1; } }
int main(int argc, char *argv[]) { int i, error_code; signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); s_v7 = v7_create(); v7_init_stdlib(s_v7); v7_set_func(s_v7, v7_get_root_namespace(s_v7), "WebsocketServer", js_ws); v7_set_func(s_v7, v7_get_root_namespace(s_v7), "RunTcpServer", js_tcp); for (i = 1; i < argc; i++) { if ((error_code = v7_exec_file(s_v7, argv[i])) != V7_OK) { fprintf(stderr, "Error executing %s line %d: %s\n", argv[i], s_v7->line_no, v7_err_to_str(error_code)); exit(EXIT_FAILURE); } } fprintf(stderr, "Existing on signal %d\n", s_received_signal); v7_destroy(&s_v7); return EXIT_SUCCESS; }
void v7_init_stdlib(struct v7 *v7) { v7_set_func(v7, v7_get_root_namespace(v7), "print", &stdlib_print); }