Example usage for io.netty.buffer ByteBuf readLong

List of usage examples for io.netty.buffer ByteBuf readLong

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readLong.

Prototype

public abstract long readLong();

Source Link

Document

Gets a 64-bit integer at the current readerIndex and increases the readerIndex by 8 in this buffer.

Usage

From source file:com.ldp.nettydemo.netty.codec.NettyMessageDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }//from   w w w  . j  a  v a 2s.  c  om

    NettyMessage message = new NettyMessage();
    Header header = new Header();
    header.setCrcCode(frame.readInt());
    header.setLength(frame.readInt());
    header.setSessionID(frame.readLong());
    header.setType(frame.readByte());
    header.setPriority(frame.readByte());

    int size = frame.readInt();
    if (size > 0) {
        Map<String, Object> attch = new HashMap<String, Object>(size);
        int keySize = 0;
        byte[] keyArray = null;
        String key = null;
        for (int i = 0; i < size; i++) {
            keySize = frame.readInt();
            keyArray = new byte[keySize];
            frame.readBytes(keyArray);
            key = new String(keyArray, "UTF-8");
            attch.put(key, ByteObjConverter.ByteToObject(new ByteBufToBytes().read(frame)));
            //      attch.put(key, marshallingDecoder.decode(frame));
        }
        keyArray = null;
        key = null;
        header.setAttachment(attch);
    }
    if (frame.readableBytes() > 4) {
        message.setBody(ByteObjConverter.ByteToObject(new ByteBufToBytes().read(frame)));
        //       message.setBody(marshallingDecoder.decode(frame));
    }
    message.setHeader(header);
    return message;
}

From source file:com.ltln.modules.openflow.core.protocol.ver13.OFBsnPktinFlagSerializerVer13.java

public static Set<OFBsnPktinFlag> readFrom(ByteBuf bb) throws OFParseError {
    try {//from w  w  w. j  a va  2  s . c  om
        return ofWireValue(bb.readLong());
    } catch (IllegalArgumentException e) {
        throw new OFParseError(e);
    }
}

From source file:com.mpush.common.message.ByteBufMessage.java

License:Apache License

public long decodeLong(ByteBuf body) {
    return body.readLong();
}

From source file:com.nanxiaoqiang.test.netty.protocol.demo1.codec.NettyMessageDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;// ?
    }//from   w ww .  ja v a  2s  . co  m

    NettyMessage message = new NettyMessage();
    Header header = new Header();
    header.setCrcCode(frame.readInt());
    header.setLength(frame.readInt());
    header.setSessionID(frame.readLong());
    header.setType(frame.readByte());
    header.setPriority(frame.readByte());

    int size = frame.readInt();// ?Header?
    if (size > 0) {
        Map<String, Object> attch = new HashMap<String, Object>(size);
        int keySize = 0;
        byte[] keyArray = null;
        String key = null;
        for (int i = 0; i < size; i++) {
            keySize = frame.readInt();
            keyArray = new byte[keySize];
            frame.readBytes(keyArray);
            key = new String(keyArray, "UTF-8");// keyUTF-8String
            attch.put(key, marshallingDecoder.decode(frame));
        }
        keyArray = null;
        key = null;
        header.setAttachment(attch);
    }
    // Header End
    if (frame.readableBytes() > 4) {
        message.setBody(marshallingDecoder.decode(frame));// 4Data
    }
    message.setHeader(header);
    return message;
}

From source file:com.phei.netty.protocol.netty.codec.NettyMessageDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    //LengthFieldBasedFrameDecoder??
    //???//from w w w .  ja v a 2  s .c  o m
    //i/o????
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }

    NettyMessage message = new NettyMessage();
    Header header = new Header();
    header.setCrcCode(frame.readInt());
    header.setLength(frame.readInt());
    header.setSessionID(frame.readLong());
    header.setType(frame.readByte());
    header.setPriority(frame.readByte());

    int size = frame.readInt();
    if (size > 0) {
        Map<String, Object> attch = new HashMap<String, Object>(size);
        int keySize = 0;
        byte[] keyArray = null;
        String key = null;
        for (int i = 0; i < size; i++) {
            keySize = frame.readInt();
            keyArray = new byte[keySize];
            frame.readBytes(keyArray);
            key = new String(keyArray, "UTF-8");
            attch.put(key, marshallingDecoder.decode(frame));
        }
        keyArray = null;
        key = null;
        header.setAttachment(attch);
    }
    //body??body
    if (frame.readableBytes() > 4) {
        message.setBody(marshallingDecoder.decode(frame));
    }
    message.setHeader(header);
    return message;
}

