Search in sources :

Example 71 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class LanternChannelRegistrar method sendPayload.

void sendPayload(Player player, String channel, Consumer<ByteBuffer> payload) {
    checkNotNull(player, "player");
    checkNotNull(payload, "payload");
    final NetworkSession session = ((LanternPlayer) player).getConnection();
    if (session.getRegisteredChannels().contains(channel)) {
        final ByteBuffer buf = ByteBufferAllocator.unpooled().buffer();
        payload.accept(buf);
        session.send(new MessagePlayInOutChannelPayload(channel, buf));
    }
}
Also used : NetworkSession(org.lanternpowered.server.network.NetworkSession) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 72 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class LanternChannelRegistrar method sendPayloadToAll.

void sendPayloadToAll(String channel, Consumer<ByteBuffer> payload) {
    checkNotNull(payload, "payload");
    final Iterator<Player> players = this.server.getOnlinePlayers().stream().filter(player -> ((LanternPlayer) player).getConnection().getRegisteredChannels().contains(channel)).iterator();
    if (players.hasNext()) {
        final ByteBuffer buf = ByteBufferAllocator.unpooled().buffer();
        payload.accept(buf);
        final Message msg = new MessagePlayInOutChannelPayload(channel, buf);
        players.forEachRemaining(player -> ((LanternPlayer) player).getConnection().send(msg));
    }
}
Also used : RemoteConnection(org.spongepowered.api.network.RemoteConnection) ByteBufferAllocator(org.lanternpowered.server.network.buffer.ByteBufferAllocator) IndexedMessageChannel(org.spongepowered.api.network.ChannelBinding.IndexedMessageChannel) Inject(com.google.inject.Inject) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) Platform(org.spongepowered.api.Platform) MessagePlayInOutUnregisterChannels(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutUnregisterChannels) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) NetworkSession(org.lanternpowered.server.network.NetworkSession) ChannelRegistrar(org.spongepowered.api.network.ChannelRegistrar) Map(java.util.Map) MessagePlayInOutRegisterChannels(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutRegisterChannels) Server(org.spongepowered.api.Server) Conditions.checkNotNullOrEmpty(org.lanternpowered.server.util.Conditions.checkNotNullOrEmpty) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Sets(com.google.common.collect.Sets) Message(org.lanternpowered.server.network.message.Message) ChannelBinding(org.spongepowered.api.network.ChannelBinding) Consumer(java.util.function.Consumer) ChannelRegistrationException(org.spongepowered.api.network.ChannelRegistrationException) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Conditions.checkPlugin(org.lanternpowered.server.util.Conditions.checkPlugin) Collections(java.util.Collections) Singleton(com.google.inject.Singleton) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Player(org.spongepowered.api.entity.living.player.Player) Message(org.lanternpowered.server.network.message.Message) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 73 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class LanternIndexedMessageChannel method encode.

private void encode(Message message, ByteBuffer buf) {
    final IndexedMessageRegistration registration = getRegistrations(Platform.Type.SERVER).classToRegistration.get(message.getClass());
    checkArgument(registration != null, "The specified message type %s is not registered", message.getClass().getName());
    final ByteBuffer content = ByteBufferAllocator.unpooled().buffer();
    message.writeTo(content);
    // noinspection ConstantConditions
    buf.writeByte(registration.opcode);
    buf.writeBytes(content);
}
Also used : ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Example 74 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class LanternIndexedMessageChannel method handlePayload.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
void handlePayload(ByteBuffer buf, RemoteConnection connection) {
    final byte opcode = buf.readByte();
    final IndexedMessageRegistration registration = getRegistrations(Platform.Type.SERVER).opcodeToRegistration.get(opcode);
    if (registration == null) {
        Lantern.getLogger().warn("Received unexpected message type with id: {}" + " in the indexed message channel: {}", opcode, getName());
        return;
    }
    final Message message;
    try {
        message = registration.messageType.newInstance();
    } catch (Exception e) {
        Lantern.getLogger().error("Failed to instantiate message: {}", registration.messageType.getName(), e);
        return;
    }
    final ByteBuffer content = buf.slice();
    try {
        message.readFrom(content);
    } catch (Exception e) {
        Lantern.getLogger().error("Failed to deserialize message: {}", registration.messageType.getName(), e);
        return;
    }
    registration.handlers.forEach(handler -> ((MessageHandler) handler).handleMessage(message, connection, Platform.Type.SERVER));
}
Also used : Message(org.spongepowered.api.network.Message) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Example 75 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class CodecPlayInOutCustomPayload method encode0.

