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:net.hasor.rsf.remoting.transport.netty.RSFProtocolDecoder.java

License:Apache License

protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }/* w  w  w.  j  a  va2s.co m*/
    //
    //* byte[1]  version                              RSF(0xC1)
    byte version = frame.getByte(0);
    short status = 0;
    //decode
    try {
        status = this.doDecode(version, ctx, frame);//???
    } catch (Throwable e) {
        status = ProtocolStatus.ProtocolError;
    } finally {
        if (status == ProtocolStatus.OK)
            return null;
        /*                    */
        frame = frame.resetReaderIndex().skipBytes(1);
        this.fireProtocolError(ctx, version, frame.readLong(), ProtocolStatus.ProtocolError);
    }
    return null;
}

From source file:net.hasor.rsf.remoting.transport.protocol.codec.RpcRequestProtocol.java

License:Apache License

/**decode stream to object*/
public RequestSocketBlock decode(ByteBuf buf) throws IOException {
    //* --------------------------------------------------------bytes =13
    //* byte[1]  version                              RSF(0xC1)
    byte version = buf.readByte();
    //* byte[8]  requestID                            ?ID
    long requestID = buf.readLong();
    //* byte[1]  keepData                             ?
    buf.skipBytes(1);/*  ww w . j  av  a 2s  .c  om*/
    //* byte[3]  contentLength                        ?
    buf.skipBytes(3);//.readUnsignedMedium()
    //
    RequestSocketBlock req = new RequestSocketBlock();
    req.setVersion(version);
    req.setRequestID(requestID);
    //* --------------------------------------------------------bytes =14
    //* byte[2]  servicesName-(attr-index)            ???
    req.setServiceName(buf.readShort());
    //* byte[2]  servicesGroup-(attr-index)           ?
    req.setServiceGroup(buf.readShort());
    //* byte[2]  servicesVersion-(attr-index)         ?
    req.setServiceVersion(buf.readShort());
    //* byte[2]  servicesMethod-(attr-index)          ???
    req.setTargetMethod(buf.readShort());
    //* byte[2]  serializeType-(attr-index)           ?
    req.setSerializeType(buf.readShort());
    //* byte[4]  clientTimeout                        
    req.setClientTimeout(buf.readInt());
    //* --------------------------------------------------------bytes =1 ~ 1021
    //* byte[1]  paramCount                           ?
    byte paramCount = buf.readByte();
    for (int i = 0; i < paramCount; i++) {
        //* byte[4]  ptype-0-(attr-index,attr-index)  ?
        int mergeData = buf.readInt();
        req.addParameter(mergeData);
    }
    //* --------------------------------------------------------bytes =1 ~ 1021
    //* byte[1]  optionCount                          ?
    byte optionCount = buf.readByte();
    for (int i = 0; i < optionCount; i++) {
        //* byte[4]  attr-0-(attr-index,attr-index)   ?
        int mergeData = buf.readInt();
        req.addOption(mergeData);
    }
    //* --------------------------------------------------------bytes =6 ~ 8192
    //* byte[2]  attrPool-size (Max = 2047)           ?
    short attrPoolSize = buf.readShort();
    for (int i = 0; i < attrPoolSize; i++) {
        //* byte[4] att-length                        1?
        int length = buf.readInt();
        req.addPoolData(length);
    }
    //* --------------------------------------------------------bytes =n
    //* dataBody                                      ?
    req.fillFrom(buf.readBytes(req.getPoolSize()));
    return req;
}

From source file:net.hasor.rsf.remoting.transport.protocol.codec.RpcResponseProtocol.java

License:Apache License

