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