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 | n = Converter::byteArrayToInt( data + i, &nAcks ); |
---|
133 | assert( n == INTSIZE ); |
---|
134 | i += n; |
---|
135 | |
---|
136 | std::list<int> acks; |
---|
137 | |
---|
138 | int number; |
---|
139 | |
---|
140 | for ( int j = 0; j < nAcks; j++ ) |
---|
141 | { |
---|
142 | n = Converter::byteArrayToInt( data + i, &number ); |
---|
143 | assert( n == INTSIZE ); |
---|
144 | i += n; |
---|
145 | |
---|
146 | acks.push_back( number ); |
---|
147 | } |
---|
148 | |
---|
149 | int nMessages; |
---|
150 | |
---|
151 | n = Converter::byteArrayToInt( data + i, &nMessages ); |
---|
152 | assert( n == INTSIZE ); |
---|
153 | i += n; |
---|
154 | |
---|
155 | for ( int j = 0; j < nMessages; j++ ) |
---|
156 | { |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * clean up memory reserved for user |
---|
162 | * @param userId userid |
---|
163 | */ |
---|
164 | void MessageManager::cleanUpUser( int userId ) |
---|
165 | { |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | * registers function to handle messages with id messageId. someData is passed to callbackfuntion |
---|
170 | * @param messageId message id to handle |
---|
171 | * @param cb function pointer to callback function |
---|
172 | * @param someData this pointer is passed to callback function without modification |
---|
173 | * @return true on success |
---|
174 | */ |
---|
175 | bool MessageManager::registerMessageHandler( MessageId messageId, MessageCallback cb, void * someData ) |
---|
176 | { |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * initializes buffers for user |
---|
181 | * @param userId userId |
---|
182 | */ |
---|
183 | void MessageManager::initUser( int userId ) |
---|
184 | { |
---|
185 | } |
---|