v86/src/browser/network.js

143 lines
2.9 KiB
JavaScript
Raw Normal View History

2014-10-11 02:15:37 +02:00
"use strict";
/**
2015-09-16 03:25:09 +02:00
* An ethernet-through-websocket adapter, to be used with
2014-10-11 02:15:37 +02:00
* https://github.com/benjamincburns/websockproxy
2015-09-16 03:25:09 +02:00
*
2014-10-11 02:15:37 +02:00
* emulated ethernet card <--> this <--> websocket proxy <--> network
*
* @constructor
2015-04-12 16:07:52 +02:00
*
* @param {string} url
* @param {BusConnector} bus
2014-10-11 02:15:37 +02:00
*/
2014-12-29 16:42:59 +01:00
function NetworkAdapter(url, bus)
2014-10-11 02:15:37 +02:00
{
2015-01-11 21:42:05 +01:00
this.bus = bus;
2014-10-11 02:15:37 +02:00
this.socket = undefined;
2016-07-18 04:37:12 +02:00
// TODO: circular buffer?
2014-10-11 02:15:37 +02:00
this.send_queue = [];
this.url = url;
this.reconnect_interval = 10000;
this.last_connect_attempt = Date.now() - this.reconnect_interval;
this.send_queue_limit = 64;
2014-12-29 16:42:59 +01:00
2015-01-11 21:42:05 +01:00
this.bus.register("net0-send", function(data)
{
this.send(data);
}, this);
2014-10-11 02:15:37 +02:00
}
NetworkAdapter.prototype.handle_message = function(e)
{
if(this.bus)
{
this.bus.send("net0-receive", new Uint8Array(e.data));
}
2014-10-11 02:15:37 +02:00
};
NetworkAdapter.prototype.handle_close = function(e)
{
//console.log("onclose", e);
this.connect();
setTimeout(this.connect.bind(this), this.reconnect_interval);
};
NetworkAdapter.prototype.handle_open = function(e)
{
//console.log("open", e);
for(var i = 0; i < this.send_queue.length; i++)
{
this.send(this.send_queue[i]);
}
this.send_queue = [];
};
NetworkAdapter.prototype.handle_error = function(e)
{
//console.log("onerror", e);
};
2015-09-16 03:25:09 +02:00
NetworkAdapter.prototype.destroy = function()
2014-10-11 02:15:37 +02:00
{
if(this.socket)
{
this.socket.close();
}
};
NetworkAdapter.prototype.connect = function()
{
if(typeof WebSocket === "undefined")
{
return;
}
2014-10-11 02:15:37 +02:00
if(this.socket)
{
var state = this.socket.readyState;
if(state === 0 || state === 1)
{
// already or almost there
return;
}
}
var now = Date.now();
if(this.last_connect_attempt + this.reconnect_interval > now)
{
return;
}
this.last_connect_attempt = Date.now();
this.socket = new WebSocket(this.url);
2014-10-11 02:15:37 +02:00
this.socket.binaryType = "arraybuffer";
this.socket.onopen = this.handle_open.bind(this);
2014-10-11 02:15:37 +02:00
this.socket.onmessage = this.handle_message.bind(this);
this.socket.onclose = this.handle_close.bind(this);
this.socket.onerror = this.handle_error.bind(this);
};
NetworkAdapter.prototype.send = function(data)
{
//console.log("send", data);
if(!this.socket || this.socket.readyState !== 1)
{
this.send_queue.push(data);
if(this.send_queue.length > 2 * this.send_queue_limit)
{
this.send_queue = this.send_queue.slice(-this.send_queue_limit);
}
this.connect();
}
else
{
this.socket.send(data);
}
};
2018-09-19 23:12:36 +02:00
NetworkAdapter.prototype.change_proxy = function(url)
{
this.url = url;
if(this.socket)
{
this.socket.onclose = function() {};
this.socket.onerror = function() {};
this.socket.close();
this.socket = undefined;
}
};