Class: GrpcKit::Stream

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
GrpcKit::Streams::Packable
Defined in:
lib/grpc_kit/stream.rb

Instance Method Summary collapse

Methods included from GrpcKit::Streams::Packable

#pack, #unpack, #unpacker

Constructor Details

#initialize(protobuf:, session:, stream:) ⇒ Stream

Returns a new instance of Stream.



17
18
19
20
21
# File 'lib/grpc_kit/stream.rb', line 17

def initialize(protobuf:, session:, stream:)
  @protobuf = protobuf
  @session = session
  @stream = stream
end

Instance Method Details

#eachObject



23
24
25
# File 'lib/grpc_kit/stream.rb', line 23

def each
  loop { yield(recv) }
end

#recv(last: false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/grpc_kit/stream.rb', line 32

def recv(last: false)
  data = unpack(read(last: last))

  unless data
    raise StopIteration
  end

  compressed, size, buf = *data

  unless size == buf.size
    raise "inconsistent data: #{buf}"
  end

  if compressed
    raise 'compress option is unsupported'
  end

  @protobuf.decode(buf)
end

#send(data, last: false) ⇒ Object



27
28
29
30
# File 'lib/grpc_kit/stream.rb', line 27

def send(data, last: false)
  req = @protobuf.encode(data)
  @stream.write_send_data(pack(req), last: last)
end