List of usage examples for io.netty.buffer ByteBuf setIndex
public abstract ByteBuf setIndex(int readerIndex, int writerIndex);
From source file:dorkbox.network.pipeline.tcp.KryoEncoder.java
License:Apache License
@Override protected void encode(final ChannelHandlerContext context, final Object msg, final ByteBuf out) throws Exception { // we don't necessarily start at 0!! // START at index = 4. This is to make room for the integer placed by the frameEncoder for TCP. int startIndex = out.writerIndex() + reservedLengthIndex; if (msg != null) { out.writerIndex(startIndex);//from w ww. j a v a 2 s . c o m try { writeObject(this.serializationManager, context, msg, out); int index = out.writerIndex(); // now set the frame length // (reservedLengthLength) 4 is the reserved space for the integer. int length = index - startIndex; // specify the header. int lengthOfTheLength = OptimizeUtilsByteBuf.intLength(length, true); // make it so the location we write out our length is aligned to the end. int indexForLength = startIndex - lengthOfTheLength; out.writerIndex(indexForLength); // do the optimized length thing! OptimizeUtilsByteBuf.writeInt(out, length, true); // newIndex is actually where we want to start reading the data as well when written to the socket out.setIndex(indexForLength, index); } catch (Exception ex) { context.fireExceptionCaught( new IOException("Unable to serialize object of type: " + msg.getClass().getName(), ex)); } } }
From source file:io.github.stormcloud_dev.stormcloud.seralization.RORObjectDecoder.java
License:Apache License
private Object readNextObject(ByteBuf buf) { if (buf.readableBytes() < 12) { return null; }/*from www . jav a 2 s . c o m*/ byte[] header = new byte[8]; buf.readBytes(8).readBytes(header); //Handshake header if (Arrays.equals(header, new byte[] { -66, -70, -2, -54, 11, -80, -83, -34 })) { if (buf.readableBytes() >= 8) { buf.readBytes(8); //We read the rest of the handshake packet //Contains the length (inclusive the header) and 2 unknown numbers return new HandshakeFrame(); } else { return null; } } int length = buf.readByte(); buf.readBytes(3); byte id = buf.readByte(); if (id != 16) { int readerIndex = buf.readerIndex(), writerIndex = buf.writerIndex(); byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(buf.readableBytes()).readBytes(bytes); buf.setIndex(readerIndex, writerIndex); // Print packets - debugging purposes //System.out.println("ID: " + id + " - " + Arrays.toString(bytes)); } switch (id) { case 0: //if (length < 39) return null; return new PositionInfoServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte()); case 1: return new SetReadyServerBoundFrame(buf.readByte()); case 2: return new SetPlayerServerBoundFrame(buf.readDouble(), buf.readDouble(), readString(buf)); case 3: return new AddPlayerServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readInt(), buf.readInt(), readString(buf)); case 4: return new ChatPlayerServerBoundFrame(readString(buf)); case 5: return new UpdatePlayerServerBoundFrame(buf.readInt(), buf.readDouble(), buf.readDouble(), readString(buf)); case 6: return new KeyPlayerServerBoundFrame(buf.readShort(), buf.readShort(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readShort(), buf.readByte()); case 7: return new HealPlayerServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readDouble()); case 8: return new NPCHPServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readShort(), buf.readShort(), buf.readShort()); case 9: return new MDeadServerBoundFrame(); case 10: return new NPCTargetServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readInt(), buf.readShort(), buf.readByte()); case 11: return new KeyMonsterServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readShort()); case 12: return new CreateObjectServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble()); case 13: return new CreateLevelObjectServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readInt()); case 14: return new SyncAlarmServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readInt()); case 15: return new SpawnClassicServerBoundFrame(buf.readShort(), buf.readDouble(), buf.readDouble(), buf.readByte(), buf.readShort(), buf.readShort(), buf.readShort()); case 16: return new LagPlayerServerBoundFrame(readString(buf)); case 17: return new DisPlayerServerBoundFrame(); case 18: return new TransportServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readByte()); case 19: return new ActivateObjectServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readShort(), buf.readShort(), buf.readByte(), buf.readByte()); case 20: return new SpawnItemServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readInt()); case 21: return new SyncVarServerBoundFrame(buf.readDouble()); case 22: return new SpawnBossServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readByte(), buf.readDouble()); case 23: return new SpawnClassicBossServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readInt(), buf.readInt(), buf.readDouble(), buf.readByte(), buf.readShort(), buf.readShort(), buf.readShort(), buf.readDouble()); case 24: return new LevelUpServerBoundFrame(); case 25: return new ItemPickupServerBoundFrame(buf.readInt(), buf.readByte()); case 26: return new ChatSystemServerBoundFrame(buf.readByte(), readString(buf)); case 27: return new TestServerBoundFrame(); case 28: return new ItemSwapServerBoundFrame(); case 29: return readItemProcFrame(buf); case 30: return new ShrineProcServerBoundFrame(buf.readByte()); case 31: return new SpawnDroneServerBoundFrame(buf.readShort(), buf.readShort(), buf.readShort(), buf.readShort(), buf.readShort(), buf.readShort(), buf.readShort()); case 32: return new SetPriceServerBoundFrame(buf.readShort(), buf.readShort()); case 33: return new CreateChestServerBoundFrame(); case 34: return new UpdateBuffsServerBoundFrame(buf.readDouble(), buf.readShort(), buf.readShort()); case 35: return new SpawnImpmServerboundFrame(buf.readShort(), buf.readShort(), buf.readShort(), buf.readByte(), buf.readByte()); case 36: return new UpdateChest4ServerBoundFrame(buf.readShort()); case 37: return new EliteTeleportServerBoundFrame(buf.readShort(), buf.readShort()); case 38: return new UpdateHpServerBoundFrame(buf.readShort()); case 39: return new UpdateDiffServerBoundFrame(buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte()); case 40: return new SpawnGhostServerBoundFrame(buf.readShort(), buf.readShort(), buf.readFloat(), buf.readFloat()); case 41: return new StopTimeServerBoundFrame(buf.readShort()); case 42: return new LandLizardServerBoundFrame(buf.readShort(), buf.readShort()); case 43: return new DestroyObjectServerBoundFrame(); case 44: return new CreateSlimeServerBoundFrame(buf.readShort(), buf.readShort(), buf.readFloat(), buf.readShort(), buf.readShort(), buf.readShort(), buf.readByte(), buf.readShort(), buf.readDouble()); case 45: return new CrewChoiceServerBoundFrame(buf.readShort()); case 46: return new CreateItemServerBoundFrame(buf.readShort(), buf.readShort(), buf.readShort()); case 47: return new ActivateSwitchServerBoundFrame(buf.readShort(), buf.readShort()); default: return null; } }
From source file:io.github.stormcloud_dev.stormcloud.seralization.RORObjectEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Frame frame, ByteBuf buf) throws Exception { buf.writeBytes(new byte[] { -34, -64, -83, -34, 12, 0, 0, 0 }); // GM:Studio header //System.out.println("ENCODING: " + frame.getClass().getSimpleName()); try {//from w ww. j a v a2s . c o m frame.writeData(buf.order(LITTLE_ENDIAN)); // frame data - dependent on frame } catch (Exception exception) { exception.printStackTrace(); } if (!(frame instanceof TestClientBoundFrame) && !(frame instanceof LagPlayerClientBoundFrame)) { int readerIndex = buf.readerIndex(), writerIndex = buf.writerIndex(); byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(buf.readableBytes()).readBytes(bytes); buf.setIndex(readerIndex, writerIndex); // Print packets - debugging purposes //System.out.println("SEND " + frame.getClass().getSimpleName() + " TO " + ctx.channel().remoteAddress() + " - " + Arrays.toString(bytes)); } }
From source file:net.petercashel.nettyCore.common.packets.CMDInPacket.java
License:Apache License
public ByteBuf getBlankPacket() { // TODO Auto-generated method stub ByteBuf b = Unpooled.buffer(Packet.packetBufSize).writeZero(Packet.packetBufSize); b.setIndex(0, 0); return b;/* ww w. j a va 2s.c o m*/ }
From source file:net.petercashel.nettyCore.common.packets.GetHistoryPacket.java
License:Apache License
@Override public ByteBuf getPacket() { // TODO Auto-generated method stub ByteBuf b = Unpooled.buffer(Packet.packetBufSize).writeZero(Packet.packetBufSize); b.setIndex(0, 0); return b;// w ww . j a v a 2 s . com }
From source file:org.apache.activemq.artemis.core.protocol.core.impl.PacketImpl.java
License:Apache License
protected ByteBuf copyMessageBuffer(ByteBuf buffer, int skipBytes) { ByteBuf newNettyBuffer = Unpooled.buffer(buffer.capacity() - PACKET_HEADERS_SIZE - skipBytes); int read = buffer.readerIndex(); int writ = buffer.writerIndex(); buffer.readerIndex(PACKET_HEADERS_SIZE); newNettyBuffer.writeBytes(buffer, buffer.readableBytes() - skipBytes); buffer.setIndex(read, writ); newNettyBuffer.setIndex(0, writ - PACKET_HEADERS_SIZE - skipBytes); return newNettyBuffer; }
From source file:org.apache.bookkeeper.bookie.storage.ldb.WriteCache.java
License:Apache License
public void forEach(EntryConsumer consumer) { sortedEntriesLock.lock();/*from w ww . j a v a2s. c o m*/ try { int entriesToSort = (int) index.size(); int arrayLen = entriesToSort * 4; if (sortedEntries == null || sortedEntries.length < arrayLen) { sortedEntries = new long[(int) (arrayLen * 2)]; } long startTime = MathUtils.nowInNano(); sortedEntriesIdx = 0; index.forEach((ledgerId, entryId, offset, length) -> { if (deletedLedgers.contains(ledgerId)) { // Ignore deleted ledgers return; } sortedEntries[sortedEntriesIdx] = ledgerId; sortedEntries[sortedEntriesIdx + 1] = entryId; sortedEntries[sortedEntriesIdx + 2] = offset; sortedEntries[sortedEntriesIdx + 3] = length; sortedEntriesIdx += 4; }); if (log.isDebugEnabled()) { log.debug("iteration took {} ms", MathUtils.elapsedNanos(startTime) / 1e6); } startTime = MathUtils.nowInNano(); // Sort entries by (ledgerId, entryId) maintaining the 4 items groups groupSorter.sort(sortedEntries, 0, sortedEntriesIdx); if (log.isDebugEnabled()) { log.debug("sorting {} ms", (MathUtils.elapsedNanos(startTime) / 1e6)); } startTime = MathUtils.nowInNano(); ByteBuf[] entrySegments = new ByteBuf[segmentsCount]; for (int i = 0; i < segmentsCount; i++) { entrySegments[i] = cacheSegments[i].slice(0, cacheSegments[i].capacity()); } for (int i = 0; i < sortedEntriesIdx; i += 4) { long ledgerId = sortedEntries[i]; long entryId = sortedEntries[i + 1]; long offset = sortedEntries[i + 2]; long length = sortedEntries[i + 3]; int localOffset = (int) (offset & segmentOffsetMask); int segmentIdx = (int) (offset >>> segmentOffsetBits); ByteBuf entry = entrySegments[segmentIdx]; entry.setIndex(localOffset, localOffset + (int) length); consumer.accept(ledgerId, entryId, entry); } if (log.isDebugEnabled()) { log.debug("entry log adding {} ms", MathUtils.elapsedNanos(startTime) / 1e6); } } finally { sortedEntriesLock.unlock(); } }
From source file:org.apache.bookkeeper.common.coder.Coder.java
License:Apache License
/** * Encodes the given value of type {@code T} onto the given output buffer. * * @param value value to encode//from w w w. j a v a 2 s . com * @return the serialized bytes buf. */ default ByteBuf encodeBuf(T value) { int len = getSerializedSize(value); ByteBuf buffer = Unpooled.buffer(len, len); buffer.setIndex(0, 0); encode(value, buffer); return buffer; }
From source file:org.apache.bookkeeper.common.coder.Coder.java
License:Apache License
/** * Encodes the given value of type {@code T} onto a bytes array. * * @param value value to encode/*from ww w . j a va 2 s. c o m*/ * @return the serialized bytes bytes. */ default byte[] encode(T value) { byte[] data = new byte[getSerializedSize(value)]; ByteBuf buf = Unpooled.wrappedBuffer(data); buf.setIndex(0, 0); encode(value, buf); return data; }
From source file:org.apache.bookkeeper.common.coder.CoderBasicTestCase.java
License:Apache License
static <T> T decode(Coder<T> coder, byte[] bytes) { ByteBuf buf = Unpooled.wrappedBuffer(bytes); buf.setIndex(0, bytes.length); return coder.decode(buf); }