2003-11-25 03:11:50 +01:00
|
|
|
#ifndef _NetMessage_H
|
|
|
|
#define _NetMessage_H
|
|
|
|
|
2005-02-26 16:13:15 +01:00
|
|
|
#include "lib/types.h"
|
2004-03-08 03:04:34 +01:00
|
|
|
#include "Serialization.h"
|
|
|
|
#include "Network/SocketBase.h"
|
2003-11-25 03:11:50 +01:00
|
|
|
|
2004-03-08 03:04:34 +01:00
|
|
|
// We need the enum from AllNetMessages.h, but we can't create any classes in
|
|
|
|
// AllNetMessages, since they in turn require CNetMessage to be defined
|
2003-11-25 03:11:50 +01:00
|
|
|
#define ALLNETMSGS_DONT_CREATE_NMTS
|
|
|
|
#include "AllNetMessages.h"
|
|
|
|
#undef ALLNETMSGS_DONT_CREATE_NMTS
|
|
|
|
|
2005-05-23 04:39:07 +02:00
|
|
|
class CNetCommand;
|
2005-05-18 23:19:56 +02:00
|
|
|
struct CEntityList;
|
2005-05-18 07:32:09 +02:00
|
|
|
|
2004-03-08 03:04:34 +01:00
|
|
|
/**
|
|
|
|
* The base class for network messages
|
|
|
|
*/
|
|
|
|
class CNetMessage: public ISerializable
|
2003-11-25 03:11:50 +01:00
|
|
|
{
|
2004-03-08 03:04:34 +01:00
|
|
|
ENetMessageType m_Type;
|
2003-11-25 03:11:50 +01:00
|
|
|
protected:
|
2004-03-08 03:04:34 +01:00
|
|
|
inline CNetMessage(ENetMessageType type):
|
2003-11-25 03:11:50 +01:00
|
|
|
m_Type(type)
|
|
|
|
{}
|
|
|
|
|
2003-11-30 17:36:43 +01:00
|
|
|
public:
|
2004-09-21 16:40:43 +02:00
|
|
|
virtual ~CNetMessage();
|
|
|
|
|
2004-03-08 03:04:34 +01:00
|
|
|
inline ENetMessageType GetType() const
|
2003-11-25 03:11:50 +01:00
|
|
|
{ return m_Type; }
|
|
|
|
|
2003-11-30 17:36:43 +01:00
|
|
|
/**
|
|
|
|
* @returns The length of the message when serialized.
|
|
|
|
*/
|
2003-11-25 03:11:50 +01:00
|
|
|
virtual uint GetSerializedLength() const;
|
2003-11-30 17:36:43 +01:00
|
|
|
/**
|
|
|
|
* Serialize the message into the buffer. The buffer will have the size
|
|
|
|
* returned from the last call to GetSerializedLength()
|
|
|
|
*/
|
2004-03-08 03:04:34 +01:00
|
|
|
virtual u8 *Serialize(u8 *buffer) const;
|
|
|
|
virtual const u8 *Deserialize(const u8 *pos, const u8 *end);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a string representation of the message. The default implementation
|
|
|
|
* returns the empty string
|
|
|
|
*/
|
|
|
|
virtual CStr GetString() const;
|
|
|
|
inline operator CStr() const
|
|
|
|
{ return GetString(); }
|
2003-11-25 03:11:50 +01:00
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
/**
|
|
|
|
* Copy the message
|
|
|
|
*/
|
|
|
|
virtual CNetMessage *Copy() const;
|
|
|
|
|
2003-11-30 17:36:43 +01:00
|
|
|
/**
|
|
|
|
* Deserialize a net message, using the globally registered deserializers.
|
|
|
|
*
|
|
|
|
* @param type The NetMessageType of the message
|
|
|
|
* @param buffer A pointer to the buffer holding the message data
|
|
|
|
* @param length The length of the message data
|
|
|
|
*
|
|
|
|
* @returns a pointer to a newly created CNetMessage subclass, or NULL if
|
|
|
|
* there was an error in data format.
|
|
|
|
*/
|
2004-03-08 03:04:34 +01:00
|
|
|
static CNetMessage *DeserializeMessage(ENetMessageType type, u8 *buffer, uint length);
|
2005-05-18 07:32:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a selection of message types as JS constants.
|
|
|
|
* The constant's names will be the same as those of the enums
|
|
|
|
*/
|
|
|
|
static void ScriptingInit();
|
|
|
|
|
2005-05-23 04:39:07 +02:00
|
|
|
static CNetCommand *CommandFromJSArgs(const CEntityList &entities, JSContext* cx, uintN argc, jsval* argv);
|
2003-11-25 03:11:50 +01:00
|
|
|
};
|
|
|
|
|
2003-11-30 17:36:43 +01:00
|
|
|
typedef CNetMessage * (*NetMessageDeserializer) (const u8 *buffer, uint length);
|
2003-11-25 03:11:50 +01:00
|
|
|
|
2005-01-19 23:32:25 +01:00
|
|
|
#include "EntityHandles.h"
|
2004-01-21 03:08:09 +01:00
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
struct SNetMessageDeserializerRegistration
|
|
|
|
{
|
2004-03-08 03:04:34 +01:00
|
|
|
ENetMessageType m_Type;
|
2003-11-25 03:11:50 +01:00
|
|
|
NetMessageDeserializer m_pDeserializer;
|
|
|
|
};
|
|
|
|
|
2004-03-08 03:04:34 +01:00
|
|
|
// This time, the classes are created
|
2003-11-25 03:11:50 +01:00
|
|
|
#include "AllNetMessages.h"
|
|
|
|
|
|
|
|
#endif // #ifndef _NetMessage_H
|