Search in sources :

Example 1 with DefaultAddressedEnvelope

use of io.netty.channel.DefaultAddressedEnvelope in project netty by netty.

the class NioDatagramChannel method filterOutboundMessage.

@Override
protected Object filterOutboundMessage(Object msg) {
    if (msg instanceof DatagramPacket) {
        DatagramPacket p = (DatagramPacket) msg;
        ByteBuf content = p.content();
        if (isSingleDirectBuffer(content)) {
            return p;
        }
        return new DatagramPacket(newDirectBuffer(p, content), p.recipient());
    }
    if (msg instanceof ByteBuf) {
        ByteBuf buf = (ByteBuf) msg;
        if (isSingleDirectBuffer(buf)) {
            return buf;
        }
        return newDirectBuffer(buf);
    }
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg;
        if (e.content() instanceof ByteBuf) {
            ByteBuf content = (ByteBuf) e.content();
            if (isSingleDirectBuffer(content)) {
                return e;
            }
            return new DefaultAddressedEnvelope<ByteBuf, SocketAddress>(newDirectBuffer(e, content), e.recipient());
        }
    }
    throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES);
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope)

Example 2 with DefaultAddressedEnvelope

use of io.netty.channel.DefaultAddressedEnvelope in project netty by netty.

the class DatagramPacketEncoderTest method testUnmatchedMessageType.

@Test
public void testUnmatchedMessageType() {
    InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
    InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
    DefaultAddressedEnvelope<Long, InetSocketAddress> envelope = new DefaultAddressedEnvelope<Long, InetSocketAddress>(1L, recipient, sender);
    assertTrue(channel.writeOutbound(envelope));
    DefaultAddressedEnvelope<Long, InetSocketAddress> output = channel.readOutbound();
    try {
        assertSame(envelope, output);
    } finally {
        output.release();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) Test(org.junit.Test)

Example 3 with DefaultAddressedEnvelope

use of io.netty.channel.DefaultAddressedEnvelope 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 4 with DefaultAddressedEnvelope

use of io.netty.channel.DefaultAddressedEnvelope in project camel by apache.

the class DatagramPacketObjectDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        ByteBuf payload = (ByteBuf) msg.content();
        Object result = delegateDecoder.decode(ctx, payload);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(result, msg.recipient(), msg.sender());
        out.add(addressedEnvelop);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope)

Example 5 with DefaultAddressedEnvelope

use of io.netty.channel.DefaultAddressedEnvelope in project camel by apache.

the class DatagramPacketObjectEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception {
    if (msg.content() instanceof Serializable) {
        Serializable payload = (Serializable) msg.content();
        ByteBuf buf = ctx.alloc().buffer();
        delegateObjectEncoder.encode(ctx, payload, buf);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(buf, msg.recipient(), msg.sender());
        out.add(addressedEnvelop);
    }
}
Also used : Serializable(java.io.Serializable) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope)

Aggregations

DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)12 InetSocketAddress (java.net.InetSocketAddress)12 ByteBuf (io.netty.buffer.ByteBuf)9 AddressedEnvelope (io.netty.channel.AddressedEnvelope)4 Test (org.junit.Test)4 DatagramPacket (io.netty.channel.socket.DatagramPacket)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 SocketAddress (java.net.SocketAddress)2 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 ChannelFuture (io.netty.channel.ChannelFuture)1 DatagramSocketAddress (io.netty.channel.unix.DatagramSocketAddress)1 Serializable (java.io.Serializable)1