/**decode stream to object*/
public ResponseSocketBlock decode(ByteBuf buf) throws IOException {
    //* --------------------------------------------------------bytes =13
    //* byte[1]  version                              RSF(0x80)
    byte version = buf.readByte();
    //* byte[8]  requestID                            ?ID
    long requestID = buf.readLong();
    //* byte[1]  keepData                             ?
    buf.skipBytes(1);/*from  ww  w. j a v a 2  s  . com*/
    //* byte[3]  contentLength                        ?
    buf.skipBytes(3);//.readUnsignedMedium()
    //
    ResponseSocketBlock res = new ResponseSocketBlock();
    res.setVersion(version);
    res.setRequestID(requestID);
    //* --------------------------------------------------------bytes =8
    //* byte[2]  status                               ??
    res.setStatus(buf.readShort());
    //* byte[2]  serializeType-(attr-index)           ?
    res.setSerializeType(buf.readShort());
    //* byte[2]  returnType-(attr-index)              
    res.setReturnType(buf.readShort());
    //* byte[2]  returnData-(attr-index)              ?
    res.setReturnData(buf.readShort());
    //* --------------------------------------------------------bytes =1 ~ 1021
    //* byte[1]  optionCount                          ?
    byte optionCount = buf.readByte();
    for (int i = 0; i < optionCount; i++) {
        //* byte[4]  attr-0-(attr-index,attr-index)   ?
        int mergeData = buf.readInt();
        res.addOption(mergeData);
    }
    //* --------------------------------------------------------bytes =6 ~ 8192
    //* byte[2]  attrPool-size (Max = 2047)           ?
    short attrPoolSize = buf.readShort();
    for (int i = 0; i < attrPoolSize; i++) {
        //* byte[4] att-length                        1?
        int length = buf.readInt();
        res.addPoolData(length);
    }
    //* --------------------------------------------------------bytes =n
    //* dataBody                                      ?
    res.fillFrom(buf.readBytes(res.getPoolSize()));
    return res;
}

From source file:net.ieldor.modules.login.LoginManager.java

License:Open Source License

