Search in sources :

Example 6 with AddressedEnvelope

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

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

the class DatagramPacketByteArrayCodecTest method testDecoder.

@Test
public void testDecoder() {
    ByteBuf buf = Unpooled.buffer();
    buf.writeBytes(VALUE.getBytes());
    ByteBuf input = buf.duplicate();
    AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(input, new InetSocketAddress(8888));
    EmbeddedChannel channel = new EmbeddedChannel(ChannelHandlerFactories.newByteArrayDecoder("udp").newChannelHandler());
    Assert.assertTrue(channel.writeInbound(addressedEnvelop));
    Assert.assertTrue(channel.finish());
    AddressedEnvelope<Object, InetSocketAddress> result = (AddressedEnvelope) channel.readInbound();
    Assert.assertEquals(result.recipient().getPort(), addressedEnvelop.recipient().getPort());
    Assert.assertTrue(result.content() instanceof byte[]);
    Assert.assertEquals(VALUE, new String((byte[]) result.content()));
    Assert.assertNull(channel.readInbound());
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) Test(org.junit.Test)

Example 8 with AddressedEnvelope

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

the class DatagramPacketByteArrayCodecTest method testEncoder.

@Test
public void testEncoder() {
    ByteBuf buf = Unpooled.buffer();
    buf.writeBytes(VALUE.getBytes());
    AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(VALUE.getBytes(), new InetSocketAddress(8888));
    EmbeddedChannel channel = new EmbeddedChannel(ChannelHandlerFactories.newByteArrayEncoder("udp").newChannelHandler());
    Assert.assertTrue(channel.writeOutbound(addressedEnvelop));
    Assert.assertTrue(channel.finish());
    AddressedEnvelope output = (AddressedEnvelope) channel.readOutbound();
    Assert.assertTrue(output.content() instanceof ByteBuf);
    ByteBuf resultContent = (ByteBuf) output.content();
    Assert.assertEquals(VALUE, new String(resultContent.array()));
    Assert.assertNull(channel.readOutbound());
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) Test(org.junit.Test)

Example 9 with AddressedEnvelope

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

the class EpollDatagramChannel method doWriteMessage.

private boolean doWriteMessage(Object msg) throws Exception {
    final ByteBuf data;
    InetSocketAddress remoteAddress;
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, InetSocketAddress> envelope = (AddressedEnvelope<ByteBuf, InetSocketAddress>) msg;
        data = envelope.content();
        remoteAddress = envelope.recipient();
    } else {
        data = (ByteBuf) msg;
        remoteAddress = null;
    }
    final int dataLen = data.readableBytes();
    if (dataLen == 0) {
        return true;
    }
    if (remoteAddress == null) {
        remoteAddress = remote;
        if (remoteAddress == null) {
            throw new NotYetConnectedException();
        }
    }
    final int writtenBytes;
    if (data.hasMemoryAddress()) {
        long memoryAddress = data.memoryAddress();
        writtenBytes = fd().sendToAddress(memoryAddress, data.readerIndex(), data.writerIndex(), remoteAddress.getAddress(), remoteAddress.getPort());
    } else if (data instanceof CompositeByteBuf) {
        IovArray array = ((EpollEventLoop) eventLoop()).cleanArray();
        array.add(data);
        int cnt = array.count();
        assert cnt != 0;
        writtenBytes = fd().sendToAddresses(array.memoryAddress(0), cnt, remoteAddress.getAddress(), remoteAddress.getPort());
    } else {
        ByteBuffer nioData = data.internalNioBuffer(data.readerIndex(), data.readableBytes());
        writtenBytes = fd().sendTo(nioData, nioData.position(), nioData.limit(), remoteAddress.getAddress(), remoteAddress.getPort());
    }
    return writtenBytes > 0;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) InetSocketAddress(java.net.InetSocketAddress) NotYetConnectedException(java.nio.channels.NotYetConnectedException) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 10 with AddressedEnvelope

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

the class DnsNameResolverContext method finishResolve.

private void finishResolve(Promise<T> promise) {
    if (!queriesInProgress.isEmpty()) {
        // If there are queries in progress, we should cancel it because we already finished the resolution.
        for (Iterator<Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>> i = queriesInProgress.iterator(); i.hasNext(); ) {
            Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> f = i.next();
            i.remove();
            if (!f.cancel(false)) {
                f.addListener(RELEASE_RESPONSE);
            }
        }
    }
    if (resolvedEntries != null) {
        // Found at least one resolved address.
        for (InternetProtocolFamily f : resolvedInternetProtocolFamilies) {
            if (finishResolve(f.addressType(), resolvedEntries, promise)) {
                return;
            }
        }
    }
    // No resolved address found.
    final int tries = maxAllowedQueries - allowedQueries;
    final StringBuilder buf = new StringBuilder(64);
    buf.append("failed to resolve '");
    if (pristineHostname != null) {
        buf.append(pristineHostname);
    } else {
        buf.append(hostname);
    }
    buf.append('\'');
    if (tries > 1) {
        if (tries < maxAllowedQueries) {
            buf.append(" after ").append(tries).append(" queries ");
        } else {
            buf.append(". Exceeded max queries per resolve ").append(maxAllowedQueries).append(' ');
        }
    }
    if (trace != null) {
        buf.append(':').append(trace);
    }
    final UnknownHostException cause = new UnknownHostException(buf.toString());
    resolveCache.cache(hostname, additionals, cause, parent.ch.eventLoop());
    promise.tryFailure(cause);
}
Also used : AddressedEnvelope(io.netty.channel.AddressedEnvelope) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) DnsResponse(io.netty.handler.codec.dns.DnsResponse) InternetProtocolFamily(io.netty.channel.socket.InternetProtocolFamily) Future(io.netty.util.concurrent.Future)

Aggregations

AddressedEnvelope (io.netty.channel.AddressedEnvelope)10 InetSocketAddress (java.net.InetSocketAddress)9 ByteBuf (io.netty.buffer.ByteBuf)8 DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)6 SocketAddress (java.net.SocketAddress)5 Test (org.junit.Test)3 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 DatagramPacket (io.netty.channel.socket.DatagramPacket)2 DnsResponse (io.netty.handler.codec.dns.DnsResponse)2 Future (io.netty.util.concurrent.Future)2 ByteBuffer (java.nio.ByteBuffer)2 NotYetConnectedException (java.nio.channels.NotYetConnectedException)2 ByteBufHolder (io.netty.buffer.ByteBufHolder)1 InternetProtocolFamily (io.netty.channel.socket.InternetProtocolFamily)1 DatagramSocketAddress (io.netty.channel.unix.DatagramSocketAddress)1 DnsRecord (io.netty.handler.codec.dns.DnsRecord)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1