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