diff --git a/src/org/ice4j/socket/DelegatingDatagramSocket.java b/src/org/ice4j/socket/DelegatingDatagramSocket.java index 86afb88..d06c9a1 100644 --- a/src/org/ice4j/socket/DelegatingDatagramSocket.java +++ b/src/org/ice4j/socket/DelegatingDatagramSocket.java @@ -568,7 +568,11 @@ public class DelegatingDatagramSocket super.receive(p); // no exception packet is successfully received, log it. - ++nbReceivedRtpPackets; + // If this is not a STUN/TURN packet, then this is a RTP packet. + if(!StunDatagramPacketFilter.isStunPacket(p)) + { + ++nbReceivedRtpPackets; + } logPacketToPcap( p, this.nbReceivedRtpPackets, diff --git a/src/org/ice4j/socket/DelegatingSocket.java b/src/org/ice4j/socket/DelegatingSocket.java index 9c8e66c..8756dfe 100644 --- a/src/org/ice4j/socket/DelegatingSocket.java +++ b/src/org/ice4j/socket/DelegatingSocket.java @@ -819,29 +819,80 @@ public class DelegatingSocket * @throws IOException if an I/O error occurs * @see #receive(DatagramPacket) */ - public void receive(DatagramPacket p) throws IOException + public void receive(DatagramPacket p) + throws IOException { if (delegate != null && delegate instanceof DelegatingSocket) { ((DelegatingSocket) delegate).receive(p); - return; + } + else + { + if (inputStream == null) + { + 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 DatagramPacket 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; - if (inputStream == null) - { - inputStream = this.getInputStream(); - } - int fb = inputStream.read(); int sb = inputStream.read(); // If we do not achieve to read the first bytes, then it was just an // hole punch packet. if(fb == -1 || sb == -1) + { + p.setLength(-1); return; + } int desiredLength = (((fb & 0xff) << 8) | (sb & 0xff)); int readLen = 0; @@ -860,26 +911,15 @@ public class DelegatingSocket { p.setData(data); p.setLength(len); - p.setAddress(this.getInetAddress()); - p.setPort(this.getPort()); + p.setAddress(inetAddress); + p.setPort(port); } else { throw new SocketException("Failed to receive data from socket"); } - // no exception packet is successfully received, log it. - ++nbReceivedRtpPackets; - InetSocketAddress localAddress - = (InetSocketAddress) super.getLocalSocketAddress(); - DelegatingDatagramSocket.logPacketToPcap( - p, - this.nbReceivedRtpPackets, - false, - localAddress.getAddress(), - localAddress.getPort()); - // Log RTP losses if > 5%. - updateRtpLosses(p); + data = null; } /** diff --git a/src/org/ice4j/socket/IceTcpSocketWrapper.java b/src/org/ice4j/socket/IceTcpSocketWrapper.java index 3e1256c..5c4d11c 100644 --- a/src/org/ice4j/socket/IceTcpSocketWrapper.java +++ b/src/org/ice4j/socket/IceTcpSocketWrapper.java @@ -85,35 +85,11 @@ public class IceTcpSocketWrapper return; } - byte data[] = p.getData(); - int len = 0; - int desiredLength = - (((inputStream.read() & 0xff) << 8) | (inputStream.read() & 0xff)); - int readLen = 0; - 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; + DelegatingSocket.receiveFromNetwork( + p, + inputStream, + this.getLocalAddress(), + this.getLocalPort()); } /** diff --git a/src/org/ice4j/socket/MultiplexingDatagramSocket.java b/src/org/ice4j/socket/MultiplexingDatagramSocket.java index dd97e37..8a66673 100644 --- a/src/org/ice4j/socket/MultiplexingDatagramSocket.java +++ b/src/org/ice4j/socket/MultiplexingDatagramSocket.java @@ -253,7 +253,7 @@ public class MultiplexingDatagramSocket int destLength = destData.length - destOffset; int srcLength = src.getLength(); - if (destLength > srcLength) + if (destLength >= srcLength) destLength = srcLength; else if (logger.isLoggable(Level.WARNING)) {