@Override
protected MessageResult encode0(CodecContext context, Message message) throws CodecException {
    if (message instanceof MessageForgeHandshakeInOutAck) {
        return new MessageResult("FML|HS", context.byteBufAlloc().buffer(2).writeByte((byte) FML_HANDSHAKE_ACK).writeByte((byte) ((ForgeServerHandshakePhase) ((MessageForgeHandshakeInOutAck) message).getPhase()).ordinal()));
    } else if (message instanceof MessageForgeHandshakeInOutHello) {
        return new MessageResult("FML|HS", context.byteBufAlloc().buffer(2).writeByte((byte) FML_HANDSHAKE_SERVER_HELLO).writeByte((byte) FORGE_PROTOCOL));
    } else if (message instanceof MessageForgeHandshakeInOutModList) {
        Map<String, String> entries = ((MessageForgeHandshakeInOutModList) message).getEntries();
        ByteBuffer buf = context.byteBufAlloc().buffer();
        buf.writeByte((byte) FML_HANDSHAKE_MOD_LIST);
        buf.writeVarInt(entries.size());
        for (Map.Entry<String, String> en : entries.entrySet()) {
            buf.writeString(en.getKey());
            buf.writeString(en.getValue());
        }
        return new MessageResult("FML|HS", buf);
    } else if (message instanceof MessageForgeHandshakeOutReset) {
        return new MessageResult("FML|HS", context.byteBufAlloc().buffer(1).writeByte((byte) FML_HANDSHAKE_RESET));
    }
    throw new EncoderException("Unsupported message type: " + message);
}
Also used : MessageForgeHandshakeInOutHello(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutHello) EncoderException(io.netty.handler.codec.EncoderException) MessageForgeHandshakeOutReset(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeOutReset) MessageForgeHandshakeInOutModList(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutModList) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) Map(java.util.Map) MessageForgeHandshakeInOutAck(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutAck)

Aggregations

ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)75 EncoderException (io.netty.handler.codec.EncoderException)9 Vector3d (com.flowpowered.math.vector.Vector3d)7 Message (org.lanternpowered.server.network.message.Message)5 Map (java.util.Map)4 MessagePlayInOutChannelPayload (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload)4 AbstractParameterList (org.lanternpowered.server.network.entity.parameter.AbstractParameterList)3 NullMessage (org.lanternpowered.server.network.message.NullMessage)3 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 Vector3i (com.flowpowered.math.vector.Vector3i)2 CodecException (io.netty.handler.codec.CodecException)2 LanternPotionEffectType (org.lanternpowered.server.effect.potion.LanternPotionEffectType)2 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)2 NetworkSession (org.lanternpowered.server.network.NetworkSession)2 LanternByteBuffer (org.lanternpowered.server.network.buffer.LanternByteBuffer)2 BulkMessage (org.lanternpowered.server.network.message.BulkMessage)2 CodecRegistration (org.lanternpowered.server.network.message.CodecRegistration)2 HandlerMessage (org.lanternpowered.server.network.message.HandlerMessage)2 Codec (org.lanternpowered.server.network.message.codec.Codec)2 RawItemStack (org.lanternpowered.server.network.objects.RawItemStack)2