Example usage for io.netty.buffer ByteBuf setShort

List of usage examples for io.netty.buffer ByteBuf setShort

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf setShort.

Prototype

public abstract ByteBuf setShort(int index, int value);

Source Link

Document

Sets the specified 16-bit short integer at the specified absolute index in this buffer.

Usage

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 * ??     </br>//from  w w  w . j  a  v  a  2s.co m
 * Encoder buffer       </br>
 * ?2         </br>
 * proto buffer         </br>
 *             </br>
 *
 * @param arg1
 * @param arg2
 * @param messageLite
 * @return + ID+protoBufferData
 */
public static ByteBuf wrappedBufferShort(int arg1, int arg2, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 10 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x2);//writeIndex?
    buffer.writeByte(arg1);
    buffer.writeByte(arg2);
    buffer.writeBytes(messageLite.toByteArray());
    /**?2*/
    buffer.setShort(0, buffer.writerIndex() - 0x2);
    messageLite = null;
    return buffer;
}

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 * Encoder buffer            </br>
 * 2            </br>//from   w  w w  .ja  va2 s. co  m
 *
 * @param ID          ??
 * @param messageLite byte[]
 * @return
 */
public static ByteBuf wrappedBufferShort(int ID, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 6 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x2);//writeIndex?     2
    buffer.writeInt(ID); //?                4
    buffer.writeBytes(src);
    buffer.setShort(0, buffer.writerIndex() - 0x2); //  short
    messageLite = null; //set null ,collection by GC
    return buffer;
}

From source file:nettyClient4.clientImpl.java

License:Apache License

private ByteBuf getWriteBuffer(int arg1, int arg2, ByteBuf buffer, Object... paras) {
    if (buffer == null) {
        buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(10);
    }//from  ww w .j a  v  a 2s  .  c  o  m
    buffer.writeShort(Short.MIN_VALUE);//?2
    buffer.writeByte(arg1);
    if (arg2 != 0)
        buffer.writeByte(arg2);
    for (Object para : paras) {
        if (para instanceof Byte) {
            buffer.writeByte((Byte) para); // ?1
        } else if ((para instanceof String)) {
            buffer.writeBytes(((String) para).getBytes());
        } else if (para instanceof Integer) {
            buffer.writeInt((Integer) para); //?4
        } else if (para instanceof Short) {
            buffer.writeShort((Short) para); //?2
        }
    }
    /**?2setShort*/
    buffer.setShort(0, buffer.writerIndex() - 0x2);
    return buffer;
}

From source file:org.bgp4j.netty.protocol.BGPv4Packet.java

License:Apache License

/**
 * build a binary representation of the protocol packet
 * //from   w w  w  . ja va 2 s. c  om
 * @param buffer The buffer to store the binary packet representation into
 */
public void encodePacket(ByteBuf buffer) {
    for (int i = 0; i < BGPv4Constants.BGP_PACKET_MARKER_LENGTH; i++)
        buffer.writeByte(0xff);

    int headerHeaderIndex = buffer.writerIndex();

    buffer.writeShort(BGPv4Constants.BGP_PACKET_HEADER_LENGTH);

    buffer.writeByte(getType());
    encodePayload(buffer);

    buffer.setShort(headerHeaderIndex,
            BGPv4Constants.BGP_PACKET_HEADER_LENGTH + (buffer.writerIndex() - headerHeaderIndex) - 3);
}

From source file:org.bgp4j.netty.protocol.refresh.RouteRefreshPacket.java

License:Apache License

@Override
protected void encodePayload(ByteBuf buffer) {

    buffer.writeShort(getAddressFamily().toCode());
    buffer.writeByte(0);//  w  ww  . j  a  v  a 2 s . com
    buffer.writeByte(getSubsequentAddressFamily().toCode());

    if (outboundRouteFilter != null) {
        buffer.writeByte(outboundRouteFilter.getRefreshType().toCode());

        for (ORFType type : outboundRouteFilter.getEntries().keySet()) {
            buffer.writeByte(type.toCode());

            int entriesLengthIndex = buffer.writerIndex();

            buffer.writeShort(0); // placeholder for entries length

            for (ORFEntry entry : outboundRouteFilter.getEntries().get(type)) {
                ORFEntryCodec.encodeORFEntry(buffer, entry);
            }

            buffer.setShort(entriesLengthIndex, buffer.writerIndex() - entriesLengthIndex - 2);
        }
    }
}

