Class: Goru::Routines::IO

Inherits:
Goru::Routine show all
Defined in:
lib/goru/routines/io.rb

Overview

public

Constant Summary collapse

INTENTS =
i[r w].freeze

Instance Attribute Summary collapse

Attributes inherited from Goru::Routine

#state, #status

Instance Method Summary collapse

Methods inherited from Goru::Routine

#call, #finished, #reactor=, #result, #sleep, #update, #wake

Constructor Details

#initialize(state = nil, io:, intent:, &block) ⇒ IO

Returns a new instance of IO.



12
13
14
15
16
17
18
19
20
# File 'lib/goru/routines/io.rb', line 12

def initialize(state = nil, io:, intent:, &block)
  super(state, &block)

  @io = io
  @intent = normalize_intent(intent)
  @status = :selecting
  @monitor = nil
  @finishers = []
end

Instance Attribute Details

#intentObject

public


24
25
26
# File 'lib/goru/routines/io.rb', line 24

def intent
  @intent
end

#ioObject (readonly)

public


24
25
26
# File 'lib/goru/routines/io.rb', line 24

def io
  @io
end

#monitor=(value) ⇒ Object (writeonly)

Sets the attribute monitor

Parameters:

  • value

    the value to set the attribute monitor to.



26
27
28
# File 'lib/goru/routines/io.rb', line 26

def monitor=(value)
  @monitor = value
end

Instance Method Details

#acceptObject

public


30
31
32
# File 'lib/goru/routines/io.rb', line 30

def accept
  @io.accept_nonblock
end

#bridge(channel, intent:) ⇒ Object

public


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/goru/routines/io.rb', line 84

def bridge(channel, intent:)
  intent = normalize_intent(intent)
  validate_intent!(intent)

  bridge = case intent
  when :r
    Bridges::Readable.new(routine: self, channel: channel)
  when :w
    Bridges::Writable.new(routine: self, channel: channel)
  end

  on_finished { bridge.finished }
  @reactor.adopt_routine(bridge)
  bridge
end

#on_finished(&block) ⇒ Object

public


102
103
104
# File 'lib/goru/routines/io.rb', line 102

def on_finished(&block)
  @finishers << block
end

#read(bytes) ⇒ Object

public


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/goru/routines/io.rb', line 36

def read(bytes)
  result = @io.read_nonblock(bytes, exception: false)

  case result
  when nil
    finished
    nil
  when :wait_readable
    # nothing to do
  else
    result
  end
rescue Errno::ECONNRESET
  finished
  nil
end

#write(data) ⇒ Object

public


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/goru/routines/io.rb', line 55

def write(data)
  result = @io.write_nonblock(data, exception: false)

  case result
  when nil
    finished
    nil
  when :wait_writable
    # nothing to do
  else
    result
  end
rescue Errno::ECONNRESET
  finished
  nil
end