private void readSettingsFrame( ChannelHandlerContext ctx, ByteBuf payload, Http2FrameListener listener) throws Http2Exception { if (flags.ack()) { listener.onSettingsAckRead(ctx); } else { int numSettings = payloadLength / SETTING_ENTRY_LENGTH; Http2Settings settings = new Http2Settings(); for (int index = 0; index < numSettings; ++index) { char id = (char) payload.readUnsignedShort(); long value = payload.readUnsignedInt(); try { settings.put(id, Long.valueOf(value)); } catch (IllegalArgumentException e) { switch (id) { case SETTINGS_MAX_FRAME_SIZE: throw connectionError(FRAME_SIZE_ERROR, e, e.getMessage()); case SETTINGS_INITIAL_WINDOW_SIZE: throw connectionError(FLOW_CONTROL_ERROR, e, e.getMessage()); default: throw connectionError(PROTOCOL_ERROR, e, e.getMessage()); } } } listener.onSettingsRead(ctx, settings); } }
private static void readGoAwayFrame( ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer) throws Http2Exception { int lastStreamId = readUnsignedInt(payload); long errorCode = payload.readUnsignedInt(); ByteBuf debugData = payload.readSlice(payload.readableBytes()); observer.onGoAwayRead(ctx, lastStreamId, errorCode, debugData); }
private void readPriorityFrame( ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer) throws Http2Exception { long word1 = payload.readUnsignedInt(); boolean exclusive = (word1 & 0x80000000L) > 0; int streamDependency = (int) (word1 & 0x7FFFFFFFL); short weight = (short) (payload.readUnsignedByte() + 1); observer.onPriorityRead(ctx, streamId, streamDependency, weight, exclusive); }
@Override public GetQueueConfigOutput deserialize(ByteBuf rawMessage) { GetQueueConfigOutputBuilder builder = new GetQueueConfigOutputBuilder(); builder.setVersion((short) EncodeConstants.OF10_VERSION_ID); builder.setXid((rawMessage.readUnsignedInt())); builder.setPort(new PortNumber((long) rawMessage.readUnsignedShort())); rawMessage.skipBytes(PADDING_IN_QUEUE_GET_CONFIG_REPLY_HEADER); builder.setQueues(createQueuesList(rawMessage)); return builder.build(); }
int decode(ByteBuf buf) { // ByteBuf bbuf = Unpooled.wrappedBuffer(buf); // ByteBuf tmpbuf = bbuf.readerIndex(pos); this.msgType = buf.readUnsignedInt(); this.seqNo = buf.readUnsignedByte(); this.msgLen = buf.readUnsignedShort(); this.flags = buf.readUnsignedByte(); return 0; }
@Override public Action deserialize(ByteBuf message) { ActionBuilder actionBuilder = deserializeHeader(message); ActionSetNshc1Builder builder = new ActionSetNshc1Builder(); NxActionSetNshc1Builder nxActionSetNspBuilder = new NxActionSetNshc1Builder(); message.skipBytes(padding); nxActionSetNspBuilder.setNshc(message.readUnsignedInt()); builder.setNxActionSetNshc1(nxActionSetNspBuilder.build()); actionBuilder.setActionChoice(builder.build()); return actionBuilder.build(); }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf m = (ByteBuf) msg; buf.writeBytes(m); m.release(); if (buf.readableBytes() >= 4) { long currentTimeMillis = (buf.readUnsignedInt() - 2208988800L) * 1000L; System.out.println(new Date(currentTimeMillis)); ctx.close(); } }
private static List<Queues> createQueuesList(ByteBuf input) { List<Queues> queuesList = new ArrayList<>(); while (input.readableBytes() > 0) { QueuesBuilder queueBuilder = new QueuesBuilder(); queueBuilder.setQueueId(new QueueId(input.readUnsignedInt())); int length = input.readUnsignedShort(); input.skipBytes(PADDING_IN_PACKET_QUEUE_HEADER); queueBuilder.setQueueProperty( createPropertiesList(input, length - PACKET_QUEUE_HEADER_LENGTH)); queuesList.add(queueBuilder.build()); } return queuesList; }
@Test public void testEncode() { MockRelayMessage message = new MockRelayMessage(); message.code(Unsigned16.get(100)); message.length(Unsigned32.get(36L)); message.flag(new FlagData(FlagImpl.REQUEST)); message.hopByHop(Integer64.get(1L)); message.endToEnd(Integer64.get(2L)); message.addAttribute(new IgnoredAttribute(new IgnoredData(new byte[] {1, 2, 3, 4}))); ByteBuf buf = MessageFactory.getInstance().generateByteBuf(message); Assert.assertEquals(message.version().get().byteValue(), buf.readUnsignedByte()); Assert.assertEquals(message.code().get().intValue(), buf.readUnsignedShort()); assertEquals(64, buf.readUnsignedByte()); Assert.assertEquals(message.length().get().intValue(), buf.readUnsignedInt()); assertEquals(message.hopByHop().get().longValue(), buf.readLong()); assertEquals(message.endToEnd().get().longValue(), buf.readLong()); assertEquals(0, buf.readUnsignedShort()); assertEquals(4, buf.readUnsignedInt()); assertEquals(1, buf.readByte()); assertEquals(2, buf.readByte()); assertEquals(3, buf.readByte()); assertEquals(4, buf.readByte()); }
private void readSettingsFrame( ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer) throws Http2Exception { if (flags.ack()) { observer.onSettingsAckRead(ctx); } else { int numSettings = payloadLength / SETTING_ENTRY_LENGTH; Http2Settings settings = new Http2Settings(); for (int index = 0; index < numSettings; ++index) { short id = payload.readUnsignedByte(); long value = payload.readUnsignedInt(); switch (id) { case SETTINGS_HEADER_TABLE_SIZE: if (value < 0 || value > Integer.MAX_VALUE) { throw protocolError("Invalid value for HEADER_TABLE_SIZE: %d", value); } settings.maxHeaderTableSize((int) value); break; case SETTINGS_COMPRESS_DATA: if (value != 0 && value != 1) { throw protocolError("Invalid value for COMPRESS_DATA: %d", value); } settings.allowCompressedData(value == 1); break; case SETTINGS_ENABLE_PUSH: if (value != 0 && value != 1) { throw protocolError("Invalid value for ENABLE_PUSH: %d", value); } settings.pushEnabled(value == 1); break; case SETTINGS_INITIAL_WINDOW_SIZE: if (value < 0 || value > Integer.MAX_VALUE) { throw protocolError("Invalid value for INITIAL_WINDOW_SIZE: %d", value); } settings.initialWindowSize((int) value); break; case SETTINGS_MAX_CONCURRENT_STREAMS: if (value < 0 || value > Integer.MAX_VALUE) { throw protocolError("Invalid value for MAX_CONCURRENT_STREAMS: %d", value); } settings.maxConcurrentStreams((int) value); break; default: throw protocolError("Unsupport setting: %d", id); } } observer.onSettingsRead(ctx, settings); } }
private void readAltSvcFrame( ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer) throws Http2Exception { long maxAge = payload.readUnsignedInt(); int port = payload.readUnsignedShort(); payload.skipBytes(1); short protocolIdLength = payload.readUnsignedByte(); ByteBuf protocolId = payload.readSlice(protocolIdLength); short hostLength = payload.readUnsignedByte(); String host = payload.toString(payload.readerIndex(), hostLength, UTF_8); payload.skipBytes(hostLength); String origin = null; if (payload.isReadable()) { origin = payload.toString(UTF_8); payload.skipBytes(payload.readableBytes()); } observer.onAltSvcRead(ctx, streamId, maxAge, port, protocolId, host, origin); }
@Override public void decode(ByteBuf buf) { id = buf.readLong(); byte[] bitSetBuf = new byte[8]; buf.readBytes(bitSetBuf); this.bitSet = BitSet.valueOf(bitSetBuf); if (bitSet.get(0)) { position = readLongVector3(buf); } if (bitSet.get(1)) { orientation = readOrientation(buf); } if (bitSet.get(2)) { velocity = readFloatVector3(buf); } if (bitSet.get(3)) { accel = readFloatVector3(buf); } if (bitSet.get(4)) { extraVel = readFloatVector3(buf); } if (bitSet.get(5)) { lookPitch = buf.readFloat(); } if (bitSet.get(6)) { physicsFlags = buf.readUnsignedInt(); } if (bitSet.get(7)) { hostileType = buf.readByte(); } if (bitSet.get(8)) { entityType = buf.readUnsignedInt(); } if (bitSet.get(9)) { currentMode = buf.readByte(); } if (bitSet.get(10)) { lastShootTime = buf.readUnsignedInt(); } if (bitSet.get(11)) { hitCounter = buf.readUnsignedInt(); } if (bitSet.get(12)) { lastHitTime = buf.readUnsignedInt(); } if (bitSet.get(13)) { app.decode(buf); } if (bitSet.get(14)) { flags1 = buf.readByte(); flags2 = buf.readByte(); } if (bitSet.get(15)) { rollTime = buf.readUnsignedInt(); } if (bitSet.get(16)) { stunTime = buf.readInt(); } if (bitSet.get(17)) { slowedTime = buf.readUnsignedInt(); } if (bitSet.get(18)) { makeBlueTime = buf.readUnsignedInt(); } if (bitSet.get(19)) { speedUpTime = buf.readUnsignedInt(); } if (bitSet.get(20)) { slowPatchTime = buf.readFloat(); } if (bitSet.get(21)) { classType = buf.readByte(); } if (bitSet.get(22)) { specialization = buf.readByte(); } if (bitSet.get(23)) { chargedMP = buf.readFloat(); } if (bitSet.get(24)) { nu1 = buf.readUnsignedInt(); nu2 = buf.readUnsignedInt(); nu3 = buf.readUnsignedInt(); } if (bitSet.get(25)) { nu4 = buf.readUnsignedInt(); nu5 = buf.readUnsignedInt(); nu6 = buf.readUnsignedInt(); } if (bitSet.get(26)) { rayHit = readFloatVector3(buf); } if (bitSet.get(27)) { HP = buf.readFloat(); } if (bitSet.get(28)) { MP = buf.readFloat(); } if (bitSet.get(29)) { blockPower = buf.readFloat(); } if (bitSet.get(30)) { maxHPMultiplier = buf.readFloat(); shootSpeed = buf.readFloat(); damageMultiplier = buf.readFloat(); armorMultiplier = buf.readFloat(); resistanceMultiplier = buf.readFloat(); } if (bitSet.get(31)) { nu7 = buf.readByte(); } if (bitSet.get(32)) { nu8 = buf.readByte(); } if (bitSet.get(33)) { level = buf.readUnsignedInt(); } if (bitSet.get(34)) { currentXP = buf.readUnsignedInt(); } if (bitSet.get(35)) { parentOwner = buf.readLong(); } if (bitSet.get(36)) { na1 = buf.readUnsignedInt(); na2 = buf.readUnsignedInt(); } if (bitSet.get(37)) { na3 = buf.readByte(); } if (bitSet.get(38)) { na4 = buf.readUnsignedInt(); } if (bitSet.get(39)) { na5 = buf.readUnsignedInt(); nu11 = buf.readUnsignedInt(); nu12 = buf.readUnsignedInt(); } if (bitSet.get(40)) { spawnPosition = readLongVector3(buf); } if (bitSet.get(41)) { nu20 = buf.readUnsignedInt(); nu21 = buf.readUnsignedInt(); nu22 = buf.readUnsignedInt(); } if (bitSet.get(42)) { nu19 = buf.readByte(); } if (bitSet.get(43)) { itemData.decode(buf); } if (bitSet.get(44)) { for (int i = 0; i < 13; i++) { GItem item = new GItem(); item.decode(buf); equipment[i] = item; } } if (bitSet.get(45)) { name = new String(buf.readBytes(16).array(), Charsets.US_ASCII).trim(); } if (bitSet.get(46)) { for (int i = 0; i < 11; i++) { skills[i] = buf.readUnsignedInt(); } } if (bitSet.get(47)) { iceBlockFour = buf.readUnsignedInt(); } debugCap = buf.capacity(); buf.resetReaderIndex(); buf.resetWriterIndex(); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { Object result = null; switch (state()) { case READ_MAGIC: magic = buf.readInt(); if (magic == MAGIC_BYTES_AS_INT) { checkpoint(DecodeState.READ_VERSION); } else { throw new IllegalArgumentException( String.format( "Expected to read magic 4 bytes: %d but received %d.", 0x5355524C, magic)); } case READ_VERSION: version = buf.readUnsignedByte(); checkpoint(DecodeState.READ_COMMAND); case READ_COMMAND: command = buf.readUnsignedByte(); checkpoint(DecodeState.READ_MSGID); case READ_MSGID: id = buf.readUnsignedInt(); checkpoint(DecodeState.READ_COMMAND_DATA); case READ_COMMAND_DATA: switch (command) { case 0x01: { final int longURILen = buf.readInt(); final String longURI = buf.readBytes(longURILen).toString(CHARSET); result = new ShurlyShrinkMessage(version, id, URI.create(longURI)); break; } case 0x02: { final int longURILen = buf.readInt(); final String longURI = buf.readBytes(longURILen).toString(CHARSET); final int shortURILen = buf.readInt(); final String shortURI = buf.readBytes(shortURILen).toString(CHARSET); result = new ShurlyShrunkMessage(version, id, URI.create(longURI), URI.create(shortURI)); break; } case 0x03: { final long errorCode = buf.readUnsignedInt(); final int reasonLength = buf.readInt(); final String reason = buf.readBytes(reasonLength).toString(CHARSET); result = new ShurlyErrorMessage(version, id, errorCode, reason); break; } case 0x04: { final int shortURILen = buf.readInt(); final String shortURI = buf.readBytes(shortURILen).toString(CHARSET); result = new ShurlyFollowMessage(version, id, URI.create(shortURI)); break; } default: throw new IllegalArgumentException("Unrecognized command: " + command); } } //noinspection ConstantConditions assert result != null; state(DecodeState.READ_MAGIC); out.add(result); }
@Override public long readUnsignedInt() { return buffer.readUnsignedInt(); }
@Override @SuppressWarnings("restriction") public void decode(ByteBuf buf) { id = buf.readLong(); buf.readBytes(bitmask); BitArray bitArray = new BitArray(8 * bitmask.length, Bitops.flipBits(bitmask)); // Size in bits, byte[] // BitArray bitArray = new BitArray(bitmask); if (bitArray.get(0)) { posX = buf.readLong(); posY = buf.readLong(); posZ = buf.readLong(); } if (bitArray.get(1)) { pitch = buf.readFloat(); roll = buf.readFloat(); yaw = buf.readFloat(); } if (bitArray.get(2)) { velocity.decode(buf); } if (bitArray.get(3)) { accel.decode(buf); } if (bitArray.get(4)) { extraVel.decode(buf); } if (bitArray.get(5)) { lookPitch = buf.readFloat(); } if (bitArray.get(6)) { physicsFlags = buf.readUnsignedInt(); } if (bitArray.get(7)) { speedFlags = buf.readByte(); } if (bitArray.get(8)) { entityType = buf.readUnsignedInt(); } if (bitArray.get(9)) { currentMode = buf.readByte(); } if (bitArray.get(10)) { lastShootTime = buf.readUnsignedInt(); } if (bitArray.get(11)) { hitCounter = buf.readUnsignedInt(); } if (bitArray.get(12)) { lastHitTime = buf.readUnsignedInt(); } if (bitArray.get(13)) { app.decode(buf); } if (bitArray.get(14)) { flags1 = buf.readByte(); flags2 = buf.readByte(); } if (bitArray.get(15)) { rollTime = buf.readUnsignedInt(); } if (bitArray.get(16)) { stunTime = buf.readInt(); } if (bitArray.get(17)) { slowedTime = buf.readUnsignedInt(); } if (bitArray.get(18)) { makeBlueTime = buf.readUnsignedInt(); } if (bitArray.get(19)) { speedUpTime = buf.readUnsignedInt(); } if (bitArray.get(20)) { slowPatchTime = buf.readFloat(); } if (bitArray.get(21)) { classType = buf.readByte(); } if (bitArray.get(22)) { specialization = buf.readByte(); } if (bitArray.get(23)) { chargedMP = buf.readFloat(); } if (bitArray.get(24)) { nu1 = buf.readUnsignedInt(); nu2 = buf.readUnsignedInt(); nu3 = buf.readUnsignedInt(); } if (bitArray.get(25)) { nu4 = buf.readUnsignedInt(); nu5 = buf.readUnsignedInt(); nu6 = buf.readUnsignedInt(); } if (bitArray.get(26)) { rayHit.decode(buf); } if (bitArray.get(27)) { HP = buf.readFloat(); } if (bitArray.get(28)) { MP = buf.readFloat(); } if (bitArray.get(29)) { blockPower = buf.readFloat(); } if (bitArray.get(30)) { maxHPMultiplier = buf.readFloat(); shootSpeed = buf.readFloat(); damageMultiplier = buf.readFloat(); armorMultiplier = buf.readFloat(); resistanceMultiplier = buf.readFloat(); } if (bitArray.get(31)) { nu7 = buf.readByte(); } if (bitArray.get(32)) { nu8 = buf.readByte(); } if (bitArray.get(33)) { level = buf.readUnsignedInt(); } if (bitArray.get(34)) { currentXP = buf.readUnsignedInt(); } if (bitArray.get(35)) { parentOwner = buf.readLong(); } if (bitArray.get(36)) { na1 = buf.readUnsignedInt(); na2 = buf.readUnsignedInt(); } if (bitArray.get(37)) { na3 = buf.readByte(); } if (bitArray.get(38)) { na4 = buf.readUnsignedInt(); } if (bitArray.get(39)) { na5 = buf.readUnsignedInt(); nu11 = buf.readUnsignedInt(); nu12 = buf.readUnsignedInt(); } if (bitArray.get(40)) { nu13 = buf.readUnsignedInt(); nu14 = buf.readUnsignedInt(); nu15 = buf.readUnsignedInt(); nu16 = buf.readUnsignedInt(); nu17 = buf.readUnsignedInt(); nu18 = buf.readUnsignedInt(); } if (bitArray.get(41)) { nu20 = buf.readUnsignedInt(); nu21 = buf.readUnsignedInt(); nu22 = buf.readUnsignedInt(); } if (bitArray.get(42)) { nu19 = buf.readByte(); } if (bitArray.get(43)) { itemData.decode(buf); } if (bitArray.get(44)) { for (int i = 0; i < 13; i++) { GItem item = new GItem(); item.decode(buf); equipment[i] = item; } } if (bitArray.get(45)) { name = new String(buf.readBytes(16).array(), Charsets.US_ASCII).trim(); } if (bitArray.get(46)) { for (int i = 0; i < 11; i++) { skills[i] = buf.readUnsignedInt(); } } if (bitArray.get(47)) { iceBlockFour = buf.readUnsignedInt(); } debugCap = buf.capacity(); buf.resetReaderIndex(); buf.resetWriterIndex(); }
private void readHeadersFrame( final ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer) throws Http2Exception { final int headersStreamId = streamId; final Http2Flags headersFlags = flags; int padding = flags.readPaddingLength(payload); // The callback that is invoked is different depending on whether priority information // is present in the headers frame. if (flags.priorityPresent()) { long word1 = payload.readUnsignedInt(); final boolean exclusive = (word1 & 0x80000000L) > 0; final int streamDependency = (int) (word1 & 0x7FFFFFFFL); final short weight = (short) (payload.readUnsignedByte() + 1); final ByteBuf fragment = payload.readSlice(payload.readableBytes() - padding); // Create a handler that invokes the observer when the header block is complete. headersContinuation = new HeadersContinuation() { @Override public int getStreamId() { return headersStreamId; } @Override public void processFragment( boolean endOfHeaders, ByteBuf fragment, int padding, Http2FrameObserver observer) throws Http2Exception { builder().addFragment(fragment, ctx.alloc(), endOfHeaders); if (endOfHeaders) { Http2Headers headers = builder().buildHeaders(); observer.onHeadersRead( ctx, headersStreamId, headers, streamDependency, weight, exclusive, padding, headersFlags.endOfStream(), headersFlags.endOfSegment()); close(); } } }; // Process the initial fragment, invoking the observer's callback if end of headers. headersContinuation.processFragment(flags.endOfHeaders(), fragment, padding, observer); return; } // The priority fields are not present in the frame. Prepare a continuation that invokes // the observer callback without priority information. headersContinuation = new HeadersContinuation() { @Override public int getStreamId() { return headersStreamId; } @Override public void processFragment( boolean endOfHeaders, ByteBuf fragment, int padding, Http2FrameObserver observer) throws Http2Exception { builder().addFragment(fragment, ctx.alloc(), endOfHeaders); if (endOfHeaders) { Http2Headers headers = builder().buildHeaders(); observer.onHeadersRead( ctx, headersStreamId, headers, padding, headersFlags.endOfStream(), headersFlags.endOfSegment()); close(); } } }; // Process the initial fragment, invoking the observer's callback if end of headers. final ByteBuf fragment = payload.readSlice(payload.readableBytes() - padding); headersContinuation.processFragment(flags.endOfHeaders(), fragment, padding, observer); }
@Override public long readUInt32() { return buf.readUnsignedInt(); }
private void readRstStreamFrame( ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer) throws Http2Exception { long errorCode = payload.readUnsignedInt(); observer.onRstStreamRead(ctx, streamId, errorCode); }