Serializes a Diameter message (header plus AVPs) into the series
 of bytes representing it on the wire.

 @return [String] The byte-encoded form.
160676
def to_wire
      content = ''
      @avps.each { |a| content += a.to_wire }
      length_8, length_16 = Internals::UInt24.to_u8_and_u16(content.length + 20)
      code_8, code_16 = Internals::UInt24.to_u8_and_u16(@command_code)
      request_flag = @request ? '1' : '0'
      proxy_flag = @proxyable ? '1' : '0'
      flags_str = "#{request_flag}#{proxy_flag}000000"

      header = [@version, length_8, length_16, flags_str, code_8, code_16, @app_id, @hbh, @ete].pack('CCnB8CnNNN')
      header + content
    end
255946
def to_bytes()
      packer = Packer.new()

      full_flags = ((@qr     << 15) & 0x8000) |
                   ((@opcode << 11) & 0x7800) |
                   ((@flags  <<  7) & 0x0780) |
                   ((@rcode  <<  0) & 0x000F)

      packer.pack('nnnnnn',
        @trn_id,             # trn_id
        full_flags,          # qr, opcode, flags, rcode
        @questions.length(), # qdcount
        @answers.length(),   # ancount
        0,                   # nscount (we don't handle)
        0,                   # arcount (we don't handle)
      )

      questions.each do |q|
        q.pack(packer)
      end

      answers.each do |a|
        a.pack(packer)
      end

      return packer.get()
    end
1074025
def generate_bytes(cls, payload, fin_bit, opcode, mask_payload):
        
        # the first byte contains the FIN bit, the 3 RSV bits and the
        # 4 opcode bits and for a client will *always* be 1000 0001 (or 129).
        # so we want the first byte to look like...
        #
        #  1 0 0 0 0 0 0 1   (1 is a text frame)
        # +-+-+-+-+-------+
        # |F|R|R|R| opcode|
        # |I|S|S|S|       |
        # |N|V|V|V|       |
        # | |1|2|3|       |
        # +-+-+-+-+-------+
        # note that because all RSV bits are zero, we can ignore them

        # this shifts each bit into position and bitwise ORs them together,
        # using the struct module to pack them as incoming network bytes
        frame = pack(
            '!B', (
                (fin_bit << 7) | opcode
            )
        )

        # the second byte - and maybe the 7 after this, we'll use to tell
        # the server how long our payload is.

        #                 +-+-------------+-------------------------------+
        #                 |M| Payload len |    Extended payload length    |
        #                 |A|     (7)     |             (16/63)           |
        #                 |S|             |   (if payload len==126/127)   |
        #                 |K|             |                               |
        # +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
        # |     Extended payload length continued, if payload len == 127  |
        # + - - - - - - - - - - - - - - - +-------------------------------+

        # the mask is always included with client -> server, so the first bit
        # of the second byte is always 1 which flags that the data is masked,
        # i.e. encoded
        if mask_payload:
            mask_bit = 1 << 7
        else:
            mask_bit = 0 << 7
        # next we have to | this bit with the payload length.
        # note that we ensure that the payload is utf-8 encoded before we take
        # the length because unicode characters can be >1 bytes in length and
        # lead to bugs if we just do ``len(payload)``.
        length = len(payload.encode('utf-8'))

        if length >= Frame.MAX_LENGTH:
            raise WebsocktProtocolError("data is too long")

        # the second byte contains the payload length and mask
        if length < Frame.LENGTH_7:
            # we can simply represent payload length with first 7 bits
            frame += pack('!B', (mask_bit | length))
        elif length < Frame.LENGTH_16:
            frame += pack('!B', (mask_bit | 126)) + pack('!H', length)
        else:
            frame += pack('!B', (mask_bit | 127)) + pack('!Q', length)

        if mask_payload:
            # we always mask frames from the client to server
            # use a string of n random buffered_bytes for the mask
            mask_key = os.urandom(4)
            mask_data = cls.generate_mask(mask_key=mask_key, data=payload)
            mask = mask_key + mask_data
            frame += mask
        else:
            frame += bytearray(payload, 'utf-8')

        return bytearray(frame)
923604
def headers
      super.merge({
        size: @raw.length,
        # HACK - this will prevent errors, but will mangle anything that isn't
        # actually utf8. We should try to handle this upstream where we might
        # know what the actual encoding is.
        summary: @message.summary.force_encoding('UTF-8').scrub,
        message_class: @message_class.to_s,
      })
    end
512569
def marshal_dump
      h = to_h
      if h[:attributes]
        h[:attributes] = h[:attributes].clone
        h[:attributes].instance_variable_set(:@client, nil)
      end

      if h[:original_object]
        h[:original_object] = h[:original_object].clone
        h[:original_object].instance_variable_set(:@client, nil)

        if h[:original_object]['attributes']
          h[:original_object]['attributes'] = h[:original_object]['attributes'].clone
          h[:original_object]['attributes'].instance_variable_set(:@client, nil)
        end
      end
      h
    end
264233
def to_msgpack(*a)
      msg = {
        'msgpack_class' => self.class.name,
        'data'          => instance_variables.inject({}) do |m, ivar|
                             name = ivar.to_s.sub(/@/, '')
                             m[name] = instance_variable_get(ivar) unless NOT_SERIALIZED.include?(name)
                             m
                           end,
        'size'          => nil
      }.to_msgpack(*a)
      @size = msg.size
      # For msgpack 0.5.1 the to_msgpack result is a MessagePack::Packer so need to convert to string
      msg = msg.to_s.sub!(PACKET_SIZE_REGEXP) { |m| "size" + @size.to_msgpack }
      msg
    end
1289313
def split(self, encoded):
        
        maxlen = len(encoded) - self.sig_size
        message = encoded[:maxlen]
        signature = encoded[-self.sig_size:]
        return message, signature
299251
def to_bytes(self):
        '''
        
        '''
        return struct.pack(Arp._PACKFMT, self._hwtype.value, self._prototype.value, self._hwaddrlen, self._protoaddrlen, self._operation.value, self._senderhwaddr.packed, self._senderprotoaddr.packed, self._targethwaddr.packed, self._targetprotoaddr.packed)
159974
def to_bytearray(self):
        
        header = bytearray(struct.pack(
            '>HHHBB',
            self.transaction_id,
            self.protocol_id,
            self.length,
            self.unit_id,
            self.fcode
        ))
        return header + self.data
469902
def encode(self):
        
        ret = bytearray()
        for name, len, typecode in self.format:
            value = getattr(self, name)
            buf = struct.pack('<' + typecode, value)
            ret.extend(buf)
        return bytes(ret)
715537
def recv_msg(sock):
    
    data = recv(sock, struct.calcsize(METHOD_STRUCT_FORMAT))
    method, = struct.unpack(METHOD_STRUCT_FORMAT, data)
    data = recv(sock, struct.calcsize(SIZE_STRUCT_FORMAT))
    size, = struct.unpack(SIZE_STRUCT_FORMAT, data)
    data = recv(sock, size)
    msg = pickle.loads(data)
    return method, msg
1270204
def message
      return {} unless params[:_message]
      begin
        Marshal.load(Merb::Parse.unescape(params[:_message]).unpack("m").first)
      rescue ArgumentError, TypeError
        {}
      end
    end
234531
def to_bytes(self):
		
		chunks = [PNG_SIGN]
		chunks.extend(c[1] for c in self.chunks)
		return b"".join(chunks)
1262872
def to_s(filter = nil, version = nil)
      v = send(version) if version
      v = (v && v != DEFAULT_VERSION) ? " v#{v}" : ""
      log_msg = "[#{name}#{v}]"
      duration = if @duration && (filter.nil? || filter.include?(:duration))
        ", #{enough_precision(@duration)} sec"
      elsif @received_at && (filter.nil? || filter.include?(:local_duration))
        ", #{enough_precision(Time.now.to_f - @received_at)} sec"
      end
      log_msg += " (#{@size.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")} bytes#{duration})" if @size && !@size.to_s.empty?
      log_msg
    end
891525
def _generate_request_head_bytes(method, endpoint, headers):
    

    head_string = _FORMAT_METHOD_AND_ENDPOINT.format(method, endpoint)
    header_tuples = sorted((k, headers[k]) for k in headers)

    for name, value in header_tuples:
        if _should_sign_request_header(name):
            head_string += _FORMAT_HEADER_STRING.format(name, value)

    return (head_string + _DELIMITER_NEWLINE).encode()
1043398
def to_bytes(self):
        '''
        
        '''
        # Verify that the properties make sense
        self.sanitize()

        # Write the next header type
        bitstream = BitStream('uint:8=%d' % self.next_header)

        # Add the reserved bits
        bitstream += BitStream(8)

        # Add the fragment offset
        bitstream += BitStream('uint:13=%d' % self.fragment_offset)

        # Add the reserved bits
        bitstream += BitStream(2)

        # Add the flags
        bitstream += BitStream('bool=%d' % self.more_fragments)

        # Add the identification
        bitstream += BitStream('uint:32=%d' % self.identification)

        return bitstream.bytes + bytes(self.payload)
971884
def encoded
      ready_to_send!
      buffer = header.encoded
      buffer << "\r\n"
      buffer << body.encoded(content_transfer_encoding)
      buffer
    end