[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 | |
---|
[9521] | 42 | typedef bool (*MessageCallback)( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId ); |
---|
[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 | { |
---|
[9519] | 63 | MessageType messageType; //!< type of the message |
---|
| 64 | byte * data; //!< data |
---|
| 65 | int length; //!< length of the data |
---|
| 66 | int number; //!< serial number |
---|
| 67 | int senderId; //!< userId of the sender |
---|
| 68 | int destinationId; //!< userId of the destination |
---|
| 69 | MessagePriority priority; //!< priority of the messages |
---|
[7671] | 70 | }; |
---|
| 71 | |
---|
| 72 | struct MessageUserQueue |
---|
| 73 | { |
---|
| 74 | std::list<NetworkMessage> messages; |
---|
| 75 | std::list<int> toAck; |
---|
[7681] | 76 | std::list<int> recievedMessages; |
---|
[7671] | 77 | }; |
---|
| 78 | |
---|
| 79 | typedef std::map<int,MessageUserQueue> MessageQueue; |
---|
| 80 | |
---|
[9508] | 81 | |
---|
| 82 | |
---|
[7671] | 83 | struct MessageHandler |
---|
| 84 | { |
---|
[9514] | 85 | MessageCallback cb; |
---|
[9508] | 86 | MessageType messageType; |
---|
[9514] | 87 | void * someData; |
---|
[7671] | 88 | }; |
---|
| 89 | |
---|
[9508] | 90 | typedef std::map<MessageType,MessageHandler> MessageHandlerMap; |
---|
[7671] | 91 | |
---|
[7631] | 92 | //! A class for sending messages over network |
---|
| 93 | class MessageManager : public Synchronizeable { |
---|
[7671] | 94 | protected: |
---|
| 95 | MessageManager(); |
---|
[7631] | 96 | public: |
---|
[7671] | 97 | inline static MessageManager * getInstance(){ if (!singletonRef) singletonRef = new MessageManager(); return singletonRef; } |
---|
[9494] | 98 | |
---|
[7631] | 99 | virtual ~MessageManager(); |
---|
[9494] | 100 | |
---|
[9508] | 101 | bool registerMessageHandler( MessageType messageType, MessageCallback cb, void * someData ); |
---|
[9494] | 102 | |
---|
[9508] | 103 | void sendMessage( MessageType messageType, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority ); |
---|
[7631] | 104 | |
---|
[9508] | 105 | void initUser( int userId ); |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | private: |
---|
[7631] | 109 | virtual int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ); |
---|
| 110 | virtual int setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ); |
---|
| 111 | virtual void cleanUpUser( int userId ); |
---|
[7872] | 112 | virtual void handleSentState( int userId, int stateId, int fromStateId ){} |
---|
| 113 | virtual void handleRecvState( int userId, int stateId, int fromStateId ){} |
---|
[9494] | 114 | |
---|
[7631] | 115 | |
---|
[9508] | 116 | private: |
---|
| 117 | static MessageManager * singletonRef; //!< the singleton reference |
---|
[7631] | 118 | |
---|
[9508] | 119 | std::list<NetworkMessage> incomingMessageQueue; //!< the incoming message buffer |
---|
| 120 | MessageQueue outgoingMessageQueue; //!< stores messages to send |
---|
[7693] | 121 | MessageHandlerMap messageHandlerMap; //!< contains handlers for messages |
---|
[7631] | 122 | |
---|
[7693] | 123 | int newNumber; //!< used to create unique message numbers |
---|
[7681] | 124 | |
---|
[9508] | 125 | |
---|
[7631] | 126 | }; |
---|
| 127 | |
---|
| 128 | #endif /* _PROTO_CLASS_H */ |
---|