Package net.yura.lobby.netty

Source Code of net.yura.lobby.netty.ProtoDecoder

package net.yura.lobby.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
import net.yura.lobby.server.LobbyServer;

/**
* @author Yura Mamyrin
*/
public class ProtoDecoder extends ByteToMessageDecoder {

    final LobbyServer server;
   
    public ProtoDecoder(LobbyServer server) {
        this.server = server;
    }
   
    @Override
    protected void decode(ChannelHandlerContext chc, ByteBuf in, List<Object> ml) throws Exception {
        while (true) {
            if (in.readableBytes() < 4) {
                return;
            }
            in.markReaderIndex();
            int dataLength = in.readInt();
            if (in.readableBytes() < dataLength) {
                in.resetReaderIndex();
                return;
            }
            ml.add( server.decode( new ByteBufInputStream(in), dataLength) );
        }
    }
}
TOP

Related Classes of net.yura.lobby.netty.ProtoDecoder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.