Search in sources :

Example 1 with Message

use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.

the class ClientContainer method init.

/**
 * Initializes the container for the bounded {@link Player}.
 */
public void init() {
    checkState(this.player != null);
    populate();
    final List<Message> messages = new ArrayList<>();
    final Message message = createInitMessage();
    if (message != null) {
        messages.add(message);
    }
    final ItemStack[] items = new ItemStack[getSlotFlags().length];
    for (int i = 0; i < items.length; i++) {
        items[serverSlotIndexToClient(i)] = this.slots[i].getItem();
        this.slots[i].dirtyState = 0;
    }
    // Send the inventory content
    messages.add(new MessagePlayOutWindowItems(this.containerId, items));
    // Send the cursor item if present
    if (!this.cursor.getRaw().isEmpty()) {
        messages.add(new MessagePlayOutSetWindowSlot(-1, -1, this.cursor.getItem()));
        this.cursor.dirtyState = 0;
    }
    // Collect additional messages
    collectInitMessages(messages);
    // Stream the messages to the player
    this.player.getConnection().send(messages);
}
Also used : MessagePlayOutSetWindowSlot(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSetWindowSlot) Message(org.lanternpowered.server.network.message.Message) MessagePlayOutWindowItems(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutWindowItems) ArrayList(java.util.ArrayList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 2 with Message

use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.

the class CodecPlayInPlayerVehicleControls method decode.

@Override
public Message decode(CodecContext context, ByteBuffer buf) throws CodecException {
    float sideways = buf.readFloat();
    float forwards = buf.readFloat();
    final byte flags = buf.readByte();
    final boolean jump = (flags & 0x1) != 0;
    final boolean sneak = (flags & 0x2) != 0;
    final List<Message> messages = new ArrayList<>();
    final boolean lastSneak = firstNonNull(context.getChannel().attr(SNEAKING).getAndSet(sneak), false);
    if (lastSneak != sneak) {
        messages.add(new MessagePlayInPlayerSneak(sneak));
    }
    final boolean lastJump = firstNonNull(context.getChannel().attr(JUMPING).getAndSet(jump), false);
    if (lastJump != jump && !firstNonNull(context.getChannel().attr(CodecPlayInPlayerAction.CANCEL_NEXT_JUMP_MESSAGE).getAndSet(false), false)) {
        messages.add(new MessagePlayInPlayerVehicleJump(jump, 0f));
    }
    // The mc client already applies the sneak speed, but we want to choose it
    if (sneak) {
        sideways /= 0.3f;
        forwards /= 0.3f;
    }
    messages.add(new MessagePlayInPlayerMovementInput(forwards, sideways));
    return messages.size() == 1 ? messages.get(0) : new BulkMessage(messages);
}
Also used : MessagePlayInPlayerMovementInput(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerMovementInput) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) Message(org.lanternpowered.server.network.message.Message) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) MessagePlayInPlayerVehicleJump(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerVehicleJump) ArrayList(java.util.ArrayList) MessagePlayInPlayerSneak(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerSneak)

Example 3 with Message

use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.

the class MessageCodecHandler method encode.

@Override
protected void encode(ChannelHandlerContext ctx, Message message, List<Object> output) throws Exception {
    final Protocol protocol = this.codecContext.getSession().getProtocol();
    final MessageRegistration<Message> registration = (MessageRegistration<Message>) protocol.outbound().findByMessageType(message.getClass()).orElse(null);
    if (registration == null) {
        throw new EncoderException("Message type (" + message.getClass().getName() + ") is not registered!");
    }
    CodecRegistration codecRegistration = registration.getCodecRegistration().orElse(null);
    if (codecRegistration == null) {
        throw new EncoderException("Message type (" + message.getClass().getName() + ") is not registered to allow encoding!");
    }
    /*
        if (message instanceof MessagePlayOutWorldTime ||
                message instanceof MessageInOutKeepAlive) {
        } else {
            System.out.println(message.getClass().getName());
        }
        */
    final ByteBuf opcode = ctx.alloc().buffer();
    // Write the opcode of the message
    writeVarInt(opcode, codecRegistration.getOpcode());
    final Codec codec = codecRegistration.getCodec();
    final ByteBuffer content;
    try {
        content = codec.encode(this.codecContext, message);
    } finally {
        ReferenceCountUtil.release(message);
    }
    // Add the buffer to the output
    output.add(Unpooled.wrappedBuffer(opcode, ((LanternByteBuffer) content).getDelegate()));
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) Codec(org.lanternpowered.server.network.message.codec.Codec) MessageToMessageCodec(io.netty.handler.codec.MessageToMessageCodec) MessageRegistration(org.lanternpowered.server.network.message.MessageRegistration) HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) CodecRegistration(org.lanternpowered.server.network.message.CodecRegistration) Protocol(org.lanternpowered.server.network.protocol.Protocol) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternByteBuffer(org.lanternpowered.server.network.buffer.LanternByteBuffer) LanternByteBuffer(org.lanternpowered.server.network.buffer.LanternByteBuffer)

