org.legacy.network.protocol.handshake.HandshakeDecoder.java Source code

Java tutorial

Introduction

Here is the source code for org.legacy.network.protocol.handshake.HandshakeDecoder.java

Source

/**
 * Copyright (c) 2014 Legacy 814
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.legacy.network.protocol.handshake;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

import org.legacy.Framework;
import org.legacy.network.protocol.ondemand.OnDemandDecoder;
import org.legacy.utility.BufferUtils;
import org.legacy.utility.LegacyLogger;

/**
 * @author Im Frizzy <skype:kfriz1998>
 * @since Jul 14, 2014
 */
public class HandshakeDecoder extends ByteToMessageDecoder {

    public HandshakeDecoder() {
        super();
    }

    /* (non-Javadoc)
     * @see io.netty.handler.codec.ByteToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)
     */
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
        if (buffer.isReadable()) {
            if (ctx.pipeline().get(HandshakeDecoder.class) != null) {
                ctx.pipeline().remove(this);
            }
            int opcode = buffer.readUnsignedByte() & 0xFF;
            switch (opcode) {
            case 15:
                int size = buffer.readUnsignedByte();
                if (buffer.readableBytes() < size) {
                    ctx.channel().close();
                    return;
                }
                int major_version = buffer.readInt();
                int minor_version = buffer.readInt();
                if (major_version != Framework.MAJOR_VERSION || minor_version != Framework.MINOR_VERSION) {
                    buffer.writeByte(6);
                    ctx.channel().close();
                    return;
                }
                String ondemand_token = BufferUtils.readBufferedString(buffer);
                if (!ondemand_token.equals(Framework.ONDEMAND_SESSION_TOKEN)) {
                    ctx.channel().close();
                    return;
                }
                ByteBuf buf = Unpooled.buffer();
                buf.writeByte(0);
                for (int element : Framework.CRC_UPDATE_KEYS) {
                    buf.writeInt(element);
                }
                ctx.channel().write(buf);
                ctx.pipeline().addBefore("channel-handler", "decoder", new OnDemandDecoder());
                break;
            default:
                LegacyLogger.fireWarnMessage(this, "Unknown Handshake Opcode: " + opcode);
            }
        }
    }

}