Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Evict closed data channels and reject non-object messages (#778)

Nguyen Xuan An6289bf4ac130
diff --git a/changes/778-data-channel-transport.size.md b/changes/778-data-channel-transport.size.md
index 5fab343e..352b742c 100644
--- a/changes/778-data-channel-transport.size.md
+++ b/changes/778-data-channel-transport.size.md
@@ -1 +1 @@
-- entry:netcode: the WebRTC data channel transport (about 417 bytes gzip) is what lets a host or server sync players and props over WebRTC, the transport #778 asks for beside WebSocket and BroadcastChannel
+- entry:netcode: the WebRTC data channel transport (6,949 to 7,446 bytes gzip, +497) is what lets a host or server sync players and props over WebRTC, the transport #778 asks for beside WebSocket and BroadcastChannel
diff --git a/docs/netcode.md b/docs/netcode.md
index d420b8be..df1ae32e 100644
--- a/docs/netcode.md
+++ b/docs/netcode.md
@@ -53,7 +53,7 @@ Other entities are drawn `interpolationDelaySeconds` (default 0.1) behind the ne
A transport is a plain object `{ id, peers(), send(peerId, message), receive(callback) }`. `peers()` returns the connected peer ids, `send` takes a JSON-compatible object and must not throw for a transient failure, and `receive(callback)` registers `callback(peerId, message)` and returns a function that unregisters it. Write your own for WebTransport.
-- `createDataChannelTransport({ id })` is the WebRTC transport. Your signalling code opens each `RTCDataChannel` and hands it over with `transport.addChannel(peerId, channel)`: a host adds one per player, a client adds one channel for the host. `removeChannel(peerId)` drops one. A peer is listed while its channel is `open`, so a player who disconnects leaves `peers()` on its own. Messages are JSON text, bounded by `maximumMessageBytes`; a malformed or oversized incoming message counts in `stats.rejected`. At most 32 channels. Use a reliable ordered channel, or `{ ordered: false, maxRetransmits: 0 }` for snapshots when you accept drops, which the delta scheme repairs. Signalling (offer, answer, ICE) is yours: this library opens no connection.
+- `createDataChannelTransport({ id })` is the WebRTC transport. Your signalling code opens each `RTCDataChannel` and hands it over with `transport.addChannel(peerId, channel)`: a host adds one per player, a client adds one channel for the host. `removeChannel(peerId)` drops one. A peer is listed while its channel is `open`, so a player who disconnects leaves `peers()` on its own. A channel that fires `close` or `error` is dropped from the transport, its listeners detached and its slot freed, so a long-lived host can see players come and go under new peer ids; a closed channel that never fired an event is dropped when `addChannel` reaches the 32-channel cap. Messages are JSON text, bounded by `maximumMessageBytes`; a malformed, oversized or non-object (`5`, `[]`, `"x"`, `null`) incoming message counts in `stats.rejected`. At most 32 channels. Use a reliable ordered channel, or `{ ordered: false, maxRetransmits: 0 }` for snapshots when you accept drops, which the delta scheme repairs. Signalling (offer, answer, ICE) is yours: this library opens no connection.
- `createLoopbackNetwork({ seed, latencyMilliseconds, jitterMilliseconds, lossRate })` is an in-memory network for tests. `network.createTransport(id)` adds a peer; `network.advance(milliseconds)` moves time and delivers what is due. Latency, jitter and loss come from the seeded generator, so a seed repeats a run exactly. Messages are copied through JSON and may arrive out of order.
- `createBroadcastChannelTransport(channel, { id })` wraps a `BroadcastChannel` so tabs, windows and workers of one browser play together with no server. Peers find each other with a hello the other side answers once, and a message to one peer reaches only that peer. A peer silent for `peerTimeoutMilliseconds` (default 5000) is forgotten until it speaks again, so a closed tab leaves the peer list; call `transport.close()` on `pagehide` to say goodbye at once. `now` swaps the clock for tests.
diff --git a/src/netcode.js b/src/netcode.js
index 666c5df1..85d14da9 100644
--- a/src/netcode.js
+++ b/src/netcode.js
@@ -1004,7 +1004,7 @@ export function createWebSocketTransport(socket, options = {}) {
// A transport over several WebRTC data channels (or any channel-like objects with send(text), addEventListener and
// readyState), one per peer. Your signalling code opens each RTCDataChannel and hands it over with addChannel(peerId, channel):
// a host adds one per player, a client adds one for the host. A peer is listed while its channel is open, so a player who
-// disconnects leaves the peer list on its own. Nothing here opens a connection.
+// disconnects leaves the peer list on its own; a channel that closes or errors is dropped and frees its peer slot. Nothing here opens a connection.
export function createDataChannelTransport(options = {}) {
const id = requireName(options.id, 'createDataChannelTransport id');
const maximumMessageBytes = boundedInteger(options.maximumMessageBytes ?? NETCODE_LIMITS.defaultMessageBytes, 'createDataChannelTransport maximumMessageBytes', 256, NETCODE_LIMITS.maximumMessageBytes);
@@ -1016,7 +1016,8 @@ export function createDataChannelTransport(options = {}) {
function readMessage(text) {
if (typeof text !== 'string' || text.length > maximumMessageBytes) return undefined;
try {
- return JSON.parse(text);
+ const parsed = JSON.parse(text);
+ return isPlainObject(parsed) ? parsed : undefined;
} catch {
return undefined;
}
@@ -1024,6 +1025,16 @@ export function createDataChannelTransport(options = {}) {
function detach(entry) {
entry.channel.removeEventListener?.('message', entry.onMessage);
+ entry.channel.removeEventListener?.('close', entry.onClose);
+ entry.channel.removeEventListener?.('error', entry.onClose);
+ }
+
+ function pruneClosedChannels() {
+ for (const [peerId, entry] of channels) {
+ if (entry.channel.readyState !== 'closed') continue;
+ detach(entry);
+ channels.delete(peerId);
+ }
}
return {
@@ -1033,6 +1044,7 @@ export function createDataChannelTransport(options = {}) {
if (channel === null || typeof channel !== 'object' || typeof channel.send !== 'function' || typeof channel.addEventListener !== 'function') {
throw new TypeError('addChannel needs a data-channel-like object with send(text) and addEventListener(type, listener), such as an RTCDataChannel.');
}
+ if (!channels.has(peerId) && channels.size >= NETCODE_LIMITS.maximumPeers) pruneClosedChannels();
if (!channels.has(peerId) && channels.size >= NETCODE_LIMITS.maximumPeers) {
throw new RangeError(`addChannel: at most ${NETCODE_LIMITS.maximumPeers} peers are supported; remove one with removeChannel(peerId) first.`);
}
@@ -1049,8 +1061,15 @@ export function createDataChannelTransport(options = {}) {
stats.received += 1;
for (const listener of [...listeners]) listener(peerId, message);
},
+ onClose() {
+ if (channels.get(peerId) !== entry) return;
+ detach(entry);
+ channels.delete(peerId);
+ },
};
channel.addEventListener('message', entry.onMessage);
+ channel.addEventListener('close', entry.onClose);
+ channel.addEventListener('error', entry.onClose);
channels.set(peerId, entry);
},
removeChannel(peerId) {
diff --git a/tests/netcode-datachannel.test.js b/tests/netcode-datachannel.test.js
index 63b36012..76a61f16 100644
--- a/tests/netcode-datachannel.test.js
+++ b/tests/netcode-datachannel.test.js
@@ -8,16 +8,22 @@ const idleScheduler = { request: () => 1, cancel: () => {} };
// Two connected data-channel-like ends, as RTCDataChannel pairs behave: send on one is a message event on the other.
function createChannelPair() {
- const ends = [0, 1].map(() => ({ readyState: 'open', listeners: new Set(), closed: false }));
+ // listeners holds message listeners; endListeners holds close and error listeners.
+ const ends = [0, 1].map(() => ({ readyState: 'open', listeners: new Set(), endListeners: new Set(), closed: false }));
ends.forEach((end, index) => {
const other = ends[1 - index];
+ const setFor = type => (type === 'message' ? end.listeners : end.endListeners);
end.send = text => other.listeners.forEach(listener => listener({ data: text }));
- end.addEventListener = (type, listener) => end.listeners.add(listener);
- end.removeEventListener = (type, listener) => end.listeners.delete(listener);
+ end.addEventListener = (type, listener) => setFor(type).add(listener);
+ end.removeEventListener = (type, listener) => setFor(type).delete(listener);
end.close = () => {
end.closed = true;
end.readyState = 'closed';
};
+ end.fireClose = () => {
+ end.readyState = 'closed';
+ [...end.endListeners].forEach(listener => listener({ type: 'close' }));
+ };
});
return ends;
}
@@ -71,7 +77,10 @@ test('a data channel transport rejects bad options, channels, oversized and malf
end.listeners.forEach(listener => listener({ data: 'not json' }));
end.listeners.forEach(listener => listener({ data: 42 }));
end.listeners.forEach(listener => listener({ data: 'x'.repeat(300) }));
- assert.equal(transport.stats.rejected, 3);
+ for (const payload of ['null', '5', '[]', '"x"', 'true']) end.listeners.forEach(listener => listener({ data: payload }));
+ end.listeners.forEach(listener => listener(undefined));
+ assert.equal(transport.stats.rejected, 9);
+ assert.equal(transport.stats.received, 0);
for (let index = 0; index < 31; index += 1) transport.addChannel(`peer-${index}`, createChannelPair()[0]);
assert.throws(() => transport.addChannel('one-too-many', createChannelPair()[0]), /at most 32 peers/);
transport.addChannel('b', createChannelPair()[0]);
@@ -80,6 +89,65 @@ test('a data channel transport rejects bad options, channels, oversized and malf
assert.equal(end.listeners.size, 0);
});
+test('a closed or errored channel is evicted, so players can come and go past the peer limit', () => {
+ const transport = createDataChannelTransport({ id: 'host' });
+ const hostEnds = [];
+ for (let index = 0; index < 32; index += 1) {
+ const [hostEnd] = createChannelPair();
+ hostEnds.push(hostEnd);
+ transport.addChannel(`player-${index}`, hostEnd);
+ }
+ hostEnds.forEach(end => end.fireClose());
+ assert.deepEqual(transport.peers(), []);
+ hostEnds.forEach(end => {
+ assert.equal(end.listeners.size, 0, 'the message listener is detached');
+ assert.equal(end.endListeners.size, 0, 'the close listener is detached');
+ });
+ const [freshEnd] = createChannelPair();
+ transport.addChannel('newcomer', freshEnd);
+ assert.deepEqual(transport.peers(), ['newcomer']);
+
+ // A channel that closes without an event is pruned when the cap is reached.
+ const quietTransport = createDataChannelTransport({ id: 'quiet' });
+ const quietEnds = [];
+ for (let index = 0; index < 32; index += 1) {
+ const [quietEnd] = createChannelPair();
+ quietEnds.push(quietEnd);
+ quietTransport.addChannel(`player-${index}`, quietEnd);
+ }
+ quietEnds.forEach(end => end.close());
+ quietTransport.addChannel('newcomer', createChannelPair()[0]);
+ assert.deepEqual(quietTransport.peers(), ['newcomer']);
+ quietEnds.forEach(end => assert.equal(end.listeners.size, 0));
+
+ // An error event evicts too.
+ const [errorEnd] = createChannelPair();
+ transport.addChannel('flaky', errorEnd);
+ errorEnd.fireClose();
+ assert.ok(!transport.peers().includes('flaky'));
+});
+
+test('adding a channel under an existing peer id replaces the old one, and a channel without close() is tolerated', () => {
+ const transport = createDataChannelTransport({ id: 'host' });
+ const heard = [];
+ transport.receive((peerId, message) => heard.push([peerId, message]));
+ const [oldEnd, oldRemote] = createChannelPair();
+ const [newEnd, newRemote] = createChannelPair();
+ transport.addChannel('player', oldEnd);
+ transport.addChannel('player', newEnd);
+ oldRemote.send('{"from":"old"}');
+ newRemote.send('{"from":"new"}');
+ assert.deepEqual(heard, [['player', { from: 'new' }]]);
+ assert.equal(oldEnd.endListeners.size, 0);
+ oldEnd.fireClose();
+ assert.deepEqual(transport.peers(), ['player'], 'the replaced channel closing does not evict the new one');
+
+ const bare = { readyState: 'open', send() {}, addEventListener() {} };
+ transport.addChannel('bare', bare);
+ transport.close();
+ assert.deepEqual(transport.peers(), []);
+});
+
test('a host and two clients sync players and a driven prop over data channels', () => {
const hostTransport = createDataChannelTransport({ id: 'host' });
const host = createPeer({ role: 'authority', authority: 'host', transport: hostTransport, localEntityId: 'host' });