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