Search in sources :

Example 11 with DatagramPacket

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

the class EpollDatagramChannel method filterOutboundMessage.

@Override
protected Object filterOutboundMessage(Object msg) {
    if (msg instanceof DatagramPacket) {
        DatagramPacket packet = (DatagramPacket) msg;
        ByteBuf content = packet.content();
        if (content.hasMemoryAddress()) {
            return msg;
        }
        if (content.isDirect() && content instanceof CompositeByteBuf) {
            // Special handling of CompositeByteBuf to reduce memory copies if some of the Components
            // in the CompositeByteBuf are backed by a memoryAddress.
            CompositeByteBuf comp = (CompositeByteBuf) content;
            if (comp.isDirect() && comp.nioBufferCount() <= Native.IOV_MAX) {
                return msg;
            }
        }
        // passed to write.
        return new DatagramPacket(newDirectBuffer(packet, content), packet.recipient());
    }
    if (msg instanceof ByteBuf) {
        ByteBuf buf = (ByteBuf) msg;
        if (!buf.hasMemoryAddress() && (PlatformDependent.hasUnsafe() || !buf.isDirect())) {
            if (buf instanceof CompositeByteBuf) {
                // Special handling of CompositeByteBuf to reduce memory copies if some of the Components
                // in the CompositeByteBuf are backed by a memoryAddress.
                CompositeByteBuf comp = (CompositeByteBuf) buf;
                if (!comp.isDirect() || comp.nioBufferCount() > Native.IOV_MAX) {
                    // more then 1024 buffers for gathering writes so just do a memory copy.
                    buf = newDirectBuffer(buf);
                    assert buf.hasMemoryAddress();
                }
            } else {
                // We can only handle buffers with memory address so we need to copy if a non direct is
                // passed to write.
                buf = newDirectBuffer(buf);
                assert buf.hasMemoryAddress();
            }
        }
        return buf;
    }
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg;
        if (e.content() instanceof ByteBuf && (e.recipient() == null || e.recipient() instanceof InetSocketAddress)) {
            ByteBuf content = (ByteBuf) e.content();
            if (content.hasMemoryAddress()) {
                return e;
            }
            if (content instanceof CompositeByteBuf) {
                // Special handling of CompositeByteBuf to reduce memory copies if some of the Components
                // in the CompositeByteBuf are backed by a memoryAddress.
                CompositeByteBuf comp = (CompositeByteBuf) content;
                if (comp.isDirect() && comp.nioBufferCount() <= Native.IOV_MAX) {
                    return e;
                }
            }
            // passed to write.
            return new DefaultAddressedEnvelope<ByteBuf, InetSocketAddress>(newDirectBuffer(e, content), (InetSocketAddress) e.recipient());
        }
    }
    throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES);
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) SocketAddress(java.net.SocketAddress) DatagramSocketAddress(io.netty.channel.unix.DatagramSocketAddress) InetSocketAddress(java.net.InetSocketAddress) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope)

Example 12 with DatagramPacket

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

the class QueryTest method testChannelRead.

private void testChannelRead(QueryHandler handler, byte[] recv, byte[] send) throws Exception {
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    ByteBufAllocator alloc = mock(ByteBufAllocator.class);
    when(ctx.alloc()).thenReturn(alloc);
    when(alloc.buffer()).thenReturn(Unpooled.buffer());
    DatagramPacket packet = new DatagramPacket(Unpooled.wrappedBuffer(recv), null, address);
    handler.channelRead(ctx, packet);
    verify(ctx).write(argThat(new DatagramPacketMatcher(send)));
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) DatagramPacket(io.netty.channel.socket.DatagramPacket) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

Example 13 with DatagramPacket

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

the class DatagramPacketDecoderTest method testDecode.

@Test
public void testDecode() {
    InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
    InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
    ByteBuf content = Unpooled.wrappedBuffer("netty".getBytes(CharsetUtil.UTF_8));
    assertTrue(channel.writeInbound(new DatagramPacket(content, recipient, sender)));
    assertEquals("netty", channel.readInbound());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 14 with DatagramPacket

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

the class DatagramPacketEncoderTest method testEncode.

@Test
public void testEncode() {
    InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
    InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
    assertTrue(channel.writeOutbound(new DefaultAddressedEnvelope<String, InetSocketAddress>("netty", recipient, sender)));
    DatagramPacket packet = channel.readOutbound();
    try {
        assertEquals("netty", packet.content().toString(CharsetUtil.UTF_8));
        assertEquals(recipient, packet.recipient());
        assertEquals(sender, packet.sender());
    } finally {
        packet.release();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) Test(org.junit.Test)

Example 15 with DatagramPacket

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

the class QuoteOfTheMomentClient method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).handler(new QuoteOfTheMomentClientHandler());
        Channel ch = b.bind(0).sync().channel();
        // Broadcast the QOTM request to port 8080.
        ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8), SocketUtils.socketAddress("255.255.255.255", PORT))).sync();
        // print an error message and quit.
        if (!ch.closeFuture().await(5000)) {
            System.err.println("QOTM request timed out.");
        }
    } finally {
        group.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Channel(io.netty.channel.Channel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DatagramPacket(io.netty.channel.socket.DatagramPacket) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

DatagramPacket (io.netty.channel.socket.DatagramPacket)25 ByteBuf (io.netty.buffer.ByteBuf)17 InetSocketAddress (java.net.InetSocketAddress)13 Test (org.junit.Test)7 Channel (io.netty.channel.Channel)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 SocketException (java.net.SocketException)3 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)2 Server (com.netflix.loadbalancer.Server)2 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 AddressedEnvelope (io.netty.channel.AddressedEnvelope)2 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)2 DatagramChannelConfig (io.netty.channel.socket.DatagramChannelConfig)2 ObservableConnection (io.reactivex.netty.channel.ObservableConnection)2 UdpServer (io.reactivex.netty.protocol.udp.server.UdpServer)2 SocketAddress (java.net.SocketAddress)2 GlowServer (net.glowstone.GlowServer)2 Observable (rx.Observable)2