From 6932bd407c403592985ae7ec2154a7d40c7fbbd7 Mon Sep 17 00:00:00 2001 From: boris Date: Wed, 3 Sep 2014 14:34:45 +0000 Subject: [PATCH] When a SocketChannel is in use, reads using the SocketChannel directly instead of through its InputStream. --- .../ice4j/ice/ConnectivityCheckClient.java | 8 +- src/org/ice4j/socket/DelegatingSocket.java | 93 +++++++++++++++---- .../socket/MultiplexingDatagramSocket.java | 2 +- 3 files changed, 80 insertions(+), 23 deletions(-) diff --git a/src/org/ice4j/ice/ConnectivityCheckClient.java b/src/org/ice4j/ice/ConnectivityCheckClient.java index 3e8f240..d9365e5 100644 --- a/src/org/ice4j/ice/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/ConnectivityCheckClient.java @@ -319,7 +319,7 @@ class ConnectivityCheckClient if (!checkSymmetricAddresses(ev)) { logger.info("Received a non-symmetric response for pair: " - + checkedPair.toShortString() + ". Failing."); + + checkedPair.toShortString() + ". Failing."); checkedPair.setStateFailed(); } else @@ -328,7 +328,7 @@ class ConnectivityCheckClient char messageType = response.getMessageType(); //handle error responses. - if(messageType== Response.BINDING_ERROR_RESPONSE) + if(messageType == Response.BINDING_ERROR_RESPONSE) { if(!response.containsAttribute(Attribute.ERROR_CODE)) { @@ -748,10 +748,6 @@ class ConnectivityCheckClient else { int code = cl * 100 + co; - logger.fine( - "Received an unrecoverable error response (code = " + code - + ") for pair " + pair.toShortString() + - " will mark the pair as FAILED."); logger.info( "Error response for pair: " + pair.toShortString() + ", failing. Code = " + code + diff --git a/src/org/ice4j/socket/DelegatingSocket.java b/src/org/ice4j/socket/DelegatingSocket.java index 70ab488..46f582c 100644 --- a/src/org/ice4j/socket/DelegatingSocket.java +++ b/src/org/ice4j/socket/DelegatingSocket.java @@ -7,6 +7,7 @@ package org.ice4j.socket; import java.io.*; +import java.nio.*; import java.nio.channels.*; import java.net.*; @@ -59,6 +60,13 @@ public class DelegatingSocket */ private long lastLostPacketLogTime = 0; + /** + * A ByteBuffer instance used in + * {@link #receiveFromChannel(java.nio.channels.SocketChannel, java.net.DatagramPacket)} + * to read the 2-byte length field into. + */ + private final ByteBuffer byteBufferLen = ByteBuffer.allocate(2); + /** * Initializes a new DelegatingSocket. */ @@ -826,40 +834,53 @@ public class DelegatingSocket } else { - if (inputStream == null) + SocketChannel channel = getChannel(); + if (channel == null) { - inputStream = this.getInputStream(); - } + // Read from our InputStream + if (inputStream == null) + { + inputStream = this.getInputStream(); + } - DelegatingSocket.receiveFromNetwork( - p, - inputStream, - this.getInetAddress(), - this.getPort()); + DelegatingSocket.receiveFromNetwork( + p, + inputStream, + this.getInetAddress(), + this.getPort()); + } + else + { + // For nio SocketChannel-s, the read() from the InputStream and + // the write() to the OutputStream both lock on the same object. + // So, read from the Channel directly in order to avoid + // preventing any writing threads from proceeding. + receiveFromChannel(channel, p); + } // no exception packet is successfully received, log it. // If this is not a STUN/TURN packet, then this is a RTP packet. - if(!StunDatagramPacketFilter.isStunPacket(p)) + if (!StunDatagramPacketFilter.isStunPacket(p)) { ++nbReceivedRtpPackets; } InetSocketAddress localAddress - = (InetSocketAddress) super.getLocalSocketAddress(); + = (InetSocketAddress) super.getLocalSocketAddress(); DelegatingDatagramSocket.logPacketToPcap( p, this.nbReceivedRtpPackets, false, localAddress.getAddress(), localAddress.getPort()); - // Log RTP losses if > 5%. - updateRtpLosses(p); + // Log RTP losses if > 5%. + updateRtpLosses(p); } - } /** - * Reads TCP stream and fit corresponding bytes to the datagram given in - * parameter. + * Receives an RFC4571-formatted frame from inputStream into + * p, and sets p's port and address to port and + * inetAddress. * * @param p the DatagramPacket into which to place the incoming * data. @@ -916,8 +937,48 @@ public class DelegatingSocket { throw new SocketException("Failed to receive data from socket"); } + } - data = null; + /** + * Receives an RFC4571-formatted frame from channel into + * p, and sets p's port and address to the remote port + * and address of this Socket. + */ + private synchronized void receiveFromChannel(SocketChannel channel, + DatagramPacket p) + throws IOException + { + int ret; + while (byteBufferLen.hasRemaining()) + { + ret = channel.read(byteBufferLen); + if (ret == -1) + throw new SocketException("Failed to receive data from socket."); + } + + byteBufferLen.flip(); + int fb = byteBufferLen.get(); + int sb = byteBufferLen.get(); + int frameLength = (((fb & 0xff) << 8) | (sb & 0xff)); + byteBufferLen.flip(); + + byte[] data = p.getData(); + if (data == null || data.length < frameLength) + data = new byte[frameLength]; + + ByteBuffer byteBuffer = ByteBuffer.wrap(data, 0, frameLength); + + while (byteBuffer.hasRemaining()) + { + ret = channel.read(byteBuffer); + if (ret == -1) + throw new SocketException("Failed to receive data from socket."); + } + + p.setData(data); + p.setLength(frameLength); + p.setAddress(getInetAddress()); + p.setPort(getPort()); } /** diff --git a/src/org/ice4j/socket/MultiplexingDatagramSocket.java b/src/org/ice4j/socket/MultiplexingDatagramSocket.java index 08d762e..8a49958 100644 --- a/src/org/ice4j/socket/MultiplexingDatagramSocket.java +++ b/src/org/ice4j/socket/MultiplexingDatagramSocket.java @@ -404,7 +404,7 @@ public class MultiplexingDatagramSocket @Override public int getSoTimeout() { - return soTimeout; + return soTimeout; } /**