Class: Proxi::Connection

Inherits:
Object
  • Object
show all
Includes:
Wisper::Publisher
Defined in:
lib/proxi/connection.rb

Overview

A `Connection` is a bidirectional pipe between two sockets.

The proxy server hands it the socket for the incoming request from, and `Connection` then initiates an outgoing request, after which it forwards all traffic in both directions.

Creating the outgoing request is delegated to a `Proxi::SocketFactory`. The reason being that the type of socket can vary (`TCPSocket`, `SSLSocket`), or there might be some logic involved to dispatch to the correct host, e.g. based on the HTTP Host header (cfr. `Proxi::HTTPHostSocketFactory`).

A socket factory can subscribe to events to make informed decision, e.g. to inspect incoming data for HTTP headers.

Proxi::Connection broadcasts the following events:

  • `start_connection(Proxi::Connection)`

  • `end_connection(Proxi::Connection)`

  • `main_loop_error(Proxi::Connection, Exception)`

  • `data_in(Proxi::Connection, Array)`

  • `data_out(Proxi::Connection, Array)`

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(in_socket, socket_factory, max_block_size: 4096) ⇒ Connection

Returns a new instance of Connection.


31
32
33
34
35
# File 'lib/proxi/connection.rb', line 31

def initialize(in_socket, socket_factory, max_block_size: 4096)
  @in_socket = in_socket
  @socket_factory = socket_factory
  @max_block_size = max_block_size
end

Instance Attribute Details

#in_socketObject (readonly)

Returns the value of attribute in_socket


29
30
31
# File 'lib/proxi/connection.rb', line 29

def in_socket
  @in_socket
end

#threadObject (readonly)

Returns the value of attribute thread


29
30
31
# File 'lib/proxi/connection.rb', line 29

def thread
  @thread
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)

45
46
47
# File 'lib/proxi/connection.rb', line 45

def alive?
  thread.alive?
end

#callObject

`Connection#call` starts the connection handler thread. This is called by the server, and spawns a new Thread that handles the forwarding of data.


39
40
41
42
43
# File 'lib/proxi/connection.rb', line 39

def call
  broadcast(:start_connection, self)
  @thread = Thread.new { proxy_loop }
  self
end

#join_threadObject


49
50
51
# File 'lib/proxi/connection.rb', line 49

def join_thread
  thread.join
end