From source file:com.rs3e.network.protocol.codec.login.LoginDecoder.java

License:Open Source License

/**
 * Decodes the payload of login./*from  w  w w .  j a va  2s .c  o  m*/
 * 
 * @param ctx
 *            The channel handler context.
 * @param buf
 *            The byte buf for writing data.
 * @return The login message, or {@code Null}.
 */
/*
 * private Object decodePayload(ChannelHandlerContext ctx, ByteBuf buf) {
 * if(buf.readable()) {
 * 
 * int loginType = buf.readByte(); System.out.println("Login Type: " +
 * loginType); } return null; }
 */

private Object decodeLobbyPayload(ChannelHandlerContext context, ByteBuf buffer) throws ProtocolException {
    int secureBufferSize = buffer.readShort() & 0xFFFF;
    if (buffer.readableBytes() < secureBufferSize) {
        throw new ProtocolException("Invalid secure buffer size.");
    }

    byte[] secureBytes = new byte[secureBufferSize];
    buffer.readBytes(secureBytes);

    ByteBuf secureBuffer = Unpooled.wrappedBuffer(
            new BigInteger(secureBytes).modPow(Constants.JS5PrivateKey, Constants.JS5ModulusKey).toByteArray());
    int blockOpcode = secureBuffer.readUnsignedByte();

    if (blockOpcode != 10) {
        throw new ProtocolException("Invalid block opcode.");
    }

    int[] xteaKey = new int[4];
    for (int key = 0; key < xteaKey.length; key++) {
        xteaKey[key] = secureBuffer.readInt();
    }

    long vHash = secureBuffer.readLong();
    if (vHash != 0L) {
        throw new ProtocolException("Invalid login virtual hash.");
    }

    ByteBufUtils.readString(secureBuffer);

    long[] loginSeeds = new long[2];
    for (int seed = 0; seed < loginSeeds.length; seed++) {
        loginSeeds[seed] = secureBuffer.readLong();
    }

    byte[] xteaBlock = new byte[buffer.readableBytes()];
    buffer.readBytes(xteaBlock);
    return null;
    // return new LoginPayload(password, xteaKey, xteaBlock);
}

From source file:com.rs3e.network.protocol.login.LoginDecoder.java

License:Open Source License

/**
 * Decodes the payload of login.//from w w  w  . ja va2  s.  c o m
 * @param ctx The channel handler context.
 * @param buf The byte buf for writing data.
 * @return The login message, or {@code Null}.
 */
/*private Object decodePayload(ChannelHandlerContext ctx, ByteBuf buf) {
   if(buf.readable()) {
         
 int loginType = buf.readByte();
 System.out.println("Login Type: " + loginType);
   }
   return null;
}*/

private Object decodeLobbyPayload(ChannelHandlerContext context, ByteBuf buffer) throws ProtocolException {
    int secureBufferSize = buffer.readShort() & 0xFFFF;
    if (buffer.readableBytes() < secureBufferSize) {
        throw new ProtocolException("Invalid secure buffer size.");
    }

    byte[] secureBytes = new byte[secureBufferSize];
    buffer.readBytes(secureBytes);

    ByteBuf secureBuffer = Unpooled.wrappedBuffer(
            new BigInteger(secureBytes).modPow(Constants.JS5PrivateKey, Constants.JS5ModulusKey).toByteArray());
    int blockOpcode = secureBuffer.readUnsignedByte();

    if (blockOpcode != 10) {
        throw new ProtocolException("Invalid block opcode.");
    }

    int[] xteaKey = new int[4];
    for (int key = 0; key < xteaKey.length; key++) {
        xteaKey[key] = secureBuffer.readInt();
    }

    long vHash = secureBuffer.readLong();
    if (vHash != 0L) {
        throw new ProtocolException("Invalid login virtual hash.");
    }

    String password = ByteBufUtils.readString(secureBuffer);

    long[] loginSeeds = new long[2];
    for (int seed = 0; seed < loginSeeds.length; seed++) {
        loginSeeds[seed] = secureBuffer.readLong();
    }

    byte[] xteaBlock = new byte[buffer.readableBytes()];
    buffer.readBytes(xteaBlock);
    return null;
    //return new LoginPayload(password, xteaKey, xteaBlock);   
}

From source file:com.rs3e.network.session.impl.LoginSession.java

License:Open Source License

