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