Class: EventMachine::Protocols::ClientConnection

Inherits:
Connection
  • Object
show all
Defined in:
lib/proxymachine/client_connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(host, port) ⇒ Object



4
5
6
7
# File 'lib/proxymachine/client_connection.rb', line 4

def self.start(host, port)
  EM.start_server(host, port, self)
  puts "Listening on #{host}:#{port}"
end

Instance Method Details

#ensure_server_side_connectionObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/proxymachine/client_connection.rb', line 25

def ensure_server_side_connection
  @timer.cancel if @timer
  unless @server_side
    commands = ProxyMachine.router.call(@buffer.join)
    close_connection unless commands.instance_of?(Hash)
    if remote = commands[:remote]
      m, host, port = *remote.match(/^(.+):(.+)$/)
      if try_server_connect(host, port.to_i)
        if data = commands[:data]
          @buffer = [data]
        end
        send_and_clear_buffer
      end
    elsif close = commands[:close]
      if close == true
        close_connection
      else
        send_data(close)
        close_connection_after_writing
      end
    elsif commands[:noop]
      # do nothing
    else
      close_connection
    end
  end
end

#post_initObject



9
10
11
12
13
# File 'lib/proxymachine/client_connection.rb', line 9

def post_init
  @buffer = []
  @tries = 0
  ProxyMachine.incr
end

#proxy_target_unboundObject

Proxy connection has been lost



89
90
91
# File 'lib/proxymachine/client_connection.rb', line 89

def proxy_target_unbound
  @server_side = nil
end

#receive_data(data) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/proxymachine/client_connection.rb', line 15

def receive_data(data)
  if !@server_side
    @buffer << data
    ensure_server_side_connection
  end
rescue => e
  close_connection
  puts "#{e.class} - #{e.message}"
end

#send_and_clear_bufferObject



74
75
76
77
78
79
80
81
# File 'lib/proxymachine/client_connection.rb', line 74

def send_and_clear_buffer
  if !@buffer.empty?
    @buffer.each do |x|
      @server_side.send_data(x)
    end
    @buffer = []
  end
end

#try_server_connect(host, port) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/proxymachine/client_connection.rb', line 53

def try_server_connect(host, port)
  @server_side = ServerConnection.request(host, port, self)
  proxy_incoming_to @server_side
  if @tries > 0
    puts "Successful connection."
  end
  true
rescue => e
  if @tries < 10
    @tries += 1
    puts "Failed on server connect attempt #{@tries}. Trying again..."
    @timer.cancel if @timer
    @timer = EventMachine::Timer.new(0.1) do
      self.ensure_server_side_connection
    end
  else
    puts "Failed after ten connection attempts."
  end
  false
end

#unbindObject



83
84
85
86
# File 'lib/proxymachine/client_connection.rb', line 83

def unbind
  @server_side.close_connection_after_writing if @server_side
  ProxyMachine.decr
end