123456789_123456789_123456789_123456789_123456789_

Class: Net::BufferedIO

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/net/protocol.rb

Overview

internal use only

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(io, read_timeout: 60, continue_timeout: nil, debug_output: nil) ⇒ BufferedIO

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 82

def initialize(io, read_timeout: 60, continue_timeout: nil, debug_output: nil)
  @io = io
  @read_timeout = read_timeout
  @continue_timeout = continue_timeout
  @debug_output = debug_output
  @rbuf = ''.dup
end

Instance Attribute Details

#closed?Boolean (readonly)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 103

def closed?
  @io.closed?
end

#continue_timeout (rw)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 92

attr_accessor :continue_timeout

#debug_output (rw)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 93

attr_accessor :debug_output

#eof?Boolean (readonly)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 99

def eof?
  @io.eof?
end

#io (readonly)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 90

attr_reader :io

#read_timeout (rw)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 91

attr_accessor :read_timeout

Instance Method Details

#<<(str)

Alias for #write.

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 211

alias << write

#close

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 107

def close
  @io.close
end

#inspect

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 95

def inspect
  "#<#{self.class} io=#{@io}>"
end

LOG(msg) (private)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 253

def LOG(msg)
  return unless @debug_output
  @debug_output << msg + "\n"
end

LOG_off (private)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 244

def LOG_off
  @save_debug_out = @debug_output
  @debug_output = nil
end

LOG_on (private)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 249

def LOG_on
  @debug_output = @save_debug_out
end

#rbuf_consume(len) (private)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 193

def rbuf_consume(len)
  s = @rbuf.slice!(0, len)
  @debug_output << %Q[-> #{s.dump}\n] if @debug_output
  s
end

#rbuf_fill (private)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 174

def rbuf_fill
  case rv = @io.read_nonblock(BUFSIZE, exception: false)
  when String
    @rbuf << rv
    rv.clear
    return
  when :wait_readable
    @io.to_io.wait_readable(@read_timeout) or raise Net::ReadTimeout
    # continue looping
  when :wait_writable
    # OpenSSL::Buffering#read_nonblock may fail with IO::WaitWritable.
    # http://www.openssl.org/support/faq.html#PROG10
    @io.to_io.wait_writable(@read_timeout) or raise Net::ReadTimeout
    # continue looping
  when nil
    raise EOFError, 'end of file reached'
  end while true
end

#read(len, dest = ''.dup, ignore_eof = false)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 117

def read(len, dest = ''.dup, ignore_eof = false)
  LOG "reading #{len} bytes..."
  read_bytes = 0
  begin
    while read_bytes + @rbuf.size < len
      s = rbuf_consume(@rbuf.size)
      read_bytes += s.size
      dest << s
      rbuf_fill
    end
    s = rbuf_consume(len - read_bytes)
    read_bytes += s.size
    dest << s
  rescue EOFError
    raise unless ignore_eof
  end
  LOG "read #{read_bytes} bytes"
  dest
end

#read_all(dest = ''.dup)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 137

def read_all(dest = ''.dup)
  LOG 'reading all...'
  read_bytes = 0
  begin
    while true
      s = rbuf_consume(@rbuf.size)
      read_bytes += s.size
      dest << s
      rbuf_fill
    end
  rescue EOFError
    ;
  end
  LOG "read #{read_bytes} bytes"
  dest
end

#readline

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 166

def readline
  readuntil("\n").chop
end

#readuntil(terminator, ignore_eof = false)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 154

def readuntil(terminator, ignore_eof = false)
  begin
    until idx = @rbuf.index(terminator)
      rbuf_fill
    end
    return rbuf_consume(idx + terminator.size)
  rescue EOFError
    raise unless ignore_eof
    return rbuf_consume(@rbuf.size)
  end
end

#write(str) Also known as: #<<

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 205

def write(str)
  writing {
    write0 str
  }
end

#write0(str) (private)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 231

def write0(str)
  @debug_output << str.dump if @debug_output
  len = @io.write(str)
  @written_bytes += len
  len
end

#writeline(str)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 213

def writeline(str)
  writing {
    write0 str + "\r\n"
  }
end

#writing (private)

[ GitHub ]

  
# File 'lib/net/protocol.rb', line 221

def writing
  @written_bytes = 0
  @debug_output << '<- ' if @debug_output
  yield
  @debug_output << "\n" if @debug_output
  bytes = @written_bytes
  @written_bytes = nil
  bytes
end