Corrects heap space consumption when TCP DelegatingSocket receives a packet with no data.

This commit is contained in:
chenzo 2012-06-28 14:18:40 +00:00
parent 525d7e14d1
commit 7634806e11
4 changed files with 72 additions and 52 deletions

View File

@ -568,7 +568,11 @@ public class DelegatingDatagramSocket
super.receive(p); super.receive(p);
// no exception packet is successfully received, log it. // 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))
{
++nbReceivedRtpPackets; ++nbReceivedRtpPackets;
}
logPacketToPcap( logPacketToPcap(
p, p,
this.nbReceivedRtpPackets, this.nbReceivedRtpPackets,

View File

@ -819,29 +819,80 @@ public class DelegatingSocket
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
* @see #receive(DatagramPacket) * @see #receive(DatagramPacket)
*/ */
public void receive(DatagramPacket p) throws IOException public void receive(DatagramPacket p)
throws IOException
{ {
if (delegate != null && delegate instanceof DelegatingSocket) if (delegate != null && delegate instanceof DelegatingSocket)
{ {
((DelegatingSocket) delegate).receive(p); ((DelegatingSocket) delegate).receive(p);
return;
} }
else
byte data[] = p.getData(); {
int len = 0;
if (inputStream == null) if (inputStream == null)
{ {
inputStream = this.getInputStream(); inputStream = this.getInputStream();
} }
DelegatingSocket.receiveFromNetwork(
p,
inputStream,
this.getInetAddress(),
this.getPort());
// 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))
{
++nbReceivedRtpPackets;
}
InetSocketAddress localAddress
= (InetSocketAddress) super.getLocalSocketAddress();
DelegatingDatagramSocket.logPacketToPcap(
p,
this.nbReceivedRtpPackets,
false,
localAddress.getAddress(),
localAddress.getPort());
// Log RTP losses if > 5%.
updateRtpLosses(p);
}
}
/**
* Reads TCP stream and fit corresponding bytes to the datagram given in
* parameter.
*
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
* data.
* @param inputStream The TCP stream to be read.
* @param inetAddress The receiver address (local address) to set to the
* datagram packet.
* @param port The receiver port (local port) to set to the datagram packet.
*
* @throws IOException if an I/O error occurs
* @see #receive(DatagramPacket)
*/
public static void receiveFromNetwork(
DatagramPacket p,
InputStream inputStream,
InetAddress inetAddress,
int port)
throws IOException
{
byte data[] = p.getData();
int len = 0;
int fb = inputStream.read(); int fb = inputStream.read();
int sb = inputStream.read(); int sb = inputStream.read();
// If we do not achieve to read the first bytes, then it was just an // If we do not achieve to read the first bytes, then it was just an
// hole punch packet. // hole punch packet.
if(fb == -1 || sb == -1) if(fb == -1 || sb == -1)
{
p.setLength(-1);
return; return;
}
int desiredLength = (((fb & 0xff) << 8) | (sb & 0xff)); int desiredLength = (((fb & 0xff) << 8) | (sb & 0xff));
int readLen = 0; int readLen = 0;
@ -860,26 +911,15 @@ public class DelegatingSocket
{ {
p.setData(data); p.setData(data);
p.setLength(len); p.setLength(len);
p.setAddress(this.getInetAddress()); p.setAddress(inetAddress);
p.setPort(this.getPort()); p.setPort(port);
} }
else else
{ {
throw new SocketException("Failed to receive data from socket"); throw new SocketException("Failed to receive data from socket");
} }
// no exception packet is successfully received, log it. data = null;
++nbReceivedRtpPackets;
InetSocketAddress localAddress
= (InetSocketAddress) super.getLocalSocketAddress();
DelegatingDatagramSocket.logPacketToPcap(
p,
this.nbReceivedRtpPackets,
false,
localAddress.getAddress(),
localAddress.getPort());
// Log RTP losses if > 5%.
updateRtpLosses(p);
} }
/** /**

View File

@ -85,35 +85,11 @@ public class IceTcpSocketWrapper
return; return;
} }
byte data[] = p.getData(); DelegatingSocket.receiveFromNetwork(
int len = 0; p,
int desiredLength = inputStream,
(((inputStream.read() & 0xff) << 8) | (inputStream.read() & 0xff)); this.getLocalAddress(),
int readLen = 0; this.getLocalPort());
int offset = 0;
while(readLen < desiredLength)
{
len = inputStream.read(data, offset, desiredLength - offset);
if(len == -1)
throw new SocketException("read failed");
offset += len;
readLen += len;
}
if (readLen == desiredLength)
{
p.setData(data);
p.setLength(len);
p.setAddress(this.getLocalAddress());
p.setPort(this.getLocalPort());
}
else
{
throw new SocketException("Failed to receive data from socket");
}
data = null;
} }
/** /**

View File

@ -253,7 +253,7 @@ public class MultiplexingDatagramSocket
int destLength = destData.length - destOffset; int destLength = destData.length - destOffset;
int srcLength = src.getLength(); int srcLength = src.getLength();
if (destLength > srcLength) if (destLength >= srcLength)
destLength = srcLength; destLength = srcLength;
else if (logger.isLoggable(Level.WARNING)) else if (logger.isLoggable(Level.WARNING))
{ {