Class: Lich::Common::PipeIO
- Inherits:
-
Object
- Object
- Lich::Common::PipeIO
- Defined in:
- documented/common/pipe_io.rb
Overview
Duplex IO adapter that lets stdin/stdout stand in for a front-end client
socket in --pipe mode. Reads come from input ($stdin), writes go to
output ($stdout).
Designed to be wrapped in a SynchronizedSocket, exactly like the real client TCPSocket. The rest of the codebase then talks to $CLIENT the same way it always does (#gets / #write / #puts / #alive? / #close).
Liveness is defined as "have not yet hit EOF on the input stream": once #gets returns nil (the upstream pipe closed), #closed? returns true, so SynchronizedSocket#alive? (@alive && !delegate.closed?) flips to false and the normal disconnect/shutdown path runs.
Instance Method Summary collapse
-
#close ⇒ nil
Marks the IO adapter as closed and flushes pending output.
-
#closed? ⇒ Boolean
Consulted (through SynchronizedSocket#alive?) by the server read loop's retry guard and the client thread.
-
#gets(*args) ⇒ Object
Client read loop calls this via SynchronizedSocket#method_missing.
-
#initialize(input: $stdin, output: $stdout) ⇒ void
constructor
Creates a duplex IO adapter wrapping standard input and output streams.
-
#puts(*args, &block) ⇒ nil
private
Writes lines to the output stream.
-
#sync ⇒ Boolean
private
Returns whether the output stream is in synchronous write mode.
-
#sync=(value) ⇒ Boolean
private
Sets whether the output stream flushes after each write.
-
#write(*args, &block) ⇒ Integer
private
Writes data to the output stream.
Constructor Details
#initialize(input: $stdin, output: $stdout) ⇒ void
Creates a duplex IO adapter wrapping standard input and output streams.
25 26 27 28 29 30 |
# File 'documented/common/pipe_io.rb', line 25 def initialize(input: $stdin, output: $stdout) @input = input @output = output @output.sync = true # pipes must flush downstream output immediately @eof = false end |
Instance Method Details
#close ⇒ nil
Marks the IO adapter as closed and flushes pending output.
Sets the EOF flag to true, signaling to SynchronizedSocket#alive? that the pipe is no longer active. The standard input and output file descriptors themselves are not closed.
75 76 77 78 79 |
# File 'documented/common/pipe_io.rb', line 75 def close @eof = true @output.flush rescue nil # Intentionally do not close the $stdin/$stdout file descriptors. end |
#closed? ⇒ Boolean
Consulted (through SynchronizedSocket#alive?) by the server read loop's retry guard and the client thread. True once the input stream is spent.
64 65 66 |
# File 'documented/common/pipe_io.rb', line 64 def closed? @eof end |
#gets(*args) ⇒ Object
Client read loop calls this via SynchronizedSocket#method_missing. Returns nil at EOF, which both ends the read loop and marks us closed.
34 35 36 37 38 |
# File 'documented/common/pipe_io.rb', line 34 def gets(*args) line = @input.gets(*args) @eof = true if line.nil? line end |
#puts(*args, &block) ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Writes lines to the output stream.
Delegates to the wrapped output stream's puts method.
58 59 60 |
# File 'documented/common/pipe_io.rb', line 58 def puts(*args, &block) @output.puts(*args, &block) end |
#sync ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns whether the output stream is in synchronous write mode.
94 95 96 |
# File 'documented/common/pipe_io.rb', line 94 def sync @output.sync end |
#sync=(value) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets whether the output stream flushes after each write.
86 87 88 |
# File 'documented/common/pipe_io.rb', line 86 def sync=(value) @output.sync = value end |
#write(*args, &block) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Writes data to the output stream.
Delegates to the wrapped output stream's write method.
47 48 49 |
# File 'documented/common/pipe_io.rb', line 47 def write(*args, &block) @output.write(*args, &block) end |