File size: 1,681 Bytes
8df6da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// For Types Only
import { BusConnector } from "../bus.js";

/**
 * Network adapter "inbrowser" which connects the emulated NIC
 * to a shared in-browser BroadcastChannel.
 *
 * NOTE: BroadcastChannel.postMessage() sends the given message to
 *       *other* BroadcastChannel objects set up for the same channel.
 *       Since we use the same BroadcastChannel instance for both
 *       sending and receiving we do not receive copies of our
 *       own sent messages.
 *       Source: https://html.spec.whatwg.org/multipage/web-messaging.html#broadcasting-to-other-browsing-contexts
 *
 * @constructor
 *
 * @param {BusConnector} bus
 * @param {*=} config
 */
export function InBrowserNetworkAdapter(bus, config)
{
    const id = config.id || 0;

    this.bus = bus;
    this.bus_send_msgid = `net${id}-send`;
    this.bus_recv_msgid = `net${id}-receive`;
    this.channel = new BroadcastChannel(`v86-inbrowser-${id}`);
    this.is_open = true;

    // forward ethernet frames from emulated NIC to hub
    this.nic_to_hub_fn = (eth_frame) => {
        this.channel.postMessage(eth_frame);
    };
    this.bus.register(this.bus_send_msgid, this.nic_to_hub_fn, this);

    // forward ethernet frames from hub to emulated NIC
    this.hub_to_nic_fn = (ev) => {
        this.bus.send(this.bus_recv_msgid, ev.data);
    };
    this.channel.addEventListener("message", this.hub_to_nic_fn);
}

InBrowserNetworkAdapter.prototype.destroy = function()
{
    if(this.is_open) {
        this.bus.unregister(this.bus_send_msgid, this.nic_to_hub_fn);
        this.channel.removeEventListener("message", this.hub_to_nic_fn);
        this.channel.close();
        this.is_open = false;
    }
};