fix non local host networking
Some checks failed
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
Build and Deploy / build-macos (push) Has been cancelled
Build and Deploy / build-linux (push) Has been cancelled
Some checks failed
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
Build and Deploy / build-macos (push) Has been cancelled
Build and Deploy / build-linux (push) Has been cancelled
This commit is contained in:
@@ -288,6 +288,7 @@ function startServer() {
|
||||
$_.portal(e => {
|
||||
console.log("Portal received contact message");
|
||||
// Reply with this actor to establish connection
|
||||
console.log (json.encode($_))
|
||||
$_.send(e, $_);
|
||||
console.log("Portal replied with server actor");
|
||||
}, 5678);
|
||||
|
||||
@@ -630,15 +630,20 @@ function print_actor() {
|
||||
|
||||
function create_actor(data = {}) {
|
||||
var newactor = Object.create($actor)
|
||||
|
||||
// Store actual address/port values on the data object
|
||||
data._address = data._address || local_address
|
||||
data._port = data._port || local_port
|
||||
|
||||
Object.defineProperty(data, 'address', {
|
||||
get: function() { return local_address },
|
||||
set: function(x) {},
|
||||
get: function() { return this._address || local_address },
|
||||
set: function(x) { this._address = x },
|
||||
enumerable: true
|
||||
})
|
||||
|
||||
Object.defineProperty(data, 'port', {
|
||||
get: function() { return local_port },
|
||||
set: function(x) {},
|
||||
get: function() { return this._port || local_port },
|
||||
set: function(x) { this._port = x },
|
||||
enumerable: true
|
||||
})
|
||||
|
||||
@@ -745,9 +750,14 @@ function handle_host(e) {
|
||||
break
|
||||
case "receive":
|
||||
var data = nota.decode(e.data)
|
||||
if (data.replycc && !data.replycc.address) {
|
||||
if (data.replycc && !data.replycc.__ACTORDATA__.address) {
|
||||
data.replycc.__ACTORDATA__.address = e.peer.address
|
||||
data.replycc.__ACTORDATA__.port = e.peer.port
|
||||
// Store in global lookup for future routing
|
||||
id_address[data.replycc.__ACTORDATA__.id] = {
|
||||
address: e.peer.address,
|
||||
port: e.peer.port
|
||||
}
|
||||
}
|
||||
// Also populate address/port for any actor objects in the message data
|
||||
function populate_actor_addresses(obj) {
|
||||
@@ -755,6 +765,11 @@ function handle_host(e) {
|
||||
if (obj.__ACTORDATA__ && !obj.__ACTORDATA__.address) {
|
||||
obj.__ACTORDATA__.address = e.peer.address
|
||||
obj.__ACTORDATA__.port = e.peer.port
|
||||
// Store in global lookup for future routing
|
||||
id_address[obj.__ACTORDATA__.id] = {
|
||||
address: e.peer.address,
|
||||
port: e.peer.port
|
||||
}
|
||||
}
|
||||
for (var key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
@@ -848,6 +863,12 @@ function actor_send(actor, message) {
|
||||
os.mailbox_push(actor.__ACTORDATA__.id, message)
|
||||
return
|
||||
}
|
||||
|
||||
// Use fallback address lookup if actor doesn't have address info
|
||||
if (!actor.__ACTORDATA__.address && id_address[actor.__ACTORDATA__.id]) {
|
||||
Object.assign(actor.__ACTORDATA__, id_address[actor.__ACTORDATA__.id])
|
||||
}
|
||||
|
||||
if (actor.__ACTORDATA__.address) {
|
||||
if (actor.__ACTORDATA__.id) message.target = actor.__ACTORDATA__.id
|
||||
else message.type = "contact"
|
||||
@@ -1018,6 +1039,9 @@ function handle_message(msg) {
|
||||
var greeter = greeters[msg.id]
|
||||
if (greeter) greeter({type: "actor_started", actor: create_actor(msg)})
|
||||
break;
|
||||
case "ping":
|
||||
// Keep-alive ping, no action needed
|
||||
break;
|
||||
default:
|
||||
if (receive_fn) receive_fn(msg)
|
||||
break;
|
||||
@@ -1028,6 +1052,19 @@ function enet_check()
|
||||
{
|
||||
if (portal) portal.service(handle_host)
|
||||
if (contactor) contactor.service(handle_host)
|
||||
send_messages();
|
||||
|
||||
// Send keep-alive ping to all active peers to prevent timeout
|
||||
for (var peerKey in peers) {
|
||||
var peer = peers[peerKey]
|
||||
if (peer && peer.state === 1) { // ENET_PEER_STATE_CONNECTED
|
||||
try {
|
||||
peer.send(nota.encode({type: 'ping'}))
|
||||
} catch (e) {
|
||||
console.debug(`Failed to send ping to ${peerKey}:`, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$_.delay(enet_check, service_delay);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user