Search in sources :

Example 1 with MessageForgeHandshakeInOutAck

use of org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutAck in project LanternServer by LanternPowered.

the class HandlerForgeHandshakeInAck method handle.

@Override
public void handle(NetworkContext context, MessageForgeHandshakeInOutAck message) {
    final NetworkSession session = context.getSession();
    final Attribute<ForgeServerHandshakePhase> phase = context.getChannel().attr(ForgeHandshakePhase.PHASE);
    switch(phase.get()) {
        case WAITING_ACK:
            if (!message.getPhase().equals(ForgeClientHandshakePhase.WAITING_SERVER_DATA)) {
                session.disconnect(t("Retrieved unexpected forge handshake ack message. (Got %s, expected %s)", message.getPhase(), ForgeClientHandshakePhase.WAITING_SERVER_DATA));
            } else {
                final List<MessageForgeHandshakeOutRegistryData.Entry> entries = new ArrayList<>();
                entries.add(new MessageForgeHandshakeOutRegistryData.Entry("minecraft:items", new HashMap<>(), new ArrayList<>()));
                entries.add(new MessageForgeHandshakeOutRegistryData.Entry("minecraft:blocks", new HashMap<>(), new ArrayList<>()));
                session.send(new MessageForgeHandshakeOutRegistryData(entries));
                session.send(new MessageForgeHandshakeInOutAck(ForgeServerHandshakePhase.WAITING_ACK));
                phase.set(ForgeServerHandshakePhase.COMPLETE);
            }
            Lantern.getLogger().info("{}: Forge handshake -> Received ack (waitingServerData) message.", session.getGameProfile().getName().get());
            break;
        case COMPLETE:
            if (!message.getPhase().equals(ForgeClientHandshakePhase.WAITING_SERVER_COMPLETE)) {
                session.disconnect(t("Retrieved unexpected forge handshake ack message. (Got %s, expected %s)", message.getPhase(), ForgeClientHandshakePhase.WAITING_SERVER_COMPLETE));
            } else {
                session.send(new MessageForgeHandshakeInOutAck(ForgeServerHandshakePhase.COMPLETE));
                phase.set(ForgeServerHandshakePhase.DONE);
            }
            Lantern.getLogger().info("{}: Forge handshake -> Received ack (waitingServerComplete) message.", session.getGameProfile().getName().get());
            break;
        case DONE:
            if (!message.getPhase().equals(ForgeClientHandshakePhase.PENDING_COMPLETE) && !message.getPhase().equals(ForgeClientHandshakePhase.COMPLETE)) {
                session.disconnect(t("Retrieved unexpected forge handshake ack message. (Got %s, expected %s or %s)", message.getPhase(), ForgeClientHandshakePhase.PENDING_COMPLETE, ForgeClientHandshakePhase.COMPLETE));
            } else {
                if (message.getPhase().equals(ForgeClientHandshakePhase.PENDING_COMPLETE)) {
                    session.send(new MessageForgeHandshakeInOutAck(ForgeServerHandshakePhase.DONE));
                    Lantern.getLogger().info("{}: Forge handshake -> Received ack (pendingComplete) message.", session.getGameProfile().getName().get());
                } else {
                    session.setProtocolState(ProtocolState.PLAY);
                    session.initPlayer();
                    Lantern.getLogger().info("{}: Forge handshake -> Received ack (complete) message.", session.getGameProfile().getName().get());
                }
            }
            break;
        case ERROR:
            break;
        default:
            session.disconnect(t("Retrieved unexpected forge handshake ack message. (Got %s)", message.getPhase()));
    }
}
Also used : ForgeServerHandshakePhase(org.lanternpowered.server.network.forge.handshake.ForgeServerHandshakePhase) NetworkSession(org.lanternpowered.server.network.NetworkSession) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MessageForgeHandshakeOutRegistryData(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeOutRegistryData) MessageForgeHandshakeInOutAck(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutAck)

Example 2 with MessageForgeHandshakeInOutAck

use of org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutAck in project LanternServer by LanternPowered.

the class CodecPlayInOutCustomPayload method decode0.

@Override
protected Message decode0(CodecContext context, String channel, ByteBuffer content) throws CodecException {
    if ("FML|HS".equals(channel)) {
        int type = content.readByte();
        switch(type) {
            case FML_HANDSHAKE_RESET:
                // server -> client message: ignore
                break;
            case FML_HANDSHAKE_ACK:
                ForgeClientHandshakePhase phase = ForgeClientHandshakePhase.values()[content.readByte()];
                return new MessageForgeHandshakeInOutAck(phase);
            case FML_HANDSHAKE_SERVER_HELLO:
                // server -> client message: ignore
                break;
            case FML_HANDSHAKE_CLIENT_HELLO:
                // The forge protocol version on the client
                content.readByte();
                return new MessageForgeHandshakeInOutHello();
            case FML_HANDSHAKE_MOD_LIST:
                int size = content.readVarInt();
                Map<String, String> entries = Maps.newHashMapWithExpectedSize(size);
                for (int i = 0; i < size; i++) {
                    entries.put(content.readString(), content.readString());
                }
                return new MessageForgeHandshakeInOutModList(entries);
            case FML_HANDSHAKE_REGISTRY_DATA:
                // server -> client message: ignore
                break;
            default:
                throw new DecoderException("Unknown forge handshake message with opcode: " + type);
        }
        throw new DecoderException("Received an unexpected forge message with opcode: " + type);
    } else {
        throw new DecoderException("Received an unexpected message with channel: " + channel);
    }
}
Also used : MessageForgeHandshakeInOutHello(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutHello) DecoderException(io.netty.handler.codec.DecoderException) MessageForgeHandshakeInOutModList(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutModList) MessageForgeHandshakeInOutAck(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutAck) ForgeClientHandshakePhase(org.lanternpowered.server.network.forge.handshake.ForgeClientHandshakePhase)

Example 3 with MessageForgeHandshakeInOutAck

use of org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutAck 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

MessageForgeHandshakeInOutAck (org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutAck)3 MessageForgeHandshakeInOutHello (org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutHello)2 MessageForgeHandshakeInOutModList (org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutModList)2 DecoderException (io.netty.handler.codec.DecoderException)1 EncoderException (io.netty.handler.codec.EncoderException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NetworkSession (org.lanternpowered.server.network.NetworkSession)1 ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)1 ForgeClientHandshakePhase (org.lanternpowered.server.network.forge.handshake.ForgeClientHandshakePhase)1 ForgeServerHandshakePhase (org.lanternpowered.server.network.forge.handshake.ForgeServerHandshakePhase)1 MessageForgeHandshakeOutRegistryData (org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeOutRegistryData)1 MessageForgeHandshakeOutReset (org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeOutReset)1