less noisy logcat. catch illegal state exception as result of race condition

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/xmpp/jingle/transports/WebRTCDataChannelTransport.java | 14 
1 file changed, 11 insertions(+), 3 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/xmpp/jingle/transports/WebRTCDataChannelTransport.java 🔗

@@ -191,7 +191,6 @@ public class WebRTCDataChannelTransport implements Transport {
                 new OnMessageObserver() {
                     @Override
                     public void onMessage(final DataChannel.Buffer buffer) {
-                        Log.d(Config.LOGTAG, "onMessage() (the other one)");
                         try {
                             WebRTCDataChannelTransport.this.writableByteChannel.write(buffer.data);
                         } catch (final IOException e) {
@@ -584,8 +583,7 @@ public class WebRTCDataChannelTransport implements Transport {
                         Log.d(Config.LOGTAG, "DataChannelWriter reached EOF");
                         return;
                     }
-                    dataChannel.send(
-                            new DataChannel.Buffer(ByteBuffer.wrap(buffer, 0, count), true));
+                    send(ByteBuffer.wrap(buffer, 0, count));
                 }
             } catch (final InterruptedException | InterruptedIOException e) {
                 if (isSending.get()) {
@@ -598,6 +596,16 @@ public class WebRTCDataChannelTransport implements Transport {
             }
         }
 
+        private void send(final ByteBuffer byteBuffer) throws IOException {
+            try {
+                dataChannel.send(new DataChannel.Buffer(byteBuffer, true));
+            } catch (final IllegalStateException e) {
+                // dataChannel can be 'disposed' if we waited too long between `isSending` check and
+                // actually trying to send
+                throw new IOException(e);
+            }
+        }
+
         public void close() {
             this.isSending.set(false);
             terminate(this.dataChannel);