Add 'command id' field to messages

This allows for correlating response messages to the command message that
caused them.
This commit is contained in:
Bert Belder 2018-07-08 02:45:16 +02:00
parent 7c5db007de
commit 8a17db8266
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
5 changed files with 31 additions and 13 deletions

View file

@ -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);