Sha256: 928aaf8a624350bea3e36d3fb0bf603634b94310ada5a230bf98c5cd787af249

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

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

      # @params data [String]
      def unpack(data)
        if data
          unpacker.feed(data)
        end

        if unpacker.readable?
          return unpacker.read
        end

        nil
      end

      def unpacker
        @unpacker ||= GrpcKit::Streams::Packable::Unpacker.new
      end

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

        def initialize
          @i = 0
          @data = nil
        end

        def readable?
          @data && !@data.empty?
        end

        def feed(data)
          if @data
            @data << data
          else
            @data = data
          end
        end

        def read
          metadata = @data.slice!(0, METADATA_SIZE)
          c, size = metadata.unpack('CN')
          # TODO: more efficient code
          data = @data.slice!(0, size)
          [c != 0, size, data]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grpc_kit-0.1.3 lib/grpc_kit/streams/packable.rb