From 8a17db8266dad0325d04762a27c4f61446d26d2e Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Sun, 8 Jul 2018 02:45:16 +0200 Subject: [PATCH] Add 'command id' field to messages This allows for correlating response messages to the command message that caused them. --- js/main.ts | 15 +++++++++++++-- src/handlers.h | 4 +++- src/handlers.rs | 7 +++++-- src/main.cc | 17 +++++++++-------- src/msg.fbs | 1 + 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/js/main.ts b/js/main.ts index 01c3ab9fb1..027baa3cf0 100644 --- a/js/main.ts +++ b/js/main.ts @@ -11,10 +11,19 @@ import { assert } from "./util"; const globalEval = eval; const window = globalEval("this"); -function startMsg(): ArrayBuffer { +let cmdIdCounter = 0; +function assignCmdId(): number { + // TODO(piscisaureus) Safely re-use so they don't overflow. + const cmdId = ++cmdIdCounter; + assert(cmdId < 2 ** 32, "cmdId overflow"); + return cmdId; +} + +function startMsg(cmdId: number): ArrayBuffer { const builder = new flatbuffers.Builder(); const msg = fbs.Start.createStart(builder, 0); fbs.Base.startBase(builder); + fbs.Base.addCmdId(builder, cmdId); fbs.Base.addMsg(builder, msg); fbs.Base.addMsgType(builder, fbs.Any.Start); builder.finish(fbs.Base.endBase(builder)); @@ -27,7 +36,8 @@ window["denoMain"] = () => { // First we send an empty "Start" message to let the privlaged side know we // are ready. The response should be a "StartRes" message containing the CLI // argv and other info. - const res = deno.send(startMsg()); + const cmdId = assignCmdId(); + const res = deno.send(startMsg(cmdId)); // TODO(ry) Remove this conditional once main.rs gets up to speed. if (res == null) { @@ -39,6 +49,7 @@ window["denoMain"] = () => { // Deserialize res into startResMsg. const bb = new flatbuffers.ByteBuffer(new Uint8Array(res)); const base = fbs.Base.getRootAsBase(bb); + assert(base.cmdId() === cmdId); assert(fbs.Any.StartRes === base.msgType()); const startResMsg = new fbs.StartRes(); assert(base.msg(startResMsg) != null); diff --git a/src/handlers.h b/src/handlers.h index 921ae62b12..b398eca61e 100644 --- a/src/handlers.h +++ b/src/handlers.h @@ -3,7 +3,9 @@ #ifndef HANDLERS_H_ #define HANDLERS_H_ extern "C" { -void handle_code_fetch(const char* module_specifier, +#include + +void handle_code_fetch(uint32_t cmd_id, const char* module_specifier, const char* containing_file); } #endif // HANDLERS_H_ diff --git a/src/handlers.rs b/src/handlers.rs index 405fe29dae..2b8e51602e 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -17,6 +17,7 @@ fn test_example() { #[no_mangle] pub extern "C" fn handle_code_fetch( + cmd_id: u32, module_specifier: *const c_char, containing_file: *const c_char, ) { @@ -24,8 +25,10 @@ pub extern "C" fn handle_code_fetch( let containing_file = string_from_ptr(containing_file); println!( - "handle_code_fetch. module_specifier = {} containing_file = {}", - module_specifier, containing_file + "handle_code_fetch. cmd_id = {} module_specifier = {} containing_file = {}", + cmd_id, + module_specifier, + containing_file ); unimplemented!(); diff --git a/src/main.cc b/src/main.cc index 78c86ee155..a801bc2921 100644 --- a/src/main.cc +++ b/src/main.cc @@ -22,7 +22,7 @@ static char** global_argv; static int global_argc; // Sends StartRes message -void HandleStart(Deno* d) { +void HandleStart(Deno* d, uint32_t cmd_id) { flatbuffers::FlatBufferBuilder builder; char cwdbuf[1024]; @@ -37,20 +37,20 @@ void HandleStart(Deno* d) { auto start_argv = builder.CreateVector(args); auto start_msg = CreateStartRes(builder, start_cwd, start_argv); - auto base = CreateBase(builder, 0, Any_StartRes, start_msg.Union()); + auto base = CreateBase(builder, cmd_id, 0, Any_StartRes, start_msg.Union()); builder.Finish(base); deno_buf bufout{reinterpret_cast(builder.GetBufferPointer()), builder.GetSize()}; deno_set_response(d, bufout); } -void HandleCodeFetch(Deno* d, const CodeFetch* msg) { +void HandleCodeFetch(Deno* d, uint32_t cmd_id, const CodeFetch* msg) { auto module_specifier = msg->module_specifier()->c_str(); auto containing_file = msg->containing_file()->c_str(); printf("HandleCodeFetch module_specifier = %s containing_file = %s\n", module_specifier, containing_file); // Call into rust. - handle_code_fetch(module_specifier, containing_file); + handle_code_fetch(cmd_id, module_specifier, containing_file); } void MessagesFromJS(Deno* d, deno_buf buf) { @@ -59,17 +59,18 @@ void MessagesFromJS(Deno* d, deno_buf buf) { DCHECK(verifier.VerifyBuffer()); auto base = flatbuffers::GetRoot(buf.data); + auto cmd_id = base->cmdId(); auto msg_type = base->msg_type(); const char* msg_type_name = EnumNamesAny()[msg_type]; - printf("MessagesFromJS msg_type = %d, msg_type_name = %s\n", msg_type, - msg_type_name); + printf("MessagesFromJS cmd_id = %d, msg_type = %d, msg_type_name = %s\n", + cmd_id, msg_type, msg_type_name); switch (msg_type) { case Any_Start: - HandleStart(d); + HandleStart(d, base->cmdId()); break; case Any_CodeFetch: - HandleCodeFetch(d, base->msg_as_CodeFetch()); + HandleCodeFetch(d, base->cmdId(), base->msg_as_CodeFetch()); break; case Any_NONE: diff --git a/src/msg.fbs b/src/msg.fbs index bda3d63548..b07da99cf4 100644 --- a/src/msg.fbs +++ b/src/msg.fbs @@ -18,6 +18,7 @@ union Any { } table Base { + cmdId: uint32; error: string; msg: Any; }