Search in sources :

Example 6 with Message

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

the class AbstractCodecPlayInOutCustomPayload method decode0.

private Message decode0(CodecContext context, ByteBuffer content, String channel) {
    if ("REGISTER".equals(channel)) {
        Set<String> channels = decodeChannels(content);
        Iterator<String> it = channels.iterator();
        while (it.hasNext()) {
            String channel0 = it.next();
            if (channel0.startsWith("FML")) {
                it.remove();
            }
        }
        if (!channels.isEmpty()) {
            return new MessagePlayInOutRegisterChannels(channels);
        }
    } else if ("UNREGISTER".equals(channel)) {
        Set<String> channels = decodeChannels(content);
        Iterator<String> it = channels.iterator();
        while (it.hasNext()) {
            String channel0 = it.next();
            if (channel0.startsWith("FML")) {
                it.remove();
            }
        }
        if (!channels.isEmpty()) {
            return new MessagePlayInOutUnregisterChannels(channels);
        }
    } else if ("FML|MP".equals(channel)) {
        Attribute<MultiPartMessage> attribute = context.getChannel().attr(FML_MULTI_PART_MESSAGE);
        MultiPartMessage message0 = attribute.get();
        if (message0 == null) {
            String channel0 = content.readString();
            int parts = content.readByte() & 0xff;
            int size = content.readInteger();
            if (size <= 0 || size >= -16797616) {
                throw new CodecException("Received FML MultiPart packet outside of valid length bounds, Max: -16797616, Received: " + size);
            }
            attribute.set(new MultiPartMessage(channel0, context.byteBufAlloc().buffer(size), parts));
        } else {
            int part = content.readByte() & 0xff;
            if (part != message0.index) {
                throw new CodecException("Received FML MultiPart packet out of order, Expected: " + message0.index + ", Got: " + part);
            }
            int len = content.available() - 1;
            content.readBytes(message0.buffer, message0.offset, len);
            message0.offset += len;
            message0.index++;
            if (message0.index >= message0.parts) {
                final Message message = decode0(context, message0.channel, message0.buffer);
                attribute.set(null);
                return message;
            }
        }
    } else {
        return decode0(context, channel, content);
    }
    return NullMessage.INSTANCE;
}
Also used : MessagePlayInOutRegisterChannels(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutRegisterChannels) MessagePlayInOutUnregisterChannels(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutUnregisterChannels) Set(java.util.Set) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) Iterator(java.util.Iterator) CodecException(io.netty.handler.codec.CodecException)

Example 7 with Message

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

the class LanternBossBar method sendStyleUpdate.

private void sendStyleUpdate() {
    if (!this.viewers.isEmpty()) {
        final Message message = new MessagePlayOutBossBar.UpdateStyle(this.uniqueId, this.color, this.overlay);
        this.viewers.forEach(player -> player.getConnection().send(message));
    }
}
Also used : Message(org.lanternpowered.server.network.message.Message)

Example 8 with Message

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

the class LanternBossBar method sendMiscUpdate.

private void sendMiscUpdate() {
    if (!this.viewers.isEmpty()) {
        final Message message = new MessagePlayOutBossBar.UpdateMisc(this.uniqueId, this.darkenSky, this.playEndBossMusic || this.createFog);
        this.viewers.forEach(player -> player.getConnection().send(message));
    }
}
Also used : Message(org.lanternpowered.server.network.message.Message)

Example 9 with Message

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

the class NetworkSession method send.

/**
 * Sends a iterable of {@link Message}s.
 *
 * @param messages The messages
 */
public void send(Iterable<Message> messages) {
    checkNotNull(messages, "messages");
    final Iterator<Message> it = messages.iterator();
    checkArgument(it.hasNext(), "messages cannot be empty");
    Message message = it.next();
    // Don't bother checking if we are in the event loop,
    // there is only one message.
    final ChannelPromise voidPromise = this.channel.voidPromise();
    if (!it.hasNext()) {
        this.channel.writeAndFlush(message, voidPromise);
    } else {
        final EventLoop eventLoop = this.channel.eventLoop();
        if (eventLoop.inEventLoop()) {
            for (Message message0 : messages) {
                this.channel.writeAndFlush(message0, voidPromise);
            }
        } else {
            // If there are more then one message, combine them inside the
            // event loop to reduce overhead of wakeup calls and object creation
            // Create a copy of the list, to avoid concurrent modifications
            final List<Message> messages0 = ImmutableList.copyOf(messages);
            eventLoop.submit(() -> {
                for (Message message0 : messages0) {
                    this.channel.writeAndFlush(message0, voidPromise);
                }
            });
        }
    }
}
Also used : EventLoop(io.netty.channel.EventLoop) 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) ChannelPromise(io.netty.channel.ChannelPromise)

Example 10 with Message

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

the class NetworkSession method sendWithFuture.

/**
 * Sends a iterable of {@link Message}s.
 *
 * @param messages The messages
 */
public ChannelFuture sendWithFuture(Iterable<Message> messages) {
    checkNotNull(messages, "messages");
    final Iterator<Message> it = messages.iterator();
    checkArgument(it.hasNext(), "messages cannot be empty");
    final ChannelPromise promise = this.channel.newPromise();
    if (!this.channel.isActive()) {
        return promise;
    }
    promise.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
    Message message = it.next();
    // there is only one message.
    if (!it.hasNext()) {
        this.channel.writeAndFlush(message, promise);
    } else {
        final EventLoop eventLoop = this.channel.eventLoop();
        final ChannelPromise voidPromise = this.channel.voidPromise();
        if (eventLoop.inEventLoop()) {
            while (true) {
                final boolean next = it.hasNext();
                // Only use a normal channel promise for the last message
                this.channel.writeAndFlush(message, next ? voidPromise : promise);
                if (!next) {
                    break;
                }
                message = it.next();
                ReferenceCountUtil.retain(message);
            }
        } else {
            // If there are more then one message, combine them inside the
            // event loop to reduce overhead of wakeup calls and object creation
            // Create a copy of the list, to avoid concurrent modifications
            final List<Message> messages0 = ImmutableList.copyOf(messages);
            messages0.forEach(ReferenceCountUtil::retain);
            eventLoop.submit(() -> {
                final Iterator<Message> it0 = messages0.iterator();
                do {
                    final Message message0 = it0.next();
                    // Only use a normal channel promise for the last message
                    this.channel.writeAndFlush(message0, it0.hasNext() ? voidPromise : promise);
                } while (it0.hasNext());
            });
        }
    }
    return promise;
}
Also used : EventLoop(io.netty.channel.EventLoop) 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) ChannelPromise(io.netty.channel.ChannelPromise) ReferenceCountUtil(io.netty.util.ReferenceCountUtil)

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