Skip to content

Instantly share code, notes, and snippets.

@tmm1
Created October 18, 2008 02:08
Show Gist options
  • Save tmm1/17580 to your computer and use it in GitHub Desktop.
Save tmm1/17580 to your computer and use it in GitHub Desktop.
simple EM TCP proxy
require 'rubygems'
require 'eventmachine'
Port = (ARGV[0] && ARGV[0].to_i) || 4567
Host = ARGV[1] || '10.0.2.4'
p ['starting proxy', Host, Port]
EM.run{
module Client
def initialize server
@server = server
end
def connection_completed
@connected = true
send_data @buf if @buf
p [:c, :connection_completed]
end
def receive_data data
p [:c, :receive, data]
@server.send_data data
end
def send data
unless @connected
(@buf ||= '') << data
else
send_data data
end
end
def unbind
p [:c, :unbind]
@server.close_connection_after_writing if @server
@server = nil
end
end
module Proxy
def post_init
p [:s, :post_init]
connect_client
end
def receive_data data
p [:s, :receive, data]
@client.send data
end
def unbind
p [:s, :unbind]
@client.close_connection_after_writing if @client
@client = nil
end
def connect_client
@client = EM.connect Host, Port, Client, self
end
end
EM.start_server '0.0.0.0', Port, Proxy
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment