List of usage examples for io.netty.buffer ByteBufInputStream readByte
@Override public byte readByte() throws IOException
From source file:org.apache.drill.exec.rpc.RpcDecoder.java
License:Apache License
public static int readRawVarint32(ByteBufInputStream is) throws IOException { byte tmp = is.readByte(); if (tmp >= 0) { return tmp; }/*from w w w . j a v a2 s .co m*/ int result = tmp & 0x7f; if ((tmp = is.readByte()) >= 0) { result |= tmp << 7; } else { result |= (tmp & 0x7f) << 7; if ((tmp = is.readByte()) >= 0) { result |= tmp << 14; } else { result |= (tmp & 0x7f) << 14; if ((tmp = is.readByte()) >= 0) { result |= tmp << 21; } else { result |= (tmp & 0x7f) << 21; result |= (tmp = is.readByte()) << 28; if (tmp < 0) { // Discard upper 32 bits. for (int i = 0; i < 5; i++) { if (is.readByte() >= 0) { return result; } } throw new CorruptedFrameException("Encountered a malformed varint."); } } } } return result; }
From source file:org.apache.giraph.comm.netty.handler.SaslClientHandler.java
License:Apache License
/** * Decode the message read by handler/*from w w w. ja v a 2 s .c om*/ * * @param ctx channel handler context * @param msg message to decode into a writable request * @return decoded writablerequest object * @throws Exception */ protected WritableRequest decode(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof ByteBuf)) { throw new IllegalStateException("decode: Got illegal message " + msg); } // Decode msg into an object whose class C implements WritableRequest: // C will be either SaslTokenMessage or SaslComplete. // // 1. Convert message to a stream that can be decoded. ByteBuf buf = (ByteBuf) msg; ByteBufInputStream inputStream = new ByteBufInputStream(buf); // 2. Get first byte: message type: int enumValue = inputStream.readByte(); RequestType type = RequestType.values()[enumValue]; if (LOG.isDebugEnabled()) { LOG.debug("decode: Got a response of type " + type + " from server:" + ctx.channel().remoteAddress()); } // 3. Create object of the type determined in step 2. Class<? extends WritableRequest> writableRequestClass = type.getRequestClass(); WritableRequest serverResponse = ReflectionUtils.newInstance(writableRequestClass, conf); // 4. Deserialize the inputStream's contents into the newly-constructed // serverResponse object. try { serverResponse.readFields(inputStream); } catch (IOException e) { LOG.error("decode: Exception when trying to read server response: " + e); } ReferenceCountUtil.release(buf); // serverResponse can now be used in the next stage in pipeline. return serverResponse; }
From source file:vn.com.vng.gsmobile.casino.client.ClientHandler.java
License:Apache License
@SuppressWarnings({ "rawtypes" }) private void receiptFormServer(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception { ByteBufInputStream bbi = new ByteBufInputStream(frame.content()); byte c1 = bbi.readByte(); byte c2 = bbi.readByte(); byte c3 = bbi.readByte(); byte c4 = bbi.readByte(); if (c4 == 0) { int gzip = ((int) c3 & (int) 0x80) >> 7; int version = (int) c3 & (int) 0x7F; String sCMD = c1 + "_" + c2 + "_" + version; Map mService = (Map) Lib.getServiceConfig(false).get(sCMD); if (mService != null) { //1. Gii nn byte[] b = null; if (gzip == 1) b = GZip.decompress(IOUtils.toByteArray(bbi)); else// w w w . j av a 2 s .c o m b = IOUtils.toByteArray(bbi); //2. Gii m byte[] b1 = null; // Object oSecKey = mService.get("server_seckey"); // if("1".equals(Const.SESSION_KEY) && "1".equals(oSecKey)){ // //Giai ma data request // Session s = new Session(sUID); // String sk = s.hget(Session.SECKEY); // if(sk !=null){ // b1 = XXTEA.decrypt(b, sk.getBytes()); // } // } ByteBuffer bb = ByteBuffer.wrap(b1 == null ? b : b1); Object oData = null; Object oServer = mService.get("server"); if (oServer != null) { Class<?> clsRequest = Class.forName(mService.get("server").toString()); try { oData = Lib.invoke(clsRequest, "getRootAs" + clsRequest.getSimpleName(), new Class<?>[] { ByteBuffer.class }, new Object[] { bb }); } catch (Exception e) { oData = new String(b, Charset.forName("UTF-8")); } } Lib.getLogService().debug(Arrays.asList(uid, ctx.channel().id(), c1, c2, c3, c4, oData.toString(), this.getClass().getSimpleName() + ".receiptFormServer")); Bot bot = Bot.list.get(uid); FlatBufferBuilder builder = new FlatBufferBuilder(0); Table data = null; Integer state = null; Integer turnIdx = null; Integer playIdx = null; Long battleId = null; Long roomId = null; CMD cmd = CMD.getCMD(c1, c2, (byte) version); switch (cmd) { case ROOM_WAIT: RoomResponse rs = (RoomResponse) oData; if (rs != null) { builder.finish(CMDJoinGame.createCMDJoinGame(builder, uid, rs.room().id(), JoinGameType.PLAY, builder.createString("trid"), 0)); data = CMDJoinGame.getRootAsCMDJoinGame(builder.dataBuffer()); bot.write(CMD.JOIN_ROOM, data); } break; case INVITE_ROOM: CMDInviteGame iv = (CMDInviteGame) oData; builder.finish(CMDJoinGame.createCMDJoinGame(builder, uid, iv.roomId(), JoinGameType.PLAY, builder.createString("trid"), 0)); data = CMDJoinGame.getRootAsCMDJoinGame(builder.dataBuffer()); bot.write(CMD.JOIN_ROOM, data); break; case JOIN_ROOM: RoomResponse rs2 = (RoomResponse) oData; if (rs2 != null) { bot.setRoomId(rs2.room().id()); } break; case QUIT_ROOM: CMDQuitGame qg = (CMDQuitGame) oData; if (qg.state() == QuitState.QUIT_ACCEPT || qg.state() == QuitState.QUIT_NOT_ENOUGH_MONEY || qg.state() == QuitState.QUIT_POOR_CONNECTION) bot.setRoomId(null); break; case TLMN_UPDATE_BATTLE_INFO: case TLMN_BATTLE_INFO: GameTLMNInfo gif = (GameTLMNInfo) oData; roomId = gif.roomId(); battleId = gif.gameId(); state = gif.state(); turnIdx = gif.turnIdx(); playIdx = gif.playerIdx(); if (state == GameRoomState.Finished) { bot.setPlayerIdx(null); } else if (state == GameRoomState.Playing) { for (int j = 0; j < gif.cardListLength(); j++) { if (gif.cardList(j).playerId() == bot.getId()) { bot.setPlayerIdx(j); break; } } } //skip turn here if (bot.getPlayerIdx() == playIdx && state == GameRoomState.Playing) { builder.finish( CMDGameTLMNSkip.createCMDGameTLMNSkip(builder, roomId, battleId, turnIdx, playIdx)); data = CMDGameTLMNSkip.getRootAsCMDGameTLMNSkip(builder.dataBuffer()); bot.write(CMD.TLMN_SKIP_TURN, data); } break; case TLMN_UPDATE_BATTLE_INFO_TINY: CMDGameTLMNUpdate gup = (CMDGameTLMNUpdate) oData; roomId = gup.roomId(); battleId = gup.gameId(); state = gup.state(); turnIdx = gup.nextTurnIdx(); playIdx = gup.nextPlayerIdx(); synchronized (bot) { bot.wait(2000); } //skip turn here if (bot.getPlayerIdx() == playIdx && state == GameRoomState.Playing) { builder.finish( CMDGameTLMNSkip.createCMDGameTLMNSkip(builder, roomId, battleId, turnIdx, playIdx)); data = CMDGameTLMNSkip.getRootAsCMDGameTLMNSkip(builder.dataBuffer()); bot.write(CMD.TLMN_SKIP_TURN, data); } break; case CAO_BATTLE_INFO: Game3LaGameInfo gif3 = (Game3LaGameInfo) oData; roomId = gif3.roomId(); battleId = gif3.gameId(); state = gif3.state(); if (state == GameRoomState.Finished) { bot.setPlayerIdx(null); } else if (state == GameRoomState.Playing) { for (int j = 0; j < gif3.cardListLength(); j++) { if (gif3.cardList(j).playerId() == bot.getId()) { bot.setPlayerIdx(j); break; } } } if (state == GameRoomState.Playing) { synchronized (bot) { bot.wait(2000); } builder.finish(CMDGame3LaShowCard.createCMDGame3LaShowCard(builder, roomId, battleId, bot.getPlayerIdx(), CMDGame3LaShowCard.createShowedCardsVector(builder, new byte[] { 0, 1, 2 }))); data = CMDGameTLMNSkip.getRootAsCMDGameTLMNSkip(builder.dataBuffer()); bot.write(CMD.CAO_SHOW_CARDS, data); } break; default: break; } } else { Lib.getLogService() .debug(Arrays.asList(uid, ctx.channel().id(), c1, c2, c3, c4, Hex.encodeHexString(IOUtils.toString(bbi).getBytes()), this.getClass().getSimpleName() + ".receiptFormServer")); } } else Lib.getLogService().debug(Arrays.asList(uid, ctx.channel().id(), c1, c2, c3, c4, Lib.getErrorMessage(c4), this.getClass().getSimpleName() + ".receiptFormServer")); }