1
0

fetch_network_harness.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const dgram = require("node:dgram");
  2. const server = dgram.createSocket("udp4");
  3. const fs = require("node:fs");
  4. const path = require("node:path");
  5. // 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
  6. globalThis.dbg_assert = require("node:assert");
  7. globalThis.dbg_log = (what, level) => console.log(what);
  8. globalThis.LOG_NET = 0;
  9. const { FetchNetworkAdapter } = require(path.join(__dirname, "..", "src", "browser", "fetch_network.js"));
  10. const events = {};
  11. const bus = {
  12. register: (name, fn, bind) => events[name] = fn.bind(bind),
  13. send: (name, data) => events[name](data)
  14. };
  15. const a = new FetchNetworkAdapter(bus);
  16. server.on("error", (err) => {
  17. console.error(`server error:\n${err.stack}`);
  18. server.close();
  19. });
  20. server.on("message", (msg, rinfo) => {
  21. //console.log(`server got: ${msg.toString("hex")} from ${rinfo.address}:${rinfo.port}`);
  22. events["net0-receive"] = (data) => {
  23. //console.log(`sending: ${Buffer.from(data).toString("hex")} to ${rinfo.address}:${rinfo.port}`);
  24. server.send(Buffer.from(data), rinfo.port);
  25. };
  26. bus.send("net0-send", new Uint8Array(msg));
  27. });
  28. server.on("listening", () => {
  29. const address = server.address();
  30. console.log(`server listening ${address.address}:${address.port}`);
  31. });
  32. server.bind(6677);