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