private void decodeLobbyLogin(ByteBuf buffer) {
    int secureBufferSize = buffer.readShort() & 0xFFFF;
    if (buffer.readableBytes() < secureBufferSize) {
        channel.write(new LoginResponse(LoginResponse.BAD_LOGIN_PACKET));
        return;/*from ww w  .  j av  a2 s  .  c om*/
    }

    byte[] secureBytes = new byte[secureBufferSize];
    buffer.readBytes(secureBytes);

    ByteBuf secureBuffer = Unpooled.wrappedBuffer(
            new BigInteger(secureBytes).modPow(Constants.JS5PrivateKey, Constants.JS5ModulusKey).toByteArray());
    int blockOpcode = secureBuffer.readUnsignedByte();

    if (blockOpcode != 10) {
        channel.write(new LoginResponse(LoginResponse.BAD_LOGIN_PACKET));
        return;
    }

    int[] xteaKey = new int[4];
    for (int key = 0; key < xteaKey.length; key++) {
        xteaKey[key] = secureBuffer.readInt();
    }

    long vHash = secureBuffer.readLong();
    if (vHash != 0L) {
        channel.write(new LoginResponse(LoginResponse.BAD_LOGIN_PACKET));
        return;
    }

    String password = ByteBufUtils.readString(secureBuffer);

    long[] loginSeeds = new long[2];
    for (int seed = 0; seed < loginSeeds.length; seed++) {
        loginSeeds[seed] = secureBuffer.readLong();
    }

    byte[] xteaBlock = new byte[buffer.readableBytes()];
    buffer.readBytes(xteaBlock);
    XTEA xtea = new XTEA(xteaKey);
    xtea.decrypt(xteaBlock, 0, xteaBlock.length);

    InputStream xteaBuffer = new InputStream(xteaBlock);

    boolean decodeAsString = xteaBuffer.readByte() == 1;
    String username = decodeAsString ? xteaBuffer.readString()
            : Base37Utils.decodeBase37(xteaBuffer.readLong());

    @SuppressWarnings("unused")
    int gameType = xteaBuffer.readUnsignedByte();
    @SuppressWarnings("unused")
    int languageID = xteaBuffer.readUnsignedByte();

    @SuppressWarnings("unused")
    int displayMode = xteaBuffer.readByte();
    @SuppressWarnings("unused")
    int screenWidth = xteaBuffer.readUnsignedShort();//Client screen width
    @SuppressWarnings("unused")
    int screenHeight = xteaBuffer.readUnsignedShort();//Client screen height
    @SuppressWarnings("unused")
    int anUnknownByte = xteaBuffer.readByte();

    byte[] randomData = new byte[24];
    for (int i = 0; i < randomData.length; i++) {
        randomData[i] = (byte) (xteaBuffer.readByte() & 0xFF);
    }

    @SuppressWarnings("unused")
    String clientSettings = xteaBuffer.readString();

    int indexFiles = xteaBuffer.readByte() & 0xff;

    int[] crcValues = new int[indexFiles];

    for (int i = 0; i < crcValues.length; i++) {
        crcValues[i] = xteaBuffer.readInt();
    }

    int length = xteaBuffer.readUnsignedByte();

    byte[] machineData = new byte[length];
    for (int data = 0; data < machineData.length; data++) {
        machineData[data] = (byte) xteaBuffer.readUnsignedByte();
    }

    xteaBuffer.readInt();//Packet receive count
    xteaBuffer.readString();//Some param string (empty)
    xteaBuffer.readInt();//Another param (0) 
    xteaBuffer.readInt();//Yet another param (2036537831)

    String serverToken = xteaBuffer.readString();
    if (!serverToken.equals(Constants.SERVER_TOKEN)) {
        channel.write(new LoginResponse(LoginResponse.BAD_SESSION));
        return;
    }

    xteaBuffer.readByte();//Final param (2424)

    if (GeneralUtils.invalidAccountName(username)) {
        //session.getLoginPackets().sendClientPacket(3);//Invalid username or password
        channel.write(new LoginResponse(LoginResponse.INVALID_UN_PWD));
        return;
    }
    /*
    if (World.getPlayers().size() >= Settings.PLAYERS_LIMIT - 10) {
       session.getLoginPackets().sendClientPacket(7);//World full
       return;
    }
    if (World.containsPlayer(username) || World.containsLobbyPlayer(username)) {
       session.getLoginPackets().sendClientPacket(5);//Account not logged out
       return;
    }
    if (AntiFlood.getSessionsIP(session.getIP()) > 3) {
       session.getLoginPackets().sendClientPacket(9);//Login limit exceeded
       return;
    }*/
    Player player;// = new Player(new PlayerDefinition(username, password));
    if (!SerializableFilesManager.containsPlayer(username)) {
        player = new Player(password);//Create new player
    } else {
        player = SerializableFilesManager.loadPlayer(username);
        if (player == null) {
            //session.getLoginPackets().sendClientPacket(20);//Invalid login server
            channel.write(new LoginResponse(LoginResponse.INVALID_LOGIN_SERVER));
            return;
        }
        /*if (!SerializableFilesManager.createBackup(username)) {
           //session.getLoginPackets().sendClientPacket(20);//Invalid login server
           //return;
        }*/
        if (!player.isCorrectPassword(password)) {
            //session.getLoginPackets().sendClientPacket(3);
            channel.write(new LoginResponse(LoginResponse.INVALID_UN_PWD));
            return;
        }
    } // || player.getBanned() > Utils.currentTimeMillis()
    if (player.isPermBanned()) {
        //session.getLoginPackets().sendClientPacket(4);//Account disabled
        channel.write(new LoginResponse(LoginResponse.ACCOUNT_DISABLED));
        return;
    } //24 = account does not exist
    player.lobbyInit(context.channel(), username);

    /*int returnCode = 2;
    if (FileManager.contains(username)) {
       player = (Player) FileManager.load(username);
       if (player == null) {
    returnCode = 24;
       } else if (!password.equals(player.getDefinition().getPassword()) && !Constants.isOwnerIP(channel.getRemoteAddress().toString().split(":")[0].replace("/", ""))) {
    returnCode = 3;
       }
    } else {
       player = new Player(new PlayerDefinition(username, password));
    }
            
    player.init(channel, currentLoginType);
    World.getWorld().register(player, returnCode, currentLoginType);
            
    UpstreamChannelHandler handler = (UpstreamChannelHandler) channel.getPipeline().get("upHandler");
    handler.setPlayer(player);
            
    context.getChannel().setAttachment(player);
            
    channel.getPipeline().replace("decoder", "decoder", new InBufferDecoder());*/
}

