tqchen commented on a change in pull request #5506:
URL: https://github.com/apache/incubator-tvm/pull/5506#discussion_r421560025



##########
File path: web/src/rpc_server.ts
##########
@@ -0,0 +1,379 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { SizeOf, TypeCode } from "./ctypes";
+import { assert, StringToUint8Array, Uint8ArrayToString } from "./support";
+import * as runtime from "./runtime";
+import { Class } from "estree";
+
+enum RPCServerState {
+  InitHeader,
+  InitHeaderKey,
+  InitServer,
+  WaitForCallback,
+  ReceivePacketHeader,
+  ReceivePacketBody,
+}
+
+/** RPC magic header */
+const RPC_MAGIC = 0xff271;
+
+/**
+ * An utility class to read from binary bytes.
+ */
+class ByteStreamReader {
+  offset = 0;
+  bytes: Uint8Array;
+
+  constructor(bytes: Uint8Array) {
+    this.bytes = bytes;
+  }
+
+  readU32(): number {
+    const i = this.offset;
+    const b = this.bytes;
+    const val = b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24);
+    this.offset += 4;
+    return val;
+  }
+
+  readU64(): number {
+    const val = this.readU32();
+    this.offset += 4;
+    return val;
+  }
+
+  readByteArray(): Uint8Array {
+    const len = this.readU64();
+    assert(this.offset + len <= this.bytes.byteLength);
+    const ret = new Uint8Array(len);
+    ret.set(this.bytes.slice(this.offset, this.offset + len));
+    this.offset += len;
+    return ret;
+  }
+}
+
+/**
+ * A websocket based RPC
+ */
+export class RPCServer {
+  url: string;
+  key: string;
+  socket: WebSocket;
+  state: RPCServerState = RPCServerState.InitHeader;
+  logger: (msg: string) => void;
+  getImports: () => Record<string, unknown>;
+  private name: string;
+  private inst?: runtime.Instance = undefined;
+  private serverRecvData?: (header: Uint8Array, body: Uint8Array) => void;
+  private currPacketHeader?: Uint8Array;
+  private currPacketLength = 0;
+  private remoteKeyLength = 0;
+  private pendingBytes = 0;
+  private buffredBytes = 0;
+  private messageQueue: Array<Uint8Array> = [];
+
+  constructor(
+    url: string,
+    key: string,
+    getImports: () => Record<string, unknown>,
+    logger: (msg: string) => void = console.log
+  ) {
+    this.url = url;
+    this.key = key;
+    this.name = "WebSocketRPCServer[" + this.key + "]: ";
+    this.getImports = getImports;
+    this.logger = logger;
+
+    this.checkLittleEndian();
+
+    if (typeof WebSocket == "undefined") {
+      // eslint-disable-next-line @typescript-eslint/no-var-requires
+      const WebSocket = require("ws");
+      this.socket = new WebSocket(url);
+    } else {
+      this.socket = new (WebSocket as any)(url);
+    }
+
+    //this.socket = this.getSocket(url);
+    this.socket.binaryType = "arraybuffer";
+
+    this.socket.addEventListener("open", (event: Event) => {
+      return this.onOpen(event);
+    });
+    this.socket.addEventListener("message", (event: MessageEvent) => {
+      return this.onMessage(event);
+    });
+    this.socket.addEventListener("close", (event: CloseEvent) => {
+      return this.onClose(event);
+    });
+  }
+
+  // eslint-disable-next-line @typescript-eslint/no-unused-vars
+  private onClose(_event: CloseEvent): void {
+    if (this.inst !== undefined) {
+      this.inst.dispose();
+    }
+    if (this.state == RPCServerState.ReceivePacketHeader) {
+      this.log("Closing the server in clean state");
+    } else {
+      this.log("Closing the server, final state=" + this.state);
+    }

Review comment:
       Yes, we could do that as a followup feature




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to