-
-
Save bmeck/e1577262267e064e0a84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static uv_async_t dispatch_debug_messages_async; | |
// Called from the main thread. | |
static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle, int status) { | |
v8::Debug::ProcessDebugMessages(); | |
} | |
// Called from V8 Debug Agent TCP thread. | |
static void DispatchMessagesDebugAgentCallback() { | |
uv_async_send(&dispatch_debug_messages_async); | |
} | |
struct node_debugger { | |
Isolate* isolate; | |
uv_loop_t* loop; | |
}; | |
uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) { | |
return uv_buf_init((char*) malloc(suggested_size), suggested_size); | |
} | |
void bmeckMessageHandler(const Debug::Message& message) { | |
printf("MESSAGE\n"); | |
printf("IS EVENT: %d\n", message.IsEvent()); | |
Handle<String> json = message.GetJSON(); | |
char* jsonChars = (char*)malloc(json->Utf8Length()); | |
json->WriteUtf8(jsonChars); | |
printf("JSON: %s\n", jsonChars); | |
} | |
void on_debug_data(uv_stream_t* stream, | |
ssize_t nread, | |
uv_buf_t buf) { | |
node_debugger* debugger = (node_debugger*)stream->data; | |
// TODO SETUP CLIENT DATA | |
const char* msg = "{\"seq\":1,\"type\":\"request\",\"command\":\"evaluate\",\"arguments\":{\"expression\":\"1+1\"}}"; | |
int length = strlen(msg); | |
uint16_t* command = (uint16_t*)malloc(sizeof(uint16_t) * (length + 1)); | |
command[length] = 0; | |
for (int i = 0; i < strlen(msg); i++) { | |
command[i] = msg[i]; | |
} | |
Debug::SendCommand(/*node_isolate, */command, length, NULL); | |
} | |
void on_new_connection(uv_stream_t *server, int status) { | |
if (status == -1) { | |
// error! | |
return; | |
} | |
uv_tcp_t* client = (uv_tcp_t*)malloc(sizeof(uv_tcp_t)); | |
uv_tcp_init(server->loop, client); | |
int r = uv_accept(server, (uv_stream_t*)client); | |
if (r) { | |
// error; | |
return; | |
} | |
client->data = server->data; | |
uv_read_start((uv_stream_t*)client, alloc_buffer, on_debug_data); | |
} | |
static void debug_run(void* data) { | |
node_debugger* debugger = (node_debugger*)data; | |
uv_run(debugger->loop); | |
} | |
static int _bmeckDebugLoop(void* data) { | |
Debug::SetMessageHandler2(&bmeckMessageHandler); | |
Isolate* isolate = Isolate::New(); | |
uv_loop_t* loop = uv_loop_new(); | |
uv_tcp_t* server = (uv_tcp_t*)malloc(sizeof(uv_tcp_t)); | |
uv_tcp_init(loop, server); | |
struct sockaddr_in bind_addr = uv_ip4_addr("0.0.0.0", 5859); | |
uv_tcp_bind(server, bind_addr); | |
int r = uv_listen((uv_stream_t*) server, 128, on_new_connection); | |
if (r) { | |
fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop))); | |
return 1; | |
} | |
node_debugger* debugger = (node_debugger*)malloc(sizeof(node_debugger)); | |
debugger->isolate = isolate; | |
debugger->loop = loop; | |
uv_thread_t debug_thread; | |
r = uv_thread_create(&debug_thread, &debug_run, debugger); | |
if (r) { | |
fprintf(stderr, "Thread creation error %s\n", uv_err_name(uv_last_error(loop))); | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment