Sha256: 08029e26401b6d2a996e2f8ccfc728b0a508bbb67c9e3dac7fadabb7b62a2b27

Contents?: true

Size: 1.29 KB

Versions: 18

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module GrpcKit
  module Transport
    module Packable
      # @param data [String]
      # @param compress [Boolean]
      # @return [String] packed value
      def pack(data, compress = false)
        c = compress ? 1 : 0
        [c, data.bytesize, data].pack('CNa*')
      end

      # @param data [String]
      # @return [String]
      def unpack(data)
        unpacker.feed(data) if data

        unpacker.read
      end

      def unpacker
        @unpacker ||= Unpacker.new
      end

      class Unpacker
        # Compressed bytes(1 Byte) + length bytes(4 Bytes)
        METADATA_SIZE = 5

        def initialize
          @data = +''.b
        end

        # @return [Boolean]
        def data_exist?
          !@data.empty?
        end

        # @param data [String]
        # @return [void]
        def feed(data)
          @data << data
        end

        # @return [nil, Array<Boolean, Integer, String>]
        def read
          return nil if @data.empty?

          d = @data.freeze
          metadata = d.byteslice(0, METADATA_SIZE)
          c, size = metadata.unpack('CN')
          data = @data.byteslice(METADATA_SIZE, size)
          @data = @data.byteslice(METADATA_SIZE + size, @data.bytesize)
          [c != 0, size, data]
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
grpc_kit-0.3.9 lib/grpc_kit/transport/packable.rb
grpc_kit-0.3.8 lib/grpc_kit/transport/packable.rb
grpc_kit-0.3.7 lib/grpc_kit/transport/packable.rb
grpc_kit-0.3.6 lib/grpc_kit/transport/packable.rb
grpc_kit-0.3.5 lib/grpc_kit/transport/packable.rb
grpc_kit-0.3.4 lib/grpc_kit/transport/packable.rb
grpc_kit-0.3.3 lib/grpc_kit/transport/packable.rb
grpc_kit-0.3.2 lib/grpc_kit/transport/packable.rb
grpc_kit-0.3.1 lib/grpc_kit/transport/packable.rb
grpc_kit-0.3.0 lib/grpc_kit/transport/packable.rb
grpc_kit-0.2.1 lib/grpc_kit/transport/packable.rb
grpc_kit-0.2.0 lib/grpc_kit/transport/packable.rb
grpc_kit-0.1.14 lib/grpc_kit/transport/packable.rb
grpc_kit-0.1.13 lib/grpc_kit/transport/packable.rb
grpc_kit-0.1.12 lib/grpc_kit/transport/packable.rb
grpc_kit-0.1.11 lib/grpc_kit/transport/packable.rb
grpc_kit-0.1.10 lib/grpc_kit/transport/packable.rb
grpc_kit-0.1.9 lib/grpc_kit/transport/packable.rb