class RSocks::TcpServer
Attributes
config[R]
Public Class Methods
new(host = '127.0.0.1', port = 8081)
click to toggle source
# File lib/r_socks/tcp_server.rb, line 10 def initialize(host = '127.0.0.1', port = 8081) @host = host @port = port @config = RSocks::Config.new end
Public Instance Methods
run!()
click to toggle source
# File lib/r_socks/tcp_server.rb, line 16 def run! begin start_tcp_server rescue Interrupt puts "\nr_socks TPC main server closed now...." end end
Private Instance Methods
attach_and_start_server(server)
click to toggle source
# File lib/r_socks/tcp_server.rb, line 37 def attach_and_start_server(server) EventMachine.run do EventMachine.attach_server(server, RSocks::ConnectionHandler, @config) end end
spawn_process(number)
click to toggle source
# File lib/r_socks/tcp_server.rb, line 43 def spawn_process(number) server = TCPServer.new(@host, @port) pids = [] number.times do |i| pids << Process.fork do puts "start r_socks instance @#{i}" Signal.trap("TERM") { exit! } begin attach_and_start_server(server) rescue Interrupt puts "r_socks TPC server instance @#{i} closed now...." rescue => e puts "r_socks instance @#{i} exit with exception: \r\n#{e.message}" exit! end end end # if main process run in backgourd and has been killed # then all sub-process should term at_exit do term_all_sub_process(pids) end Process.waitall end
start_tcp_server()
click to toggle source
# File lib/r_socks/tcp_server.rb, line 26 def start_tcp_server if @config.instances > 1 spawn_process(@config.instances.to_i) else EventMachine.run do EventMachine.start_server @host, @port, RSocks::ConnectionHandler, @config end end end
term_all_sub_process(pids)
click to toggle source
# File lib/r_socks/tcp_server.rb, line 72 def term_all_sub_process(pids) pids.each do |id| next unless id Process.kill("TERM", id) end end