lib/em-proxy/connection.rb in em-proxy-0.1.6 vs lib/em-proxy/connection.rb in em-proxy-0.1.7
- old
+ new
@@ -41,11 +41,11 @@
#
# initialize connections to backend servers
#
def server(name, opts)
- srv = EventMachine::connect(opts[:host], opts[:port], EventMachine::ProxyServer::Backend, @debug) do |c|
+ srv = EventMachine::bind_connect(opts[:bind_host], opts[:bind_port], opts[:host], opts[:port], EventMachine::ProxyServer::Backend, @debug) do |c|
c.name = name
c.plexer = self
c.proxy_incoming_to(self, 10240) if opts[:relay_server]
end
self.proxy_incoming_to(srv, 10240) if opts[:relay_client]
@@ -55,15 +55,27 @@
#
# [ip, port] of the connected client
#
def peer
- peername = get_peername
- @peer ||= peername ? Socket.unpack_sockaddr_in(peername).reverse : nil
+ @peer ||= begin
+ peername = get_peername
+ peername ? Socket.unpack_sockaddr_in(peername).reverse : nil
+ end
end
#
+ # [ip, port] of the local server connect
+ #
+ def sock
+ @sock ||= begin
+ sockname = get_sockname
+ sockname ? Socket.unpack_sockaddr_in(sockname).reverse : nil
+ end
+ end
+
+ #
# relay data from backend server to client
#
def relay_from_backend(name, data)
debug [:relay_from_backend, name, data]
@@ -93,10 +105,10 @@
if @on_finish
close = @on_finish.call(name)
end
# if all connections are terminated downstream, then notify client
- if @servers.values.compact.size.zero? and close != :keep
+ if (@servers.values.compact.size.zero? && close != :keep) || (close == :close)
close_connection_after_writing
end
end
private