Created
October 18, 2008 02:08
-
-
Save tmm1/17580 to your computer and use it in GitHub Desktop.
simple EM TCP proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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