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