Search in sources :

Example 51 with DatagramPacket

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

the class NetworkMessageHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (msg instanceof NetworkMessage) {
        NetworkMessage message = (NetworkMessage) msg;
        if (ctx.channel() instanceof DatagramChannel) {
            InetSocketAddress recipient = (InetSocketAddress) message.getRemoteAddress();
            InetSocketAddress sender = (InetSocketAddress) ctx.channel().localAddress();
            ctx.write(new DatagramPacket((ByteBuf) message.getMessage(), recipient, sender), promise);
        } else {
            ctx.write(message.getMessage(), promise);
        }
    } else {
        ctx.write(msg, promise);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) DatagramChannel(io.netty.channel.socket.DatagramChannel) NetworkMessage(org.traccar.NetworkMessage) ByteBuf(io.netty.buffer.ByteBuf)

Example 52 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 53 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 54 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)

Example 55 with DatagramPacket

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

the class LispSouthboundHandlerTest method handleMapRequestPacket.

private DatagramPacket handleMapRequestPacket(byte[] inPacket) {
    DatagramPacket dp = new DatagramPacket(wrappedBuffer(inPacket), new InetSocketAddress(0), new InetSocketAddress(0));
    // Unless we explicitly set the source port, it will be -1, which breaks some tests
    // This is till not the real port number, but it's better
    // dp.setPort(LispMessage.PORT_NUM);
    testedLispService.handlePacket(dp);
    return lastMapReplyPacket();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket)

Aggregations

DatagramPacket (io.netty.channel.socket.DatagramPacket)75 InetSocketAddress (java.net.InetSocketAddress)45 ByteBuf (io.netty.buffer.ByteBuf)38 Test (org.junit.Test)15 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)14 Channel (io.netty.channel.Channel)8 ChannelFuture (io.netty.channel.ChannelFuture)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)8 DatagramChannel (io.netty.channel.socket.DatagramChannel)8 ByteBuffer (java.nio.ByteBuffer)6 Test (org.junit.jupiter.api.Test)6 Bootstrap (io.netty.bootstrap.Bootstrap)5 DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)5 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)5 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)5 InetAddress (java.net.InetAddress)5 SocketAddress (java.net.SocketAddress)5 ArrayList (java.util.ArrayList)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AddressedEnvelope (io.netty.channel.AddressedEnvelope)4