forked from Niap/node-freerdp2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.cc
89 lines (72 loc) · 2.46 KB
/
generator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <nan.h>
#include "generator.h"
using Nan::Callback;
using Nan::HandleScope;
using Nan::New;
using Nan::Null;
using v8::Array;
using v8::Local;
using v8::String;
using v8::Value;
using v8::Number;
using v8::Object;
struct GeneratorBaton {
uv_work_t request; // libuv
Callback *callback; // javascript callback
const GeneratorType *type;
void *data;
};
// called by libuv worker in separate thread
static void DelayAsync(uv_work_t *req) { }
static void DelayAsyncAfter(uv_work_t *req, int status) {
HandleScope scope;
GeneratorBaton *baton = static_cast<GeneratorBaton *>(req->data);
Local<Array> env_args = baton->type->arg_parser(baton->data);
Local<Value> argv[] = {
New<String>(baton->type->name).ToLocalChecked(),
env_args
};
baton->callback->Call(2, argv);
delete baton;
}
void generator_emit(GeneratorContext *generator, const GeneratorType *type, void * data) {
// Create context for work queue
GeneratorBaton *baton = new GeneratorBaton;
baton->callback = generator->callback;
baton->type = type;
baton->data = data;
baton->request.data = baton;
uv_queue_work(uv_default_loop(), &baton->request, DelayAsync, DelayAsyncAfter);
}
Local<Array> connect_args_parser(void *generic) {
connect_args *args = static_cast<connect_args *>(generic);
Local<Array> argv = New<Array>();
free(args);
return argv;
}
Local<Array> close_args_parser(void *generic) {
close_args *args = static_cast<close_args *>(generic);
Local<Array> argv = New<Array>();
Local<Object> obj = New<Object>();
obj->Set(New<String>("msg").ToLocalChecked(), New<String>(args->msg).ToLocalChecked());
argv->Set(0, obj);
free(args);
return argv;
}
Local<Array> draw_args_parser(void *generic) {
draw_args *args = static_cast<draw_args *>(generic);
Local<Object> obj = New<Object>();
obj->Set(New<String>("x").ToLocalChecked(), New<Number>(args->x));
obj->Set(New<String>("y").ToLocalChecked(), New<Number>(args->y));
obj->Set(New<String>("w").ToLocalChecked(), New<Number>(args->w));
obj->Set(New<String>("h").ToLocalChecked(), New<Number>(args->h));
obj->Set(New<String>("bpp").ToLocalChecked(), New<Number>(args->bpp));
int size = args->w * args->h * args->bpp;
Nan::MaybeLocal<v8::Object> buffer = Nan::CopyBuffer((const char *)args->buffer, size);
obj->Set(New<String>("buffer").ToLocalChecked(), buffer.ToLocalChecked());
Local<Array> argv = New<Array>();
argv->Set(0, obj);
delete[] args->buffer;
delete args;
return argv;
}