Search in sources :

Example 1 with FMLProxyPacket

use of net.minecraftforge.fml.common.network.internal.FMLProxyPacket in project MinecraftForge by MinecraftForge.

the class NetworkDispatcher method handleServerSideCustomPacket.

private boolean handleServerSideCustomPacket(CPacketCustomPayload msg, ChannelHandlerContext context) {
    if (state == ConnectionState.AWAITING_HANDSHAKE) {
        synchronized (this) {
            // guard from other threads changing the state on us
            if (state == ConnectionState.AWAITING_HANDSHAKE) {
                state = ConnectionState.HANDSHAKING;
            }
        }
    }
    String channelName = msg.getChannelName();
    if ("FML|HS".equals(channelName) || "REGISTER".equals(channelName) || "UNREGISTER".equals(channelName)) {
        FMLProxyPacket proxy = new FMLProxyPacket(msg);
        proxy.setDispatcher(this);
        handshakeChannel.writeInbound(proxy);
        for (Object push : handshakeChannel.inboundMessages()) {
            List<FMLProxyPacket> messageResult = FMLNetworkHandler.forwardHandshake((FMLMessage.CompleteHandshake) push, this, Side.SERVER);
            for (FMLProxyPacket result : messageResult) {
                result.setTarget(Side.SERVER);
                result.payload().resetReaderIndex();
                context.fireChannelRead(result);
            }
        }
        handshakeChannel.inboundMessages().clear();
        return true;
    } else if (NetworkRegistry.INSTANCE.hasChannel(channelName, Side.SERVER)) {
        FMLProxyPacket proxy = new FMLProxyPacket(msg);
        proxy.setDispatcher(this);
        context.fireChannelRead(proxy);
        return true;
    }
    return false;
}
Also used : FMLMessage(net.minecraftforge.fml.common.network.internal.FMLMessage) FMLProxyPacket(net.minecraftforge.fml.common.network.internal.FMLProxyPacket) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 2 with FMLProxyPacket

use of net.minecraftforge.fml.common.network.internal.FMLProxyPacket 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 FMLProxyPacket

use of net.minecraftforge.fml.common.network.internal.FMLProxyPacket in project MinecraftForge by MinecraftForge.

the class FMLIndexedMessageToMessageCodec method decode.

@Override
protected final void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception {
    testMessageValidity(msg);
    ByteBuf payload = msg.payload().duplicate();
    if (payload.readableBytes() < 1) {
        FMLLog.log(Level.ERROR, "The FMLIndexedCodec has received an empty buffer on channel %s, likely a result of a LAN server issue. Pipeline parts : %s", ctx.channel().attr(NetworkRegistry.FML_CHANNEL), ctx.pipeline().toString());
    }
    byte discriminator = payload.readByte();
    Class<? extends A> clazz = discriminators.get(discriminator);
    if (clazz == null) {
        throw new NullPointerException("Undefined message for discriminator " + discriminator + " in channel " + msg.channel());
    }
    A newMsg = clazz.newInstance();
    ctx.attr(INBOUNDPACKETTRACKER).get().set(new WeakReference<FMLProxyPacket>(msg));
    decodeInto(ctx, payload.slice(), newMsg);
    out.add(newMsg);
}
Also used : FMLProxyPacket(net.minecraftforge.fml.common.network.internal.FMLProxyPacket) ByteBuf(io.netty.buffer.ByteBuf)

Example 4 with FMLProxyPacket

use of net.minecraftforge.fml.common.network.internal.FMLProxyPacket in project MinecraftForge by MinecraftForge.

the class FMLIndexedMessageToMessageCodec method encode.

@Override
protected final void encode(ChannelHandlerContext ctx, A msg, List<Object> out) throws Exception {
    PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
    byte discriminator = types.get(msg.getClass());
    buffer.writeByte(discriminator);
    encodeInto(ctx, msg, buffer);
    FMLProxyPacket proxy = new FMLProxyPacket(buffer, /*.copy()*/
    ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get());
    WeakReference<FMLProxyPacket> ref = ctx.attr(INBOUNDPACKETTRACKER).get().get();
    FMLProxyPacket old = ref == null ? null : ref.get();
    if (old != null) {
        proxy.setDispatcher(old.getDispatcher());
    }
    out.add(proxy);
}
Also used : FMLProxyPacket(net.minecraftforge.fml.common.network.internal.FMLProxyPacket) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 5 with FMLProxyPacket

use of net.minecraftforge.fml.common.network.internal.FMLProxyPacket in project MinecraftForge by MinecraftForge.

the class FMLOutboundHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof FMLProxyPacket)) {
        return;
    }
    FMLProxyPacket pkt = (FMLProxyPacket) msg;
    OutboundTarget outboundTarget;
    Object args = null;
    NetworkDispatcher dispatcher = ctx.channel().attr(NetworkDispatcher.FML_DISPATCHER).get();
    // INTERNAL message callback - let it pass out
    if (dispatcher != null) {
        ctx.write(msg, promise);
        return;
    }
    outboundTarget = ctx.channel().attr(FML_MESSAGETARGET).get();
    Side channelSide = ctx.channel().attr(NetworkRegistry.CHANNEL_SOURCE).get();
    if (outboundTarget != null && outboundTarget.allowed.contains(channelSide)) {
        args = ctx.channel().attr(FML_MESSAGETARGETARGS).get();
        outboundTarget.validateArgs(args);
    } else if (channelSide == Side.CLIENT) {
        outboundTarget = OutboundTarget.TOSERVER;
    } else {
        throw new FMLNetworkException("Packet arrived at the outbound handler without a valid target!");
    }
    List<NetworkDispatcher> dispatchers = outboundTarget.selectNetworks(args, ctx, pkt);
    // This will drop the messages into the output queue at the embedded channel
    if (dispatchers == null) {
        ctx.write(msg, promise);
        return;
    }
    for (NetworkDispatcher targetDispatcher : dispatchers) {
        targetDispatcher.sendProxy((FMLProxyPacket) msg);
    }
}
Also used : Side(net.minecraftforge.fml.relauncher.Side) NetworkDispatcher(net.minecraftforge.fml.common.network.handshake.NetworkDispatcher) FMLProxyPacket(net.minecraftforge.fml.common.network.internal.FMLProxyPacket)

Aggregations

FMLProxyPacket (net.minecraftforge.fml.common.network.internal.FMLProxyPacket)9 PacketBuffer (net.minecraft.network.PacketBuffer)4 ByteBuf (io.netty.buffer.ByteBuf)2 IOException (java.io.IOException)2 TextComponentString (net.minecraft.util.text.TextComponentString)2 FMLMessage (net.minecraftforge.fml.common.network.internal.FMLMessage)2 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)1 Packet (net.minecraft.network.Packet)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1 OutboundTarget (net.minecraftforge.fml.common.network.FMLOutboundHandler.OutboundTarget)1 NetworkDispatcher (net.minecraftforge.fml.common.network.handshake.NetworkDispatcher)1 Side (net.minecraftforge.fml.relauncher.Side)1