Commit fe3ca9e2 authored by Jonathan Michalon's avatar Jonathan Michalon
Browse files

SignalHandle: block our listener when applying remote signals

parent e3c0488f
......@@ -53,7 +53,7 @@ namespace OVCC.Network
private DataInputStream input;
private DataOutputStream output;
private int[] ignore_next = new int[SignalType.TYPES_N];
private ulong[] handle_ids = new ulong[SignalType.TYPES_N];
[Signal (detailed=true)]
public signal void signal_received (SignalMessage msg);
......@@ -63,6 +63,15 @@ namespace OVCC.Network
signal_received[msg.stype.to_string ()] (msg);
}
private void stop_forward (Object o, SignalType t)
{
SignalHandler.block (o, handle_ids[t]);
}
private void resume_forward (Object o, SignalType t)
{
SignalHandler.unblock (o, handle_ids[t]);
}
public SignalHandle (Game game, Player player,
DataInputStream input_p, DataOutputStream output_p)
{
......@@ -70,31 +79,35 @@ namespace OVCC.Network
input = input_p;
output = output_p;
game_object.player_added.connect ((p) => {
send_to_peer (SignalType.PLAYER_ADDED, "s", p.nick);
});
game_object.player_removed.connect ((p) => {
send_to_peer (SignalType.PLAYER_REMOVED, "s", p.nick);
});
game_object.notify["state"].connect ((s, p) => {
OVCC.GameState state = ((OVCC.Game)s).state;
/* other states should be automatic (useful until full communication) */
// if (state == OVCC.GameState.STARTED ||
// state == OVCC.GameState.ABORTED ) {
send_to_peer (SignalType.GAME_STATE, "i", state);
// }
});
handle_ids[SignalType.PLAYER_ADDED] =
game_object.player_added.connect ((p) => {
send_to_peer (SignalType.PLAYER_ADDED, "s", p.nick);
});
handle_ids[SignalType.PLAYER_REMOVED] =
game_object.player_removed.connect ((p) => {
send_to_peer (SignalType.PLAYER_REMOVED, "s", p.nick);
});
handle_ids[SignalType.GAME_STATE] =
game_object.notify["state"].connect ((s, p) => {
OVCC.GameState state = ((OVCC.Game)s).state;
/* other states should be automatic (useful until full communication) */
// if (state == OVCC.GameState.STARTED ||
// state == OVCC.GameState.ABORTED ) {
send_to_peer (SignalType.GAME_STATE, "i", state);
// }
});
/* incoming signal messages handling */
signal_received.connect ((msg) => {
debug ("Received signal %s", msg.stype.to_string());
ignore_next[msg.stype]++;
});
signal_received[SignalType.PLAYER_ADDED.to_string ()].connect ((msg) => {
string nick;
msg.parse ("s", out nick);
debug ("Adding new player %s", nick);
stop_forward (game_object, SignalType.PLAYER_ADDED);
game_object.add_player (new Player(nick));
resume_forward (game_object, SignalType.PLAYER_ADDED);
});
signal_received[SignalType.PLAYER_REMOVED.to_string ()].connect ((msg) => {
string nick;
......@@ -102,7 +115,9 @@ namespace OVCC.Network
debug ("Removing player %s", nick);
foreach (var p in game_object.players) {
if (p.nick == nick) {
stop_forward (game_object, SignalType.PLAYER_REMOVED);
game_object.remove_player (p);
resume_forward (game_object, SignalType.PLAYER_REMOVED);
break;
}
}
......@@ -112,6 +127,7 @@ namespace OVCC.Network
msg.parse ("i", out s);
debug ("Game state changed from %s to %s",
game_object.state.to_string(), s.to_string());
stop_forward (game_object, SignalType.GAME_STATE);
switch (s) {
case OVCC.GameState.STARTED:
game_object.start();
......@@ -123,16 +139,12 @@ namespace OVCC.Network
game_object.abort();
break;
}
resume_forward (game_object, SignalType.GAME_STATE);
});
}
private bool send_to_peer (SignalType s, string format, ...)
{
if (ignore_next[s] > 0) {
ignore_next[s]--;
return false;
}
var ap = va_list();
Variant v = new Variant.va (format, null, &ap);
SignalMessage msg = new SignalMessage (s, v);
......
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