Sha256: 2cf989e7874605816b8b24a14aa4b66bec85930e78036b7fa7c3920eee3ba8e7

Contents?: true

Size: 870 Bytes

Versions: 12

Compression:

Stored size: 870 Bytes

Contents

# frozen_string_literal: true

require 'bundler/setup'
require 'polyphony'

DESTINATION = ['127.0.0.1', 1234]

def handle_client(conn)
  spin do
    dest = TCPSocket.new(*DESTINATION)
    # w_buffer = Polyphony.pipe
    # r_buffer = Polyphony.pipe
    
    # spin { IO.splice(conn, w_buffer, -1000) }
    # spin { IO.splice(w_buffer, dest, -1000) }

    # spin { IO.splice(dest, r_buffer, -1000) }
    # spin { IO.splice(r_buffer, conn, -1000) }

    # Fiber.current.await_all_children

    f = spin do
      IO.double_splice(conn, dest)
      raise EOFError
    end
    IO.double_splice(dest, conn)
    f.await
  rescue EOFError, SystemCallError
    # ignore
  ensure
    conn.close rescue nil
    dest.close rescue nil
  end
end

puts "Serving TCP proxy on port 4321..."
server = TCPServer.new('127.0.0.1', 4321)
while (conn = server.accept)
  handle_client(conn)
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
polyphony-0.99.4 examples/pipes/tcp_proxy.rb
polyphony-0.99.3 examples/pipes/tcp_proxy.rb
polyphony-0.99.2 examples/pipes/tcp_proxy.rb
polyphony-0.99.1 examples/pipes/tcp_proxy.rb
polyphony-0.99 examples/pipes/tcp_proxy.rb
polyphony-0.98 examples/pipes/tcp_proxy.rb
polyphony-0.97 examples/pipes/tcp_proxy.rb
polyphony-0.96 examples/pipes/tcp_proxy.rb
polyphony-0.95 examples/pipes/tcp_proxy.rb
polyphony-0.94 examples/pipes/tcp_proxy.rb
polyphony-0.93 examples/pipes/tcp_proxy.rb
polyphony-0.92 examples/pipes/tcp_proxy.rb