Class: GrpcKit::Streams::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/grpc_kit/streams/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(path:, protobuf:, session:, authority:) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/grpc_kit/streams/client.rb', line 9

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

Instance Method Details

#close_and_recvObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/grpc_kit/streams/client.rb', line 48

def close_and_recv
  unless @stream
    raise 'You should call `send` method to send data'
  end

  unless @stream.end_write?
    @session.resume_data(@stream.stream_id)
  end

  @stream.end_write
  @session.start(@stream.stream_id)
  @stream.end_read

  data = []
  @stream.each { |d| data.push(d) }
  data
end

#each(&block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/grpc_kit/streams/client.rb', line 32

def each(&block)
  unless @stream
    raise 'You should call `send` method to send data'
  end

  @stream.each(&block)
end

#recv(last: false) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/grpc_kit/streams/client.rb', line 40

def recv(last: false)
  unless @stream
    raise 'You should call `send` method to send data'
  end

  @stream.recv(last: last)
end

#send_msg(data, metadata: {}, timeout: nil, last: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/grpc_kit/streams/client.rb', line 17

def send_msg(data, metadata: {}, timeout: nil, last: false)
  if @stream
    unless @stream.end_write?
      @session.resume_data(@stream.stream_id)
    end
  else
    headers = build_headers(metadata: , timeout: timeout)
    stream = @session.start_request(GrpcKit::Streams::SendBuffer.new, headers)
    @stream = GrpcKit::Stream.new(protobuf: @protobuf, session: @session, stream: stream)
  end

  @stream.send(data, last: last)
  @session.run_once
end