Class: TunnelRb::Server::PendingConnections

Inherits:
Object
  • Object
show all
Defined in:
lib/tunnel_rb/server/pending_connections.rb

Overview

Coordination between the public-request thread (which generates a conn_id and asks the tunneled client for a fresh data socket) and the control connection where the tunneled client eventually ‘bind’s that socket. The public side registers a Queue, the control side delivers into it.

Instance Method Summary collapse

Constructor Details

#initializePendingConnections

Returns a new instance of PendingConnections.



13
14
15
16
# File 'lib/tunnel_rb/server/pending_connections.rb', line 13

def initialize
  @pending = {}
  @mutex = Mutex.new
end

Instance Method Details

#close(conn_id) ⇒ Object

Public side cleans up its slot whether it consumed the data socket or timed out.



26
27
28
# File 'lib/tunnel_rb/server/pending_connections.rb', line 26

def close(conn_id)
  @mutex.synchronize { @pending.delete(conn_id) }
end

#deliver(conn_id, socket) ⇒ Object

Control side hands a freshly-bound data socket to whichever public request is waiting. If nobody is waiting (e.g. browser closed first), the socket is closed.



33
34
35
36
37
38
39
40
# File 'lib/tunnel_rb/server/pending_connections.rb', line 33

def deliver(conn_id, socket)
  queue = @mutex.synchronize { @pending.delete(conn_id) }
  if queue
    queue.push(socket)
  else
    socket.close rescue nil
  end
end

#register(conn_id) ⇒ Object



18
19
20
21
22
# File 'lib/tunnel_rb/server/pending_connections.rb', line 18

def register(conn_id)
  queue = Queue.new
  @mutex.synchronize { @pending[conn_id] = queue }
  queue
end