From 00a3451004d664a5386f3750540facddc0d2686b Mon Sep 17 00:00:00 2001 From: Alexander M Pickering Date: Mon, 9 Dec 2024 21:48:17 -0600 Subject: Add a channel abstraction to use for networking --- src/channel.moon | 25 +++++++++++++++---------- src/js_bridge.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 src/js_bridge.js (limited to 'src') diff --git a/src/channel.moon b/src/channel.moon index 968ae09..6f87d46 100644 --- a/src/channel.moon +++ b/src/channel.moon @@ -34,26 +34,31 @@ class FaultyChannel extends Channel @avg_latency = 0 -- in ms @latency_std = 0 -- Latency can never be below 0, but can go up as much as it likes - @avg_loss = 0 -- between 0 (never) and 1 (always) - @loss_std = 0.1 - -- Loss is always a percentage between 0 and 1 + @loss = 0.1 -- between 0 (never) and 1 (always) super(settings) - normal: (avg, std, n) => + @normal_at: (avg, std, n) -> assert(avg and std and n, string.format("normal(avg, std, n) called with %q %q %q", tostring(avg), tostring(std), tostring(n))) -- Normal curve probability at N (1/ (math.sqrt(2*math.pi) * avg)) * math.exp(-(n - avg)^2 / (2 * (std^2))) + @normal: (avg, std) => + -- Box-Muller transform + bm = math.sqrt(-2 * math.log(math.random())) * math.cos(2 * math.pi * math.random()) + -- Box-Muller gives us std = e^-0.5 , avg = 0 + ((bm / math.exp(-1/2)) * std) + avg poll: => @pump! #@buffer > 0 send: (message) => -- Do we deliver? - if @normal(@avg_loss, @loss_std, math.random()) < 0.5 + rng = math.random() + if @loss > rng return -- How long does it take? - time = @normal(@avg_latency, @latency_std, math.random()) + -- Only uses the positive half of the normal distribution, double the standard deviation? + time = @@normal(@avg_latency, @latency_std * 2, math.random()) if time < 0 then time = 0 -- We can't deliver messages in the past - table.insert(@to_deliver, {message,time}) + table.insert(@to_deliver, {message,@@time + time}) recv: => @pump! table.remove(@buffer, 1) @@ -62,11 +67,11 @@ class FaultyChannel extends Channel deliver_len = #@to_deliver for k,tbl in ipairs(@to_deliver) {m, t} = tbl + @to_deliver[defrag] = tbl if @@time > t - @to_deliver[k] = nil table.insert(@buffer, m) - if @to_deliver[defrag] == nil - @to_deliver[defrag] = tbl + else + defrag = defrag + 1 for i = defrag, deliver_len do @to_deliver[i] = nil diff --git a/src/js_bridge.js b/src/js_bridge.js new file mode 100644 index 0000000..2d32739 --- /dev/null +++ b/src/js_bridge.js @@ -0,0 +1,54 @@ +var s = document.createElement('script'); +s.setAttribute('src','https://unpkg.com/peerjs@1.5.2/dist/peerjs.min.js'); +document.body.appendChild(s); +function genRanHex(size) { + return Array.apply(null,Array(size)).map(() => Math.floor(Math.random() * 16).toString(16)).join(''); +} +PEER = {} +PEER.event_queue = {} +PEER.peers = {} +PEER.message_queue = {} +PEER["create"] = function(name, options) { + var peer = new Peer(name, options) + PEER.peers[name] = peer +} +PEER["on"] = function(name, e, message) { + PEER.peers[name].on(e, function(conn) { + PEER.message_queue.push(message) + } +} +PEER["connect"] = function(name, id, options) { + PEER.peers[name].connect(id, options) +} +var peer = new Peer("ANGRY_ADVENTURE_" + GLOBAL.lobby_id, {"debug": 3}); +var peer_id = null; +var poo = peer.on('open', function(id) { + console.log('My peer ID is: ' + id); + peer.on("connection",function(conn){ + console.log("Got a connection!") + GLOBAL.connections[conn.peer] = conn + conn.send("Hello!") + GLOBAL.message_queue.push({ + "msg": "data", + "peer": conn.peer, + "data": '{"msg":"player_joined"}' + }) + conn.on("data",function(data){ + console.log("Got some data from a peer!:" + data) + GLOBAL.message_queue.push({ + "msg": "data", + "peer": conn.peer, + "data": data + }) + }) + conn.on("error",function(err){ + console.log("Error on a connection:" + err) + GLOBAL.message_queue.push({ + "msg": "error", + "peer": conn.peer, + "err": err + }) + }) + }) + GLOBAL.peer_id = id; +}); -- cgit v1.2.3-70-g09d2