When a SocketChannel is in use, reads using the SocketChannel directly instead of through its InputStream.

This commit is contained in:
boris 2014-09-03 14:34:45 +00:00
parent 3b7a913eac
commit 6932bd407c
3 changed files with 80 additions and 23 deletions

View File

@ -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 +

View File

@ -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 <tt>ByteBuffer</tt> 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 <tt>DelegatingSocket</tt>.
*/
@ -826,6 +834,10 @@ public class DelegatingSocket
}
else
{
SocketChannel channel = getChannel();
if (channel == null)
{
// Read from our InputStream
if (inputStream == null)
{
inputStream = this.getInputStream();
@ -836,6 +848,15 @@ public class DelegatingSocket
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.
@ -854,12 +875,12 @@ public class DelegatingSocket
// 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 <tt>inputStream</tt> into
* <tt>p</tt>, and sets <tt>p</tt>'s port and address to <tt>port</tt> and
* <tt>inetAddress</tt>.
*
* @param p the <tt>DatagramPacket</tt> 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 <tt>channel</tt> into
* <tt>p</tt>, and sets <tt>p</tt>'s port and address to the remote port
* and address of this <tt>Socket</tt>.
*/
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());
}
/**