Search in sources :

Example 46 with DatagramChannel

use of java.nio.channels.DatagramChannel in project netty by netty.

the class NioDatagramChannel method doReadMessages.

@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannel ch = javaChannel();
    DatagramChannelConfig config = config();
    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    ByteBuf data = allocHandle.allocate(config.getAllocator());
    allocHandle.attemptedBytesRead(data.writableBytes());
    boolean free = true;
    try {
        ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
        int pos = nioData.position();
        InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
        if (remoteAddress == null) {
            return 0;
        }
        allocHandle.lastBytesRead(nioData.position() - pos);
        buf.add(new DatagramPacket(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()), localAddress(), remoteAddress));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    } finally {
        if (free) {
            data.release();
        }
    }
}
Also used : RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) DatagramChannel(java.nio.channels.DatagramChannel) DatagramChannelConfig(io.netty.channel.socket.DatagramChannelConfig) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 47 with DatagramChannel

use of java.nio.channels.DatagramChannel in project jeromq by zeromq.

the class TestZLoop method testZLoopWithUDP.

@Test
public void testZLoopWithUDP() throws IOException {
    final int port = Utils.findOpenPort();
    final InetSocketAddress addr = new InetSocketAddress("127.0.0.1", port);
    ZLoop loop = new ZLoop(ctx);
    DatagramChannel udpIn = DatagramChannel.open();
    assertThat(udpIn, notNullValue());
    udpIn.configureBlocking(false);
    udpIn.socket().bind(new InetSocketAddress(port));
    DatagramChannel udpOut = DatagramChannel.open();
    assertThat(udpOut, notNullValue());
    udpOut.configureBlocking(false);
    udpOut.socket().connect(addr);
    final AtomicInteger counter = new AtomicInteger();
    final AtomicBoolean done = new AtomicBoolean();
    loop.addPoller(new PollItem(udpIn, ZMQ.Poller.POLLIN), new IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            DatagramChannel udpIn = (DatagramChannel) arg;
            ByteBuffer bb = ByteBuffer.allocate(3);
            try {
                udpIn.receive(bb);
                String read = new String(bb.array(), 0, bb.limit(), ZMQ.CHARSET);
                assertThat(read, is("udp"));
                done.set(true);
                counter.incrementAndGet();
            } catch (IOException e) {
                e.printStackTrace();
                fail();
            }
            return -1;
        }
    }, udpIn);
    loop.addPoller(new PollItem(udpOut, ZMQ.Poller.POLLOUT), new IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            DatagramChannel udpOut = (DatagramChannel) arg;
            try {
                ByteBuffer bb = ByteBuffer.allocate(3);
                bb.put("udp".getBytes(ZMQ.CHARSET));
                bb.flip();
                int written = udpOut.send(bb, addr);
                assertThat(written, is(3));
                counter.incrementAndGet();
            } catch (IOException e) {
                e.printStackTrace();
                fail();
            }
            return 0;
        }
    }, udpOut);
    loop.start();
    assertThat(done.get(), is(true));
    assertThat(counter.get(), is(2));
    udpIn.close();
    udpOut.close();
}
Also used : PollItem(org.zeromq.ZMQ.PollItem) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IZLoopHandler(org.zeromq.ZLoop.IZLoopHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 48 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelTest method test_getRemoteAddress.

public void test_getRemoteAddress() throws IOException {
    InetSocketAddress socketAddress = new InetSocketAddress(Inet4Address.LOOPBACK, 0);
    try (DatagramChannel clientChannel = DatagramChannel.open();
        DatagramChannel serverChannel = DatagramChannel.open()) {
        serverChannel.bind(socketAddress);
        assertNull(clientChannel.getRemoteAddress());
        clientChannel.connect(serverChannel.getLocalAddress());
        assertEquals(socketAddress.getAddress(), ((InetSocketAddress) (clientChannel.getRemoteAddress())).getAddress());
        assertEquals(((InetSocketAddress) (serverChannel.getLocalAddress())).getPort(), ((InetSocketAddress) (clientChannel.getRemoteAddress())).getPort());
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel)

Example 49 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelTest method test_bounded_harmony6493.

public void test_bounded_harmony6493() throws IOException {
    DatagramChannel server = DatagramChannel.open();
    InetSocketAddress addr = new InetSocketAddress("localhost", 0);
    server.socket().bind(addr);
    SocketAddress boundedAddress = server.socket().getLocalSocketAddress();
    DatagramChannel client = DatagramChannel.open();
    ByteBuffer sent = ByteBuffer.allocate(1024);
    sent.put("test".getBytes());
    sent.flip();
    client.send(sent, boundedAddress);
    assertTrue(client.socket().isBound());
    server.close();
    client.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ByteBuffer(java.nio.ByteBuffer)

Example 50 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelTest method testReceive_UnboundBufFull.

public void testReceive_UnboundBufFull() throws Exception {
    DatagramChannel dc = DatagramChannel.open();
    assertFalse(dc.isConnected());
    assertFalse(dc.socket().isBound());
    ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ONE);
    // buf is full
    dst.put((byte) 88);
    assertEquals(dst.position(), dst.limit());
    assertNull(dc.receive(dst));
    dc.close();
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DatagramChannel (java.nio.channels.DatagramChannel)211 InetSocketAddress (java.net.InetSocketAddress)91 ByteBuffer (java.nio.ByteBuffer)71 IOException (java.io.IOException)57 DatagramSocket (java.net.DatagramSocket)21 SocketAddress (java.net.SocketAddress)21 MembershipKey (java.nio.channels.MembershipKey)21 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 SocketChannel (java.nio.channels.SocketChannel)17 SelectionKey (java.nio.channels.SelectionKey)16 InetAddress (java.net.InetAddress)13 Selector (java.nio.channels.Selector)13 Test (org.junit.Test)11 SocketException (java.net.SocketException)9 ClosedChannelException (java.nio.channels.ClosedChannelException)9 Histogram (org.HdrHistogram.Histogram)8 CancelledKeyException (java.nio.channels.CancelledKeyException)7 DatagramPacket (java.net.DatagramPacket)5 NetworkInterface (java.net.NetworkInterface)5 ArrayList (java.util.ArrayList)5