fetch_network_harness.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import dgram from "node:dgram";
  2. import { V86, FetchNetworkAdapter } from "../nodejs-loader.mjs";
  3. // qemu-system-i386 -m 2G -nographic -hda ~/disk.qcow2 -netdev dgram,id=net0,remote.type=inet,remote.host=127.0.0.1,remote.port=6677,local.host=127.0.0.1,local.port=7744,local.type=inet -device e1000,netdev=net0
  4. const server = dgram.createSocket("udp4");
  5. const events = {};
  6. const bus = {
  7. register: (name, fn, bind) => events[name] = fn.bind(bind),
  8. send: (name, data) => events[name](data)
  9. };
  10. const a = new FetchNetworkAdapter(bus);
  11. server.on("error", (err) => {
  12. console.error(`server error:\n${err.stack}`);
  13. server.close();
  14. });
  15. server.on("message", (msg, rinfo) => {
  16. //console.log(`server got: ${msg.toString("hex")} from ${rinfo.address}:${rinfo.port}`);
  17. events["net0-receive"] = (data) => {
  18. //console.log(`sending: ${Buffer.from(data).toString("hex")} to ${rinfo.address}:${rinfo.port}`);
  19. server.send(Buffer.from(data), rinfo.port);
  20. };
  21. bus.send("net0-send", new Uint8Array(msg));
  22. });
  23. server.on("listening", () => {
  24. const address = server.address();
  25. console.log(`server listening ${address.address}:${address.port}`);
  26. });
  27. server.bind(6677);