Guest User

Untitled

a guest
Oct 17th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.10 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # Copyright 2012 IllFonic, LLC.
  3.  
  4. require './lib/core.rb'
  5. require './lib/master_client_connection.rb'
  6.  
  7. module Dossier
  8.     class MasterService < Command
  9.         def init arguments
  10.             @thread_list = []
  11.             @server_list = { :AuthService => [], :GameService => [] }
  12.             @semaphore = Mutex.new
  13.  
  14.             # Technically, nil is '0.0.0.0', but we may as well clean it up for the rescue case
  15.             listen_address = Config::LISTEN_ADDRESS || "0.0.0.0"
  16.             begin
  17.                 @listen_server = TCPServer.open listen_address, Config::LISTEN_PORT
  18.                 info "Opened listen socket on #{@listen_server.addr[3]}:#{@listen_server.addr[1]}"
  19.             rescue => e
  20.                 error "Failed to open listen socket on #{listen_address}:#{Config::LISTEN_PORT}"
  21.             end
  22.         end
  23.  
  24.         def shutdown
  25.             # Close the listen socket
  26.             unless @listen_server.nil?
  27.                 info "Closing #{@thread_list.length} connections"
  28.                 @thread_list.each { |connection| connection.die_and_join! }
  29.  
  30.                 info "Closing listen socket on #{@listen_server.addr[3]}:#{@listen_server.addr[1]}"
  31.                 @listen_server.close
  32.             end
  33.         end
  34.  
  35.         def tick delta_seconds
  36.             # Check for new connections
  37.             unless @thread_list.length >= Config::MAX_CLIENT_CONNECTIONS
  38.                 begin
  39.                     # Continue to poll for new connections until we run out
  40.                     loop do
  41.                         socket = @listen_server.accept_nonblock
  42.                         @thread_list.push MasterClientConnection.run(self, { :socket => socket })
  43.                     end
  44.                 rescue
  45.                 end
  46.             end
  47.  
  48.             # Clean up dead connections
  49.             @thread_list.each do |connection|
  50.                 @thread_list.delete connection unless connection.alive?
  51.             end
  52.  
  53.             # Only tick MAX_CONNECTION times per second
  54.             sleep 1.0 / Config::MAX_CLIENT_CONNECTIONS
  55.         end
  56.  
  57.         def register_server type, address
  58.             @semaphore.synchronize do
  59.                 info "New #{type.to_s} client #{address}"
  60.                 @server_list[type].push address
  61.             end
  62.         end
  63.  
  64.         def unregister_server type, address
  65.             @semaphore.synchronize do
  66.                 @server_list[type].delete address
  67.             end
  68.         end
  69.  
  70.         def get_server_list type
  71.             @semaphore.lock
  72.             copy = @server_list[type]
  73.             @semaphore.unlock
  74.             copy
  75.         end
  76.     end
  77. end
  78.  
  79. Dossier::MasterService::run
Add Comment
Please, Sign In to add comment