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:
bbassieandClaude Fable 5.1 committed 2026-10-15 16:38:34 +00:00
1 parent 4b0238b64b
commit 7a64706c82
8 files changed
+222 -134

No files matched your search

+4 -5
View File
@@ -253,17 +253,16 @@ Parameters:
#### clusterio_api.send_udp(channel, data)
Send JSON data to Clusterio over the given channel using UDP instead of stdout.
Clusterio plugins receive it the same way as data sent with `send_json`.
Send data to Clusterio over the given channel using UDP instead of stdout.
Clusterio plugins receive it as `udp-channel_name` events, see the [Communicating over UDP section](writing-plugins.md#communicating-over-udp) in the Writing Plugins document.
Requires `factorio.enable_lua_udp` to be enabled on the instance and Factorio 2.0.59 or later, otherwise the data is silently dropped.
The port to send to is passed to the module with a script command on startup, so `factorio.enable_script_commands` must be enabled as well.
See the [Communicating over UDP section](writing-plugins.md#communicating-over-udp) in the Writing Plugins document for more information.
**Note**: Delivery is not guaranteed and payloads must be less than 64 kB.
**Note**: Delivery is not guaranteed and payloads should be kept well below 32 kB.
Parameters:
- `channel`: string identifying which channel to send it on.
- `data`: table that can be converted to JSON with game.table_to_json
- `data`: string with the payload to send, any bytes are allowed.
### serialize library
+15 -3
View File
@@ -488,14 +488,26 @@ Instances can optionally exchange UDP packets with the Factorio server for low l
This is disabled by default and enabled by setting `factorio.enable_lua_udp` on the instance.
Sending data out of Factorio also needs `factorio.enable_script_commands`, as the port to send to is passed to the module with a script command on startup.
Plugins requiring it should declare the `LuaUdp` feature.
Note that UDP does not guarantee delivery or ordering, and packets should be kept well below 64 kB.
Note that UDP does not guarantee delivery or ordering, and packets should be kept well below 32 kB.
Data out from Factorio is sent with the `send_udp` API of the Clusterio module and is delivered to plugins as the same `ipc-channel_name` events as `send_json` uses:
Data out from Factorio is sent with the `send_udp` API of the Clusterio module.
Unlike `send_json` the payload is an arbitrary string, and it is delivered to plugins as a `udp-channel_name` event with the payload as a Buffer:
```lua
clusterio_api.send_udp("my_plugin_foo", { data = 123 })
clusterio_api.send_udp("my_plugin_foo", "123")
```
```js
async init() {
this.instance.server.handleUdp("my_plugin_foo", async data => {
this.logger.info(`got ${data.toString()}`);
});
}
```
Like `handle` for `send_json`, `handleUdp` logs errors thrown by the handler.
Use `this.instance.server.on("udp-my_plugin_foo", ...)` for custom error handling.
Data into Factorio is sent with the `sendUdp` method on the plugin object and delivered in-game as `defines.events.on_udp_packet_received` events with the packet in `event.payload`.
The Clusterio module polls for received packets every tick, all modules and mods receive every packet so make sure the payload identifies your plugin.