(header: Uint8Array, body: Uint8Array)
| 218 | } |
| 219 | |
| 220 | private onPacketReady(header: Uint8Array, body: Uint8Array): void { |
| 221 | if (this.inst === undefined) { |
| 222 | // initialize server. |
| 223 | const reader = new ByteStreamReader(body); |
| 224 | const code = reader.readU32(); |
| 225 | const ver = Uint8ArrayToString(reader.readByteArray()); |
| 226 | const nargs = reader.readU32(); |
| 227 | |
| 228 | // nargs=0 means no session_constructor_args (LocalSession request). |
| 229 | // WASM RPC requires ["rpc.WasmSession", wasm_binary]. Wait for proper init. |
| 230 | if (nargs === 0) { |
| 231 | this.log("Received LocalSession init (nargs=0), waiting for WasmSession init..."); |
| 232 | this.requestBytes(SizeOf.I64); |
| 233 | this.state = RPCServerState.ReceivePacketHeader; |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | const args = []; |
| 238 | for (let i = 0; i < nargs; ++i) { |
| 239 | const typeIndex = reader.readU32(); |
| 240 | if (typeIndex === TypeIndex.kTVMFFIRawStr) { |
| 241 | const str = Uint8ArrayToString(reader.readByteArray()); |
| 242 | args.push(str); |
| 243 | } else if (typeIndex === TypeIndex.kTVMFFIStr) { |
| 244 | reader.readU32(); // skip duplicate type_index |
| 245 | const str = Uint8ArrayToString(reader.readByteArray()); |
| 246 | args.push(str); |
| 247 | } else if (typeIndex === TypeIndex.kTVMFFIByteArrayPtr) { |
| 248 | args.push(reader.readByteArray()); |
| 249 | } else if (typeIndex === TypeIndex.kTVMFFIBytes) { |
| 250 | reader.readU32(); // skip duplicate type_index |
| 251 | args.push(reader.readByteArray()); |
| 252 | } else { |
| 253 | throw new Error("cannot support type index " + typeIndex); |
| 254 | } |
| 255 | } |
| 256 | this.onInitServer(args, header, body); |
| 257 | } else { |
| 258 | assert(this.serverRecvData !== undefined); |
| 259 | this.serverRecvData(header, body); |
| 260 | this.requestBytes(SizeOf.I64); |
| 261 | this.state = RPCServerState.ReceivePacketHeader; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** Event handler during server initialization. */ |
| 266 | private onInitServer( |
no test coverage detected