OSDN Git Service

Terminate threads of players so that the server shuts down (Closes #38544)
authorDaigo Moriwaki <daigo@debian.org>
Sun, 26 Aug 2018 02:56:32 +0000 (11:56 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Sun, 26 Aug 2018 02:57:37 +0000 (11:57 +0900)
shogi-server

index f1cfbb3..a436628 100755 (executable)
@@ -35,6 +35,7 @@ require 'shogi_server/config'
 require 'shogi_server/util'
 require 'shogi_server/league/floodgate_thread.rb'
 require 'pathname'
+require 'set'
 require 'tempfile'
 
 #################################################
@@ -435,6 +436,9 @@ def main
 
   $league.dir = $topdir
 
+  # Set of connected players
+  $players = Set.new
+
   config = {}
   config[:BindAddress] = "0.0.0.0"
   config[:Port]       = port
@@ -462,8 +466,9 @@ def main
 
   srand
   server = WEBrick::GenericServer.new(config)
-  ["INT", "TERM"].each do |signal| 
+  ["INT", "TERM"].each do |signal|
     trap(signal) do
+      $players.each {|p| p.kill}
       server.shutdown
       setup_floodgate.kill
     end
@@ -495,6 +500,14 @@ def main
       log_message(sprintf("user %s login", player.name))
       login.process
       player.setup_logger($options["player-log-dir"]) if $options["player-log-dir"]
+
+      $mutex.lock
+      begin
+       $players.add(player)
+      ensure
+        $mutex.unlock
+      end
+
       player.run(login.csa_1st_str) # loop
       $mutex.lock
       begin
@@ -504,6 +517,7 @@ def main
         player.finish
         $league.delete(player)
         log_message(sprintf("user %s logout", player.name))
+       $players.delete(player)
       ensure
         $mutex.unlock
       end