public Player runGameLogin(ByteBuf buffer, Channel channel, ChannelHandlerContext context) {
    boolean unknownEquals14 = buffer.readUnsignedByte() == 1;
    int secureBufferSize = buffer.readShort() & 0xFFFF;
    if (buffer.readableBytes() < secureBufferSize) {
        channel.write(new LoginResponse(LoginResponse.BAD_SESSION));
        //session.getLoginPackets().sendClientPacket(10);
        return null;
    }/*w  w  w.j a  v a 2  s . c  o  m*/
    byte[] secureBytes = new byte[secureBufferSize];
    buffer.readBytes(secureBytes);

    ByteBuf secureBuffer = Unpooled.wrappedBuffer(
            new BigInteger(secureBytes).modPow(Constants.JS5PrivateKey, Constants.JS5ModulusKey).toByteArray());
    /*byte[] data = new byte[rsaBlockSize];
    buffer.readBytes(data, 0, rsaBlockSize);
    InputStream rsaStream = new InputStream(Utils.cryptRSA(data, Settings.PRIVATE_EXPONENT, Settings.MODULUS));*/
    int blockOpcode = secureBuffer.readUnsignedByte();

    if (blockOpcode != 10) {
        channel.write(new LoginResponse(LoginResponse.BAD_LOGIN_PACKET));
        //session.getLoginPackets().sendClientPacket(10);
        return null;
    }

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

    long vHash = secureBuffer.readLong();
    if (vHash != 0L) {// rsa block check, pass part
        channel.write(new LoginResponse(LoginResponse.BAD_LOGIN_PACKET));
        return null;
    }

    String password = ByteBufUtils.readString(secureBuffer);
    System.out.println("Password: " + password);
    //TODO: Implement password encryption
    //password = Encrypt.encryptSHA1(password);
    //System.out.println("Found password: "+password);
    long[] loginSeeds = new long[2];
    for (int seed = 0; seed < loginSeeds.length; seed++) {
        loginSeeds[seed] = secureBuffer.readLong();
    }

    byte[] xteaBlock = new byte[buffer.readableBytes()];
    System.out.println("Xtea size: " + xteaBlock.length);
    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());
    System.out.println("Username: " + username);
    int displayMode = xteaBuffer.readUnsignedByte();
    int screenWidth = xteaBuffer.readUnsignedShort();
    int screenHeight = xteaBuffer.readUnsignedShort();
    int unknown2 = xteaBuffer.readUnsignedByte();

    xteaBuffer.skip(24); // 24bytes directly from a file, no idea whats there

    String settings = xteaBuffer.readString();
    int affid = xteaBuffer.readInt();
    int indexFiles = xteaBuffer.readByte() & 0xff;

    int[] crcValues = new int[indexFiles];
    int crcCount = xteaBuffer.readUnsignedByte();
    for (int i = 1; i < crcValues.length; i++) {
        crcValues[i] = xteaBuffer.readUnsignedByte();
    }

    @SuppressWarnings("unused")
    MachineData data = new MachineData(xteaBuffer);

    xteaBuffer.readInt();// Packet receive count
    xteaBuffer.readInt();//Unknown
    xteaBuffer.readInt();//Unknown
    xteaBuffer.readString();// Some param string (empty)
    boolean hasAditionalInformation = xteaBuffer.readUnsignedByte() == 1;
    if (hasAditionalInformation) {
        xteaBuffer.readString(); // aditionalInformation
    }
    boolean hasJagtheora = xteaBuffer.readUnsignedByte() == 1;
    boolean js = xteaBuffer.readUnsignedByte() == 1;
    boolean hc = xteaBuffer.readUnsignedByte() == 1;
    int unknown4 = xteaBuffer.readByte();
    int unknown5 = xteaBuffer.readInt();

    String serverToken = xteaBuffer.readString();
    if (!serverToken.equals(Constants.SERVER_TOKEN)) {
        System.out.println("Expected token: " + Constants.SERVER_TOKEN + ", found: " + serverToken);
        channel.write(new LoginResponse(LoginResponse.BAD_SESSION));
        return null;
    }
    boolean unknown7 = xteaBuffer.readUnsignedByte() == 1;

    for (int index = 0; index < crcCount; index++) {
        //int crc = CacheManager.STORE.getIndexes()[index] == null ? -1011863738 : CacheManager.STORE
        //      .getIndexes()[index].getCRC();
        int receivedCRC = xteaBuffer.readInt();
        /*if (crc != receivedCRC && index < 32) {
           Logger.log(this,
            "Invalid CRC at index: "+index+", "+receivedCRC+", "+crc);
           session.getLoginPackets().sendClientPacket(6);
           return;
        }*/
    }
    //TODO: Implement the following checks
    /*if (Utils.invalidAccountName(username)) {
       session.getLoginPackets().sendClientPacket(3);
       return;
    }
    if (World.getPlayers().size() >= Settings.PLAYERS_LIMIT - 10) {
       session.getLoginPackets().sendClientPacket(7);
       return;
    }
    if (World.containsPlayer(username)) {
       session.getLoginPackets().sendClientPacket(5);
       return;
    }
    if (AntiFlood.getSessionsIP(session.getIP()) > 3) {
       session.getLoginPackets().sendClientPacket(9);
       return;
    }*/
    LoadResult result = null;
    try {
        result = BinaryPlayerManager.loadPlayer(new LoginHandshakeMessage(username, password, context));
    } catch (IOException e) {
        channel.write(new LoginResponse(LoginResponse.ERROR_PROFILE_LOAD));
        return null;
    }
    if (result.getReturnCode() != LoginResponse.SUCCESS) {
        channel.write(new LoginResponse(result.getReturnCode()));
        return null;
    }
    Player player = result.getPlayer();
    player.initDisplayName();
    channel.write(new LoginConfigData(Constants.NIS_CONFIG, true));
    return player;
    /*player.init(session, username, displayMode, screenWidth, screenHeight, mInformation, new IsaacKeyPair(isaacKeys));
    session.getLoginPackets().sendLoginDetails(player);
    session.setDecoder(3, player);
    session.setEncoder(2, player);*/
    //player.start();
    /*Player player;
    if (!SerializableFilesManager.containsPlayer(username)) 
       player = new Player(password);
    else {
       player = SerializableFilesManager.loadPlayer(username);
       if (player == null) {
    session.getLoginPackets().sendClientPacket(20);
    return;
       }
       if (!SerializableFilesManager.createBackup(username)) {
    session.getLoginPackets().sendClientPacket(20);
    return;
       }
       if (!password.equals(player.getPassword())) {
    session.getLoginPackets().sendClientPacket(3);
    return;
       }
    }
    if (player.isPermBanned() || player.getBanned() > Utils.currentTimeMillis()) {
       session.getLoginPackets().sendClientPacket(4);
       return;
    }*/
}

