Search in sources :

Example 16 with DatagramPacket

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

the class DatagramPacketEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<M, InetSocketAddress> msg, List<Object> out) throws Exception {
    assert out.isEmpty();
    encoder.encode(ctx, msg.content(), out);
    if (out.size() != 1) {
        throw new EncoderException(StringUtil.simpleClassName(encoder) + " must produce only one message.");
    }
    Object content = out.get(0);
    if (content instanceof ByteBuf) {
        // Replace the ByteBuf with a DatagramPacket.
        out.set(0, new DatagramPacket((ByteBuf) content, msg.recipient(), msg.sender()));
    } else {
        throw new EncoderException(StringUtil.simpleClassName(encoder) + " must produce only ByteBuf.");
    }
}
Also used : DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf)

Example 17 with DatagramPacket

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

the class DnsQueryTest method writeQueryTest.

@Test
public void writeQueryTest() throws Exception {
    InetSocketAddress addr = SocketUtils.socketAddress("8.8.8.8", 53);
    EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsQueryEncoder());
    List<DnsQuery> queries = new ArrayList<DnsQuery>(5);
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("1.0.0.127.in-addr.arpa", DnsRecordType.PTR)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("www.example.com", DnsRecordType.A)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("example.com", DnsRecordType.AAAA)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("example.com", DnsRecordType.MX)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("example.com", DnsRecordType.CNAME)));
    for (DnsQuery query : queries) {
        assertThat(query.count(DnsSection.QUESTION), is(1));
        assertThat(query.count(DnsSection.ANSWER), is(0));
        assertThat(query.count(DnsSection.AUTHORITY), is(0));
        assertThat(query.count(DnsSection.ADDITIONAL), is(0));
        embedder.writeOutbound(query);
        DatagramPacket packet = embedder.readOutbound();
        Assert.assertTrue(packet.content().isReadable());
        packet.release();
        Assert.assertNull(embedder.readOutbound());
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 18 with DatagramPacket

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

the class DnsResponseTest method readMalormedResponseTest.

@Test
public void readMalormedResponseTest() throws Exception {
    EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
    ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket);
    exception.expect(CorruptedFrameException.class);
    embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 19 with DatagramPacket

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

the class DnsResponseTest method readResponseTest.

@Test
public void readResponseTest() throws Exception {
    EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
    for (byte[] p : packets) {
        ByteBuf packet = embedder.alloc().buffer(512).writeBytes(p);
        embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
        AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = embedder.readInbound();
        assertThat(envelope, is(instanceOf(DatagramDnsResponse.class)));
        DnsResponse response = envelope.content();
        assertThat(response, is(sameInstance((Object) envelope)));
        ByteBuf raw = Unpooled.wrappedBuffer(p);
        assertThat(response.id(), is(raw.getUnsignedShort(0)));
        assertThat(response.count(DnsSection.QUESTION), is(raw.getUnsignedShort(4)));
        assertThat(response.count(DnsSection.ANSWER), is(raw.getUnsignedShort(6)));
        assertThat(response.count(DnsSection.AUTHORITY), is(raw.getUnsignedShort(8)));
        assertThat(response.count(DnsSection.ADDITIONAL), is(raw.getUnsignedShort(10)));
        envelope.release();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 20 with DatagramPacket

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

the class DatagramDnsQueryEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<DnsQuery, InetSocketAddress> in, List<Object> out) throws Exception {
    final InetSocketAddress recipient = in.recipient();
    final DnsQuery query = in.content();
    final ByteBuf buf = allocateBuffer(ctx, in);
    boolean success = false;
    try {
        encodeHeader(query, buf);
        encodeQuestions(query, buf);
        encodeRecords(query, DnsSection.ADDITIONAL, buf);
        success = true;
    } finally {
        if (!success) {
            buf.release();
        }
    }
    out.add(new DatagramPacket(buf, recipient, null));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf)

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