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, write_timeout: 60, continue_timeout: nil, debug_output: nil) ⇒ BufferedIO

[ GitHub ]

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

def initialize(io, read_timeout: 60, write_timeout: 60, continue_timeout: nil, debug_output: nil)
  @io = io
  @read_timeout = read_timeout
  @write_timeout = write_timeout
  @continue_timeout = continue_timeout
  @debug_output = debug_output
  @rbuf = ''.b
  @rbuf_empty = true
  @rbuf_offset = 0
end

Instance Attribute Details

#closed?Boolean (readonly)

[ GitHub ]

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

def closed?
  @io.closed?
end

#continue_timeout (rw)

[ GitHub ]

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

attr_accessor :continue_timeout

#debug_output (rw)

[ GitHub ]

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

attr_accessor :debug_output

#eof?Boolean (readonly)

[ GitHub ]

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

def eof?
  @io.eof?
end

#io (readonly)

[ GitHub ]

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

attr_reader :io

#read_timeout (rw)

[ GitHub ]

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

attr_accessor :read_timeout

#write_timeout (rw)

[ GitHub ]

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

attr_accessor :write_timeout

Instance Method Details

#<<(*strs)

Alias for #write.

[ GitHub ]

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

alias << write

#close

[ GitHub ]

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

def close
  @io.close
end

#inspect

[ GitHub ]

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

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

LOG(msg) (private)

[ GitHub ]

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

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

LOG_off (private)

[ GitHub ]

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

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

LOG_on (private)

[ GitHub ]

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

def LOG_on
  @debug_output = @save_debug_out
end

#rbuf_consume(len = nil) (private)

[ GitHub ]

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

def rbuf_consume(len = nil)
  if @rbuf_offset == 0 && (len.nil? || len == @rbuf.bytesize)
    s = @rbuf
    @rbuf = ''.b
    @rbuf_offset = 0
    @rbuf_empty = true
  elsif len.nil?
    s = @rbuf.byteslice(@rbuf_offset..-1)
    @rbuf = ''.b
    @rbuf_offset = 0
    @rbuf_empty = true
  else
    s = @rbuf.byteslice(@rbuf_offset, len)
    @rbuf_offset += len
    @rbuf_empty = @rbuf_offset == @rbuf.bytesize
    rbuf_flush
  end

  @debug_output << %Q[-> #{s.dump}\n] if @debug_output
  s
end

#rbuf_consume_all (private)

[ GitHub ]

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

def rbuf_consume_all
  rbuf_consume if rbuf_size > 0
end

#rbuf_fill (private)

[ GitHub ]

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

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

#rbuf_flush (private)

[ GitHub ]

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

def rbuf_flush
  if @rbuf_empty
    @rbuf.clear
    @rbuf_offset = 0
  end
  nil
end

#rbuf_size (private)

[ GitHub ]

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

def rbuf_size
  @rbuf.bytesize - @rbuf_offset
end

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

[ GitHub ]

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

def read(len, dest = ''.b, ignore_eof = false)
  LOG "reading #{len} bytes..."
  read_bytes = 0
  begin
    while read_bytes + rbuf_size < len
      if s = rbuf_consume_all
        read_bytes += s.bytesize
        dest << s
      end
      rbuf_fill
    end
    s = rbuf_consume(len - read_bytes)
    read_bytes += s.bytesize
    dest << s
  rescue EOFError
    raise unless ignore_eof
  end
  LOG "read #{read_bytes} bytes"
  dest
end

#read_all(dest = ''.b)

[ GitHub ]

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

def read_all(dest = ''.b)
  LOG 'reading all...'
  read_bytes = 0
  begin
    while true
      if s = rbuf_consume_all
        read_bytes += s.bytesize
        dest << s
      end
      rbuf_fill
    end
  rescue EOFError
    ;
  end
  LOG "read #{read_bytes} bytes"
  dest
end

#readline

[ GitHub ]

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

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

#readuntil(terminator, ignore_eof = false)

[ GitHub ]

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

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

#write(*strs) Also known as: #<<

[ GitHub ]

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

def write(*strs)
  writing {
    write0(*strs)
  }
end

#write0(*strs) (private)

[ GitHub ]

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

def write0(*strs)
  @debug_output << strs.map(&:dump).join if @debug_output
  orig_written_bytes = @written_bytes
  strs.each_with_index do |str, i|
    need_retry = true
    case len = @io.write_nonblock(str, exception: false)
    when Integer
      @written_bytes += len
      len -= str.bytesize
      if len == 0
        if strs.size == i+1
          return @written_bytes - orig_written_bytes
        else
          need_retry = false
          # next string
        end
      elsif len < 0
        str = str.byteslice(len, -len)
      else # len > 0
        need_retry = false
        # next string
      end
      # continue looping
    when :wait_writable
      (io = @io.to_io).wait_writable(@write_timeout) or raise Net::WriteTimeout.new(io)
      # continue looping
    end while need_retry
  end
end

#writeline(str)

[ GitHub ]

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

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

#writing (private)

[ GitHub ]

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

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