From source file:net.ieldor.modules.login.LoginManager.java

License:Open Source License

public void runLobbyLogin(ByteBuf buffer, Channel channel, ChannelHandlerContext context) {
    int secureBufferSize = buffer.readShort() & 0xFFFF;
    if (buffer.readableBytes() < secureBufferSize) {
        channel.write(new LoginResponse(LoginResponse.BAD_LOGIN_PACKET));
        return;/*from w ww  .j ava  2  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);
    //System.out.println("Found password: "+password);
    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());
    //System.out.println("Found username: "+username);
    @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.readUnsignedByte();
    }
    @SuppressWarnings("unused")
    MachineData data = new MachineData(xteaBuffer);
    /*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)) {
        System.out.println("Expected token: " + Constants.SERVER_TOKEN + ", found: " + serverToken);
        channel.write(new LoginResponse(LoginResponse.BAD_SESSION));
        return;
    }

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

    LoadResult result = null;
    try {
        result = BinaryPlayerManager.loadPlayer(new LoginHandshakeMessage(username, password, context));
    } catch (IOException e) {
        channel.write(new LoginResponse(LoginResponse.ERROR_PROFILE_LOAD));
        return;
    }
    if (result.getReturnCode() != LoginResponse.SUCCESS) {
        channel.write(new LoginResponse(result.getReturnCode()));
        return;
    }
    Player player = result.getPlayer();
    player.initDisplayName();
    int rights = 0;
    long memberEndDate = 1420073999999L;
    int memberFlags = 0x1;//0x1 - if members, 0x2 - subscription
    int lastLoginDay = 0;
    int recoverySetDay = 4000;//The day on which recovery questions were last set
    int msgCount = 0;
    String loginIp = null;
    int emailStatus = 3;//email status (0 - no email, 1 - pending parental confirmation, 2 - pending confirmation, 3 - registered)
    String displayName = player.getDisplayName();
    WorldData defaultWorld = WorldList.DEFAULT_WORLD;

    LobbyLoginData lobbyData = new LobbyLoginData(rights, memberEndDate, memberFlags, lastLoginDay,
            recoverySetDay, msgCount, loginIp, emailStatus, displayName, defaultWorld);

    channel.write(lobbyData);
    player.lobbyLogin(context);
    /*Packet response = encodeLobbyResponse(player);
    channel.write(new LoginResponse(LoginResponse.SUCCESS, response.getPayload(), response.getLength()));
    */
}

From source file:net.ieldor.utility.flooder.FlooderChannelHandler.java

License:Open Source License

@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf buf) throws IOException {
    Channel channel = ctx.channel();

    if (state == State.READ_SERVER_SESSION_KEY) {
        if (buf.readableBytes() >= 9) {
            if (buf.readUnsignedByte() != 0)
                throw new IOException("expecting EXCHANGE_KEYS opcode");

            serverSessionKey = buf.readLong();

            ByteBuf payload = Unpooled.buffer();
            payload.writeInt(530);// w  w w. ja  v  a  2 s .c  o  m

            payload.writeByte(0);
            payload.writeByte(0);
            payload.writeByte(0);

            payload.writeByte(0);
            payload.writeShort(765);
            payload.writeShort(503);

            payload.writeByte(0);

            for (int i = 0; i < 24; i++)
                payload.writeByte(0);

            ByteBufUtils.writeString(payload, "kKmok3kJqOeN6D3mDdihco3oPeYN2KFy6W5--vZUbNA");

            payload.writeInt(0);
            payload.writeInt(0);
            payload.writeShort(0);

            for (int i = 0; i < 28; i++) {
                payload.writeInt(crc[i]);
            }

            payload.writeByte(10);
            payload.writeLong(clientSessionKey);
            payload.writeLong(serverSessionKey);
            payload.writeLong(encodedUsername);
            ByteBufUtils.writeString(payload, password);

            ByteBuf packet = Unpooled.buffer();
            packet.writeByte(18);
            packet.writeShort(payload.readableBytes());
            packet.writeBytes(payload);

            channel.write(packet);

            int[] seed = new int[4];
            seed[0] = (int) (clientSessionKey >> 32);
            seed[1] = (int) clientSessionKey;
            seed[2] = (int) (serverSessionKey >> 32);
            seed[3] = (int) serverSessionKey;

            state = State.READ_LOGIN_STATUS;
        }
    }

    if (state == State.READ_LOGIN_STATUS) {
        if (buf.readable()) {
            int status = buf.readUnsignedByte();
            if (status != 2)
                throw new IOException("expecting OK login response");

            state = State.READ_LOGIN_PAYLOAD;
        }
    }

    if (state == State.READ_LOGIN_PAYLOAD) {
        if (buf.readableBytes() >= 11) {
            buf.readerIndex(buf.readerIndex() + 11);

            state = State.READ_GAME_OPCODE;
        }
    }

    if (state == State.READ_GAME_OPCODE) {

    }
}

