Search in sources :

Example 11 with Message

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

the class NetworkSession method send.

/**
 * Sends a array of {@link Message}s.
 *
 * @param messages The messages
 */
public void send(Message... messages) {
    checkNotNull(messages, "messages");
    checkArgument(messages.length != 0, "messages cannot be empty");
    if (!this.channel.isActive()) {
        return;
    }
    final ChannelPromise voidPromise = this.channel.voidPromise();
    if (messages.length == 1) {
        this.channel.writeAndFlush(messages[0], voidPromise);
    } else {
        final EventLoop eventLoop = this.channel.eventLoop();
        if (eventLoop.inEventLoop()) {
            for (Message message : messages) {
                ReferenceCountUtil.retain(message);
                this.channel.writeAndFlush(message, 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);
            messages0.forEach(ReferenceCountUtil::retain);
            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) ReferenceCountUtil(io.netty.util.ReferenceCountUtil)

Example 12 with Message

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

the class LanternScore method setScore.

@Override
public void setScore(int score) {
    if (this.score == score) {
        return;
    }
    this.score = score;
    final Multimap<Scoreboard, Objective> scoreboards = HashMultimap.create();
    for (Objective objective : this.objectives) {
        for (Scoreboard scoreboard : ((LanternObjective) objective).scoreboards) {
            scoreboards.put(scoreboard, objective);
        }
    }
    if (!scoreboards.isEmpty()) {
        final Map<Objective, Message> messages = new HashMap<>();
        for (Map.Entry<Scoreboard, Objective> entry : scoreboards.entries()) {
            ((LanternScoreboard) entry.getKey()).sendToPlayers(() -> Collections.singletonList(messages.computeIfAbsent(entry.getValue(), obj -> new MessagePlayOutScoreboardScore.CreateOrUpdate(obj.getName(), this.legacyName, score))));
        }
    }
}
Also used : Objective(org.spongepowered.api.scoreboard.objective.Objective) MessagePlayOutScoreboardScore(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutScoreboardScore) Message(org.lanternpowered.server.network.message.Message) Scoreboard(org.spongepowered.api.scoreboard.Scoreboard) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with Message

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

the class LanternWorld method broadcast.

public void broadcast(Supplier<Message> message, @Nullable Predicate<LanternPlayer> filter) {
    Set<LanternPlayer> players = this.players;
    if (filter != null) {
        players = players.stream().filter(filter).collect(Collectors.toSet());
    }
    if (players.isEmpty()) {
        return;
    }
    final Message message0 = message.get();
    players.forEach(player -> player.getConnection().send(message0));
}
Also used : Message(org.lanternpowered.server.network.message.Message) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 14 with Message

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

the class NetworkSession method sendWithFuture.

/**
 * Sends a array of {@link Message}s and returns the {@link ChannelFuture}.
 *
 * @param messages The messages
 * @return The channel future
 */
public ChannelFuture sendWithFuture(Message... messages) {
    checkNotNull(messages, "messages");
    checkArgument(messages.length != 0, "messages cannot be empty");
    final ChannelPromise promise = this.channel.newPromise();
    if (!this.channel.isActive()) {
        return promise;
    }
    promise.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
    // there is only one message.
    if (messages.length == 1) {
        this.channel.writeAndFlush(messages[0], promise);
    } else {
        final EventLoop eventLoop = this.channel.eventLoop();
        final ChannelPromise voidPromise = this.channel.voidPromise();
        if (eventLoop.inEventLoop()) {
            final int last = messages.length - 1;
            for (int i = 0; i < last; i++) {
                ReferenceCountUtil.retain(messages[i]);
                this.channel.writeAndFlush(messages[i], voidPromise);
            }
            ReferenceCountUtil.retain(messages[last]);
            this.channel.writeAndFlush(messages[last], promise);
        } 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)

Example 15 with Message

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

the class LanternScoreboard method createObjectiveInitMessages.

private List<Message> createObjectiveInitMessages(Objective objective) {
    final List<Message> messages = new ArrayList<>();
    messages.add(new MessagePlayOutScoreboardObjective.Create(objective.getName(), ((LanternObjective) objective).getLegacyDisplayName(), objective.getDisplayMode()));
    for (Score score : ((LanternObjective) objective).scores.values()) {
        messages.add(new MessagePlayOutScoreboardScore.CreateOrUpdate(objective.getName(), LanternTexts.toLegacy(score.getName()), score.getScore()));
    }
    return messages;
}
Also used : MessagePlayOutScoreboardScore(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutScoreboardScore) Score(org.spongepowered.api.scoreboard.Score) MessagePlayOutScoreboardScore(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutScoreboardScore) Message(org.lanternpowered.server.network.message.Message) ArrayList(java.util.ArrayList) MessagePlayOutScoreboardObjective(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutScoreboardObjective)

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