glchess: Kill engine processes on new game/exit

This commit is contained in:
Robert Ancell 2012-07-14 17:13:12 +12:00
parent f79e366568
commit fb2a75ef70
2 changed files with 34 additions and 7 deletions

View file

@ -35,9 +35,9 @@ public class ChessEngine : Object
try
{
Process.spawn_async_with_pipes (null, argv, null,
SpawnFlags.SEARCH_PATH,
null,
out pid, out stdin_fd, out stdout_fd, out stderr_fd);
SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
null,
out pid, out stdin_fd, out stdout_fd, out stderr_fd);
}
catch (SpawnError e)
{
@ -45,6 +45,8 @@ public class ChessEngine : Object
return false;
}
ChildWatch.add (pid, engine_stopped_cb);
stdout_channel = new IOChannel.unix_new (stdout_fd);
try
{
@ -60,7 +62,12 @@ public class ChessEngine : Object
return true;
}
private void engine_stopped_cb (Pid pid, int status)
{
stopped ();
}
public virtual void start_game ()
{
}
@ -79,8 +86,8 @@ public class ChessEngine : Object
public void stop ()
{
// FIXME
stopped ();
if (pid != 0)
Posix.kill (pid, Posix.SIGTERM);
}
private bool read_cb (IOChannel source, IOCondition condition)

View file

@ -124,6 +124,13 @@ public class Application : Gtk.Application
settings_changed_cb (settings, "show-3d");
}
protected override void shutdown ()
{
base.shutdown ();
if (opponent_engine != null)
opponent_engine.stop ();
}
public void quit_game ()
{
if (save_duration_timeout != 0)
@ -298,7 +305,14 @@ public class Application : Gtk.Application
black_level = "normal";
opponent = null;
opponent_engine = null;
if (opponent_engine != null)
{
opponent_engine.stop ();
opponent_engine.ready_changed.disconnect (engine_ready_cb);
opponent_engine.moved.disconnect (engine_move_cb);
opponent_engine.stopped.disconnect (engine_stopped_cb);
opponent_engine = null;
}
if (white_engine != null)
{
opponent = game.white;
@ -316,6 +330,7 @@ public class Application : Gtk.Application
{
opponent_engine.ready_changed.connect (engine_ready_cb);
opponent_engine.moved.connect (engine_move_cb);
opponent_engine.stopped.connect (engine_stopped_cb);
opponent_engine.start ();
}
@ -436,6 +451,11 @@ public class Application : Gtk.Application
opponent.move (move);
}
private void engine_stopped_cb (ChessEngine engine)
{
opponent.resign ();
}
private void game_start_cb (ChessGame game)
{
if (opponent_engine != null)