From source file:net.kuujo.copycat.io.transport.NettyConnection.java

License:Apache License

/**
 * Handles a request./*w  w  w.  ja va  2  s  . co  m*/
 */
void handleRequest(ByteBuf buffer) {
    long requestId = buffer.readLong();
    int address = buffer.readInt();
    HandlerHolder handler = handlers.get(address);
    if (handler != null) {
        Object request = readRequest(buffer);
        handler.context.executor().execute(() -> handleRequest(requestId, request, handler));
    }
}

From source file:net.malisis.advert.advert.ClientAdvert.java

License:Open Source License

public static ClientAdvert fromBytes(ByteBuf buf) {
    int id = buf.readInt();
    ClientAdvert advert = ClientAdvert.get(id, true);
    advert.setInfos(ByteBufUtils.readUTF8String(buf), ByteBufUtils.readUTF8String(buf));
    advert.setHash(ByteBufUtils.readUTF8String(buf));
    advert.setData(buf.readLong(), buf.readInt(), buf.readInt());

    return advert;
}

From source file:net.malisis.switches.tileentity.SwitchTileEntity.java

License:Open Source License

@Override
public void readFromNBT(NBTTagCompound tag) {
    super.readFromNBT(tag);
    ByteBuf bytes = Unpooled.copiedBuffer(tag.getByteArray("linkedPos"));
    for (int i = 0; i < bytes.capacity() / 8; i++)
        linkedPos.add(BlockPos.fromLong(bytes.readLong()));
}

From source file:net.tridentsdk.packets.play.in.PacketPlayInBlockPlace.java

License:Open Source License

@Override
public Packet decode(ByteBuf buf) {
    long encodedLocation = buf.readLong();

    this.location = new Location(null, (double) (encodedLocation >> 38), (double) (encodedLocation << 26 >> 52),
            (double) (encodedLocation << 38 >> 38));
    this.direction = buf.readByte();

    // ignore held item
    for (int i = 0; i < buf.readableBytes() - 3; i++) {
        buf.readByte();/*from   w w  w.  j av  a  2  s  .com*/
    }

    double x = (double) buf.readByte();
    double y = (double) buf.readByte();
    double z = (double) buf.readByte();

    this.cursorPosition = new Vector(x, y, z);
    return this;
}