NioUdpTransport implements DatagramTransport { @Override public int receive(byte[] buf, int off, int len, int waitMillis) throws IOException { if (this.hasTimeout()) { throw new IllegalStateException("Handshake is taking too long! (>" + MAX_DELAY + "ms"); } ByteBuffer buffer = ByteBuffer.wrap(buf, off, len); return this.channel.read(buffer); } NioUdpTransport(DatagramChannel channel); void start(); @Override int getReceiveLimit(); @Override int getSendLimit(); @Override int receive(byte[] buf, int off, int len, int waitMillis); @Override void send(byte[] buf, int off, int len); @Override void close(); static final int DEFAULT_MTU; final static int MAX_DELAY; }
@Test public void testReceive() throws IOException { NioUdpTransport dtlsTransport = new NioUdpTransport(localChannel); String msg= "this is data"; byte[] data = msg.getBytes(); byte[] recvBuffer = new byte[data.length]; ByteBuffer sendBuffer = ByteBuffer.allocate(data.length); sendBuffer.put(data); remoteChannel.send(sendBuffer, localChannel.getLocalAddress()); dtlsTransport.receive(recvBuffer, 0, recvBuffer.length, 0); Assert.assertEquals(msg, new String(data)); }
@Test(timeout = NioUdpTransport.MAX_DELAY * 2, expected = IllegalStateException.class) public void testTimeout() throws InterruptedException, IOException { NioUdpTransport dtlsTransport = new NioUdpTransport(localChannel); Thread.sleep(NioUdpTransport.MAX_DELAY + 1000); dtlsTransport.receive(new byte[5], 0, 5, 0); }