1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-18 11:53:59 -05:00

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

View file

@ -3,7 +3,9 @@
#ifndef HANDLERS_H_
#define HANDLERS_H_
extern "C" {
void handle_code_fetch(const char* module_specifier,
#include <stdint.h>
void handle_code_fetch(uint32_t cmd_id, const char* module_specifier,
const char* containing_file);
}
#endif // HANDLERS_H_

View file

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

View file

@ -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<const char*>(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<Base>());
auto base = flatbuffers::GetRoot<Base>(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:

View file

@ -18,6 +18,7 @@ union Any {
}
table Base {
cmdId: uint32;
error: string;
msg: Any;
}