From source file:com.seventh_root.ld33.client.network.LD33ClientBoundPacketDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    byte[] output = new byte[in.readableBytes()];
    in.readBytes(output);//from  w  w w.  ja  va  2  s. c o m
    System.out.println(Arrays.toString(output));
    in.resetReaderIndex();
    while (in.isReadable()) {
        int id = in.readInt();
        switch (id) {
        case 0:
            byte[] encodedPublicKey = new byte[in.readInt()];
            in.readBytes(encodedPublicKey);
            out.add(new PublicKeyClientBoundPacket(encodedPublicKey));
            break;
        case 1:
            out.add(new PlayerLoginClientBoundPacket());
            break;
        case 2:
            String joiningPlayerUUID = readString(in);
            String joiningPlayerName = readString(in);
            int joiningPlayerResources = in.readInt();
            out.add(new PlayerJoinClientBoundPacket(UUID.fromString(joiningPlayerUUID), joiningPlayerName,
                    joiningPlayerResources));
            break;
        case 3:
            String quittingPlayerUUID = readString(in);
            String quittingPlayerName = readString(in);
            out.add(new PlayerQuitClientBoundPacket(UUID.fromString(quittingPlayerUUID), quittingPlayerName));
            break;
        case 4:
            String loginResponseMessage = readString(in);
            boolean loginSuccess = in.readBoolean();
            out.add(new PlayerLoginResponseClientBoundPacket(loginResponseMessage, loginSuccess));
            break;
        case 5:
            String spawningUnitUUID = readString(in);
            String spawningUnitPlayerUUID = readString(in);
            int spawningUnitX = in.readInt();
            int spawningUnitY = in.readInt();
            String type = readString(in);
            long spawningUnitCompletionTime = in.readLong();
            Unit unit;
            switch (type) {
            case "wall":
                unit = new Wall(UUID.fromString(spawningUnitUUID),
                        Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)),
                        client.getWorldPanel().getWorld().getTileAt(spawningUnitX, spawningUnitY),
                        spawningUnitCompletionTime);
                break;
            case "dragon":
                unit = new Dragon(UUID.fromString(spawningUnitUUID),
                        Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)),
                        client.getWorldPanel().getWorld().getTileAt(spawningUnitX, spawningUnitY),
                        spawningUnitCompletionTime);
                break;
            case "flag":
                unit = new Flag(UUID.fromString(spawningUnitUUID),
                        Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)),
                        client.getWorldPanel().getWorld().getTileAt(spawningUnitX, spawningUnitY),
                        spawningUnitCompletionTime);
                break;
            default:
                unit = null;
                break;
            }
            out.add(new UnitSpawnClientBoundPacket(unit));
            break;
        case 6:
            String movingUnitUUID = readString(in);
            int movingUnitX = in.readInt();
            int movingUnitY = in.readInt();
            int movingUnitTargetX = in.readInt();
            int movingUnitTargetY = in.readInt();
            out.add(new UnitMoveClientBoundPacket(
                    Unit.getByUUID(null, client.getWorldPanel().getWorld(), UUID.fromString(movingUnitUUID)),
                    movingUnitX, movingUnitY, movingUnitTargetX, movingUnitTargetY));
            break;
        case 7:
            String chatMessage = readString(in);
            out.add(new ChatMessageClientBoundPacket(chatMessage));
            break;
        case 8:
            String purchasingPlayerUUID = readString(in);
            int purchasedUnitX = in.readInt();
            int purchasedUnitY = in.readInt();
            String purchasedUnitType = readString(in);
            out.add(new UnitPurchaseClientBoundPacket(UUID.fromString(purchasingPlayerUUID), purchasedUnitX,
                    purchasedUnitY, purchasedUnitType));
            break;
        }
    }
}

