1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Christoph Renner |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
17 | |
---|
18 | #include "message_manager.h" |
---|
19 | |
---|
20 | using namespace std; |
---|
21 | |
---|
22 | MessageManager* MessageManager::singletonRef = NULL; |
---|
23 | |
---|
24 | |
---|
25 | /** |
---|
26 | * standard constructor |
---|
27 | */ |
---|
28 | MessageManager::MessageManager () |
---|
29 | { |
---|
30 | this->setClassID( CL_MESSAGE_MANAGER, "MessageManager" ); |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | * standard deconstructor |
---|
36 | */ |
---|
37 | MessageManager::~MessageManager () |
---|
38 | { |
---|
39 | for ( MessageQueue::iterator it = messageQueue.begin(); it != messageQueue.end(); it++ ) |
---|
40 | { |
---|
41 | for ( std::list<NetworkMessage>::iterator it2 = it->second.messages.begin(); it2 != it->second.messages.end(); it2++ ) |
---|
42 | { |
---|
43 | if ( it2->data ) |
---|
44 | { |
---|
45 | delete it2->data; |
---|
46 | it2->data = NULL; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | it->second.messages.clear(); |
---|
51 | it->second.toAck.clear(); |
---|
52 | } |
---|
53 | |
---|
54 | messageQueue.clear(); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * get the diff to last acked state of userId |
---|
59 | * |
---|
60 | * this class does not use the normal SynchronizeableVars for zynchronisation. instead |
---|
61 | * it defines its own protocol |
---|
62 | * |
---|
63 | * @param userId user to create diff for |
---|
64 | * @param data buffer to copy diff in |
---|
65 | * @param maxLength max bytes to copy into data |
---|
66 | * @param stateId id of current state |
---|
67 | * @param fromStateId the reference state for the delta state |
---|
68 | * @param priorityTH tells getStateDiff to not send element with priority \< priorityTH |
---|
69 | * @return n bytes copied into data |
---|
70 | */ |
---|
71 | int MessageManager::getStateDiff( int userId, byte * data, int maxLength, int stateId, int fromStateId, int priorityTH ) |
---|
72 | { |
---|
73 | int i = 0; |
---|
74 | int n; |
---|
75 | |
---|
76 | n = Converter::intToByteArray( messageQueue[userId].toAck.size(), data + i, maxLength ); |
---|
77 | i += n; |
---|
78 | assert( n == INTSIZE ); |
---|
79 | |
---|
80 | for ( std::list<int>::iterator it = messageQueue[userId].toAck.begin(); it != messageQueue[userId].toAck.end(); it++) |
---|
81 | { |
---|
82 | n = Converter::intToByteArray( *it, data + i, maxLength ); |
---|
83 | i += n; |
---|
84 | assert( n == INTSIZE ); |
---|
85 | } |
---|
86 | |
---|
87 | messageQueue[userId].toAck.clear(); |
---|
88 | |
---|
89 | n = Converter::intToByteArray( messageQueue[userId].messages.size(), data + i, maxLength ); |
---|
90 | i += n; |
---|
91 | assert( n == INTSIZE ); |
---|
92 | |
---|
93 | for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); it++ ) |
---|
94 | { |
---|
95 | n = Converter::intToByteArray( it->length, data + i, maxLength ); |
---|
96 | i += n; |
---|
97 | assert( n == INTSIZE ); |
---|
98 | |
---|
99 | n = Converter::intToByteArray( it->number, data + i, maxLength ); |
---|
100 | i += n; |
---|
101 | assert( n == INTSIZE ); |
---|
102 | |
---|
103 | n = Converter::intToByteArray( it->messageId, data + i, maxLength ); |
---|
104 | i += n; |
---|
105 | assert( n == INTSIZE ); |
---|
106 | |
---|
107 | assert( i + it->length <= maxLength ); |
---|
108 | memcpy( data + i, it->data, it->length ); |
---|
109 | i += it->length; |
---|
110 | } |
---|
111 | |
---|
112 | return i; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * sets a new state out of a diff created on another host |
---|
117 | * @param userId hostId of user who send me that diff |
---|
118 | * @param data pointer to diff |
---|
119 | * @param length length of diff |
---|
120 | * @param stateId id of current state |
---|
121 | * @param fromStateId id of the base state id |
---|
122 | * @return number bytes read |
---|
123 | * @todo check for permissions |
---|
124 | */ |
---|
125 | int MessageManager::setStateDiff( int userId, byte * data, int length, int stateId, int fromStateId ) |
---|
126 | { |
---|
127 | int i = 0; |
---|
128 | int n; |
---|
129 | |
---|
130 | int nAcks; |
---|
131 | |
---|
132 | assert( i + INTSIZE <= length ); |
---|
133 | n = Converter::byteArrayToInt( data + i, &nAcks ); |
---|
134 | assert( n == INTSIZE ); |
---|
135 | i += n; |
---|
136 | |
---|
137 | std::list<int> acks; |
---|
138 | |
---|
139 | int number; |
---|
140 | |
---|
141 | for ( int j = 0; j < nAcks; j++ ) |
---|
142 | { |
---|
143 | assert( i + INTSIZE <= length ); |
---|
144 | n = Converter::byteArrayToInt( data + i, &number ); |
---|
145 | assert( n == INTSIZE ); |
---|
146 | i += n; |
---|
147 | |
---|
148 | acks.push_back( number ); |
---|
149 | } |
---|
150 | |
---|
151 | int nMessages; |
---|
152 | |
---|
153 | assert( i + INTSIZE <= length ); |
---|
154 | n = Converter::byteArrayToInt( data + i, &nMessages ); |
---|
155 | assert( n == INTSIZE ); |
---|
156 | i += n; |
---|
157 | |
---|
158 | int messageLength, messageId; |
---|
159 | |
---|
160 | for ( int j = 0; j < nMessages; j++ ) |
---|
161 | { |
---|
162 | assert( i + INTSIZE <= length ); |
---|
163 | n = Converter::byteArrayToInt( data + i, &messageLength ); |
---|
164 | assert( n == INTSIZE ); |
---|
165 | i += n; |
---|
166 | |
---|
167 | assert( i + INTSIZE <= length ); |
---|
168 | n = Converter::byteArrayToInt( data + i, &number ); |
---|
169 | assert( n == INTSIZE ); |
---|
170 | i += n; |
---|
171 | |
---|
172 | assert( i + INTSIZE <= length ); |
---|
173 | n = Converter::byteArrayToInt( data + i, &messageId ); |
---|
174 | assert( n == INTSIZE ); |
---|
175 | i += n; |
---|
176 | |
---|
177 | if ( number > 0 ) |
---|
178 | messageQueue[userId].toAck.push_back( number ); |
---|
179 | |
---|
180 | assert( i + messageLength <= length ); |
---|
181 | assert( messageHandlerMap.find( (MessageId)messageId ) != messageHandlerMap.end() ); |
---|
182 | (*(messageHandlerMap[(MessageId)messageId].cb))( (MessageId)messageId, data + i, messageLength, messageHandlerMap[(MessageId)messageId].someData, userId ); |
---|
183 | i += messageLength; |
---|
184 | } |
---|
185 | |
---|
186 | return i; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * clean up memory reserved for user |
---|
191 | * @param userId userid |
---|
192 | */ |
---|
193 | void MessageManager::cleanUpUser( int userId ) |
---|
194 | { |
---|
195 | if ( messageQueue.find( userId ) == messageQueue.end() ) |
---|
196 | return; |
---|
197 | |
---|
198 | for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); it++ ) |
---|
199 | { |
---|
200 | if ( it->data ) |
---|
201 | delete it->data; |
---|
202 | it->data = NULL; |
---|
203 | } |
---|
204 | |
---|
205 | messageQueue[userId].toAck.clear(); |
---|
206 | |
---|
207 | messageQueue.erase( userId ); |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * registers function to handle messages with id messageId. someData is passed to callbackfuntion |
---|
212 | * @param messageId message id to handle |
---|
213 | * @param cb function pointer to callback function |
---|
214 | * @param someData this pointer is passed to callback function without modification |
---|
215 | * @return true on success |
---|
216 | */ |
---|
217 | bool MessageManager::registerMessageHandler( MessageId messageId, MessageCallback cb, void * someData ) |
---|
218 | { |
---|
219 | MessageHandler messageHandler; |
---|
220 | |
---|
221 | messageHandler.cb = cb; |
---|
222 | messageHandler.messageId = messageId; |
---|
223 | messageHandler.someData = someData; |
---|
224 | |
---|
225 | messageHandlerMap[messageId] = messageHandler; |
---|
226 | |
---|
227 | return true; |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | * initializes buffers for user |
---|
232 | * @param userId userId |
---|
233 | */ |
---|
234 | void MessageManager::initUser( int userId ) |
---|
235 | { |
---|
236 | // just do something so map creates a new entry |
---|
237 | messageQueue[userId].toAck.clear(); |
---|
238 | assert( messageQueue[userId].messages.size() == 0 ); |
---|
239 | } |
---|