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