当前位置: 首页>>代码示例>>Python>>正文


Python TlvEncoder.writeBuffer方法代码示例

本文整理汇总了Python中pyndn.encoding.tlv.tlv_encoder.TlvEncoder.writeBuffer方法的典型用法代码示例。如果您正苦于以下问题:Python TlvEncoder.writeBuffer方法的具体用法?Python TlvEncoder.writeBuffer怎么用?Python TlvEncoder.writeBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyndn.encoding.tlv.tlv_encoder.TlvEncoder的用法示例。


在下文中一共展示了TlvEncoder.writeBuffer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: wireEncode

# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBuffer [as 别名]
    def wireEncode(self, wireFormat = None):
        """
        Encode this as an NDN-TLV SafeBag.

        :return: The encoded byte array as a Blob.
        :rtype: Blob
        """
        # Encode directly as TLV. We don't support the WireFormat abstraction
        # because this isn't meant to go directly on the wire.
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.
        encoder.writeBlobTlv(
          Tlv.SafeBag_EncryptedKeyBag, self._privateKeyBag.buf())
        # Add the entire Data packet encoding as is.
        encoder.writeBuffer(
          self._certificate.wireEncode(TlvWireFormat.get()).buf())

        encoder.writeTypeAndLength(
          Tlv.SafeBag_SafeBag, len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
开发者ID:named-data,项目名称:PyNDN2,代码行数:25,代码来源:safe_bag.py

示例2: encodeInterest

# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBuffer [as 别名]
    def encodeInterest(self, interest):
        """
        Encode interest in NDN-TLV and return the encoding.

        :param Interest interest: The Interest object to encode.
        :return: A Tuple of (encoding, signedPortionBeginOffset,
          signedPortionEndOffset) where encoding is a Blob containing the
          encoding, signedPortionBeginOffset is the offset in the encoding of
          the beginning of the signed portion, and signedPortionEndOffset is
          the offset in the encoding of the end of the signed portion. The
          signed portion starts from the first name component and ends just
          before the final name component (which is assumed to be a signature
          for a signed interest).
        :rtype: (Blob, int, int)
        """
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.
        encoder.writeOptionalNonNegativeIntegerTlv(
          Tlv.SelectedDelegation, interest.getSelectedDelegationIndex())
        linkWireEncoding = interest.getLinkWireEncoding(self)
        if not linkWireEncoding.isNull():
          # Encode the entire link as is.
          encoder.writeBuffer(linkWireEncoding.buf())

        encoder.writeOptionalNonNegativeIntegerTlvFromFloat(
          Tlv.InterestLifetime, interest.getInterestLifetimeMilliseconds())

        # Encode the Nonce as 4 bytes.
        if interest.getNonce().size() == 0:
            # This is the most common case. Generate a nonce.
            nonce = bytearray(4)
            for i in range(4):
                nonce[i] = _systemRandom.randint(0, 0xff)
            encoder.writeBlobTlv(Tlv.Nonce, nonce)
        elif interest.getNonce().size() < 4:
            nonce = bytearray(4)
            # Copy existing nonce bytes.
            nonce[:interest.getNonce().size()] = interest.getNonce().buf()

            # Generate random bytes for remaining bytes in the nonce.
            for i in range(interest.getNonce().size(), 4):
                nonce[i] = _systemRandom.randint(0, 0xff)

            encoder.writeBlobTlv(Tlv.Nonce, nonce)
        elif interest.getNonce().size() == 4:
            # Use the nonce as-is.
            encoder.writeBlobTlv(Tlv.Nonce, interest.getNonce().buf())
        else:
            # Truncate.
            encoder.writeBlobTlv(Tlv.Nonce, interest.getNonce().buf()[:4])

        self._encodeSelectors(interest, encoder)

        (tempSignedPortionBeginOffset, tempSignedPortionEndOffset) = \
          self._encodeName(interest.getName(), encoder)
        signedPortionBeginOffsetFromBack = (len(encoder) -
                                            tempSignedPortionBeginOffset)
        signedPortionEndOffsetFromBack = (len(encoder) -
                                          tempSignedPortionEndOffset)

        encoder.writeTypeAndLength(Tlv.Interest, len(encoder) - saveLength)
        signedPortionBeginOffset = (len(encoder) -
                                    signedPortionBeginOffsetFromBack)
        signedPortionEndOffset = len(encoder) - signedPortionEndOffsetFromBack

        return (Blob(encoder.getOutput(), False), signedPortionBeginOffset,
                signedPortionEndOffset)
开发者ID:MAHIS,项目名称:PyNDN2,代码行数:71,代码来源:tlv_0_1_1_wire_format.py


注:本文中的pyndn.encoding.tlv.tlv_encoder.TlvEncoder.writeBuffer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。