Search in sources :

Example 1 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project vert.x by eclipse.

the class DatagramServerHandler method safeObject.

@Override
protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception {
    if (msg instanceof DatagramPacket) {
        DatagramPacket packet = (DatagramPacket) msg;
        ByteBuf content = packet.content();
        if (content.isDirect()) {
            content = safeBuffer(content, allocator);
        }
        return new DatagramPacketImpl(packet.sender(), Buffer.buffer(content));
    }
    return msg;
}
Also used : DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project vert.x by eclipse.

the class DatagramSocketImpl method doSend.

private void doSend(Buffer packet, InetSocketAddress addr, Handler<AsyncResult<DatagramSocket>> handler) {
    ChannelFuture future = channel().writeAndFlush(new DatagramPacket(packet.getByteBuf(), addr));
    addListener(future, handler);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DatagramPacket(io.netty.channel.socket.DatagramPacket)

Example 3 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project netty by netty.

the class DatagramUnicastTest method testSimpleSend0.

@SuppressWarnings("deprecation")
private void testSimpleSend0(Bootstrap sb, Bootstrap cb, ByteBuf buf, boolean bindClient, final byte[] bytes, int count) throws Throwable {
    final CountDownLatch latch = new CountDownLatch(count);
    sb.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(new SimpleChannelInboundHandler<DatagramPacket>() {

                @Override
                public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
                    ByteBuf buf = msg.content();
                    assertEquals(bytes.length, buf.readableBytes());
                    for (byte b : bytes) {
                        assertEquals(b, buf.readByte());
                    }
                    latch.countDown();
                }
            });
        }
    });
    cb.handler(new SimpleChannelInboundHandler<Object>() {

        @Override
        public void channelRead0(ChannelHandlerContext ctx, Object msgs) throws Exception {
        // Nothing will be sent.
        }
    });
    Channel sc = null;
    BindException bindFailureCause = null;
    for (int i = 0; i < 3; i++) {
        try {
            sc = sb.bind().sync().channel();
            break;
        } catch (Exception e) {
            if (e instanceof BindException) {
                logger.warn("Failed to bind to a free port; trying again", e);
                bindFailureCause = (BindException) e;
                refreshLocalAddress(sb);
            } else {
                throw e;
            }
        }
    }
    if (sc == null) {
        throw bindFailureCause;
    }
    Channel cc;
    if (bindClient) {
        cc = cb.bind().sync().channel();
    } else {
        cb.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true);
        cc = cb.register().sync().channel();
    }
    for (int i = 0; i < count; i++) {
        cc.write(new DatagramPacket(buf.retain().duplicate(), addr));
    }
    // release as we used buf.retain() before
    buf.release();
    cc.flush();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    sc.close().sync();
    cc.close().sync();
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) Channel(io.netty.channel.Channel) BindException(java.net.BindException) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) BindException(java.net.BindException) DatagramPacket(io.netty.channel.socket.DatagramPacket)

Example 4 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project java-in-action by xinghalo.

the class LogEventEncoder method encode.

@Override
protected void encode(ChannelHandlerContext channelHandlerContext, LogEvent logEvent, List<Object> list) throws Exception {
    byte[] file = logEvent.getLogfile().getBytes(CharsetUtil.UTF_8);
    byte[] msg = logEvent.getMsg().getBytes(CharsetUtil.UTF_8);
    ByteBuf buf = channelHandlerContext.alloc().buffer(file.length + msg.length + 1);
    buf.writeBytes(file);
    buf.writeByte(LogEvent.SEPARATOR);
    buf.writeBytes(msg);
    list.add(new DatagramPacket(buf, address));
}
Also used : DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf)

Example 5 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project java-tron by tronprotocol.

the class MessageHandler method sendPacket.

void sendPacket(byte[] wire, InetSocketAddress address) {
    DatagramPacket packet = new DatagramPacket(Unpooled.copiedBuffer(wire), address);
    channel.write(packet);
    channel.flush();
}
Also used : DatagramPacket(io.netty.channel.socket.DatagramPacket)

Aggregations

DatagramPacket (io.netty.channel.socket.DatagramPacket)74 InetSocketAddress (java.net.InetSocketAddress)45 ByteBuf (io.netty.buffer.ByteBuf)38 Test (org.junit.Test)15 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)14 Channel (io.netty.channel.Channel)8 ChannelFuture (io.netty.channel.ChannelFuture)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)8 DatagramChannel (io.netty.channel.socket.DatagramChannel)8 ByteBuffer (java.nio.ByteBuffer)6 Test (org.junit.jupiter.api.Test)6 Bootstrap (io.netty.bootstrap.Bootstrap)5 DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)5 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)5 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)5 InetAddress (java.net.InetAddress)5 SocketAddress (java.net.SocketAddress)5 ArrayList (java.util.ArrayList)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AddressedEnvelope (io.netty.channel.AddressedEnvelope)4