List of usage examples for io.netty.buffer ByteBuf slice
public abstract ByteBuf slice();
From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROLabelSubobjectParser.java
License:Open Source License
@Override public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean loose) throws RSVPParsingException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); if (buffer.readableBytes() < HEADER_LENGTH) { throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >" + HEADER_LENGTH + "."); }/*from ww w.j av a2 s. co m*/ final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE); final short cType = buffer.readUnsignedByte(); final LabelType labelType = this.registry.parseLabel(cType, buffer.slice()); if (labelType == null) { throw new RSVPParsingException("Unknown C-TYPE for ero label subobject. Passed: " + cType); } final LabelBuilder builder = new LabelBuilder(); builder.setUniDirectional(reserved.get(U_FLAG_OFFSET)); builder.setLabelType(labelType); return new SubobjectContainerBuilder().setLoose(loose) .setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build(); }
From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.rro.RROLabelSubobjectParser.java
License:Open Source License
@Override public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); if (buffer.readableBytes() < HEADER_LENGTH) { throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >" + HEADER_LENGTH + "."); }//from w w w . j a va 2s .c om final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE); final short cType = buffer.readUnsignedByte(); final LabelType labelType = this.registry.parseLabel(cType, buffer.slice()); if (labelType == null) { throw new RSVPParsingException("Unknown C-TYPE for ero label subobject. Passed: " + cType); } final LabelBuilder builder = new LabelBuilder(); builder.setUniDirectional(reserved.get(U_FLAG_OFFSET)); builder.setGlobal(reserved.get(G_FLAG_OFFSET)); builder.setLabelType(labelType); return new SubobjectContainerBuilder() .setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build(); }
From source file:ratpack.session.internal.LocalMemorySessionStore.java
License:Apache License
@Override public Promise<ByteBuf> load(AsciiString sessionId) { return Promise.sync(() -> { maybeCleanup();//from w w w . j av a2 s . c o m ByteBuf value = cache.getIfPresent(sessionId); if (value != null) { return Unpooled.unreleasableBuffer(value.slice()); } else { return Unpooled.EMPTY_BUFFER; } }); }
From source file:ru.jts_dev.authserver.packets.LoginClientPacketHandler.java
License:Open Source License
public final IncomingMessageWrapper handle(final ByteBuf buf, @Header(CONNECTION_ID) final String connectionId) { if (buf.readableBytes() == 0) throw new RuntimeException("At least 1 readable byte excepted in buffer"); final int opcode = buf.readByte(); final IncomingMessageWrapper msg; switch (opcode) { case 0x00:/*w w w . j a v a2 s .c om*/ msg = context.getBean(RequestAuthLogin.class); break; case 0x02: msg = context.getBean(RequestServerLogin.class); break; case 0x05: msg = context.getBean(RequestServerList.class); break; case 0x07: msg = context.getBean(AuthGameGuard.class); break; default: throw new RuntimeException("Invalid packet opcode: " + Integer.toHexString(opcode)); } final ByteBuf data = buf.slice(); Class<? extends IncomingMessageWrapper> packetClass = msg.getClass(); log.trace("received packet: {}, length: {}", packetClass.getSimpleName(), data.readableBytes()); final MessageHeaders headers = msg.getHeaders(); headers.put(CONNECTION_ID, connectionId); msg.setPayload(data); return msg; }
From source file:ru.jts_dev.gameserver.packets.GameClientPacketHandler.java
License:Open Source License
/** * Handle incoming packet by first bytes (opcode) * * @param buf - packet data/*ww w. j a va 2 s. c o m*/ * @param connectionId - client connectionId from Spring Integration * @return - handled packet */ public IncomingMessageWrapper handle(ByteBuf buf, @Header(CONNECTION_ID) String connectionId) { if (buf.readableBytes() == 0) throw new RuntimeException("At least 1 readable byte excepted in buffer"); int opcode = buf.readUnsignedByte(); if (!packets.containsKey(opcode)) throw new RuntimeException("Invalid first packet opcode: " + String.format("0x%02X", (byte) opcode)); IncomingMessageWrapper msg; Object node = packets.get(opcode); if (node instanceof String) { msg = context.getBean((String) node, IncomingMessageWrapper.class); } else { // (node instanceof Map) opcode = buf.readUnsignedShort(); if (!((Map) node).containsKey(opcode)) throw new RuntimeException( "Invalid second packet opcode: " + String.format("0x%02X", (byte) opcode)); node = ((Map) node).get(opcode); msg = context.getBean((String) node, IncomingMessageWrapper.class); } ByteBuf data = buf.slice(); log.debug("received packet: {}, length: {}", msg.getClass().getSimpleName(), data.readableBytes()); msg.getHeaders().put(CONNECTION_ID, connectionId); msg.setPayload(data); return msg; }