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