Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion design/client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ sequenceDiagram
ordering holds whatever executor is given.
4. **No lost events from registration races.** `onStateChange` is called with the current state
first. `onTrack` and `onChannel` replay what already arrived. A data channel keeps the messages
that arrive before its first `onMessage` handler.
that arrive before its first `onMessage` handler. A handler registered from within another
handler takes effect at once, so it also sees the events already queued behind that one.
5. **Operations run in order.** Each peer runs its operations one at a time, like the W3C
operations chain. `addTrack` followed by `createChannel` always negotiates both, in that order.
6. **Close is final and quiet.** After `close()` the only event is `onStateChange(CLOSED)`. Pending
Expand Down
8 changes: 7 additions & 1 deletion webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,15 @@ void deliverError(WebRtcException error) {

/**
* Registers a handler on the event executor, where it sees exactly the
* events the other handlers saw.
* events the other handlers saw. A handler that registers another does so
* at once, so that the new one also sees the events already queued, such
* as the messages of a channel it was just handed.
*/
Subscription subscribe(Supplier<Subscription> register) {
if (events.inExecutor()) {
return register.get();
}

DeferredSubscription subscription = new DeferredSubscription();

events.execute(() -> subscription.set(register.get()));
Expand Down
38 changes: 38 additions & 0 deletions webrtc/src/test/java/dev/onvoid/webrtc/client/PeerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,44 @@ void slowHandlerClosesChannel() throws Exception {
assertEquals(Arrays.asList(0, 1, 2, 3), handled);
}

@Test
void handlerAddedOnChannelGetsQueuedMessages() throws Exception {
// The channel handler returns only after the receiver closed the
// channel, so the messages and the close wait behind it. The message
// handler it adds still gets those that fit the limit.
int limit = 64 * 1024;
Peer[] peers = pair(options -> options.channelReceiveLimit(limit));
CountDownLatch overflowed = new CountDownLatch(1);
List<Integer> handled = new CopyOnWriteArrayList<>();
CompletableFuture<WebRtcException> error = new CompletableFuture<>();

peers[1].onError(error::complete);
peers[1].onChannel(remote -> {
awaitQuietly(overflowed);
remote.onMessage(message -> handled.add(message.bytes()[0] & 0xFF));
});

DataChannel channel = peers[0].createChannel("bulk");
CompletableFuture<Void> closed = whenClosed(channel);
closed.thenRun(overflowed::countDown);
byte[] payload = new byte[16 * 1024];

try {
for (int i = 0; i < 10; i++) {
payload[0] = (byte) i;
channel.send(payload);
}

await(closed);
}
finally {
overflowed.countDown();
}

assertTrue(await(error).getMessage().contains("fell more than"), () -> error.join().getMessage());
assertEquals(Arrays.asList(0, 1, 2, 3), handled);
}

@Test
void handledMessagesFreeLimit() throws Exception {
// Far more than the limit passes through, a message at a time.
Expand Down
Loading