Commit 27fec2da authored by Colomban Wendling's avatar Colomban Wendling
Browse files

Make sure to disconnect signal handlers

Take care of disconnecting signal handlers, making possible for the
owning object to live longer than the signal is expected to live
without unwanted side effects.
parent c5e25631
......@@ -6,8 +6,9 @@
/* maybe inherit from Client? */
public class Bot
{
private MainLoop loop = null;
private OVCCClient.Client client = null;
private MainLoop loop = null;
private OVCCClient.Client client = null;
private OVCC.SigQueue sigqueue = new OVCC.SigQueue ();
public bool join (OVCCClient.Server server)
throws Error
......@@ -47,9 +48,8 @@ public class Bot
}
debug ("Joined");
/* react on game state changes */
client.game.notify["state"].connect ((s, p) => {
sigqueue.add (client.game, client.game.notify["state"].connect ((s, p) => {
switch (((OVCC.Game)s).state) {
case OVCC.GameState.STARTED:
debug ("Game started!");
......@@ -63,9 +63,9 @@ public class Bot
debug ("Abnormal game termination!");
break;
}
});
}));
/* we should place a tile? OK, why not */
client.place_tile.connect ((tile) => {
sigqueue.add (client, client.place_tile.connect ((tile) => {
debug ("Seems we should place a tile...");
client.game.board.foreach ((b, p, t) => {
for (var i = 0; i < 4; i++) {
......@@ -88,9 +88,9 @@ public class Bot
}
return true;
});
});
}));
/* we should place a pawn? let's do so */
client.place_pawn.connect ((tile) => {
sigqueue.add (client, client.place_pawn.connect ((tile) => {
debug ("We're asked to place a pawn!");
foreach (var o in tile.objects) {
/* we guess all pawns can go anywhere another can -- which isn't true
......@@ -100,7 +100,7 @@ public class Bot
// break;
// }
}
});
}));
return true;
......@@ -119,6 +119,8 @@ public class Bot
public void leave ()
{
sigqueue.remove_all ();
client.leave ();
client = null;
loop.quit ();
......
......@@ -13,6 +13,8 @@ namespace OVCCClient
public Server server {get; private set; default = null;}
public Player player {get; private set;}
public Game game {get; private set; default = null;}
private SigQueue sigqueue = new SigQueue ();
public signal void place_tile (Tile t);
public signal void place_pawn (Tile t);
......@@ -41,11 +43,14 @@ namespace OVCCClient
public bool leave ()
{
sigqueue.remove_all ();
game.abort();
server.disconnect_from ();
/* destroy server */
server = null;
game = null;
player = null;
return false;
}
......@@ -68,18 +73,18 @@ namespace OVCCClient
player.joined (game);
game.notify["current-player"].connect (() => {
debug ("Current player changed");
if (game.current_player == player) {
this.place_tile (game.current_tile);
}
});
game.board.tile_added.connect ((t, p) => {
if (game.current_player == player) {
// this.place_pawn (t);
}
});
sigqueue.add (game, game.notify["current-player"].connect (() => {
debug ("Current player changed");
if (game.current_player == player) {
this.place_tile (game.current_tile);
}
}));
sigqueue.add (game.board, game.board.tile_added.connect ((t, p) => {
if (game.current_player == player) {
// this.place_pawn (t);
}
}));
return game;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment