Class: TCPProxy::Connection
- Inherits:
-
Object
- Object
- TCPProxy::Connection
- Defined in:
- lib/tcp_proxy.rb
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(in_sock, out_sock) ⇒ Connection
constructor
A new instance of Connection.
- #pump(n) ⇒ Object
- #pump_synchronized(n = 10) ⇒ Object
Constructor Details
#initialize(in_sock, out_sock) ⇒ Connection
Returns a new instance of Connection.
71 72 73 74 |
# File 'lib/tcp_proxy.rb', line 71 def initialize(in_sock, out_sock) @m = Mutex.new @in_sock, @out_sock = in_sock, out_sock end |
Instance Method Details
#close ⇒ Object
76 77 78 79 80 |
# File 'lib/tcp_proxy.rb', line 76 def close @m.synchronize { @in_sock.close; @in_sock = nil @out_sock.close; @out_sock = nil } end |
#pump(n) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tcp_proxy.rb', line 88 def pump(n) while n>0 available_sockets = [@in_sock, @out_sock] ready_sockets, (*) = IO.select(available_sockets, nil, nil, 0) break unless ready_sockets && !ready_sockets.empty? ready_sockets.each do |socket| buf = socket.read_nonblock(16*1024) if socket == @in_sock puts "--> #{buf.size}" @out_sock.write(buf) else puts "<-- #{buf.size}" @in_sock.write(buf) end end n -= 1 end rescue Errno::EAGAIN # Read would block, attempt later end |
#pump_synchronized(n = 10) ⇒ Object
82 83 84 85 86 |
# File 'lib/tcp_proxy.rb', line 82 def pump_synchronized(n=10) @m.synchronize { return unless @in_sock && @out_sock pump(n) } end |