Sha256: f9a5eecf5ba9868bce5052a1c01fd2f4c1caddac9c544b89b671f665b2281167

Contents?: true

Size: 1.03 KB

Versions: 16

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

require 'bundler/setup'
require 'polyphony'

Ports = ARGV[0..1]
EndPoints = []

def log(msg)
  puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%3N')} #{msg}"
end

def endpoint_loop(idx, peer_idx)
  port = Ports[idx]
  server = Polyphony::Net.tcp_listen(
    '0.0.0.0',
    port,
    reuse_addr: true
  ) 
  # server = TCPServer.open('0.0.0.0', port)
  log "Listening on port #{port}"
  loop do
    conn = server.accept
    conn.binmode
    EndPoints[idx] = conn
    log "Client connected on port #{port} (#{conn.remote_address.inspect})"
    while data = conn.readpartial(8192)
      peer = EndPoints[peer_idx]
      if peer
        peer << data
        log "#{idx} => #{peer_idx} #{data.inspect}"
      else
        log "#{idx}: #{data.inspect}"
      end
    end
    EndPoints[idx] = nil
    log "Connection closed on port #{port}"
  rescue => e
    log "Error on port #{port}: #{e.inspect}"
  end
end

spin { endpoint_loop(0, 1) }
spin { endpoint_loop(1, 0) }

log "Tunneling port #{Ports[0]} to port #{Ports[1]}..."
sleep

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
polyphony-0.45.0 examples/io/tunnel.rb
polyphony-0.44.0 examples/io/tunnel.rb
polyphony-0.43.11 examples/io/tunnel.rb
polyphony-0.43.10 examples/io/tunnel.rb
polyphony-0.43.9 examples/io/tunnel.rb
polyphony-0.43.8 examples/io/tunnel.rb
polyphony-0.43.6 examples/io/tunnel.rb
polyphony-0.43.5 examples/io/tunnel.rb
polyphony-0.43.4 examples/io/tunnel.rb
polyphony-0.43.3 examples/io/tunnel.rb
polyphony-0.43.2 examples/io/tunnel.rb
polyphony-0.43.1 examples/io/tunnel.rb
polyphony-0.43 examples/io/tunnel.rb
polyphony-0.42 examples/io/tunnel.rb
polyphony-0.41 examples/io/tunnel.rb
polyphony-0.40 examples/io/tunnel.rb