[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 |
---|
[9656] | 22 | int MessageType |
---|
[7671] | 23 | byte * data |
---|
| 24 | )[1..nmsg] |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | |
---|
[9656] | 28 | //!< different message ids |
---|
| 29 | enum MessageType |
---|
[7671] | 30 | { |
---|
[9656] | 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_PROXY_NEWCLIENT, //!< informs the master server about a new client |
---|
| 39 | MSGID_PROXY_LEAVECLIENT, //!< informs the master and other proxy servers about a leaving client |
---|
| 40 | MSGID_PROXY_COMMAND, //!< command handler: delivers commands from and to proxies/clients |
---|
[7671] | 41 | }; |
---|
| 42 | |
---|
[7678] | 43 | |
---|
[9656] | 44 | typedef bool (*MessageCallback)( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId ); |
---|
| 45 | |
---|
| 46 | typedef enum RecieverType |
---|
[7671] | 47 | { |
---|
[9494] | 48 | RT_ALL_BUT_ME = 1, //!< message is sent to all users but myself |
---|
[8068] | 49 | RT_ALL_ME, //!< message is sent to all users |
---|
| 50 | RT_USER, //!< message is only sent to reciever |
---|
[8228] | 51 | RT_NOT_USER, //!< message is sent to all but reciever |
---|
| 52 | RT_SERVER //!< message is sent to server only |
---|
[7671] | 53 | }; |
---|
| 54 | |
---|
[7681] | 55 | //TODO implement priority handling |
---|
| 56 | enum MessagePriority |
---|
| 57 | { |
---|
| 58 | MP_HIGHBANDWIDTH = 1, //!< fast and reliable but uses a lot of bandwidth |
---|
| 59 | MP_LOWBANDWIDTH, //!< may take a long time to arrive. reliable |
---|
[9494] | 60 | MP_UNRELIABLE //!< unreliable. low bandwidth |
---|
[7681] | 61 | }; |
---|
| 62 | |
---|
[7671] | 63 | struct NetworkMessage |
---|
| 64 | { |
---|
[9656] | 65 | MessageType messageType; //!< type of the message |
---|
| 66 | byte * data; //!< data |
---|
| 67 | int length; //!< length of the data |
---|
| 68 | int number; //!< serial number |
---|
| 69 | int senderId; //!< userId of the sender |
---|
| 70 | int destinationId; //!< userId of the destination |
---|
| 71 | RecieverType recieverType; //!< type of the receiver |
---|
| 72 | MessagePriority priority; //!< priority of the messages |
---|
[7671] | 73 | }; |
---|
| 74 | |
---|
| 75 | struct MessageUserQueue |
---|
| 76 | { |
---|
| 77 | std::list<NetworkMessage> messages; |
---|
| 78 | std::list<int> toAck; |
---|
[7681] | 79 | std::list<int> recievedMessages; |
---|
[7671] | 80 | }; |
---|
| 81 | |
---|
| 82 | typedef std::map<int,MessageUserQueue> MessageQueue; |
---|
| 83 | |
---|
[9656] | 84 | |
---|
| 85 | |
---|
[7671] | 86 | struct MessageHandler |
---|
| 87 | { |
---|
[9656] | 88 | MessageCallback cb; |
---|
| 89 | MessageType messageType; |
---|
| 90 | void * someData; |
---|
[7671] | 91 | }; |
---|
| 92 | |
---|
[9656] | 93 | typedef std::map<MessageType,MessageHandler> MessageHandlerMap; |
---|
[7671] | 94 | |
---|
[7631] | 95 | //! A class for sending messages over network |
---|
| 96 | class MessageManager : public Synchronizeable { |
---|
[9656] | 97 | |
---|
| 98 | |
---|
[7631] | 99 | public: |
---|
[7671] | 100 | inline static MessageManager * getInstance(){ if (!singletonRef) singletonRef = new MessageManager(); return singletonRef; } |
---|
[9494] | 101 | |
---|
[7631] | 102 | virtual ~MessageManager(); |
---|
[9494] | 103 | |
---|
[9656] | 104 | bool registerMessageHandler( MessageType messageType, MessageCallback cb, void * someData ); |
---|
[9494] | 105 | |
---|
[9656] | 106 | void sendMessage( MessageType messageType, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority ); |
---|
| 107 | void sendMessage( MessageType messageType, byte * data, int dataLength, RecieverType recieverType, int sender, int reciever, MessagePriority messagePriority ); |
---|
[7631] | 108 | |
---|
[9656] | 109 | void initUser( int userId ); |
---|
| 110 | |
---|
| 111 | void processData(); |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | protected: |
---|
| 115 | MessageManager(); |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | private: |
---|
[7631] | 119 | virtual int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ); |
---|
| 120 | virtual int setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ); |
---|
| 121 | virtual void cleanUpUser( int userId ); |
---|
[7872] | 122 | virtual void handleSentState( int userId, int stateId, int fromStateId ){} |
---|
| 123 | virtual void handleRecvState( int userId, int stateId, int fromStateId ){} |
---|
[9494] | 124 | |
---|
[7631] | 125 | |
---|
[9656] | 126 | private: |
---|
| 127 | static MessageManager * singletonRef; //!< the singleton reference |
---|
[7631] | 128 | |
---|
[9656] | 129 | std::list<NetworkMessage> incomingMessageQueue; //!< the incoming message buffer |
---|
| 130 | MessageQueue outgoingMessageQueue; //!< stores messages to send |
---|
[7693] | 131 | MessageHandlerMap messageHandlerMap; //!< contains handlers for messages |
---|
[7631] | 132 | |
---|
[7693] | 133 | int newNumber; //!< used to create unique message numbers |
---|
[7681] | 134 | |
---|
[9656] | 135 | |
---|
[7631] | 136 | }; |
---|
| 137 | |
---|
[9656] | 138 | #endif /* _MESSAGE_MANAGER_H */ |
---|