diff --git a/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java b/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java index ffbb78e..affd621 100644 --- a/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java +++ b/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java @@ -510,12 +510,6 @@ public class MultiplexingTcpHostHarvester private final List toRemove = new LinkedList(); - /** - * A DatagramPacket into which we will try to read STUN - * messages. - */ - private final DatagramPacket datagramPacket; - /** * Initializes a new ReadThread. * @throws IOException if the selector to be used fails to open. @@ -524,7 +518,6 @@ public class MultiplexingTcpHostHarvester throws IOException { selector = MultiplexingTcpHostHarvester.this.readSelector; - datagramPacket = new DatagramPacket(new byte[1500], 1500); } /** @@ -676,7 +669,7 @@ public class MultiplexingTcpHostHarvester = new byte[GoogleTurnSSLCandidateHarvester .SSL_CLIENT_HANDSHAKE.length]; - socket.getInputStream().read(buf); + inputStream.read(buf); if (Arrays.equals(buf, GoogleTurnSSLCandidateHarvester .SSL_CLIENT_HANDSHAKE)) @@ -691,12 +684,19 @@ public class MultiplexingTcpHostHarvester "Expected a pseudo ssl handshake, didn't get one."); } } + // read an RFC4571 frame into datagramPacket + + // TODO refactor so that: + // 1. We don't block + // 2. We know the length of the buffer to allocate + DatagramPacket datagramPacket + = new DatagramPacket(new byte[1500],1500); DelegatingSocket.receiveFromNetwork( datagramPacket, inputStream, - socket.getLocalAddress(), - socket.getLocalPort()); + socket.getInetAddress(), + socket.getPort()); // Does this look like a STUN binding request? // What's the username? @@ -724,7 +724,7 @@ public class MultiplexingTcpHostHarvester //phew, finally - handSocketToComponent(socket, component); + handSocketToComponent(socket, component, datagramPacket); } catch (IOException e) { @@ -745,11 +745,15 @@ public class MultiplexingTcpHostHarvester } /** - * Makes socket available to component. + * Makes socket available to component and pushes + * back datagramPacket into the STUN socket. * @param socket the Socket. * @param component the Component. + * @param datagramPacket the DatagramPacket to push back. */ - private void handSocketToComponent(Socket socket, Component component) + private void handSocketToComponent(Socket socket, + Component component, + DatagramPacket datagramPacket) { IceProcessingState state = component.getParentStream().getParentAgent().getState(); @@ -775,6 +779,8 @@ public class MultiplexingTcpHostHarvester stunSocket = new IceTcpSocketWrapper( multiplexing.getSocket(new StunDatagramPacketFilter())); + stunSocket + = new PushBackIceSocketWrapper(stunSocket, datagramPacket); } catch (IOException ioe) { @@ -887,4 +893,131 @@ public class MultiplexingTcpHostHarvester super(s); } } + + /** + * An IceSocketWrapper implementation which allows a + * DatagramPacket to be pushed back and received on the first + * call to {@link #receive(java.net.DatagramPacket)} + */ + private static class PushBackIceSocketWrapper + extends IceSocketWrapper + { + /** + * The IceSocketWrapper that this instance wraps around. + */ + private IceSocketWrapper wrapped; + + /** + * The DatagramPacket which will be used on the first call + * to {@link #receive(java.net.DatagramPacket)}. + */ + private DatagramPacket datagramPacket; + + /** + * Initializes a new PushBackIceSocketWrapper instance that + * wraps around wrappedWrapper and reads from + * datagramSocket on the first call to + * {@link #receive(java.net.DatagramPacket)} + * + * @param wrappedWrapper the IceSocketWrapper instance that we + * wrap around. + * @param datagramPacket the DatagramPacket which will be used + * on the first call to {@link #receive(java.net.DatagramPacket)} + */ + private PushBackIceSocketWrapper(IceSocketWrapper wrappedWrapper, + DatagramPacket datagramPacket) + { + this.wrapped = wrappedWrapper; + this.datagramPacket = datagramPacket; + } + + /** + * {@inheritDoc} + */ + @Override + public void send(DatagramPacket p) throws IOException + { + wrapped.send(p); + } + + /** + * {@inheritDoc} + * + * On the first call to this instance reads from {@link #datagramPacket}, + * on subsequent calls delegates to {@link #wrapped}. + */ + @Override + public void receive(DatagramPacket p) throws IOException + { + if (datagramPacket != null) + { + int len = Math.min(p.getLength(), datagramPacket.getLength()); + System.arraycopy(datagramPacket.getData(), 0, + p.getData(), 0, + len); + p.setAddress(datagramPacket.getAddress()); + p.setPort(datagramPacket.getPort()); + datagramPacket = null; + } + else + { + wrapped.receive(p); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void close() + { + wrapped.close(); + } + + /** + * {@inheritDoc} + */ + @Override + public InetAddress getLocalAddress() + { + return wrapped.getLocalAddress(); + } + + /** + * {@inheritDoc} + */ + @Override + public int getLocalPort() + { + return wrapped.getLocalPort(); + } + + /** + * {@inheritDoc} + */ + @Override + public SocketAddress getLocalSocketAddress() + { + return wrapped.getLocalSocketAddress(); + } + + /** + * {@inheritDoc} + */ + @Override + public Socket getTCPSocket() + { + return wrapped.getTCPSocket(); + } + + /** + * {@inheritDoc} + */ + @Override + public DatagramSocket getUDPSocket() + { + return wrapped.getUDPSocket(); + } + } + }