diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 3ab4ad6..3df6fc2 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -1229,8 +1229,11 @@ public class Agent if(compatibilityMode == CompatibilityMode.GTALK) { - localCandidate = findLocalCandidate(localAddress, - remoteUFrag); + if(localAddress.getTransport() == Transport.TCP) + localCandidate = findLocalCandidate(localAddress); + else + localCandidate = findLocalCandidate(localAddress, + remoteUFrag); } else { @@ -1328,10 +1331,21 @@ public class Agent triggerPair.getLocalCandidate().getTransportAddress(), triggerPair.getRemoteCandidate().getTransportAddress()); else - knownPair = findCandidatePair( + { + if(triggerPair.getLocalCandidate().getTransport() == Transport.TCP) + { + knownPair = findCandidatePair( + triggerPair.getLocalCandidate().getTransportAddress(), + triggerPair.getRemoteCandidate().getTransportAddress()); + } + else + { + knownPair = findCandidatePair( ((RemoteCandidate)triggerPair.getRemoteCandidate()). getUfrag(), triggerPair.getLocalCandidate().getUfrag()); + } + } IceMediaStream parentStream = triggerPair.getLocalCandidate() .getParentComponent().getParentStream(); @@ -1407,6 +1421,8 @@ public class Agent // Its state is set to Waiting [and it] is enqueued into the // triggered check queue. // + logger.info("Add peer CandidatePair with new reflexive address to" + + " checkList"); parentStream.addToCheckList(triggerPair); } diff --git a/src/org/ice4j/ice/ConnectivityCheckClient.java b/src/org/ice4j/ice/ConnectivityCheckClient.java index 98cca6f..38efd1d 100644 --- a/src/org/ice4j/ice/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/ConnectivityCheckClient.java @@ -578,6 +578,12 @@ class ConnectivityCheckClient //generated from it momentarily validLocalCandidate = peerReflexiveCandidate; + if(parentAgent.getCompatibilityMode() == CompatibilityMode.GTALK) + { + validLocalCandidate.setUfrag( + checkedPair.getLocalCandidate().getUfrag()); + } + logger.info("Receive a peer-reflexive candidate: " + peerReflexiveCandidate.getTransportAddress()); } diff --git a/src/org/ice4j/socket/DelegatingSocket.java b/src/org/ice4j/socket/DelegatingSocket.java index f9d2987..2daf805 100644 --- a/src/org/ice4j/socket/DelegatingSocket.java +++ b/src/org/ice4j/socket/DelegatingSocket.java @@ -795,9 +795,13 @@ public class DelegatingSocket inputStream = this.getInputStream(); } - short desiredLength = (short) - (((inputStream.read() << 8) & 0xff) | (inputStream.read() & 0xff)); + int fb = inputStream.read(); + int sb = inputStream.read(); + if(fb == -1 || sb == -1) + throw new SocketException("failed to read first two bytes"); + + int desiredLength = (((fb & 0xff) << 8) | (sb & 0xff)); int readLen = 0; int offset = 0; diff --git a/src/org/ice4j/socket/GoogleRelayedCandidateSocket.java b/src/org/ice4j/socket/GoogleRelayedCandidateSocket.java index 2818647..4031882 100644 --- a/src/org/ice4j/socket/GoogleRelayedCandidateSocket.java +++ b/src/org/ice4j/socket/GoogleRelayedCandidateSocket.java @@ -264,7 +264,7 @@ public class GoogleRelayedCandidateSocket * * @author Sebastien Vincent */ - private class TCPOutputStream + protected class TCPOutputStream extends OutputStream { /** diff --git a/src/org/ice4j/socket/IceTcpSocketWrapper.java b/src/org/ice4j/socket/IceTcpSocketWrapper.java index 48a5dbf..3e1256c 100644 --- a/src/org/ice4j/socket/IceTcpSocketWrapper.java +++ b/src/org/ice4j/socket/IceTcpSocketWrapper.java @@ -87,16 +87,32 @@ public class IceTcpSocketWrapper byte data[] = p.getData(); int len = 0; + int desiredLength = + (((inputStream.read() & 0xff) << 8) | (inputStream.read() & 0xff)); + int readLen = 0; + int offset = 0; - inputStream.skip(2); - len = inputStream.read(data); - if(len > 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; } diff --git a/src/org/ice4j/socket/TCPOutputStream.java b/src/org/ice4j/socket/TCPOutputStream.java index 8b31d5c..a62b56f 100644 --- a/src/org/ice4j/socket/TCPOutputStream.java +++ b/src/org/ice4j/socket/TCPOutputStream.java @@ -40,6 +40,7 @@ public class TCPOutputStream public void close() throws IOException { + outputStream.close(); } /** @@ -49,6 +50,7 @@ public class TCPOutputStream public void flush() throws IOException { + outputStream.flush(); } /** @@ -68,6 +70,14 @@ public class TCPOutputStream public void write(byte[] b, int off, int len) throws IOException { + // GoogleRelayedCandidateSocket will encapsulate data in TURN message + // so do not add framing here + if(outputStream instanceof GoogleRelayedCandidateSocket.TCPOutputStream) + { + outputStream.write(b, off, len); + return; + } + byte data[] = new byte[len + 2]; data[0] = (byte)((len >> 8) & 0xff); data[1] = (byte)(len & 0xff);