Commit 29b9ad36 authored by Colomban Wendling's avatar Colomban Wendling
Browse files

Add Message objects to abstract network communications

parent 87353dc2
......@@ -17,7 +17,13 @@ libovcc_la_SOURCES = board.vala \
tileobject.vala \
tileset.vala \
tilesdef.vala \
utils.vala
utils.vala \
ovccnetworkmessage.vala \
ovccnetworkemptymessage.vala \
ovccnetworkstringmessage.vala \
ovccnetworkdisconnectmessage.vala \
ovccnetworkwelcomemessage.vala \
ovccnetworkerrormessage.vala
ovccinclude_HEADERS = ovcc.h
vapi_DATA = ovcc.vapi
......
/*
*
* 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/>.
*
*/
public class OVCC.Network.DisconnectMessage : EmptyMessage
{
public override MessageType message_type { get { return MessageType.DISCONNECT; } }
}
/*
*
* 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/>.
*
*/
/**
* A base class to easily implement a message without data.
*/
public abstract class OVCC.Network.EmptyMessage : Message
{
public override bool serialize (DataOutputStream stream,
Cancellable? cancel = null)
throws Error
{
return true;
}
public override bool deserialize (DataInputStream stream,
Cancellable? canecl = null)
throws Error
{
return true;
}
}
/*
*
* 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/>.
*
*/
public class OVCC.Network.ErrorMessage : StringMessage
{
public override MessageType message_type { get { return MessageType.ERROR; } }
public uint code { get; set; default = 0; }
public ErrorMessage (uint code,
string message)
{
Object (code: code, message: message);
}
public override bool serialize (DataOutputStream stream,
Cancellable? cancel = null)
throws Error
{
stream.put_uint16 ((uint16)code, cancel);
return base.serialize (stream, cancel);
}
public override bool deserialize (DataInputStream stream,
Cancellable? cancel = null)
throws Error
{
var cde = stream.read_uint16 (cancel);
if (cde == 0) {
throw new MessageError.MALFORMED_MESSAGE ("Invalid error code");
}
this.code = cde;
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/>.
*
*/
/**
*
* How to add a new message:
* 1) Subclass OVCC.Network.Message (or one of its subclass)
* 2) Implement the abstract methods and properties
* 3) Append the message type in OVCC.Network.MessageType
* 4) Append the type of your class in OVCC.Network.Message.message_types
*
*/
namespace OVCC.Network
{
public errordomain MessageError
{
MALFORMED_MESSAGE,
INVALID_TYPE,
FAILED
}
public enum MessageType
{
ERROR,
WELCOME,
DISCONNECT,
INVALID
}
public abstract class Message : Object
{
private static Type[] message_types = {
typeof (ErrorMessage),
typeof (WelcomeMessage),
typeof (DisconnectMessage)
};
public abstract MessageType message_type { get; }
public abstract bool serialize (DataOutputStream stream,
Cancellable? cancel = null)
throws Error;
public abstract bool deserialize (DataInputStream stream,
Cancellable? cancel = null)
throws Error;
public static Message receive (DataInputStream stream,
Cancellable? cancel = null)
throws Error
{
Message message = null;
MessageType type;
type = (MessageType)stream.read_uint16 (cancel);
if (type >= message_types.length) {
throw new MessageError.INVALID_TYPE ("Invalid message type %u", type);
}
message = Object.new (message_types[type]) as Message;
assert (message.message_type == type);
message.deserialize (stream, cancel);
return message;
}
public bool send (DataOutputStream stream,
Cancellable? cancel = null)
throws Error
{
stream.put_uint16 ((uint16)this.message_type, cancel);
this.serialize (stream, cancel);
return true;
}
}
}
/*
*
* 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 a string data
*/
public abstract class OVCC.Network.StringMessage : Message
{
public string message { get; set; default = null; }
public StringMessage (string message)
{
Object (message: message);
}
public override bool serialize (DataOutputStream stream,
Cancellable? cancel = null)
throws Error
{
var len = message.length;
stream.put_uint16 ((uint16)len, cancel);
var data = new uint8[len];
Memory.copy (data, message, len);
return stream.write (data, cancel) == len;
}
public override bool deserialize (DataInputStream stream,
Cancellable? cancel = null)
throws Error
{
var len = stream.read_uint16 (cancel);
if (len < 1 || len > 0x400 /* arbitrary limit not to fulfill memory */) {
throw new MessageError.MALFORMED_MESSAGE ("Invalid message length %u",
len);
}
var buf = new uint8[len];
stream.read (buf, cancel);
var msg = "%.*s".printf (len, buf);
if (! msg.validate (len)) {
throw new MessageError.MALFORMED_MESSAGE ("Invalid string message");
}
this.message = msg;
return true;
}
}
/*
*
* 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/>.
*
*/
public class OVCC.Network.WelcomeMessage : StringMessage
{
public override MessageType message_type { get { return MessageType.WELCOME; } }
public WelcomeMessage (string message = "Welcome")
{
Object (message: message);
}
}
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