Class: GQTP::Connection::Thread::IO

Inherits:
Object
  • Object
show all
Defined in:
lib/gqtp/connection/thread.rb

Instance Method Summary collapse

Constructor Details

#initialize(real_io) ⇒ IO

Returns a new instance of IO.



36
37
38
# File 'lib/gqtp/connection/thread.rb', line 36

def initialize(real_io)
  @real_io = real_io
end

Instance Method Details

#closeObject



66
67
68
# File 'lib/gqtp/connection/thread.rb', line 66

def close
  @real_io.close
end

#read(size = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gqtp/connection/thread.rb', line 54

def read(size=nil)
  thread = ::Thread.new do
    data = @real_io.read(size)
    if block_given?
      yield(data)
    else
      data
    end
  end
  Request.new(thread)
end

#write(*chunks) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gqtp/connection/thread.rb', line 40

def write(*chunks)
  thread = ::Thread.new do
    chunks.each do |chunk|
      until chunk.empty?
        written_bytes = @real_io.write(chunk)
        break if chunk.bytesize == written_bytes
        chunk = chunk[written_bytes..-1]
      end
    end
    yield if block_given?
  end
  Request.new(thread)
end