Example 4 with Message

use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.

the class MessageCodecHandler method decode.

@Override
public void decode(ChannelHandlerContext ctx, ByteBuf input, List<Object> output) throws Exception {
    if (input.readableBytes() == 0) {
        return;
    }
    final int opcode = readVarInt(input);
    final ProtocolState state = this.codecContext.getSession().getProtocolState();
    final Protocol protocol = state.getProtocol();
    final CodecRegistration registration = protocol.inbound().find(opcode).orElse(null);
    if (registration == null) {
        if (warnedMissingOpcodes.add(opcode)) {
            Lantern.getLogger().warn("Failed to find a message registration with opcode 0x{} in state {}!", Integer.toHexString(opcode), state);
        }
        return;
    }
    // Copy the remaining content of the buffer to a new buffer used by the
    // message decoding
    final ByteBuffer content = this.codecContext.byteBufAlloc().buffer(input.readableBytes());
    input.readBytes(((LanternByteBuffer) content).getDelegate(), input.readableBytes());
    // Read the content of the message
    final Message message;
    try {
        message = registration.getCodec().decode(this.codecContext, content);
        if (content.available() > 0) {
            Lantern.getLogger().warn("Trailing bytes {}b after decoding with message codec {} with opcode 0x{} in state {}!\n{}", content.available(), registration.getCodec().getClass().getName(), Integer.toHexString(opcode), state, message);
        }
    } finally {
        content.release();
    }
    /*
        if (message instanceof MessageInOutKeepAlive ||
                message instanceof MessagePlayInPlayerMovement) {
        } else {
            System.out.println(message.getClass().getName());
        }
        */
    processMessage(message, output, protocol, state, this.codecContext);
}
Also used : HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) CodecRegistration(org.lanternpowered.server.network.message.CodecRegistration) ProtocolState(org.lanternpowered.server.network.protocol.ProtocolState) Protocol(org.lanternpowered.server.network.protocol.Protocol) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternByteBuffer(org.lanternpowered.server.network.buffer.LanternByteBuffer)

Example 5 with Message

use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.

the class AbstractCodecPlayInOutCustomPayload method decode.

@Override
public Message decode(CodecContext context, ByteBuffer buf) throws CodecException {
    final String channel = buf.readLimitedString(LanternChannelRegistrar.MAX_NAME_LENGTH);
    final int length = buf.available();
    if (length > Short.MAX_VALUE) {
        throw new DecoderException("CustomPayload messages may not be longer then " + Short.MAX_VALUE + " bytes");
    }
    final ByteBuffer content = buf.slice();
    final Message message = decode0(context, content, channel);
    if (content.available() > 0) {
        Lantern.getLogger().warn("Trailing bytes {}b after decoding with custom payload message codec {} with channel {}!\n{}", content.available(), getClass().getName(), channel, message);
    }
    // Skip all the bytes, we already processed them
    buf.setReadIndex(buf.readerIndex() + buf.available());
    return message;
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Aggregations

Message (org.lanternpowered.server.network.message.Message)21 NullMessage (org.lanternpowered.server.network.message.NullMessage)7 BulkMessage (org.lanternpowered.server.network.message.BulkMessage)6 ArrayList (java.util.ArrayList)5 ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)5 HandlerMessage (org.lanternpowered.server.network.message.HandlerMessage)5 ChannelPromise (io.netty.channel.ChannelPromise)4 EventLoop (io.netty.channel.EventLoop)4 EncoderException (io.netty.handler.codec.EncoderException)3 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)3 Iterator (java.util.Iterator)3 Map (java.util.Map)3 Set (java.util.Set)3 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Sets (com.google.common.collect.Sets)2 CodecException (io.netty.handler.codec.CodecException)2 Collections (java.util.Collections)2