List of usage examples for io.netty.buffer ByteBuf setShort
public abstract ByteBuf setShort(int index, int value);
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactory.java
License:Open Source License
private void serializeGroupBody(final MultipartReplyBody body, final ByteBuf outBuffer) { MultipartReplyGroupCase groupCase = (MultipartReplyGroupCase) body; MultipartReplyGroup group = groupCase.getMultipartReplyGroup(); for (GroupStats groupStats : group.getGroupStats()) { ByteBuf groupStatsBuff = UnpooledByteBufAllocator.DEFAULT.buffer(); groupStatsBuff.writeShort(EncodeConstants.EMPTY_LENGTH); groupStatsBuff.writeZero(GROUP_STATS_PADDING_1); groupStatsBuff.writeInt(groupStats.getGroupId().getValue().intValue()); groupStatsBuff.writeInt(groupStats.getRefCount().intValue()); groupStatsBuff.writeZero(GROUP_STATS_PADDING_2); groupStatsBuff.writeLong(groupStats.getPacketCount().longValue()); groupStatsBuff.writeLong(groupStats.getByteCount().longValue()); groupStatsBuff.writeInt(groupStats.getDurationSec().intValue()); groupStatsBuff.writeInt(groupStats.getDurationNsec().intValue()); for (BucketStats bucketStats : groupStats.getBucketStats()) { groupStatsBuff.writeLong(bucketStats.getPacketCount().longValue()); groupStatsBuff.writeLong(bucketStats.getByteCount().longValue()); }//from www . ja v a 2 s . c o m groupStatsBuff.setShort(GROUP_STATS_LENGTH_INDEX, groupStatsBuff.readableBytes()); outBuffer.writeBytes(groupStatsBuff); } }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactory.java
License:Open Source License
private void serializeFlowBody(final MultipartReplyBody body, final ByteBuf outBuffer, final MultipartReplyMessage message) { MultipartReplyFlowCase flowCase = (MultipartReplyFlowCase) body; MultipartReplyFlow flow = flowCase.getMultipartReplyFlow(); for (FlowStats flowStats : flow.getFlowStats()) { ByteBuf flowStatsBuff = UnpooledByteBufAllocator.DEFAULT.buffer(); flowStatsBuff.writeShort(EncodeConstants.EMPTY_LENGTH); flowStatsBuff.writeByte(new Long(flowStats.getTableId()).byteValue()); flowStatsBuff.writeZero(FLOW_STATS_PADDING_1); flowStatsBuff.writeInt(flowStats.getDurationSec().intValue()); flowStatsBuff.writeInt(flowStats.getDurationNsec().intValue()); flowStatsBuff.writeShort(flowStats.getPriority()); flowStatsBuff.writeShort(flowStats.getIdleTimeout()); flowStatsBuff.writeShort(flowStats.getHardTimeout()); flowStatsBuff.writeZero(FLOW_STATS_PADDING_2); flowStatsBuff.writeLong(flowStats.getCookie().longValue()); flowStatsBuff.writeLong(flowStats.getPacketCount().longValue()); flowStatsBuff.writeLong(flowStats.getByteCount().longValue()); OFSerializer<Match> matchSerializer = registry.<Match, OFSerializer<Match>>getSerializer( new MessageTypeKey<>(message.getVersion(), Match.class)); matchSerializer.serialize(flowStats.getMatch(), flowStatsBuff); ListSerializer.serializeList(flowStats.getInstruction(), TypeKeyMakerFactory.createInstructionKeyMaker(message.getVersion()), registry, flowStatsBuff); flowStatsBuff.setShort(FLOW_STATS_LENGTH_INDEX, flowStatsBuff.readableBytes()); outBuffer.writeBytes(flowStatsBuff); }/*from w w w . j av a2 s . co m*/ }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.instruction.AbstractActionInstructionSerializer.java
License:Open Source License
protected void writeActions(final List<Action> actions, final ByteBuf outBuffer, int startIndex) { int lengthIndex = outBuffer.writerIndex(); outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH); outBuffer.writeZero(InstructionConstants.PADDING_IN_ACTIONS_INSTRUCTION); ListSerializer.serializeList(actions, ACTION_KEY_MAKER, getRegistry(), outBuffer); int instructionLength = outBuffer.writerIndex() - startIndex; outBuffer.setShort(lengthIndex, instructionLength); }
From source file:org.opendaylight.openflowjava.protocol.impl.util.OF13MatchSerializer.java
License:Open Source License
@Override public void serialize(Match match, ByteBuf outBuffer) { if (match == null) { LOG.debug("Match is null"); return;//from w w w. j av a 2s. c o m } int matchStartIndex = outBuffer.writerIndex(); serializeType(match, outBuffer); int matchLengthIndex = outBuffer.writerIndex(); outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH); serializeMatchEntries(match.getMatchEntry(), outBuffer); // Length of ofp_match (excluding padding) int matchLength = outBuffer.writerIndex() - matchStartIndex; outBuffer.setShort(matchLengthIndex, matchLength); int paddingRemainder = matchLength % EncodeConstants.PADDING; if (paddingRemainder != 0) { outBuffer.writeZero(EncodeConstants.PADDING - paddingRemainder); } }
From source file:org.opendaylight.openflowjava.util.ByteBufUtils.java
License:Open Source License
/** * Write length standard OF header/* ww w. j a v a2 s . com*/ * @param out writing buffer */ public static void updateOFHeaderLength(final ByteBuf out) { out.setShort(EncodeConstants.OFHEADER_LENGTH_INDEX, out.readableBytes()); }
From source file:se.sics.kompics.network.netty.MessageEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, Msg msg, List<Object> outL) throws Exception { ByteBuf out = ctx.alloc().buffer(NettyNetwork.INITIAL_BUFFER_SIZE, NettyNetwork.SEND_BUFFER_SIZE); NettyNetwork.LOG.trace("Trying to encode outgoing data to {} from {}.", ctx.channel().remoteAddress(), ctx.channel().localAddress()); int startIdx = out.writerIndex(); out.writeBytes(LENGTH_PLACEHOLDER);/*w w w. j a v a2s . c o m*/ Serializers.toBinary(msg, out); int endIdx = out.writerIndex(); int diff = endIdx - startIdx - LENGTH_PLACEHOLDER.length; if (diff > 65532) { //2^16 - 2bytes for the length header (snappy wants no more than 65536 bytes uncompressed) throw new Exception("Can't encode message longer than 65532 bytes!"); } out.setShort(startIdx, diff); NettyNetwork.LOG.trace("Encoded outgoing {} bytes of data to {}: {}.", new Object[] { diff, ctx.channel().remoteAddress(), ByteBufUtil.hexDump(out) }); outL.add(out); }
From source file:uk.co.thinkofdeath.thinkcraft.bukkit.world.ChunkManager.java
License:Apache License
private void gzipChunk(ChunkSnapshot chunk, ByteBuf out) { int mask = 0; int count = 0; for (int i = 0; i < 16; i++) { if (!chunk.isSectionEmpty(i)) { mask |= 1 << i;// w w w . j ava2 s . c om count++; } } ByteBuf data = allocator.buffer(16 * 16 * 16 * 4 * count + 3 + 256); data.writeByte(1); // The chunk exists data.writeShort(mask); int offset = 0; int blockDataOffset = 16 * 16 * 16 * 2 * count; int skyDataOffset = blockDataOffset + 16 * 16 * 16 * count; for (int i = 0; i < 16; i++) { if (!chunk.isSectionEmpty(i)) { for (int oy = 0; oy < 16; oy++) { for (int oz = 0; oz < 16; oz++) { for (int ox = 0; ox < 16; ox++) { int y = oy + (i << 4); int id = chunk.getBlockTypeId(ox, y, oz); int dValue = chunk.getBlockData(ox, y, oz); data.setShort((offset << 1) + 3, (id << 4) | dValue); data.setByte(blockDataOffset + offset + 3, chunk.getBlockEmittedLight(ox, y, oz)); data.setByte(skyDataOffset + offset + 3, chunk.getBlockSkyLight(ox, y, oz)); offset++; } } } } } for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { data.setByte(skyDataOffset + offset + 3 + x + z * 16, ThinkBiome.bukkitToId(chunk.getBiome(x, z))); } } data.writerIndex(data.capacity()); try { GZIPOutputStream gzip = new GZIPOutputStream(new ByteBufOutputStream(out)); byte[] bytes = new byte[data.readableBytes()]; data.readBytes(bytes); gzip.write(bytes); gzip.close(); } catch (IOException e) { throw new RuntimeException(); } finally { data.release(); } }