Class: Lich::Common::PipeIO

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(input: $stdin, output: $stdout) ⇒ void

Creates a duplex IO adapter wrapping standard input and output streams.

Parameters:

  • input (IO) (defaults to: $stdin)

    the input stream to read from (default: $stdin)

  • output (IO) (defaults to: $stdout)

    the output stream to write to (default: $stdout)



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

#closenil

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.

Returns:

  • (nil)


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.

Returns:

  • (Boolean)


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.

Parameters:

  • args (Object)

    arguments passed to the output stream's puts method

Returns:

  • (nil)


58
59
60
# File 'documented/common/pipe_io.rb', line 58

def puts(*args, &block)
  @output.puts(*args, &block)
end

#syncBoolean

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.

Returns:

  • (Boolean)

    true if the output stream flushes after each write



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.

Parameters:

  • value (Boolean)

    true to enable synchronous writes, false otherwise

Returns:

  • (Boolean)


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.

Parameters:

  • args (Object)

    arguments passed to the output stream's write method

Returns:

  • (Integer)

    the number of bytes written



47
48
49
# File 'documented/common/pipe_io.rb', line 47

def write(*args, &block)
  @output.write(*args, &block)
end