From source file:org.bgp4j.netty.protocol.update.UpdatePacket.java

License:Apache License

@Override
protected void encodePayload(ByteBuf buffer) {
    int withdrawnRoutesLengthIndex = buffer.writerIndex();

    buffer.writeShort(0); // placeholder for withdrawn routes length
    encodeWithdrawnRoutes(buffer);//from ww w.  j  a v a  2s.  c  o m
    buffer.setShort(withdrawnRoutesLengthIndex, buffer.writerIndex() - withdrawnRoutesLengthIndex - 2);

    int pathAttributesLengthIndex = buffer.writerIndex();

    buffer.writeShort(0); // placeholder for path attributes length
    encodePathAttributes(buffer);
    buffer.setShort(pathAttributesLengthIndex, buffer.writerIndex() - pathAttributesLengthIndex - 2);

    encodeNlris(buffer);
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private void serializeTableFeaturesBody(MultipartReplyBody body, ByteBuf outBuffer) {
    MultipartReplyTableFeaturesCase tableFeaturesCase = (MultipartReplyTableFeaturesCase) body;
    MultipartReplyTableFeatures tableFeatures = tableFeaturesCase.getMultipartReplyTableFeatures();
    for (TableFeatures tableFeature : tableFeatures.getTableFeatures()) {
        ByteBuf tableFeatureBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
        tableFeatureBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
        tableFeatureBuff.writeByte(tableFeature.getTableId());
        tableFeatureBuff.writeZero(TABLE_FEATURES_PADDING);
        write32String(tableFeature.getName(), tableFeatureBuff);
        tableFeatureBuff.writeBytes(tableFeature.getMetadataMatch());
        tableFeatureBuff.writeZero(64 - tableFeature.getMetadataMatch().length);
        tableFeatureBuff.writeBytes(tableFeature.getMetadataWrite());
        tableFeatureBuff.writeZero(64 - tableFeature.getMetadataWrite().length);
        writeTableConfig(tableFeature.getConfig(), tableFeatureBuff);
        tableFeatureBuff.writeInt(tableFeature.getMaxEntries().intValue());
        for (TableFeatureProperties tableFeatureProp : tableFeature.getTableFeatureProperties()) {
            switch (tableFeatureProp.getType()) {
            case OFPTFPTINSTRUCTIONS:
                writeInstructionRelatedTableProperty(tableFeatureBuff, tableFeatureProp, INSTRUCTIONS_CODE);
                break;
            case OFPTFPTINSTRUCTIONSMISS:
                writeInstructionRelatedTableProperty(tableFeatureBuff, tableFeatureProp,
                        INSTRUCTIONS_MISS_CODE);
                break;
            case OFPTFPTNEXTTABLES:
                writeNextTableRelatedTableProperty(tableFeatureBuff, tableFeatureProp, NEXT_TABLE_CODE);
                break;
            case OFPTFPTNEXTTABLESMISS:
                writeNextTableRelatedTableProperty(tableFeatureBuff, tableFeatureProp, NEXT_TABLE_MISS_CODE);
                break;
            case OFPTFPTWRITEACTIONS:
                writeActionsRelatedTableProperty(tableFeatureBuff, tableFeatureProp, WRITE_ACTIONS_CODE);
                break;
            case OFPTFPTWRITEACTIONSMISS:
                writeActionsRelatedTableProperty(tableFeatureBuff, tableFeatureProp, WRITE_ACTIONS_MISS_CODE);
                break;
            case OFPTFPTAPPLYACTIONS:
                writeActionsRelatedTableProperty(tableFeatureBuff, tableFeatureProp, APPLY_ACTIONS_CODE);
                break;
            case OFPTFPTAPPLYACTIONSMISS:
                writeActionsRelatedTableProperty(tableFeatureBuff, tableFeatureProp, APPLY_ACTIONS_MISS_CODE);
                break;
            case OFPTFPTMATCH:
                writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, MATCH_CODE);
                break;
            case OFPTFPTWILDCARDS:
                writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, WILDCARDS_CODE);
                break;
            case OFPTFPTWRITESETFIELD:
                writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, WRITE_SETFIELD_CODE);
                break;
            case OFPTFPTWRITESETFIELDMISS:
                writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, WRITE_SETFIELD_MISS_CODE);
                break;
            case OFPTFPTAPPLYSETFIELD:
                writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, APPLY_SETFIELD_CODE);
                break;
            case OFPTFPTAPPLYSETFIELDMISS:
                writeOxmRelatedTableProperty(tableFeatureBuff, tableFeatureProp, APPLY_SETFIELD_MISS_CODE);
                break;
            case OFPTFPTEXPERIMENTER:
                writeExperimenterRelatedTableProperty(tableFeatureBuff, tableFeatureProp);
                break;
            case OFPTFPTEXPERIMENTERMISS:
                writeExperimenterRelatedTableProperty(tableFeatureBuff, tableFeatureProp);
                break;
            }// w w  w  .ja v a2s.c  o  m
        }
        tableFeatureBuff.setShort(TABLE_FEATURES_LENGTH_INDEX, tableFeatureBuff.readableBytes());
        outBuffer.writeBytes(tableFeatureBuff);
    }
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private void writeOxmRelatedTableProperty(final ByteBuf output, final TableFeatureProperties property,
        final byte code) {
    int startIndex = output.writerIndex();
    output.writeShort(code);/*from w ww  .j  av a 2s.  c  o  m*/
    int lengthIndex = output.writerIndex();
    output.writeShort(EncodeConstants.EMPTY_LENGTH);
    List<MatchEntry> entries = property.getAugmentation(OxmRelatedTableFeatureProperty.class).getMatchEntry();
    if (entries != null) {
        TypeKeyMaker<MatchEntry> keyMaker = TypeKeyMakerFactory
                .createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
        ListSerializer.serializeHeaderList(entries, keyMaker, registry, output);
    }
    int length = output.writerIndex() - startIndex;
    output.setShort(lengthIndex, length);
    output.writeZero(paddingNeeded(length));
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private void writeActionsRelatedTableProperty(final ByteBuf output, final TableFeatureProperties property,
        final byte code) {
    int startIndex = output.writerIndex();
    output.writeShort(code);//from  www. j  a  va2s .  com
    int lengthIndex = output.writerIndex();
    output.writeShort(EncodeConstants.EMPTY_LENGTH);
    List<Action> actions = property.getAugmentation(ActionRelatedTableFeatureProperty.class).getAction();
    if (actions != null) {
        TypeKeyMaker<Action> keyMaker = TypeKeyMakerFactory
                .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID);
        ListSerializer.serializeHeaderList(actions, keyMaker, registry, output);
    }
    int length = output.writerIndex() - startIndex;
    output.setShort(lengthIndex, length);
    output.writeZero(paddingNeeded(length));
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private static void writeNextTableRelatedTableProperty(final ByteBuf output,
        final TableFeatureProperties property, final byte code) {
    int startIndex = output.writerIndex();
    output.writeShort(code);//from  ww w.jav a 2 s  .co  m
    int lengthIndex = output.writerIndex();
    output.writeShort(EncodeConstants.EMPTY_LENGTH);
    List<NextTableIds> nextTableIds = property.getAugmentation(NextTableRelatedTableFeatureProperty.class)
            .getNextTableIds();
    if (nextTableIds != null) {
        for (NextTableIds next : nextTableIds) {
            output.writeByte(next.getTableId());
        }
    }
    int length = output.writerIndex() - startIndex;
    output.setShort(lengthIndex, length);
    output.writeZero(paddingNeeded(length));
}