List of usage examples for io.netty.buffer ByteBuf readUnsignedByte
public abstract short readUnsignedByte();
From source file:com.rs3e.network.protocol.codec.js5.UpdateDecoder.java
License:Open Source License
@Override public Object decode(ChannelHandlerContext ctx, ByteBuf buf) throws Exception { if (buf.readableBytes() < 6) return null; if (updateStage == UpdateStage.READ_VERSION) { updateStage = UpdateStage.READ_REQUEST; int length = buf.readUnsignedByte(); if (buf.readableBytes() >= length) { int version = buf.readInt(); int subVersion = buf.readInt(); String key = ByteBufUtils.readString(buf); return new UpdateVersionMessage(version, subVersion, key); }// w w w.j a v a2 s .co m } else { int opcode = buf.readUnsignedByte(); if (opcode == 0 || opcode == 1) { int type = buf.readUnsignedByte(); int file = buf.readInt(); return new FileRequest(opcode == 1, type, file); } else if (opcode == 4) { int key = buf.readUnsignedByte(); buf.readerIndex(buf.readerIndex() + 2); return new UpdateEncryptionMessage(key); } else { buf.readerIndex(buf.readerIndex() + 5); return null; } } return null; }
From source file:com.rs3e.network.protocol.codec.js5.UpdateEncoder.java
License:Open Source License
@Override public void encode(ChannelHandlerContext ctx, FileResponse response, ByteBuf buf) throws Exception { ByteBuf container = response.getContainer(); int type = response.getType(); int file = response.getFile(); int compression = container.readUnsignedByte(); int size = ((container.readByte() & 0xff) << 24) + ((container.readByte() & 0xff) << 16) + ((container.readByte() & 0xff) << 8) + (container.readByte() & 0xff); if (!response.isPriority()) { file |= 0x80000000;/* w w w .j av a 2s . c o m*/ } buf.writeByte(type); buf.writeInt(file); buf.writeByte(compression); buf.writeInt(size); int bytes = container.readableBytes(); if (bytes > 502) { bytes = 502; } buf.writeBytes(container.readBytes(bytes)); for (;;) { bytes = container.readableBytes(); if (bytes == 0) { break; } else if (bytes > 507) { bytes = 507; } buf.writeByte(type); buf.writeInt(file); buf.writeBytes(container.readBytes(bytes)); } }
From source file:com.rs3e.network.protocol.codec.js5.XorEncoder.java
License:Open Source License
@Override public void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) { while (in.readable()) { out.writeByte(in.readUnsignedByte() ^ key); }/*from w w w. j a va2 s. com*/ }
From source file:com.rs3e.network.protocol.codec.login.LoginDecoder.java
License:Open Source License
/** * Decodes the payload of login./* w w w . j a va 2s . co 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.codec.login.LoginDecoder.java
License:Open Source License
private Object decodeConnectionType(ByteBuf buffer) { int loginType = buffer.readUnsignedByte(); if (loginType != 16 && loginType != 18 && loginType != 19) { return new LoginResponse(LoginResponse.BAD_LOGIN_PACKET); // throw new ProtocolException("Invalid login opcode: " + // loginType); }/* w ww .ja va2 s . com*/ currentLoginType = loginType == 19 ? LoginType.LOBBY : LoginType.GAME; loginSize = buffer.readShort() & 0xFFFF; state = LoginState.CLIENT_DETAILS; return null; }
From source file:com.rs3e.network.protocol.login.LoginDecoder.java
License:Open Source License
/** * Decodes the payload of login.//from ww w. ja v a2 s .c om * @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.protocol.login.LoginDecoder.java
License:Open Source License
private Object decodeConnectionType(ByteBuf buffer) { int loginType = buffer.readUnsignedByte(); if (loginType != 16 && loginType != 18 && loginType != 19) { return new LoginResponse(LoginResponse.BAD_LOGIN_PACKET); //throw new ProtocolException("Invalid login opcode: " + loginType); }//from w ww .j a va 2s .c o m currentLoginType = loginType == 19 ? LoginType.LOBBY : LoginType.GAME; loginSize = buffer.readShort() & 0xFFFF; state = LoginState.CLIENT_DETAILS; return null; }
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 a va 2 s .co m } 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.rs3e.utility.ByteBufUtils.java
License:Open Source License
public static String readString(ByteBuf buffer) { buffer.markReaderIndex();//from ww w . ja v a 2 s . c o m int len = 0; while (buffer.readUnsignedByte() != 0) len++; buffer.resetReaderIndex(); byte[] bytes = new byte[len]; buffer.readBytes(bytes); buffer.readerIndex(buffer.readerIndex() + 1); return new String(bytes, Charsets.ASCII_LATIN1_CHARSET); }
From source file:com.seagate.kinetic.common.protocol.codec.KineticDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { in.markReaderIndex();/*from www .ja va 2 s . c om*/ // Wait until the length prefix is available // (magic ('F') + proto-msg-size + value-size) if (in.readableBytes() < 9) { in.resetReaderIndex(); return; } // 1. Read magic number. int magicNumber = in.readUnsignedByte(); if (magicNumber != 'F') { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid magic number: " + magicNumber); } // 2. protobuf message size int protoMessageLength = in.readInt(); // 3. attched value size int attachedValueLength = in.readInt(); // wait until whole message is available if (in.readableBytes() < (protoMessageLength + attachedValueLength)) { in.resetReaderIndex(); return; } // 4. read protobuf message byte[] decoded = new byte[protoMessageLength]; in.readBytes(decoded); // kinetic message KineticMessage km = new KineticMessage(); // construct protobuf message Message.Builder mbuilder = Message.newBuilder(); try { mbuilder.mergeFrom(decoded); } catch (Exception e) { in.resetReaderIndex(); logger.log(Level.WARNING, e.getMessage(), e); throw new RuntimeException(e); } // 5. read attched value if any if (attachedValueLength > 0) { // construct byte[] byte[] attachedValue = new byte[attachedValueLength]; // read from buffer in.readBytes(attachedValue); // set to message // mbuilder.setValue(ByteString.copyFrom(attachedValue)); km.setValue(attachedValue); } Message message = mbuilder.build(); km.setMessage(message); // get command bytes ByteString commandBytes = message.getCommandBytes(); // build command Command.Builder commandBuilder = Command.newBuilder(); try { commandBuilder.mergeFrom(commandBytes); km.setCommand(commandBuilder.build()); } catch (InvalidProtocolBufferException e) { logger.log(Level.WARNING, e.getMessage(), e); } // the whole message out.add(km); // print inbound message if (printMessage) { logger.info("Inbound protocol message: "); String printMsg = ProtocolMessageUtil.toString(km); logger.info(printMsg); } }