Search in sources :

Example 1 with Packet

use of net.minecraft.network.Packet in project MinecraftForge by MinecraftForge.

the class FMLProxyPacket method toS3FPackets.

public List<Packet<INetHandlerPlayClient>> toS3FPackets() throws IOException {
    List<Packet<INetHandlerPlayClient>> ret = Lists.newArrayList();
    byte[] data = payload.array();
    if (data.length < PART_SIZE) {
        ret.add(new SPacketCustomPayload(channel, new PacketBuffer(payload.duplicate())));
    } else {
        //We add a byte header so -1
        int parts = (int) Math.ceil(data.length / (double) (PART_SIZE - 1));
        if (parts > 255) {
            throw new IllegalArgumentException("Payload may not be larger than " + MAX_LENGTH + " bytes");
        }
        PacketBuffer preamble = new PacketBuffer(Unpooled.buffer());
        preamble.writeString(channel);
        preamble.writeByte(parts);
        preamble.writeInt(data.length);
        ret.add(new SPacketCustomPayload("FML|MP", preamble));
        int offset = 0;
        for (int x = 0; x < parts; x++) {
            int length = Math.min(PART_SIZE, data.length - offset + 1);
            byte[] tmp = new byte[length];
            tmp[0] = (byte) (x & 0xFF);
            System.arraycopy(data, offset, tmp, 1, tmp.length - 1);
            offset += tmp.length - 1;
            ret.add(new SPacketCustomPayload("FML|MP", new PacketBuffer(Unpooled.wrappedBuffer(tmp))));
        }
    }
    return ret;
}
Also used : Packet(net.minecraft.network.Packet) SPacketCustomPayload(net.minecraft.network.play.server.SPacketCustomPayload) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 2 with Packet

use of net.minecraft.network.Packet in project MinecraftForge by MinecraftForge.

the class FMLEmbeddedChannel method generatePacketFrom.

/**
     * Utility method to generate a regular packet from a custom packet. Basically, it writes the packet through the
     * outbound side which should have a message to message codec present (such as {@link FMLIndexedMessageToMessageCodec},
     * transforming from mod packets to standard {@link FMLProxyPacket}s.
     *
     * This is mostly useful in cases where vanilla expects a packet, such as the TileEntity getDescriptionPacket.
     *
     * @param object The inbound packet
     * @return A Packet suitable for passing to vanilla network code.
     */
public Packet<?> generatePacketFrom(Object object) {
    OutboundTarget outboundTarget = attr(FMLOutboundHandler.FML_MESSAGETARGET).getAndSet(OutboundTarget.NOWHERE);
    writeOutbound(object);
    Packet<?> pkt = (Packet<?>) outboundMessages().poll();
    attr(FMLOutboundHandler.FML_MESSAGETARGET).set(outboundTarget);
    return pkt;
}
Also used : OutboundTarget(net.minecraftforge.fml.common.network.FMLOutboundHandler.OutboundTarget) Packet(net.minecraft.network.Packet) FMLProxyPacket(net.minecraftforge.fml.common.network.internal.FMLProxyPacket)

Example 3 with Packet

use of net.minecraft.network.Packet in project MinecraftForge by MinecraftForge.

the class PacketLoggingHandler method register.

public static void register(NetworkManager manager) {
    ChannelPipeline pipeline = manager.channel().pipeline();
    final EnumPacketDirection direction = manager.getDirection();
    if (manager.isLocalChannel()) {
        pipeline.addBefore("packet_handler", "splitter", new SimpleChannelInboundHandler<Packet<?>>() {

            String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: C->S" : "CLIENT: S->C");

            @Override
            protected void channelRead0(ChannelHandlerContext ctx, Packet<?> msg) throws Exception {
                PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
                msg.writePacketData(buf);
                FMLLog.log(Level.DEBUG, "%s %s:\n%s", prefix, msg.getClass().getSimpleName(), ByteBufUtils.getContentDump(buf));
                ctx.fireChannelRead(msg);
            }
        });
        pipeline.addBefore("splitter", "prepender", new ChannelOutboundHandlerAdapter() {

            String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: S->C" : "CLIENT: C->S");

            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                if (msg instanceof Packet<?>) {
                    PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
                    ((Packet<?>) msg).writePacketData(buf);
                    FMLLog.log(Level.DEBUG, "%s %s:\n%s", prefix, msg.getClass().getSimpleName(), ByteBufUtils.getContentDump(buf));
                }
                ctx.write(msg, promise);
            }
        });
    } else {
        pipeline.replace("splitter", "splitter", new NettyVarint21FrameDecoder() {

            String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: C->S" : "CLIENT: S->C");

            @Override
            protected void decode(ChannelHandlerContext context, ByteBuf input, List<Object> output) throws Exception {
                super.decode(context, input, output);
                Iterator<Object> itr = output.iterator();
                while (itr.hasNext()) {
                    ByteBuf pkt = (ByteBuf) itr.next();
                    pkt.markReaderIndex();
                    FMLLog.log(Level.DEBUG, "%s:\n%s", prefix, ByteBufUtils.getContentDump(pkt));
                    pkt.resetReaderIndex();
                }
            }
        });
        pipeline.replace("prepender", "prepender", new NettyVarint21FrameEncoder() {

            String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: S->C" : "CLIENT: C->S");

            @Override
            protected void encode(ChannelHandlerContext context, ByteBuf input, ByteBuf output) throws Exception {
                input.markReaderIndex();
                FMLLog.log(Level.DEBUG, "%s:\n%s", prefix, ByteBufUtils.getContentDump(input));
                input.resetReaderIndex();
                super.encode(context, input, output);
            }
        });
    }
}
Also used : Packet(net.minecraft.network.Packet) NettyVarint21FrameDecoder(net.minecraft.network.NettyVarint21FrameDecoder) EnumPacketDirection(net.minecraft.network.EnumPacketDirection) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) ChannelPipeline(io.netty.channel.ChannelPipeline) Iterator(java.util.Iterator) NettyVarint21FrameEncoder(net.minecraft.network.NettyVarint21FrameEncoder) PacketBuffer(net.minecraft.network.PacketBuffer)

Aggregations

Packet (net.minecraft.network.Packet)3 PacketBuffer (net.minecraft.network.PacketBuffer)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 ChannelPromise (io.netty.channel.ChannelPromise)1 Iterator (java.util.Iterator)1 EnumPacketDirection (net.minecraft.network.EnumPacketDirection)1 NettyVarint21FrameDecoder (net.minecraft.network.NettyVarint21FrameDecoder)1 NettyVarint21FrameEncoder (net.minecraft.network.NettyVarint21FrameEncoder)1 SPacketCustomPayload (net.minecraft.network.play.server.SPacketCustomPayload)1 OutboundTarget (net.minecraftforge.fml.common.network.FMLOutboundHandler.OutboundTarget)1 FMLProxyPacket (net.minecraftforge.fml.common.network.internal.FMLProxyPacket)1