1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/* Copyright (C) 2010 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. 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 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NETMESSAGE_H
#define NETMESSAGE_H
#include "Serialization.h"
// We need the enum from NetMessages.h, but we can't create any classes in
// NetMessages.h, since they in turn require CNetMessage to be defined
#define ALLNETMSGS_DONT_CREATE_NMTS
#include "NetMessages.h"
#undef ALLNETMSGS_DONT_CREATE_NMTS
/**
* The base class for all network messages exchanged within the game.
*/
class CNetMessage : public ISerializable
{
friend class CNetSession;
public:
CNetMessage();
CNetMessage(NetMessageType type);
virtual ~CNetMessage();
/**
* Retrieves the message type.
* @return Message type
*/
NetMessageType GetType() const { return m_Type; }
/**
* Serialize the message into the specified buffer parameter. The size
* required by the buffer parameter can be found by a call to
* GetSerializedLength method. The information contained within the message
* must be serialized before the message is sent. By default only the
* message type and its size are serialized in the buffer parameter.
*
* @param pBuffer Buffer where to serialize the message
* @return The position in the buffer right after the
* serialized message
*/
virtual u8* Serialize(u8* pBuffer) const;
/**
* Deserializes the message from the specified buffer.
*
* @param pStart Message start within the serialized buffer
* @param pEnd Message end within the serialized buffer
* @return The position in the buffer right after the
* message or NULL if an error occured
*/
virtual const u8* Deserialize(const u8* pStart, const u8* pEnd);
/**
* Retrieves the size in bytes of the serialized message. Before calling
* Serialize, the memory size for the buffer where to serialize the message
* object can be found by calling this method.
*
* @return The size of serialized message
*/
virtual size_t GetSerializedLength() const;
/**
* Returns a string representation for the message
*
* @return The message as a string
*/
virtual CStr ToString() const;
private:
NetMessageType m_Type; // Message type
};
/**
* Creates messages from data received through the network.
*/
class CNetMessageFactory
{
public:
/**
* Factory method which creates a message object based on the given data
*
* @param pData Data buffer
* @param dataSize Size of data buffer
* @param scriptInterface Script instance to use when constructing scripted messages
* @return The new message created
*/
static CNetMessage* CreateMessage(const void* pData, size_t dataSize, ScriptInterface& scriptInterface);
};
/**
* Special message type for simulation commands.
* These commands are exposed as arbitrary JS objects, associated with a specific player.
*/
class CSimulationMessage : public CNetMessage
{
public:
CSimulationMessage(ScriptInterface& scriptInterface);
CSimulationMessage(ScriptInterface& scriptInterface, u32 client, i32 player, u32 turn, jsval data);
virtual u8* Serialize(u8* pBuffer) const;
virtual const u8* Deserialize(const u8* pStart, const u8* pEnd);
virtual size_t GetSerializedLength() const;
virtual CStr ToString() const;
u32 m_Client;
i32 m_Player;
u32 m_Turn;
CScriptValRooted m_Data;
private:
ScriptInterface* m_ScriptInterface;
};
/**
* Special message type for updated to game startup settings.
*/
class CGameSetupMessage : public CNetMessage
{
NONCOPYABLE(CGameSetupMessage);
public:
CGameSetupMessage(ScriptInterface& scriptInterface);
CGameSetupMessage(ScriptInterface& scriptInterface, jsval data);
virtual u8* Serialize(u8* pBuffer) const;
virtual const u8* Deserialize(const u8* pStart, const u8* pEnd);
virtual size_t GetSerializedLength() const;
virtual CStr ToString() const;
CScriptValRooted m_Data;
private:
ScriptInterface& m_ScriptInterface;
};
// This time, the classes are created
#include "NetMessages.h"
#endif // NETMESSAGE_H
|