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