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