mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-15 15:24:52 +09:00
Send raw payloads over UDP and emit them as udp events
Review feedback from Cooldude2606: UDP is most useful for arbitrary blobs, so send_udp now takes any string and the packet is just "channel?payload". Plugins get it as a udp-<channel> event with the payload as a Buffer, or through handleUdp which logs handler errors like handle does for ipc events. The host socket is now opened when the RCON client is started and closed where the RCON client is ended, matching how the RCON client is handled, instead of being bound in start() and closed by _resetState. The integration round trip moved to the FactorioServer tests, where it sends a packet each way through a real server without the module. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
4b0238b64b
commit
7a64706c82
8 files changed
+222
-134
No files matched your search
@@ -158,24 +158,6 @@ describe("Clusterio Instance", function() {
|
||||
const userLeave = await getUser("JoiningPlayer");
|
||||
assert(!userLeave.instances.has(instId), "Player is not shown as offline");
|
||||
});
|
||||
it("should respond to a player join and leave event sent over UDP", async function() {
|
||||
// The host port is passed to the module by updateInstanceData on startup
|
||||
if (!hasLuaUdp() || !scriptCommandsEnabled) {
|
||||
this.skip();
|
||||
}
|
||||
await sendRcon(instId,
|
||||
`/sc ${requireApi} api.send_udp("player_event",` +
|
||||
"{ type='join', name='UdpPlayer' })"
|
||||
);
|
||||
const userJoin = await getUser("UdpPlayer");
|
||||
assert(userJoin.instances.has(instId), "Player is not shown as online");
|
||||
await sendRcon(instId,
|
||||
`/sc ${requireApi} api.send_udp("player_event",` +
|
||||
"{ type='leave', name='UdpPlayer', reason='quit' })"
|
||||
);
|
||||
const userLeave = await getUser("UdpPlayer");
|
||||
assert(!userLeave.instances.has(instId), "Player is not shown as offline");
|
||||
});
|
||||
}
|
||||
it("should respond to a player ban and unban event", async function() {
|
||||
await execCtl(`${instSetConfig} factorio.sync_banlist bidirectional`);
|
||||
|
||||
@@ -125,6 +125,8 @@ describe("Integration of host/src/server", function() {
|
||||
let mapPath = server.writePath("saves", "test.zip");
|
||||
await assert.doesNotReject(fs.access(mapPath), "save is missing");
|
||||
|
||||
// --enable-lua-udp was added in Factorio 2.0.59
|
||||
server.enableLuaUdp = lib.integerFullVersion(server.version) >= lib.integerFullVersion("2.0.59");
|
||||
await server.start("test.zip");
|
||||
});
|
||||
});
|
||||
@@ -159,6 +161,53 @@ describe("Integration of host/src/server", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Lua UDP", function() {
|
||||
it("emits udp events for packets sent from the game", async function() {
|
||||
slowTest(this);
|
||||
if (!server.enableLuaUdp) {
|
||||
this.skip();
|
||||
}
|
||||
log("Lua UDP from game");
|
||||
|
||||
assert(server.hostUdpPort > 0, "host UDP socket is not open");
|
||||
let waiter = events.once(server, "udp-test_channel");
|
||||
await server.sendRcon(`/sc helpers.send_udp(${server.hostUdpPort}, "test_channel?hello", 0)`);
|
||||
let [data] = await waiter;
|
||||
assert.equal(data.toString(), "hello");
|
||||
});
|
||||
it("delivers packets sent with sendUdp to the game", async function() {
|
||||
slowTest(this);
|
||||
if (!server.enableLuaUdp) {
|
||||
this.skip();
|
||||
}
|
||||
log("Lua UDP to game");
|
||||
|
||||
await server.sendRcon(
|
||||
"/sc script.on_event(defines.events.on_udp_packet_received, " +
|
||||
"function(event) print('udp:' .. event.payload) end)"
|
||||
);
|
||||
let pass = false;
|
||||
function filter(output) {
|
||||
if (output.message === "udp:hello") {
|
||||
pass = true;
|
||||
}
|
||||
}
|
||||
server.on("output", filter);
|
||||
|
||||
await server.sendUdp("hello");
|
||||
for (let i = 0; i < 10; i++) {
|
||||
await server.sendRcon("/sc helpers.recv_udp(0)");
|
||||
await lib.wait(10);
|
||||
if (pass) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
server.off("output", filter);
|
||||
assert(pass, "server did not output the packet payload");
|
||||
});
|
||||
});
|
||||
|
||||
describe(".stop()", function() {
|
||||
it("stops the server", async function() {
|
||||
slowTest(this);
|
||||
@@ -179,8 +228,6 @@ describe("Integration of host/src/server", function() {
|
||||
slowTest(this);
|
||||
log(".startScenario()");
|
||||
|
||||
// --enable-lua-udp was added in Factorio 2.0.59
|
||||
server.enableLuaUdp = lib.integerFullVersion(server.version) >= lib.integerFullVersion("2.0.59");
|
||||
let pass = false;
|
||||
function filter(output) {
|
||||
if (output.message === "test_scenario init") {
|
||||
@@ -193,7 +240,6 @@ describe("Integration of host/src/server", function() {
|
||||
|
||||
log(".stop()");
|
||||
await server.stop();
|
||||
server.enableLuaUdp = false;
|
||||
|
||||
server.off("output", filter);
|
||||
assert(pass, "server did not output line from test scenario");
|
||||
|
||||
Reference in new issue
Block a user