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 MessageType |
---|
23 | byte * data |
---|
24 | )[1..nmsg] |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | //!< different message ids |
---|
29 | enum MessageType |
---|
30 | { |
---|
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 |
---|
41 | }; |
---|
42 | |
---|
43 | |
---|
44 | typedef bool (*MessageCallback)( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId ); |
---|
45 | |
---|
46 | typedef enum RecieverType |
---|
47 | { |
---|
48 | RT_ALL_BUT_ME = 1, //!< message is sent to all users but myself |
---|
49 | RT_ALL_ME, //!< message is sent to all users |
---|
50 | RT_USER, //!< message is only sent to reciever |
---|
51 | RT_NOT_USER, //!< message is sent to all but reciever |
---|
52 | RT_SERVER //!< message is sent to server only |
---|
53 | }; |
---|
54 | |
---|
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 |
---|
60 | MP_UNRELIABLE //!< unreliable. low bandwidth |
---|
61 | }; |
---|
62 | |
---|
63 | struct NetworkMessage |
---|
64 | { |
---|
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 |
---|
73 | }; |
---|
74 | |
---|
75 | struct MessageUserQueue |
---|
76 | { |
---|
77 | std::list<NetworkMessage> messages; |
---|
78 | std::list<int> toAck; |
---|
79 | std::list<int> recievedMessages; |
---|
80 | }; |
---|
81 | |
---|
82 | typedef std::map<int,MessageUserQueue> MessageQueue; |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | struct MessageHandler |
---|
87 | { |
---|
88 | MessageCallback cb; |
---|
89 | MessageType messageType; |
---|
90 | void * someData; |
---|
91 | }; |
---|
92 | |
---|
93 | typedef std::map<MessageType,MessageHandler> MessageHandlerMap; |
---|
94 | |
---|
95 | //! A class for sending messages over network |
---|
96 | class MessageManager : public Synchronizeable { |
---|
97 | |
---|
98 | ObjectListDeclaration(MessageManager); |
---|
99 | public: |
---|
100 | inline static MessageManager * getInstance(){ if (!singletonRef) singletonRef = new MessageManager(); return singletonRef; } |
---|
101 | |
---|
102 | virtual ~MessageManager(); |
---|
103 | |
---|
104 | bool registerMessageHandler( MessageType messageType, MessageCallback cb, void * someData ); |
---|
105 | |
---|
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 ); |
---|
108 | |
---|
109 | void initUser( int userId ); |
---|
110 | |
---|
111 | void processData(); |
---|
112 | |
---|
113 | |
---|
114 | protected: |
---|
115 | MessageManager(); |
---|
116 | |
---|
117 | |
---|
118 | private: |
---|
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 ); |
---|
122 | virtual void handleSentState( int userId, int stateId, int fromStateId ){} |
---|
123 | virtual void handleRecvState( int userId, int stateId, int fromStateId ){} |
---|
124 | |
---|
125 | |
---|
126 | private: |
---|
127 | static MessageManager * singletonRef; //!< the singleton reference |
---|
128 | |
---|
129 | std::list<NetworkMessage> incomingMessageQueue; //!< the incoming message buffer |
---|
130 | MessageQueue outgoingMessageQueue; //!< stores messages to send |
---|
131 | MessageHandlerMap messageHandlerMap; //!< contains handlers for messages |
---|
132 | |
---|
133 | int newNumber; //!< used to create unique message numbers |
---|
134 | |
---|
135 | |
---|
136 | }; |
---|
137 | |
---|
138 | #endif /* _MESSAGE_MANAGER_H */ |
---|