From source file:com.seventh_root.ld33.server.network.LD33ServerBoundPacketDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    while (in.isReadable()) {
        int id = in.readInt();
        switch (id) {
        case 0:/*from  ww w .  j a va  2 s  . c  om*/
            byte[] encodedPublicKey = new byte[in.readInt()];
            in.readBytes(encodedPublicKey);
            out.add(new PublicKeyServerBoundPacket(encodedPublicKey));
            break;
        case 1:
            String loggingInPlayerName = readString(in);
            byte[] encryptedPassword = new byte[in.readInt()];
            in.readBytes(encryptedPassword);
            boolean signUp = in.readBoolean();
            out.add(new PlayerLoginServerBoundPacket(loggingInPlayerName, encryptedPassword, signUp));
            break;
        case 2:
            out.add(new PlayerJoinServerBoundPacket());
            break;
        case 3:
            out.add(new PlayerQuitServerBoundPacket());
            break;
        case 4:
            String loginResponseMessage = readString(in);
            boolean loginSuccess = in.readBoolean();
            out.add(new PlayerLoginResponseServerBoundPacket(loginResponseMessage, loginSuccess));
            break;
        case 5:
            String spawningUnitUUID = readString(in);
            String spawningUnitPlayerUUID = readString(in);
            int spawningUnitX = in.readInt();
            int spawningUnitY = in.readInt();
            String spawningUnitType = readString(in);
            long spawningUnitCompletionTime = in.readLong();
            Unit spawningUnit;
            switch (spawningUnitType) {
            case "wall":
                spawningUnit = new Wall(UUID.fromString(spawningUnitUUID),
                        Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)),
                        server.getWorld().getTileAt(spawningUnitX, spawningUnitY), spawningUnitCompletionTime);
                break;
            case "dragon":
                spawningUnit = new Dragon(UUID.fromString(spawningUnitUUID),
                        Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)),
                        server.getWorld().getTileAt(spawningUnitX, spawningUnitY), spawningUnitCompletionTime);
                break;
            case "flag":
                spawningUnit = new Flag(UUID.fromString(spawningUnitUUID),
                        Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)),
                        server.getWorld().getTileAt(spawningUnitX, spawningUnitY), spawningUnitCompletionTime);
                break;
            default:
                spawningUnit = null;
                break;
            }
            out.add(new UnitSpawnServerBoundPacket(spawningUnit));
            break;
        case 6:
            String movingUnitUUID = readString(in);
            int movingUnitTargetX = in.readInt();
            int movingUnitTargetY = in.readInt();
            out.add(new UnitMoveServerBoundPacket(Unit.getByUUID(server.getDatabaseConnection(),
                    server.getWorld(), UUID.fromString(movingUnitUUID)), movingUnitTargetX, movingUnitTargetY));
            break;
        case 7:
            String chatMessage = readString(in);
            out.add(new ChatMessageServerBoundPacket(chatMessage));
            break;
        case 8:
            int purchasedUnitX = in.readInt();
            int purchasedUnitY = in.readInt();
            String purchasedUnitType = readString(in);
            out.add(new UnitPurchaseServerBoundPacket(purchasedUnitX, purchasedUnitY, purchasedUnitType));
            break;
        }
    }
}