| 1 | /*! | 
|---|
| 2 | * @file message_manager.h | 
|---|
| 3 | * @brief Definition of MessageManager | 
|---|
| 4 | */ | 
|---|
| 5 |  | 
|---|
| 6 | #ifndef _MESSAGE_MANAGER_H | 
|---|
| 7 | #define _MESSAGE_MANAGER_H | 
|---|
| 8 |  | 
|---|
| 9 | #include "synchronizeable.h" | 
|---|
| 10 |  | 
|---|
| 11 | #include <map> | 
|---|
| 12 | #include <list> | 
|---|
| 13 |  | 
|---|
| 14 | /* | 
|---|
| 15 | Protocol: | 
|---|
| 16 | int nacks | 
|---|
| 17 | int acks[1..nacks] | 
|---|
| 18 | int nmsg | 
|---|
| 19 | ( | 
|---|
| 20 | int length | 
|---|
| 21 | int number | 
|---|
| 22 | int MessageId | 
|---|
| 23 | byte * data | 
|---|
| 24 | )[1..nmsg] | 
|---|
| 25 | */ | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | enum MessageId | 
|---|
| 29 | { | 
|---|
| 30 | TESTMESSAGEID = 1, | 
|---|
| 31 | MSGID_DELETESYNCHRONIZEABLE, | 
|---|
| 32 | MSGID_PREFEREDTEAM, | 
|---|
| 33 | MSGID_CHANGEPLAYERNAME | 
|---|
| 34 | }; | 
|---|
| 35 |  | 
|---|
| 36 | typedef bool (*MessageCallback)( MessageId messageId, byte * data, int dataLength, void * someData, int userId ); | 
|---|
| 37 |  | 
|---|
| 38 | enum RecieverType | 
|---|
| 39 | { | 
|---|
| 40 | RT_ALL_NOT_ME = 1,   //!< message is sent to all users | 
|---|
| 41 | RT_ALL_ME,           //!< message is sent to all users | 
|---|
| 42 | RT_USER,             //!< message is only sent to reciever | 
|---|
| 43 | RT_NOT_USER,         //!< message is sent to all but reciever | 
|---|
| 44 | RT_SERVER            //!< message is sent to server only | 
|---|
| 45 | }; | 
|---|
| 46 |  | 
|---|
| 47 | //TODO implement priority handling | 
|---|
| 48 | enum MessagePriority | 
|---|
| 49 | { | 
|---|
| 50 | MP_HIGHBANDWIDTH = 1,  //!< fast and reliable but uses a lot of bandwidth | 
|---|
| 51 | MP_LOWBANDWIDTH,       //!< may take a long time to arrive. reliable | 
|---|
| 52 | MP_UNRELIABLE          //!< unreliable. low bandwidth | 
|---|
| 53 | }; | 
|---|
| 54 |  | 
|---|
| 55 | struct NetworkMessage | 
|---|
| 56 | { | 
|---|
| 57 | MessageId        messageId; | 
|---|
| 58 | byte *           data; | 
|---|
| 59 | int              length; | 
|---|
| 60 | int              number; | 
|---|
| 61 | MessagePriority  priority; | 
|---|
| 62 | }; | 
|---|
| 63 |  | 
|---|
| 64 | struct MessageUserQueue | 
|---|
| 65 | { | 
|---|
| 66 | std::list<NetworkMessage> messages; | 
|---|
| 67 | std::list<int>            toAck; | 
|---|
| 68 | std::list<int>            recievedMessages; | 
|---|
| 69 | }; | 
|---|
| 70 |  | 
|---|
| 71 | typedef std::map<int,MessageUserQueue> MessageQueue; | 
|---|
| 72 |  | 
|---|
| 73 | struct MessageHandler | 
|---|
| 74 | { | 
|---|
| 75 | MessageCallback cb; | 
|---|
| 76 | MessageId       messageId; | 
|---|
| 77 | void *          someData; | 
|---|
| 78 | }; | 
|---|
| 79 |  | 
|---|
| 80 | typedef std::map<MessageId,MessageHandler> MessageHandlerMap; | 
|---|
| 81 |  | 
|---|
| 82 | //! A class for sending messages over network | 
|---|
| 83 | class MessageManager : public Synchronizeable { | 
|---|
| 84 | protected: | 
|---|
| 85 | MessageManager(); | 
|---|
| 86 | public: | 
|---|
| 87 | inline static MessageManager * getInstance(){ if (!singletonRef) singletonRef = new MessageManager();  return singletonRef; } | 
|---|
| 88 |  | 
|---|
| 89 | virtual ~MessageManager(); | 
|---|
| 90 |  | 
|---|
| 91 | bool registerMessageHandler( MessageId messageId, MessageCallback cb, void * someData ); | 
|---|
| 92 |  | 
|---|
| 93 | void sendMessage( MessageId messageId, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority ); | 
|---|
| 94 |  | 
|---|
| 95 | virtual int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ); | 
|---|
| 96 | virtual int setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ); | 
|---|
| 97 | virtual void cleanUpUser( int userId ); | 
|---|
| 98 | virtual void handleSentState( int userId, int stateId, int fromStateId ){} | 
|---|
| 99 | virtual void handleRecvState( int userId, int stateId, int fromStateId ){} | 
|---|
| 100 |  | 
|---|
| 101 | void initUser( int userId ); | 
|---|
| 102 |  | 
|---|
| 103 |  | 
|---|
| 104 | private: | 
|---|
| 105 | static MessageManager *   singletonRef; | 
|---|
| 106 | MessageQueue              messageQueue;           //!< stores messages to send | 
|---|
| 107 | MessageHandlerMap         messageHandlerMap;      //!< contains handlers for messages | 
|---|
| 108 |  | 
|---|
| 109 | int                       newNumber;              //!< used to create unique message numbers | 
|---|
| 110 | std::list<NetworkMessage> incomingMessabeBuffer; | 
|---|
| 111 |  | 
|---|
| 112 | }; | 
|---|
| 113 |  | 
|---|
| 114 | #endif /* _PROTO_CLASS_H */ | 
|---|