Class: GrpcKit::Session::IO

Inherits:
Object
  • Object
show all
Defined in:
lib/grpc_kit/session/io.rb

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ IO

Returns a new instance of IO.



8
9
10
# File 'lib/grpc_kit/session/io.rb', line 8

def initialize(io)
  @io = io
end

Instance Method Details

#closeObject



12
13
14
# File 'lib/grpc_kit/session/io.rb', line 12

def close
  @io.close
end

#flushObject



47
48
49
# File 'lib/grpc_kit/session/io.rb', line 47

def flush
  @io.flush
end

#recv_event(length) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/grpc_kit/session/io.rb', line 16

def recv_event(length)
  data = @io.read_nonblock(length, nil, exception: false)

  case data
  when :wait_readable
    DS9::ERR_WOULDBLOCK
  when nil # nil means EOF
    DS9::ERR_EOF
  else
    data
  end
end

#send_event(data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/grpc_kit/session/io.rb', line 29

def send_event(data)
  return 0 if data.empty?

  bytes = @io.write_nonblock(data, exception: false)
  if bytes == :wait_writable
    DS9::ERR_WOULDBLOCK
  else
    bytes
  end
end

#wait_readableObject



40
41
42
43
44
45
# File 'lib/grpc_kit/session/io.rb', line 40

def wait_readable
  ::IO.select([@io], [], [])
  true
rescue IOError
  false
end