class GrpcKit::Session::IO

Public Class Methods

new(io) click to toggle source
# File lib/grpc_kit/session/io.rb, line 8
def initialize(io)
  @io = io
  @wake_o, @wake_i = ::IO.pipe
end

Public Instance Methods

close() click to toggle source
# File lib/grpc_kit/session/io.rb, line 13
def close
  @wake_i.close
  @wake_o.close
  @io.close
end
flush() click to toggle source

@return [void]

# File lib/grpc_kit/session/io.rb, line 74
def flush
  @io.flush
end
recv_event(length) click to toggle source

@param length [Integer] @return [DS9::ERR_WOULDBLOCK, DS9::ERR_EOF, String]

# File lib/grpc_kit/session/io.rb, line 21
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
select(timeout: 1, write: true) click to toggle source

Blocking until io object is readable or writable @return [void]

# File lib/grpc_kit/session/io.rb, line 58
def select(timeout: 1, write: true)
  rs, ws = ::IO.select([@io, @wake_o], write ? [@io] : [], [], timeout)
  @wake_o.read(@wake_o.stat.size) if rs&.delete(@wake_o) && !@wake_o.closed?
  [rs || [], ws || []]
end
send_event(data) click to toggle source

@param data [String] @return [DS9::ERR_WOULDBLOCK, Integer]

# File lib/grpc_kit/session/io.rb, line 36
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_readable() click to toggle source

Blocking until io object is readable @return [void]

# File lib/grpc_kit/session/io.rb, line 49
def wait_readable
  ::IO.select([@io], [], [])
  true
rescue IOError
  false
end
wake!(memo = nil) click to toggle source

Wake thread blocked at select method @param [Symbol] Indicate what event needed to invoke blocking thread. This argument is for debugging purpose.

# File lib/grpc_kit/session/io.rb, line 66
def wake!(memo = nil)
  @wake_i.write_nonblock(?\0, exception: false)
rescue Errno::EPIPE
rescue IOError
  raise unless @wake_i.closed?
end