Search in sources :

Example 21 with DatagramPacket

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

the class DatagramPacketEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        ByteBuf payload = (ByteBuf) msg.content();
        // Just wrap the message as DatagramPacket, need to make sure the message content is ByteBuf
        DatagramPacket dp = new DatagramPacket(payload.retain(), msg.recipient());
        out.add(dp);
    }
}
Also used : DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf)

Example 22 with DatagramPacket

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

the class UdpClientTest method testUdpClientWithoutTimeout.

@Test
public void testUdpClientWithoutTimeout() throws Exception {
    int port = choosePort();
    UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 0).createServer();
    server.start();
    BaseLoadBalancer lb = new BaseLoadBalancer();
    lb.setServersList(Lists.newArrayList(new Server("localhost", port)));
    RxClient<DatagramPacket, DatagramPacket> client = RibbonTransport.newUdpClient(lb, DefaultClientConfigImpl.getClientConfigWithDefaultValues());
    try {
        String response = client.connect().flatMap(new Func1<ObservableConnection<DatagramPacket, DatagramPacket>, Observable<DatagramPacket>>() {

            @Override
            public Observable<DatagramPacket> call(ObservableConnection<DatagramPacket, DatagramPacket> connection) {
                connection.writeStringAndFlush("Is there anybody out there?");
                return connection.getInput();
            }
        }).take(1).map(new Func1<DatagramPacket, String>() {

            @Override
            public String call(DatagramPacket datagramPacket) {
                return datagramPacket.content().toString(Charset.defaultCharset());
            }
        }).toBlocking().first();
        assertEquals(HelloUdpServer.WELCOME_MSG, response);
    } finally {
        server.shutdown();
    }
}
Also used : Server(com.netflix.loadbalancer.Server) UdpServer(io.reactivex.netty.protocol.udp.server.UdpServer) ObservableConnection(io.reactivex.netty.channel.ObservableConnection) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) Observable(rx.Observable) DatagramPacket(io.netty.channel.socket.DatagramPacket) Test(org.junit.Test)

Example 23 with DatagramPacket

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

the class QueryHandler method handleFullStats.

private void handleFullStats(ChannelHandlerContext ctx, DatagramPacket packet, int sessionId) {
    GlowServer server = queryServer.getServer();
    StringBuilder plugins = new StringBuilder("Glowstone ").append(server.getVersion()).append(" on Bukkit ").append(server.getBukkitVersion());
    if (showPlugins) {
        char delim = ':';
        for (Plugin plugin : server.getPluginManager().getPlugins()) {
            plugins.append(delim).append(' ').append(plugin.getDescription().getFullName());
            delim = ';';
        }
    }
    Map<String, Object> data = new LinkedHashMap<>();
    data.put("hostname", server.getMotd());
    data.put("gametype", "SMP");
    data.put("game_id", "MINECRAFT");
    data.put("version", GlowServer.GAME_VERSION);
    data.put("plugins", plugins);
    data.put("map", server.getWorlds().get(0).getName());
    data.put("numplayers", server.getOnlinePlayers().size());
    data.put("maxplayers", server.getMaxPlayers());
    data.put("hostport", server.getPort());
    data.put("hostip", getIpString());
    ByteBuf buf = ctx.alloc().buffer();
    buf.writeByte(ACTION_STATS);
    buf.writeInt(sessionId);
    // constant: splitnum\x00\x80\x00
    buf.writeBytes(new byte[] { 0x73, 0x70, 0x6C, 0x69, 0x74, 0x6E, 0x75, 0x6D, 0x00, (byte) 0x80, 0x00 });
    for (Entry<String, Object> e : data.entrySet()) {
        writeString(buf, e.getKey());
        writeString(buf, String.valueOf(e.getValue()));
    }
    buf.writeByte(0);
    // constant: \x01player_\x00\x00
    buf.writeBytes(new byte[] { 0x01, 0x70, 0x6C, 0x61, 0x79, 0x65, 0x72, 0x5F, 0x00, 0x00 });
    for (Player player : server.getOnlinePlayers()) {
        writeString(buf, player.getName());
    }
    buf.writeByte(0);
    ctx.write(new DatagramPacket(buf, packet.sender()));
}
Also used : Player(org.bukkit.entity.Player) DatagramPacket(io.netty.channel.socket.DatagramPacket) GlowServer(net.glowstone.GlowServer) ByteBuf(io.netty.buffer.ByteBuf) Plugin(org.bukkit.plugin.Plugin) LinkedHashMap(java.util.LinkedHashMap)

Example 24 with DatagramPacket

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

the class QueryHandler method handleHandshake.

private void handleHandshake(ChannelHandlerContext ctx, DatagramPacket packet, int sessionId) {
    int challengeToken = queryServer.generateChallengeToken(packet.sender());
    ByteBuf out = ctx.alloc().buffer();
    out.writeByte(ACTION_HANDSHAKE);
    out.writeInt(sessionId);
    writeString(out, String.valueOf(challengeToken));
    ctx.write(new DatagramPacket(out, packet.sender()));
}
Also used : DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf)

Example 25 with DatagramPacket

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

the class QueryHandler method handleBasicStats.

private void handleBasicStats(ChannelHandlerContext ctx, DatagramPacket packet, int sessionId) {
    GlowServer server = queryServer.getServer();
    ByteBuf buf = ctx.alloc().buffer();
    buf.writeByte(ACTION_STATS);
    buf.writeInt(sessionId);
    writeString(buf, server.getMotd());
    writeString(buf, "SMP");
    writeString(buf, server.getWorlds().get(0).getName());
    writeString(buf, String.valueOf(server.getOnlinePlayers().size()));
    writeString(buf, String.valueOf(server.getMaxPlayers()));
    buf.order(ByteOrder.LITTLE_ENDIAN).writeShort(server.getPort());
    writeString(buf, getIpString());
    ctx.write(new DatagramPacket(buf, packet.sender()));
}
Also used : DatagramPacket(io.netty.channel.socket.DatagramPacket) GlowServer(net.glowstone.GlowServer) 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