Class: GDB::Tube::Tube

Inherits:
Object
  • Object
show all
Defined in:
lib/gdb/tube/tube.rb

Overview

For simpler IO manipulation.

Constant Summary collapse

READ_SIZE =

Batch read size.

4096

Instance Method Summary collapse

Constructor Details

#initialize(io_in, io_out) ⇒ Tube

Instantiate a GDB::Tube::Tube object.

Parameters:

  • io_in (IO)

    Input.

  • io_out (IO)

    Output.


19
20
21
22
23
# File 'lib/gdb/tube/tube.rb', line 19

def initialize(io_in, io_out)
  @in = io_in
  @out = io_out
  @buffer = ::GDB::Tube::Buffer.new
end

Instance Method Details

#clearString

Clear received data.

Returns:

  • (String)

    The data cleared. An empty string is returned if the buffer is already empty.


42
43
44
# File 'lib/gdb/tube/tube.rb', line 42

def clear
  @buffer.get
end

#closevoid

This method returns an undefined value.

Close both side.


113
114
115
116
# File 'lib/gdb/tube/tube.rb', line 113

def close
  @in.close
  @out.close
end

#closed?Boolean

Is #close invoked?

Returns:

  • (Boolean)

121
122
123
# File 'lib/gdb/tube/tube.rb', line 121

def closed?
  @in.closed? && @out.closed?
end

#interact(output_hook) ⇒ void

This method returns an undefined value.

Enter interactive mode.

Parameters:

  • output_hook (Proc)

    String received from output would be passed into this proc. Only data yielded by this proc would be flushed to $stdout.

    Use lambda { |s, &block| block.call(s) } or :tap.to_proc (since Ruby 2.2) for a do-nothing hook.


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gdb/tube/tube.rb', line 93

def interact(output_hook)
  @out.ungetc(@buffer.get)
  loop do
    io, = IO.select([$stdin, @out])
    @in.write($stdin.readpartial(READ_SIZE)) if io.include?($stdin)
    next unless io.include?(@out)

    begin
      recv = @out.readpartial(READ_SIZE)
      output_hook.call(recv) { |str| $stdout.write(str) }
      @out.ungetc(@buffer.get) unless @buffer.empty?
    rescue Errno::EIO, EOFError
      break
    end
  end
end

#puts(data) ⇒ void

This method returns an undefined value.

Parameters:

  • data (#to_s)

    Data to be sent.


76
77
78
79
80
81
# File 'lib/gdb/tube/tube.rb', line 76

def puts(data)
  return data.split("\n").each(&method(:puts)) if data.strip.include?("\n")

  @in.puts(data)
  readuntil("\n")
end

#readn(n) ⇒ String

Read n bytes.

Parameters:

  • n (Integer)

    Number of bytes.

Returns:

  • (String)

    Returns n bytes from current buffer.


32
33
34
35
# File 'lib/gdb/tube/tube.rb', line 32

def readn(n)
  partial while @buffer.size < n
  @buffer.get(n)
end

#readuntil(str, drop: true) ⇒ Stirng

Receive from io until string str appears.

Parameters:

  • str (String)

    String to be looking for.

  • drop (Boolean) (defaults to: true)

    If need keep str in end of returned string.

Returns:

  • (Stirng)

    Data.


55
56
57
58
59
60
# File 'lib/gdb/tube/tube.rb', line 55

def readuntil(str, drop: true)
  cur = readn(str.size)
  cur << readn(1) until cur.index(str)
  cur.slice!(-str.size..-1) if drop
  cur
end

#unget(str) ⇒ nil

Put to front of buffer.

Parameters:

  • str (String)

Returns:

  • (nil)

67
68
69
70
# File 'lib/gdb/tube/tube.rb', line 67

def unget(str)
  @buffer.unshift(str)
  nil
end