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

#flushvoid

This method returns an undefined value.


60
61
62
# File 'lib/grpc_kit/session/io.rb', line 60

def flush
  @io.flush
end

#recv_event(length) ⇒ DS9::ERR_WOULDBLOCK, ...

Parameters:

  • length (Integer)

Returns:

  • (DS9::ERR_WOULDBLOCK, DS9::ERR_EOF, String)

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

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) ⇒ void

This method returns an undefined value.

Blocking until io object is readable or writable


55
56
57
# File 'lib/grpc_kit/session/io.rb', line 55

def select(timeout = 1)
  ::IO.select([@io], [@io], [], timeout)
end

#send_event(data) ⇒ DS9::ERR_WOULDBLOCK, Integer

Parameters:

  • data (String)

Returns:

  • (DS9::ERR_WOULDBLOCK, Integer)

33
34
35
36
37
38
39
40
41
42
# File 'lib/grpc_kit/session/io.rb', line 33

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_readablevoid

This method returns an undefined value.

Blocking until io object is readable


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

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