0ad/source/network/NetMessage.h
janwas c0ed950657 had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).

it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.

after several hours, the code now requires fewer casts and less
guesswork.

other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.

This was SVN commit r5942.
2008-05-11 18:48:32 +00:00

98 lines
2.8 KiB
C++

#ifndef INCLUDED_NETWORK_NETMESSAGE
#define INCLUDED_NETWORK_NETMESSAGE
#include "Serialization.h"
#include "SocketBase.h"
// 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
#define ALLNETMSGS_DONT_CREATE_NMTS
#include "AllNetMessages.h"
#undef ALLNETMSGS_DONT_CREATE_NMTS
class CNetCommand;
class CVector2D;
struct CEntityList;
/**
* The base class for network messages
*/
class CNetMessage: public ISerializable
{
ENetMessageType m_Type;
protected:
inline CNetMessage(ENetMessageType type):
m_Type(type)
{}
public:
virtual ~CNetMessage();
inline ENetMessageType GetType() const
{ return m_Type; }
/**
* @returns The length of the message when serialized.
*/
virtual size_t GetSerializedLength() const;
/**
* Serialize the message into the buffer. The buffer will have the size
* returned from the last call to GetSerializedLength()
*/
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(); }
/**
* Copy the message
*/
virtual CNetMessage *Copy() const;
/**
* 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.
*/
static CNetMessage *DeserializeMessage(ENetMessageType type, u8 *buffer, size_t length);
/**
* 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();
static CNetCommand *CommandFromJSArgs(const CEntityList &entities, JSContext* cx, uintN argc, jsval* argv, bool isQueued);
//These can create a net message without JS args
static CNetMessage *CreatePositionMessage( const CEntityList& entities, const int type, CVector2D pos );
static CNetMessage *CreateEntityIntMessage( const CEntityList& entities, const int type, HEntity& target, int action );
static CNetMessage *CreateProduceMessage( const CEntityList& entities, const int type, int proType, const CStrW& name );
};
typedef CNetMessage * (*NetMessageDeserializer) (const u8 *buffer, size_t length);
#include "simulation/EntityHandles.h"
struct SNetMessageDeserializerRegistration
{
ENetMessageType m_Type;
NetMessageDeserializer m_pDeserializer;
};
// This time, the classes are created
#include "AllNetMessages.h"
#endif // #ifndef INCLUDED_NETWORK_NETMESSAGE