Sha256: e141fb2c31d60b5d91a6f2bdff2045afee2dbfe271ee979d03ff9348e0aad7da

Contents?: true

Size: 1.3 KB

Versions: 20

Compression:

Stored size: 1.3 KB

Contents

# -*- encoding: binary -*-
# :enddoc:
# Generic IO wrapper for proxying pipe and socket objects
# this behaves more like Rainbows::Fiber::IO than anything,
# making it highly suitable for proxying data from pipes/sockets
class Rainbows::Revactor::Proxy < Rev::IO
  def initialize(io)
    @receiver = Actor.current
    super(io)
    attach(Rev::Loop.default)
  end

  def close
    if @_io
      super
      @_io = nil
    end
  end

  def each
    # when yield-ing, Revactor::TCP#write may raise EOFError
    # (instead of Errno::EPIPE), so we need to limit the rescue
    # to just readpartial and let EOFErrors during yield bubble up
    begin
      buf = readpartial(INPUT_SIZE)
    rescue EOFError
      break
    end while yield(buf) || true
  end

  # this may return more than the specified length, Rainbows! won't care...
  def readpartial(length)
    @receiver = Actor.current
    enable if attached? && ! enabled?

    Actor.receive do |filter|
      filter.when(T[:rainbows_io_input, self]) do |_, _, data|
        return data
      end

      filter.when(T[:rainbows_io_closed, self]) do
        raise EOFError, "connection closed"
      end
    end
  end

  def on_close
    @receiver << T[:rainbows_io_closed, self]
  end

  def on_read(data)
    @receiver << T[:rainbows_io_input, self, data ]
    disable
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
rainbows-4.7.0 lib/rainbows/revactor/proxy.rb
rainbows-4.6.2 lib/rainbows/revactor/proxy.rb
rainbows-4.6.1 lib/rainbows/revactor/proxy.rb
rainbows-4.6.0.4.g4108 lib/rainbows/revactor/proxy.rb
rainbows-4.6.0 lib/rainbows/revactor/proxy.rb
rainbows-4.5.0 lib/rainbows/revactor/proxy.rb
rainbows-4.4.3 lib/rainbows/revactor/proxy.rb
rainbows-4.4.2 lib/rainbows/revactor/proxy.rb
rainbows-4.4.1.1.gd5c8c lib/rainbows/revactor/proxy.rb
rainbows-4.4.1 lib/rainbows/revactor/proxy.rb
rainbows-4.4.0 lib/rainbows/revactor/proxy.rb
rainbows-4.3.1 lib/rainbows/revactor/proxy.rb
rainbows-4.3.0 lib/rainbows/revactor/proxy.rb
rainbows-4.2.0 lib/rainbows/revactor/proxy.rb
rainbows-4.1.0 lib/rainbows/revactor/proxy.rb
rainbows-4.0.0 lib/rainbows/revactor/proxy.rb
rainbows-3.4.0 lib/rainbows/revactor/proxy.rb
rainbows-3.3.0 lib/rainbows/revactor/proxy.rb
rainbows-3.2.0 lib/rainbows/revactor/proxy.rb
rainbows-3.1.0 lib/rainbows/revactor/proxy.rb