Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # Copyright 2012 IllFonic, LLC.
- require './lib/core.rb'
- require './lib/master_client_connection.rb'
- module Dossier
- class MasterService < Command
- def init arguments
- @thread_list = []
- @server_list = { :AuthService => [], :GameService => [] }
- @semaphore = Mutex.new
- # Technically, nil is '0.0.0.0', but we may as well clean it up for the rescue case
- listen_address = Config::LISTEN_ADDRESS || "0.0.0.0"
- begin
- @listen_server = TCPServer.open listen_address, Config::LISTEN_PORT
- info "Opened listen socket on #{@listen_server.addr[3]}:#{@listen_server.addr[1]}"
- rescue => e
- error "Failed to open listen socket on #{listen_address}:#{Config::LISTEN_PORT}"
- end
- end
- def shutdown
- # Close the listen socket
- unless @listen_server.nil?
- info "Closing #{@thread_list.length} connections"
- @thread_list.each { |connection| connection.die_and_join! }
- info "Closing listen socket on #{@listen_server.addr[3]}:#{@listen_server.addr[1]}"
- @listen_server.close
- end
- end
- def tick delta_seconds
- # Check for new connections
- unless @thread_list.length >= Config::MAX_CLIENT_CONNECTIONS
- begin
- # Continue to poll for new connections until we run out
- loop do
- socket = @listen_server.accept_nonblock
- @thread_list.push MasterClientConnection.run(self, { :socket => socket })
- end
- rescue
- end
- end
- # Clean up dead connections
- @thread_list.each do |connection|
- @thread_list.delete connection unless connection.alive?
- end
- # Only tick MAX_CONNECTION times per second
- sleep 1.0 / Config::MAX_CLIENT_CONNECTIONS
- end
- def register_server type, address
- @semaphore.synchronize do
- info "New #{type.to_s} client #{address}"
- @server_list[type].push address
- end
- end
- def unregister_server type, address
- @semaphore.synchronize do
- @server_list[type].delete address
- end
- end
- def get_server_list type
- @semaphore.lock
- copy = @server_list[type]
- @semaphore.unlock
- copy
- end
- end
- end
- Dossier::MasterService::run
Add Comment
Please, Sign In to add comment