Class: ProxyMachine::ServerConnection

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ ServerConnection

Returns a new instance of ServerConnection.



7
8
9
10
11
12
# File 'lib/proxymachine/server_connection.rb', line 7

def initialize(conn)
  @client_side = conn
  @connected = false
  @data_received = false
  @timeout = nil
end

Class Method Details

.request(host, port, client_side) ⇒ Object



3
4
5
# File 'lib/proxymachine/server_connection.rb', line 3

def self.request(host, port, client_side)
  EventMachine.connect(host, port, self, client_side)
end

Instance Method Details

#connection_completedObject



21
22
23
24
25
# File 'lib/proxymachine/server_connection.rb', line 21

def connection_completed
  @connected = Time.now
  @timeout = comm_inactivity_timeout || 0.0
  @client_side.server_connection_success
end

#receive_data(data) ⇒ Object



14
15
16
17
18
19
# File 'lib/proxymachine/server_connection.rb', line 14

def receive_data(data)
  fail "receive_data called after raw proxy enabled" if @data_received
  @data_received = true
  @client_side.send_data(data)
  proxy_incoming_to(@client_side, 10240)
end

#unbindObject



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

def unbind
  now = Time.now
  if @client_side.error?
    # the client side disconnected while we were in progress with
    # the server. do nothing.
    LOGGER.info "Client closed while server connection in progress. Dropping."
  elsif !@connected
    # a connection error or timeout occurred
    @client_side.server_connection_failed
  elsif !@data_received
    if @timeout > 0.0 && (elapsed = now - @connected) >= @timeout
      # EM aborted the connection due to an inactivity timeout
      @client_side.server_inactivity_timeout(@timeout, elapsed)
    else
      # server disconnected soon after connecting without sending data
      # treat this like a failed server connection
      @client_side.server_connection_failed
    end
  else
    @client_side.close_connection_after_writing
  end
end