- Timestamp:
- Apr 28, 2008, 11:09:34 PM (17 years ago)
- Location:
- code/branches/network3
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network3/CMakeLists.txt
r1126 r1199 36 36 37 37 # global compiler/linker flags. force -O2! 38 SET(CMAKE_C_FLAGS "$ENV{CFLAGS} -O 2-Wall -g -ggdb")39 SET(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -O 2-Wall -g -ggdb")38 SET(CMAKE_C_FLAGS "$ENV{CFLAGS} -O0 -Wall -g -ggdb") 39 SET(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb") 40 40 SET(CMAKE_LD_FLAGS "$ENV{LDFLAGS}") 41 41 SET(CMAKE_EXE_LINKER_FLAGS " --no-undefined") -
code/branches/network3/src/network/Client.cc
r1184 r1199 230 230 int id = data->id; 231 231 COUT(5) << "received gamestate id: " << data->id << std::endl; 232 gamestate.pushGameState(data); 233 client_connection.addPacket(pck_gen.acknowledgement(id)); 234 client_connection.sendPackets(); 235 return; 232 if(gamestate.pushGameState(data)){ 233 client_connection.addPacket(pck_gen.acknowledgement(id)); 234 if(!client_connection.sendPackets()) 235 COUT(2) << "Could not send acknowledgment" << std::endl; 236 } 236 237 } 237 238 -
code/branches/network3/src/network/GameStateClient.cc
r1179 r1199 34 34 #include "core/BaseObject.h" 35 35 #include "Synchronisable.h" 36 37 #define GAMESTATEID_INITIAL -1 36 38 37 39 namespace network … … 44 46 GameStateClient::GameStateClient() { 45 47 COUT(5) << "this: " << this << std::endl; 48 last_diff_=0; 46 49 } 47 50 … … 50 53 51 54 bool GameStateClient::pushGameState(GameStateCompressed *compstate) { 52 GameState *gs;53 if(compstate->diffed){54 while(compstate->base_id > gameStateList.front()->id){55 // clean up old gamestates56 free(gameStateList.front()->data);57 // TODO: critical section58 delete gameStateList.front();59 gameStateList.pop();60 }61 if( compstate->base_id!=gameStateList.front()->id){55 cleanup(); 56 printGameStateMap(); 57 GameState *gs, *reference; 58 if(compstate->diffed && compstate->base_id!=GAMESTATEID_INITIAL){ 59 std::map<int, GameState*>::iterator it = gameStateMap.find(compstate->base_id); 60 if(it!=gameStateMap.end()) 61 reference = (it)->second; 62 else 63 reference = NULL; 64 if(!reference){ 62 65 COUT(4) << "pushGameState: no reference found to diff" << std::endl; 63 66 return false; 64 67 } 65 gs = decode( gameStateList.front(), compstate);68 gs = decode(reference, compstate); 66 69 } 67 70 else 68 71 gs = decode(compstate); 69 if(gs) 70 return loadSnapshot(gs); 72 if(gs){ 73 if (loadSnapshot(gs)){ 74 gameStateMap.insert(std::pair<int, GameState*>(gs->id, gs)); 75 COUT(4) << "adding decoded gs with id: " << gs->id << " diffed from: " << gs->base_id << std::endl; 76 last_diff_=gs->base_id; 77 return true; 78 }else{ 79 COUT(4) << "could not decode gs with id: " << gs->id << " diffed from: " << gs->base_id << std::endl; 80 delete[] gs->data; 81 delete gs; 82 return false; 83 } 84 } 71 85 COUT(4) << "could not use gamestate sent by server" << std::endl; 72 86 return false; … … 117 131 ///sigsegv may happen here again for some reason 118 132 ///sigsegv is receved after the COUT(4) above 119 Synchronisable *no = dynamic_cast<Synchronisable *>(ID((unsigned int) sync.classID)->fabricate()); 133 orxonox::Identifier* id = ID((unsigned int)sync.classID); 134 if(!id){ 135 COUT(4) << "We could not identify a new object" << std::endl; 136 continue; 137 } 138 Synchronisable *no = dynamic_cast<Synchronisable *>(id->fabricate()); 120 139 COUT(4) << "loadsnapshort: classid: " << sync.classID << " objectID: " << sync.objectID << " length: " << sync.length << std::endl; 121 140 no->objectID=sync.objectID; … … 140 159 } 141 160 142 GameState *GameStateClient::undiff(GameState * a, GameState *b) {143 unsigned char *ap = a->data, *bp = b->data;161 GameState *GameStateClient::undiff(GameState *old, GameState *diff) { 162 unsigned char *ap = old->data, *bp = diff->data; 144 163 int of=0; // pointers offset 145 164 int dest_length=0; 146 if( a->size>=b->size)147 dest_length= a->size;165 if(old->size>=diff->size) 166 dest_length=old->size; 148 167 else 149 dest_length=b->size; 150 unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 151 while(of<a->size && of<b->size){ 168 dest_length=diff->size; 169 // unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 170 unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)]; 171 while(of<old->size && of<diff->size){ 152 172 *(dp+of)=*(ap+of)^*(bp+of); // do the xor 153 173 ++of; 154 174 } 155 if( a->size!=b->size){ // do we have to fill up ?175 if(old->size!=diff->size){ // do we have to fill up ? 156 176 unsigned char n=0; 157 if( a->size<b->size){177 if(old->size<diff->size){ 158 178 while(of<dest_length){ 159 179 *(dp+of)=n^*(bp+of); … … 170 190 // FIXME: is it true or false now? (struct has changed, producing warnings) 171 191 GameState *r = new GameState; 172 r->id = b->id;192 r->id = diff->id; 173 193 r->size = dest_length; 194 r->base_id = diff->base_id; 174 195 r->diffed = false; 175 196 r->data = dp; … … 196 217 else 197 218 bufsize = normsize; 198 unsigned char* dest = (unsigned char*)malloc( bufsize ); 219 // unsigned char* dest = (unsigned char*)malloc( bufsize ); 220 unsigned char *dest = new unsigned char[bufsize]; 199 221 int retval; 200 222 uLongf length=normsize; … … 216 238 gamestate->diffed = a->diffed; 217 239 218 delete a->data; //delete compressed data240 delete[] a->data; //delete compressed data 219 241 delete a; //we do not need the old (struct) gamestate anymore 220 242 … … 222 244 } 223 245 224 GameState *GameStateClient::decode(GameState * a, GameStateCompressed *x) {225 GameState *t = decode(x);226 gameStateList.push(t);227 //return undiff(a, t);228 return t;246 GameState *GameStateClient::decode(GameState *old, GameStateCompressed *diff) { 247 COUT(4) << "using diffed gamestate" << std::endl; 248 GameState *t = decode(diff); 249 return undiff(old, t); 250 // return t; 229 251 } 230 252 … … 237 259 t->data = x->data; 238 260 t->size = x->normsize; 239 gameStateList.push(t);240 261 return t; 241 262 } 242 263 264 void GameStateClient::cleanup(){ 265 std::map<int, GameState*>::iterator temp, it = gameStateMap.begin(); 266 while(it!=gameStateMap.end()){ 267 if(it->first>=last_diff_) 268 break; 269 // otherwise delete that stuff 270 delete[] (*it).second->data; 271 delete (*it).second; 272 temp=it++; 273 gameStateMap.erase(temp); 274 } 275 } 276 277 void GameStateClient::printGameStateMap(){ 278 std::map<int, GameState*>::iterator it; 279 COUT(4) << "gamestates: "; 280 for(it=gameStateMap.begin(); it!=gameStateMap.end(); it++){ 281 COUT(4) << it->first << ":" << it->second << "|"; 282 } 283 COUT(4) << std::endl; 284 285 } 286 243 287 } -
code/branches/network3/src/network/GameStateClient.h
r1168 r1199 41 41 #define _GameStateClient_H__ 42 42 43 #include < queue>43 #include <map> 44 44 45 45 #include "NetworkPrereqs.h" … … 61 61 private: 62 62 bool loadSnapshot(GameState *state); 63 GameState *undiff(GameState *a, GameState *b); 63 void cleanup(); 64 GameState *undiff(GameState *old, GameState *diff); 64 65 GameState *decompress(GameStateCompressed *a); 65 GameState *decode(GameState * a, GameStateCompressed *x);66 GameState *decode(GameState *old, GameStateCompressed *diff); 66 67 GameState *decode(GameStateCompressed *x); 67 68 void removeObject(orxonox::Iterator<Synchronisable> &it); 69 void printGameStateMap(); 68 70 69 71 GameState *reference; 70 std::queue<GameState *> gameStateList; 72 int last_diff_; 73 std::map<int, GameState *> gameStateMap; 71 74 }; 72 75 -
code/branches/network3/src/network/GameStateManager.cc
r1173 r1199 52 52 { 53 53 GameStateManager::GameStateManager(ClientInformation *head) { 54 id =0;54 id_=0; 55 55 head_=head; 56 56 } … … 61 61 void GameStateManager::update(){ 62 62 cleanup(); 63 reference = getSnapshot(id); 64 gameStateMap.insert(std::pair<int, GameState*>(id, reference)); 65 gameStateUsed[id]=0; 66 ++id; 63 reference = getSnapshot(); 64 COUT(4) << "inserting gamestate: " << reference << std::endl; 65 gameStateMap.insert(std::pair<int, GameState*>(id_, reference)); 66 gameStateUsed[id_]=0; 67 printGameStates(); 67 68 return; 68 69 } … … 79 80 while(it!=gameStateUsed.end()){ 80 81 if( (*it).second <= 0 ){ 81 free(gameStateMap[(*it).first]->data); 82 COUT(4) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl; 83 delete[] gameStateMap[(*it).first]->data; 82 84 delete gameStateMap[(*it).first]; 83 85 gameStateMap.erase((*it).first); 84 86 gameStateUsed.erase(it++); 85 }else //as soon as we got a used gamestate break here because we could use newer gamestates in future 87 continue; 88 }else if(id_-it->first<=KEEP_GAMESTATES){ //as soon as we got a used gamestate break here because we could use newer gamestates in future but only if we do not exceed KEEP_GAMESTATES # of gamestates in cache 89 COUT(4) << "breaking " << std::endl; 86 90 break; 91 } 92 it++; 87 93 } 88 94 } … … 92 98 //Server::sendGameState? 93 99 int gID = head_->findClient(clientID)->getGamestateID(); 94 COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id << " diffed from: " << gID << " (not diffed yet)" << std::endl;95 100 COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << " (not diffed yet)" << std::endl; 101 // COUT(3) << "gamestatemap: " << &gameStateMap << std::endl; 96 102 //chose wheather the next gamestate is the first or not 97 103 if(gID != GAMESTATEID_INITIAL){ 98 GameState *client = gameStateMap[gID]; 104 // TODO something with the gamestatemap is wrong 105 GameState *client = gameStateMap.find(gID)->second; 99 106 GameState *server = reference; 100 107 //head_->findClient(clientID)->setGamestateID(id); 101 return encode(client, server); 108 COUT(3) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << std::endl; 109 if(client) 110 return encode(client, server); 111 else 112 return encode(server); 102 113 } else { 114 COUT(4) << "we got a GAMESTATEID_INITIAL for clientID: " << clientID << std::endl; 103 115 GameState *server = reference; 116 ackGameState(clientID, reference->id); 104 117 //head_->findClient(clientID)->setGamestateID(id); 105 118 return encode(server); … … 113 126 * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list 114 127 */ 115 GameState *GameStateManager::getSnapshot( int id)128 GameState *GameStateManager::getSnapshot() 116 129 { 117 130 //std::cout << "begin getSnapshot" << std::endl; 118 131 //the size of the gamestate 119 132 int totalsize=0; 120 int memsize= 1000;133 int memsize=0; 121 134 //the size of one specific synchronisable 122 135 int tempsize=0; … … 127 140 128 141 GameState *retval=new GameState; //return value 129 retval->id= id++;142 retval->id=++id_; 130 143 COUT(4) << "G.ST.Man: producing gamestate with id: " << retval->id << std::endl; 131 144 // offset of memory functions … … 133 146 // get total size of gamestate 134 147 for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){ 135 size+=it->getSize(); 136 size+=3*sizeof(int); 137 } 138 retval->data = (unsigned char*)malloc(size); 148 size+=it->getSize(); // size of the actual data of the synchronisable 149 size+=3*sizeof(int); // size of datasize, classID and objectID 150 } 151 //retval->data = (unsigned char*)malloc(size); 152 retval->data = new unsigned char[size]; 139 153 if(!retval->data){ 140 154 COUT(2) << "GameStateManager: could not allocate memory" << std::endl; … … 148 162 // add place for data and 3 ints (length,classid,objectid) 149 163 totalsize+=tempsize+3*sizeof(int); 150 // allocate additional space151 if( (totalsize+tempsize) > memsize){152 COUT( 5) << "G.St.Man: need additional memory" << std::endl;164 // allocate+tempsize additional space 165 if(totalsize > size){ 166 COUT(3) << "G.St.Man: need additional memory" << std::endl; 153 167 // if(tempsize < 1000){ 154 168 // retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000); … … 190 204 GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) { 191 205 COUT(5) << "G.St.Man: this will be a DIFFED gamestate" << std::endl; 192 //GameStater = diff(a,b);206 GameState *r = diff(a,b); 193 207 //r.diffed = true; 194 GameState *r = b;195 r->diffed = false;208 // GameState *r = b; 209 // r->diffed = false; 196 210 //return compress_(r); 197 211 GameStateCompressed *g = new GameStateCompressed; 198 g->base_id = b->base_id;199 g->id = b->id;200 g->diffed = b->diffed;201 g->data = b->data;202 g->normsize = b->size;203 g->compsize = b->size;212 g->base_id = r->base_id; 213 g->id = r->id; 214 g->diffed = r->diffed; 215 g->data = r->data; 216 g->normsize = r->size; 217 g->compsize = r->size; 204 218 return g; 205 219 } … … 226 240 else 227 241 dest_length=b->size; 228 unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 242 //unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 243 unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)]; 229 244 while(of<a->size && of<b->size){ 230 245 *(dp+of)=*(ap+of)^*(bp+of); // do the xor … … 263 278 uLongf buffer = (uLongf)((a->size + 12)*1.01)+1; 264 279 //COUT(4) << "size: " << size << ", buffer: " << buffer << std::endl; 265 unsigned char* dest = (unsigned char*)malloc( buffer ); 280 //unsigned char* dest = (unsigned char*)malloc( buffer ); 281 unsigned char *dest = new unsigned char[buffer]; 266 282 //COUT(4) << "dest: " << dest << std::endl; 267 283 int retval; … … 296 312 void GameStateManager::ackGameState(int clientID, int gamestateID) { 297 313 ClientInformation *temp = head_->findClient(clientID); 298 int curid = temp->getID(); 314 int curid = temp->getGamestateID(); 315 COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl; 299 316 // decrease usage of gamestate and save it 300 deleteUnusedGameState(curid);317 // deleteUnusedGameState(curid); 301 318 //increase gamestateused 302 ++gameStateUsed.find(gamestateID)->second; 319 --(gameStateUsed.find(curid)->second); 320 ++(gameStateUsed.find(gamestateID)->second); 303 321 temp->setGamestateID(gamestateID); 304 322 /* … … 308 326 } 309 327 310 bool GameStateManager::deleteUnusedGameState(int gamestateID) { 311 int used = --(gameStateUsed.find(gamestateID)->second); 312 if(id-gamestateID>KEEP_GAMESTATES && used==0){ 313 // delete gamestate 314 delete gameStateMap.find(gamestateID)->second; 315 gameStateMap.erase(gamestateID); 316 return true; 317 } 318 return false; 328 bool GameStateManager::printGameStates() { 329 std::map<int, GameState*>::iterator it; 330 COUT(4) << "gamestates: "; 331 for(it = gameStateMap.begin(); it!=gameStateMap.end(); it++){ 332 COUT(4) << (*it).first << ":" << (*it).second << " | "; 333 } 334 COUT(4) << std::endl; 319 335 } 320 336 -
code/branches/network3/src/network/GameStateManager.h
r1168 r1199 50 50 { 51 51 52 #define KEEP_GAMESTATES 2052 #define KEEP_GAMESTATES 100 53 53 54 54 /** … … 78 78 GameStateCompressed *popGameState(int clientID); 79 79 void ackGameState(int clientID, int gamestateID); 80 int id;81 80 private: 82 81 void cleanup(); // "garbage handler" 83 GameState *getSnapshot( int id);82 GameState *getSnapshot(); 84 83 GameStateCompressed *encode(GameState *a, GameState *b); 85 84 GameStateCompressed *encode(GameState *a); 86 85 GameState *diff(GameState *a, GameState *b); 87 86 GameStateCompressed *compress_(GameState *a); 88 bool deleteUnusedGameState(int gamestateID);87 bool printGameStates(); 89 88 90 89 std::map<int, GameState*> gameStateMap; //map gsID to gamestate* … … 92 91 GameState *reference; 93 92 ClientInformation *head_; 93 int id_; 94 94 }; 95 95 -
code/branches/network3/src/network/PacketDecoder.cc
r1177 r1199 97 97 { 98 98 ack* a = new ack; 99 *a = *(ack*) packet->data; //press pattern of ack on new data100 101 102 COUT( 5) << "PacketDecoder: got ack id: " << a->id<< std::endl;99 *a = *(ack*)(packet->data); //press pattern of ack on new data 100 101 102 COUT(4) << "PacketDecoder: got ack id: " << a->a << std::endl; 103 103 processAck( a, clientId ); //debug info 104 104 -
code/branches/network3/src/network/Server.cc
r1184 r1199 143 143 updateGamestate(); 144 144 145 //sleep(1); // TODO remove145 usleep(200000); // TODO remove 146 146 return; 147 147 } … … 220 220 221 221 void Server::processAck( ack *data, int clientID) { 222 COUT( 5) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->id<< std::endl;223 clients->findClient(clientID)->setGamestateID(data->a);222 COUT(4) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl; 223 gamestates->ackGameState(clientID, data->a); 224 224 } 225 225 -
code/branches/network3/src/network/Synchronisable.cc
r1180 r1199 147 147 retVal.classID=this->classID; 148 148 retVal.length=getSize(); 149 COUT( 4) << "Synchronisable getting data from objectID: " << retVal.objectID << " classID: " << retVal.classID << " length: " << retVal.length << std::endl;149 COUT(5) << "Synchronisable getting data from objectID: " << retVal.objectID << " classID: " << retVal.classID << " length: " << retVal.length << std::endl; 150 150 retVal.data=mem; 151 151 // copy to location … … 163 163 const char *data = ( ( *(std::string *) (*i)->var).c_str()); 164 164 std::memcpy( retVal.data+n, (void*)data, (*i)->size); 165 COUT( 4) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;165 COUT(5) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl; 166 166 n+=(*i)->size; 167 167 break; … … 193 193 case STRING: 194 194 (*i)->size = *(int *)data; 195 COUT( 4) << "string size: " << (*i)->size << std::endl;195 COUT(5) << "string size: " << (*i)->size << std::endl; 196 196 data+=sizeof(int); 197 197 *((std::string *)((*i)->var)) = std::string((const char*)data); 198 COUT( 4) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl;198 COUT(5) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl; 199 199 data += (*i)->size; 200 200 break; … … 219 219 tsize+=sizeof(int); 220 220 (*i)->size=((std::string *)(*i)->var)->length()+1; 221 COUT( 4) << "String size: " << (*i)->size << std::endl;221 COUT(5) << "String size: " << (*i)->size << std::endl; 222 222 tsize+=(*i)->size; 223 223 break;
Note: See TracChangeset
for help on using the changeset viewer.