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

New VariantMessage allowing multiple field messages transmital.

VariantMessage now allows to transmit messages with Variants as data,
allowing to have as much fields as you want. SignalMessage now uses it.
parent ee32a859
......@@ -22,6 +22,7 @@ libovcc_la_SOURCES = board.vala \
ovccnetworkmessage.vala \
ovccnetworkemptymessage.vala \
ovccnetworkstringmessage.vala \
ovccnetworkvariantmessage.vala \
ovccnetworkloginmessage.vala \
ovccnetworksignals.vala \
ovccnetworkdisconnectmessage.vala \
......
......@@ -84,11 +84,10 @@ namespace OVCC.Network
ignore_next[s]--;
return false;
}
SignalMessage msg = new SignalMessage ();
msg.stype = s;
var ap = va_list();
msg.build_data_va (format, &ap);
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);
......@@ -105,13 +104,13 @@ namespace OVCC.Network
switch (msg.stype) {
case SignalType.PLAYER_ADDED:
string nick;
msg.parse_data ("s", out nick);
msg.parse ("s", out nick);
debug ("Adding new player %s", nick);
game_object.add_player (new Player(nick));
break;
case SignalType.PLAYER_REMOVED:
string nick;
msg.parse_data ("s", out nick);
msg.parse ("s", out nick);
debug ("Removing player %s", nick);
foreach (var p in game_object.players) {
if (p.nick == nick) {
......@@ -122,7 +121,7 @@ namespace OVCC.Network
break;
case SignalType.GAME_STATE:
OVCC.GameState s;
msg.parse_data ("i", out s);
msg.parse ("i", out s);
debug ("Game state changed from %s to %s",
game_object.state.to_string(), s.to_string());
switch (s) {
......@@ -142,19 +141,24 @@ namespace OVCC.Network
}
}
public class SignalMessage: Message
public class SignalMessage: VariantMessage
{
public override MessageType message_type { get { return MessageType.SIGNAL; } }
public SignalType stype { get; set; default = 0; }
public uint8[] data { get; private set; default = null; }
public SignalMessage (SignalType s, Variant v)
{
base (v);
stype = s;
}
public override bool serialize (DataOutputStream stream,
Cancellable? cancel = null)
throws Error
{
stream.put_uint16 ((uint16)stype, cancel);
return write_buffer (stream, data, cancel);
return base.serialize (stream, cancel);
}
public override bool deserialize (DataInputStream stream,
......@@ -162,34 +166,8 @@ namespace OVCC.Network
throws Error
{
stype = (SignalType)stream.read_uint16 (cancel);
data = read_buffer (stream, cancel);
return true;
}
public void build_data_va (string format,
va_list *ap)
{
Variant v = new Variant.va (format, null, ap);
this.data = new uint8[v.get_size()];
v.store (this.data);
}
public void build_data (string format,
...)
{
var ap = va_list();
build_data_va (format, &ap);
}
public void parse_data (string format,
...)
{
var ap = va_list ();
Variant v = Variant.new_from_data<Object> (new VariantType(format), data, false, this);
v.get_va (format, null, &ap);
return base.deserialize (stream, cancel);
}
}
}
/*
*
* Copyright (C) 2011 Colomban Wendling <ban@herbesfolles.org>
* Jonathan Michalon <studios.chalmion@no-log.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* An abstact message with variant data
*/
public abstract class OVCC.Network.VariantMessage : Message
{
protected uint8[] data = null;
protected Variant variant = null;
public VariantMessage (Variant v)
{
data = new uint8[v.get_size()];
v.store (data);
variant = v;
}
public override bool serialize (DataOutputStream stream,
Cancellable? cancel = null)
throws Error
{
return write_buffer (stream, data, cancel);
}
public override bool deserialize (DataInputStream stream,
Cancellable? cancel = null)
throws Error
{
data = read_buffer (stream, cancel);
return true;
}
public Variant get_variant (string format)
{
if (variant == null) {
variant = Variant.new_from_data<Object> (new VariantType(format), data, false, this);
}
return variant;
}
public void parse (string format,
...)
{
var ap = va_list ();
var v = get_variant (format);
v.get_va (format, null, &ap);
}
}
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