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
9 changes: 7 additions & 2 deletions webrtc-jni/src/main/cpp/src/JNI_RTCDataChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "JavaEnums.h"
#include "JavaError.h"
#include "JavaRef.h"
#include "JavaRuntimeException.h"
#include "JavaString.h"
#include "JavaUtils.h"

Expand Down Expand Up @@ -174,7 +175,9 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCDataChannel_sendDirectBuffer

webrtc::CopyOnWriteBuffer data(address, static_cast<size_t>(bufferLength));

channel->Send(webrtc::DataBuffer(data, static_cast<bool>(isBinary)));
if (!channel->Send(webrtc::DataBuffer(data, static_cast<bool>(isBinary)))) {
env->Throw(jni::JavaRuntimeException(env, "Data channel rejected the send"));
}
}
else {
env->Throw(jni::JavaError(env, "Non-direct buffer provided"));
Expand All @@ -195,7 +198,9 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_RTCDataChannel_sendByteArrayBuffer
env->ReleaseByteArrayElements(jBufferArray, arrayPtr, JNI_ABORT);

try {
channel->Send(webrtc::DataBuffer(data, static_cast<bool>(isBinary)));
if (!channel->Send(webrtc::DataBuffer(data, static_cast<bool>(isBinary)))) {
env->Throw(jni::JavaRuntimeException(env, "Data channel rejected the send"));
}
}
catch (...) {
ThrowCxxJavaException(env);
Expand Down
4 changes: 2 additions & 2 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCDataChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ private RTCDataChannel() {
*
* @param buffer The buffer to be queued for transmission.
*
* @throws Exception If queuing data is not possible because not enough
* buffer space is available.
* @throws Exception If the native channel rejects the send, for example
* because it is not open or its send buffer is full.
*/
public void send(RTCDataChannelBuffer buffer) throws Exception {
ByteBuffer data = buffer.data;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.onvoid.webrtc;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.nio.ByteBuffer;

import org.junit.jupiter.api.Test;

class RTCDataChannelSendRejectionTests extends TestBase {

@Test
void rejectsHeapBufferBeforeOpen() {
assertRejected(ByteBuffer.allocate(8), false);
}

@Test
void rejectsDirectBufferBeforeOpen() {
assertRejected(ByteBuffer.allocateDirect(8), false);
}

@Test
void rejectsHeapBufferAfterClose() {
assertRejected(ByteBuffer.allocate(8), true);
}

@Test
void rejectsDirectBufferAfterClose() {
assertRejected(ByteBuffer.allocateDirect(8), true);
}

private void assertRejected(ByteBuffer data, boolean closed) {
RTCPeerConnection peer = factory.createPeerConnection(
new RTCConfiguration(), candidate -> { });
RTCDataChannel channel = peer.createDataChannel("send", new RTCDataChannelInit());

try {
if (closed) {
channel.close();
}
assertEquals(closed ? RTCDataChannelState.CLOSED : RTCDataChannelState.CONNECTING,
channel.getState());
RuntimeException error = assertThrows(RuntimeException.class,
() -> channel.send(new RTCDataChannelBuffer(data, true)));
assertEquals("Data channel rejected the send", error.getMessage());
}
finally {
peer.close();
channel.dispose();
}
}
}