Commit e3c0488f authored by Colomban Wendling's avatar Colomban Wendling
Browse files

Add the message type as the signal detail of SignalHandle:signal_received()

This allows to connect to signal_receive() only for a particular type of
message, generally useful since we want to do something different for each
type.
parent c943647c
......@@ -55,8 +55,14 @@ namespace OVCC.Network
private int[] ignore_next = new int[SignalType.TYPES_N];
[Signal (detailed=true)]
public signal void signal_received (SignalMessage msg);
public void emit_received (SignalMessage msg)
{
signal_received[msg.stype.to_string ()] (msg);
}
public SignalHandle (Game game, Player player,
DataInputStream input_p, DataOutputStream output_p)
{
......@@ -78,40 +84,19 @@ namespace OVCC.Network
send_to_peer (SignalType.GAME_STATE, "i", state);
// }
});
this.signal_received.connect (this.signal_received_handler);
}
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);
debug ("Transmitting signal %s", s.to_string());
try {
msg.send (output);
} catch (Error e) {
warning ("Cannot transmit signal %s: %s", s.to_string(), e.message);
}
return true;
}
private void signal_received_handler (SignalMessage msg)
{
debug ("Received signal %s", msg.stype.to_string());
ignore_next[msg.stype]++;
switch (msg.stype) {
case SignalType.PLAYER_ADDED:
/* 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);
game_object.add_player (new Player(nick));
break;
case SignalType.PLAYER_REMOVED:
});
signal_received[SignalType.PLAYER_REMOVED.to_string ()].connect ((msg) => {
string nick;
msg.parse ("s", out nick);
debug ("Removing player %s", nick);
......@@ -121,8 +106,8 @@ namespace OVCC.Network
break;
}
}
break;
case SignalType.GAME_STATE:
});
signal_received[SignalType.GAME_STATE.to_string ()].connect ((msg) => {
OVCC.GameState s;
msg.parse ("i", out s);
debug ("Game state changed from %s to %s",
......@@ -138,8 +123,26 @@ namespace OVCC.Network
game_object.abort();
break;
}
break;
});
}
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);
debug ("Transmitting signal %s", s.to_string());
try {
msg.send (output);
} catch (Error e) {
warning ("Cannot transmit signal %s: %s", s.to_string(), e.message);
}
return true;
}
}
......
......@@ -218,7 +218,7 @@ namespace OVCCClient
case MessageType.SIGNAL:
Idle.add (() => {
if (! listen_loop_cancel.is_cancelled ()) {
signal_handle.signal_received (msg as SignalMessage);
signal_handle.emit_received (msg as SignalMessage);
}
return false;
});
......
......@@ -141,7 +141,7 @@ public class Server: ThreadedSocketService
}
break;
case MessageType.SIGNAL:
client.signal_handle.signal_received ((msg as SignalMessage));
client.signal_handle.emit_received (msg as SignalMessage);
break;
case MessageType.DISCONNECT: running = false; break;
}
......
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