Search in sources :

Example 1 with AddressedEnvelope

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

the class DnsMessageUtil method appendAddresses.

private static StringBuilder appendAddresses(StringBuilder buf, DnsMessage msg) {
    if (!(msg instanceof AddressedEnvelope)) {
        return buf;
    }
    @SuppressWarnings("unchecked") AddressedEnvelope<?, SocketAddress> envelope = (AddressedEnvelope<?, SocketAddress>) msg;
    SocketAddress addr = envelope.sender();
    if (addr != null) {
        buf.append("from: ").append(addr).append(", ");
    }
    addr = envelope.recipient();
    if (addr != null) {
        buf.append("to: ").append(addr).append(", ");
    }
    return buf;
}
Also used : AddressedEnvelope(io.netty.channel.AddressedEnvelope) SocketAddress(java.net.SocketAddress)

Example 2 with AddressedEnvelope

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

the class NioDatagramChannel method doWriteMessage.

@Override
protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
    final SocketAddress remoteAddress;
    final ByteBuf data;
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, SocketAddress> envelope = (AddressedEnvelope<ByteBuf, SocketAddress>) msg;
        remoteAddress = envelope.recipient();
        data = envelope.content();
    } else {
        data = (ByteBuf) msg;
        remoteAddress = null;
    }
    final int dataLen = data.readableBytes();
    if (dataLen == 0) {
        return true;
    }
    final ByteBuffer nioData = data.internalNioBuffer(data.readerIndex(), dataLen);
    final int writtenBytes;
    if (remoteAddress != null) {
        writtenBytes = javaChannel().send(nioData, remoteAddress);
    } else {
        writtenBytes = javaChannel().write(nioData);
    }
    return writtenBytes > 0;
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 3 with AddressedEnvelope

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

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

the class OioDatagramChannel method doWrite.

@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
    for (; ; ) {
        final Object o = in.current();
        if (o == null) {
            break;
        }
        final ByteBuf data;
        final SocketAddress remoteAddress;
        if (o instanceof AddressedEnvelope) {
            @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, SocketAddress> envelope = (AddressedEnvelope<ByteBuf, SocketAddress>) o;
            remoteAddress = envelope.recipient();
            data = envelope.content();
        } else {
            data = (ByteBuf) o;
            remoteAddress = null;
        }
        final int length = data.readableBytes();
        try {
            if (remoteAddress != null) {
                tmpPacket.setSocketAddress(remoteAddress);
            } else {
                if (!isConnected()) {
                    // NioDatagramChannel
                    throw new NotYetConnectedException();
                }
            }
            if (data.hasArray()) {
                tmpPacket.setData(data.array(), data.arrayOffset() + data.readerIndex(), length);
            } else {
                byte[] tmp = new byte[length];
                data.getBytes(data.readerIndex(), tmp);
                tmpPacket.setData(tmp);
            }
            socket.send(tmpPacket);
            in.remove();
        } catch (IOException e) {
            // Continue on write error as a DatagramChannel can write to multiple remote peers
            //
            // See https://github.com/netty/netty/issues/2665
            in.remove(e);
        }
    }
}
Also used : AddressedEnvelope(io.netty.channel.AddressedEnvelope) NotYetConnectedException(java.nio.channels.NotYetConnectedException) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 5 with AddressedEnvelope

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

the class DnsNameResolverTest method testQueryMx.

@Test
public void testQueryMx() throws Exception {
    DnsNameResolver resolver = newResolver().build();
    try {
        assertThat(resolver.isRecursionDesired(), is(true));
        Map<String, Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>> futures = new LinkedHashMap<String, Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>>();
        for (String name : DOMAINS) {
            if (EXCLUSIONS_QUERY_MX.contains(name)) {
                continue;
            }
            queryMx(resolver, futures, name);
        }
        for (Entry<String, Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>> e : futures.entrySet()) {
            String hostname = e.getKey();
            Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> f = e.getValue().awaitUninterruptibly();
            DnsResponse response = f.getNow().content();
            assertThat(response.code(), is(DnsResponseCode.NOERROR));
            final int answerCount = response.count(DnsSection.ANSWER);
            final List<DnsRecord> mxList = new ArrayList<DnsRecord>(answerCount);
            for (int i = 0; i < answerCount; i++) {
                final DnsRecord r = response.recordAt(DnsSection.ANSWER, i);
                if (r.type() == DnsRecordType.MX) {
                    mxList.add(r);
                }
            }
            assertThat(mxList.size(), is(greaterThan(0)));
            StringBuilder buf = new StringBuilder();
            for (DnsRecord r : mxList) {
                ByteBuf recordContent = ((ByteBufHolder) r).content();
                buf.append(StringUtil.NEWLINE);
                buf.append('\t');
                buf.append(r.name());
                buf.append(' ');
                buf.append(r.type().name());
                buf.append(' ');
                buf.append(recordContent.readUnsignedShort());
                buf.append(' ');
                buf.append(DnsNameResolverContext.decodeDomainName(recordContent));
            }
            logger.info("{} has the following MX records:{}", hostname, buf);
            response.release();
        }
    } finally {
        resolver.close();
    }
}
Also used : AddressedEnvelope(io.netty.channel.AddressedEnvelope) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) LinkedHashMap(java.util.LinkedHashMap) DnsResponse(io.netty.handler.codec.dns.DnsResponse) Future(io.netty.util.concurrent.Future) ByteBufHolder(io.netty.buffer.ByteBufHolder) DnsRecord(io.netty.handler.codec.dns.DnsRecord) Test(org.junit.Test)

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