diff options
author | Bert Belder <bertbelder@gmail.com> | 2018-07-08 02:45:16 +0200 |
---|---|---|
committer | Bert Belder <bertbelder@gmail.com> | 2018-07-08 21:01:24 +0200 |
commit | 8a17db8266dad0325d04762a27c4f61446d26d2e (patch) | |
tree | 7fca8ff2d7936b11d38c0edc972012540580b12e /js/main.ts | |
parent | 7c5db007deb43d65550213ff07ad95bc0cce6f00 (diff) |
Add 'command id' field to messages
This allows for correlating response messages to the command message that
caused them.
Diffstat (limited to 'js/main.ts')
-rw-r--r-- | js/main.ts | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/js/main.ts b/js/main.ts index 01c3ab9fb..027baa3cf 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); |