List of usage examples for io.netty.buffer ByteBuf readShort
public abstract short readShort();
From source file:org.virtue.network.protocol.Stage.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { if (buf.isReadable()) { switch (state()) { case READ_OPCODE: opcode = (buf.readByte() - cipher.nextInt()) & 0xFF; //System.out.println("Received opcode: " + opcode); checkpoint(Stage.READ_LENGTH); break; case READ_LENGTH: //System.out.println("Getting length of opcode: " + opcode); length = Constants.PACKET_SIZES[opcode]; switch (length) { case -1: if (buf.isReadable()) { length = buf.readByte() & 0xff; }/*from w ww . j ava 2 s.com*/ break; case -2: if (buf.readableBytes() >= 2) { length = buf.readShort() & 0xffff; } break; } checkpoint(Stage.FINALIZE); break; case FINALIZE: if (buf.readableBytes() >= length) { byte[] payload = new byte[length]; buf.readBytes(payload, 0, length); out.add(new GameEventMessage(opcode, new InboundBuffer(payload))); } checkpoint(Stage.READ_OPCODE); break; } } }
From source file:org.virtue.network.protocol.social.SocialNetworkDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { int size = buf.readShort() & 0xFFFF; if (buf.readableBytes() < size) throw new IllegalStateException("Not enough readable bytes from buffer!"); int version = buf.readInt(); int subVersion = buf.readInt(); if (version != Constants.FRAME_MAJOR && subVersion != Constants.FRAME_MINOR) throw new IllegalStateException("Invalid client version/sub-version!"); //if (type.equals(LoginTypeMessage.LOGIN_WORLD)) //buf.readByte();//TODO if its world login, client does send this byte. however social doesnt send the login type int secureBufferSize = buf.readShort() & 0xFFFF; if (buf.readableBytes() < secureBufferSize) throw new IllegalStateException("Not enough readable bytes from buffer."); byte[] secureBytes = new byte[secureBufferSize]; buf.readBytes(secureBytes);/*from w ww. j a v a 2s . co m*/ ByteBuf secureBuffer = Unpooled.wrappedBuffer(new BigInteger(secureBytes) .modPow(Constants.getLoginKey(), Constants.getLoginModulus()).toByteArray()); secureBuffer.readByte(); secureBuffer.skipBytes(4); secureBuffer.readByte(); secureBuffer.readByte();//int langID = secureBuffer.readInt(); int[] random = new int[5]; for (int index = 0; index < random.length; index++) { random[index] = secureBuffer.readInt(); } secureBuffer.readLong(); secureBuffer.readByte(); secureBuffer.readByte(); ctx.channel().writeAndFlush(Unpooled.buffer().writeByte(0).writeByte(1).writeByte(2)); }
From source file:pl.tcs.btppllib.telegrams.OcitResponseTelegram.java
public OcitResponseTelegram fromByteBuf(ByteBuf buf) { buf.readerIndex(0);/* ww w . j av a2 s .c om*/ setHdrLen(buf.readByte()); setOpts(buf.readByte()); setJobTime(buf.readShort()); setJobTimeCnt(buf.readShort()); setMember(buf.readShort()); setOtype(buf.readShort()); setMethod(buf.readShort()); setZnr(buf.readShort()); setFnr(buf.readShort()); setRetcode(buf.readShort()); log.debug(String.format("znr=%d fnr=%d member=%d otype=%d method=%d", getZnr(), getFnr(), getMember(), getOtype(), getMethod())); return this; }
From source file:qunar.tc.qmq.metainfoclient.MetaInfoClientHandler.java
License:Apache License
private static BrokerCluster deserializeBrokerCluster(ByteBuf buf) { final int brokerGroupSize = buf.readShort(); final List<BrokerGroup> brokerGroups = new ArrayList<>(brokerGroupSize); for (int i = 0; i < brokerGroupSize; i++) { final BrokerGroup brokerGroup = new BrokerGroup(); brokerGroup.setGroupName(PayloadHolderUtils.readString(buf)); brokerGroup.setMaster(PayloadHolderUtils.readString(buf)); brokerGroup.setUpdateTime(buf.readLong()); final int brokerStateCode = buf.readByte(); final BrokerState brokerState = BrokerState.codeOf(brokerStateCode); brokerGroup.setBrokerState(brokerState); brokerGroups.add(brokerGroup);/*ww w.j a v a2s . co m*/ } return new BrokerCluster(brokerGroups); }
From source file:qunar.tc.qmq.netty.DecodeHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list) throws Exception { if (in.readableBytes() < RemotingHeader.MIN_HEADER_SIZE + RemotingHeader.LENGTH_FIELD) return;//from ww w . ja v a2s . com int magicCode = in.getInt(in.readerIndex() + RemotingHeader.LENGTH_FIELD); if (DEFAULT_MAGIC_CODE != magicCode) { throw new IOException("Illegal Data, MagicCode=" + Integer.toHexString(magicCode)); } in.markReaderIndex(); int total = in.readInt(); if (in.readableBytes() < total) { in.resetReaderIndex(); return; } short headerSize = in.readShort(); RemotingHeader remotingHeader = decodeHeader(in); int bodyLength = total - headerSize - RemotingHeader.HEADER_SIZE_LEN; RemotingCommand remotingCommand = new RemotingCommand(); //because netty(lower version) has memory leak when ByteBuf cross thread //We can ensure server use high version netty, bu we can't ensure client if (isServer) { ByteBuf bodyData = in.readSlice(bodyLength); bodyData.retain(); remotingCommand.setBody(bodyData); } else { ByteBuf bodyData = Unpooled.buffer(bodyLength, bodyLength); in.readBytes(bodyData, bodyLength); remotingCommand.setBody(bodyData); } remotingCommand.setHeader(remotingHeader); list.add(remotingCommand); }
From source file:qunar.tc.qmq.netty.DecodeHandler.java
License:Apache License
private RemotingHeader decodeHeader(ByteBuf in) { RemotingHeader remotingHeader = new RemotingHeader(); // int magicCode(4 bytes) remotingHeader.setMagicCode(in.readInt()); // short code remotingHeader.setCode(in.readShort()); // short version remotingHeader.setVersion(in.readShort()); // int opaque remotingHeader.setOpaque(in.readInt()); // int flag/*from ww w . j a v a 2s . co m*/ remotingHeader.setFlag(in.readInt()); // int requestCode remotingHeader.setRequestCode(in.readShort()); return remotingHeader; }
From source file:qunar.tc.qmq.protocol.consumer.PullRequestSerde.java
License:Apache License
private PullFilter readFilter(final ByteBuf in) { final short typeCode = in.readShort(); final PullFilterType type = PullFilterType.fromCode(typeCode); switch (type) { case TAG:/*from w w w . jav a 2 s . c om*/ return readTagPullFilter(in); case SUB_ENV_ISOLATION: return readSubEnvMatchPullFilter(in); default: throw new RuntimeException("unsupported pull filter type " + type); } }
From source file:qunar.tc.qmq.protocol.consumer.PullRequestSerde.java
License:Apache License
private PullFilter readTagPullFilter(final ByteBuf in) { final int tagTypeCode = in.readShort(); final byte tagSize = in.readByte(); final List<byte[]> tags = new ArrayList<>(tagSize); for (int i = 0; i < tagSize; i++) { final int len = in.readShort(); final byte[] bs = new byte[len]; in.readBytes(bs);/* w w w.ja v a 2 s . co m*/ tags.add(bs); } if (tagTypeCode == TagType.NO_TAG.getCode()) { return null; } else { return new TagPullFilter(tagTypeCode, tags); } }
From source file:qunar.tc.qmq.utils.PayloadHolderUtils.java
License:Apache License
public static String readString(ByteBuf in) { int len = in.readShort(); byte[] bs = new byte[len]; in.readBytes(bs);//w w w .j av a2 s .c o m return CharsetUtils.toUTF8String(bs); }
From source file:qunar.tc.qmq.utils.PayloadHolderUtils.java
License:Apache License
public static Map<String, String> readStringMap(ByteBuf in, Map<String, String> map) { short size = in.readShort(); for (int i = 0; i < size; i++) { map.put(readString(in), readString(in)); }/* w w w . j a v a 2 s. co m*/ return map; }