Changeset 1907 for code/trunk/src/network/packet
- Timestamp:
- Oct 12, 2008, 7:40:47 PM (16 years ago)
- Location:
- code/trunk/src/network/packet
- Files:
-
- 12 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/packet/Acknowledgement.cc
r1769 r1907 40 40 #define _ACKID _PACKETID + sizeof(network::packet::ENUM::Type) 41 41 42 Acknowledgement::Acknowledgement( unsigned int id, int clientID )42 Acknowledgement::Acknowledgement( unsigned int id, unsigned int clientID ) 43 43 : Packet() 44 44 { 45 45 flags_ = flags_ | PACKET_FLAGS_ACK; 46 data_=new u nsigned char[ getSize() ];46 data_=new uint8_t[ getSize() ]; 47 47 *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::Acknowledgement; 48 *(u nsigned int *)(data_ + _ACKID ) = id;48 *(uint32_t *)(data_ + _ACKID ) = id; 49 49 clientID_=clientID; 50 50 } 51 51 52 Acknowledgement::Acknowledgement( u nsigned char *data,int clientID )52 Acknowledgement::Acknowledgement( uint8_t *data, unsigned int clientID ) 53 53 : Packet(data, clientID) 54 54 { … … 60 60 61 61 unsigned int Acknowledgement::getSize() const{ 62 return _ACKID + sizeof(u nsigned int);62 return _ACKID + sizeof(uint32_t); 63 63 } 64 64 … … 70 70 71 71 unsigned int Acknowledgement::getAckID(){ 72 return *(u nsigned int *)(data_ + _ACKID);72 return *(uint32_t *)(data_ + _ACKID); 73 73 } 74 74 -
code/trunk/src/network/packet/Acknowledgement.h
r1763 r1907 40 40 { 41 41 public: 42 Acknowledgement( unsigned int id, int clientID );43 Acknowledgement( u nsigned char* data,int clientID );42 Acknowledgement( unsigned int id, unsigned int clientID ); 43 Acknowledgement( uint8_t* data, unsigned int clientID ); 44 44 ~Acknowledgement(); 45 45 -
code/trunk/src/network/packet/Chat.cc
r1763 r1907 29 29 #include "Chat.h" 30 30 #include <assert.h> 31 #include "network/Host.h" 31 32 32 33 namespace network { 33 34 namespace packet { 34 35 35 #define PACKET_FLAGS_CHAT ENET_PACKET_FLAG_RELIABLE 36 #define _PACKETID 0 37 #define _MESSAGELENGTH _PACKETID + sizeof(ENUM::Type) 38 #define _MESSAGE _MESSAGELENGTH + sizeof(unsigned int) 36 #define PACKET_FLAGS_CHAT ENET_PACKET_FLAG_RELIABLE 37 #define _PACKETID 0 38 const int _PLAYERID = _PACKETID + sizeof(ENUM::Type); 39 #define _MESSAGELENGTH _PLAYERID + sizeof(uint32_t) 40 #define _MESSAGE _MESSAGELENGTH + sizeof(uint32_t) 39 41 40 Chat::Chat( std::string & message, int clientID )42 Chat::Chat( std::string message, unsigned int playerID ) 41 43 : Packet() 42 44 { … … 44 46 messageLength_ = message.length()+1; 45 47 data_=new unsigned char[ getSize() ]; 46 *(ENUM::Type *) &data_[ _PACKETID ]= ENUM::Chat;47 *(unsigned int *) &data_[ _MESSAGELENGTH ] = messageLength_;48 memcpy( &data_[ _MESSAGE ], (void *)message.c_str(), messageLength_ );49 clientID_=clientID;48 *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::Chat; 49 *(unsigned int *)(data_ + _PLAYERID ) = playerID; 50 *(unsigned int *)(data_ + _MESSAGELENGTH ) = messageLength_; 51 memcpy( data_+_MESSAGE, (void *)message.c_str(), messageLength_ ); 50 52 } 51 53 52 Chat::Chat( u nsigned char *data,int clientID )54 Chat::Chat( uint8_t* data, unsigned int clientID ) 53 55 : Packet(data, clientID) 54 56 { 55 messageLength_ = *(u nsigned int *)&data[ _MESSAGELENGTH ];57 messageLength_ = *(uint32_t *)(data + _MESSAGELENGTH ); 56 58 } 57 59 … … 65 67 66 68 bool Chat::process(){ 67 //TODO: change this !!! 68 assert(0); 69 bool b = Host::incomingChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_PLAYERID)); 69 70 delete this; 70 return true;71 return b; 71 72 } 72 73 73 74 unsigned char *Chat::getMessage(){ 74 return &data_[ _MESSAGE ];75 return data_ + _MESSAGE; 75 76 } 76 77 -
code/trunk/src/network/packet/Chat.h
r1837 r1907 16 16 { 17 17 public: 18 Chat( std::string & message, int clientID );19 Chat( u nsigned char* data,int clientID );18 Chat( std::string message, unsigned int playerID ); 19 Chat( uint8_t* data, unsigned int clientID ); 20 20 ~Chat(); 21 21 … … 26 26 unsigned char *getMessage(); 27 27 private: 28 u nsigned int messageLength_;29 int clientID_;28 uint32_t messageLength_; 29 unsigned int clientID_; 30 30 }; 31 31 -
code/trunk/src/network/packet/ClassID.cc
r1856 r1907 40 40 #define _PACKETID 0 41 41 #define _CLASSID _PACKETID + sizeof(ENUM::Type) 42 #define _CLASSNAMELENGTH _CLASSID + sizeof(u nsigned int)42 #define _CLASSNAMELENGTH _CLASSID + sizeof(uint32_t) 43 43 #define _CLASSNAME _CLASSNAMELENGTH + sizeof(classNameLength_) 44 44 … … 57 57 } 58 58 59 ClassID::ClassID( u nsigned char *data,int clientID )59 ClassID::ClassID( uint8_t* data, unsigned int clientID ) 60 60 : Packet(data, clientID) 61 61 { … … 68 68 69 69 unsigned int ClassID::getSize() const{ 70 return sizeof(network::packet::ENUM::Type) + 2*sizeof(u nsigned int) + classNameLength_;70 return sizeof(network::packet::ENUM::Type) + 2*sizeof(uint32_t) + classNameLength_; 71 71 } 72 72 … … 82 82 83 83 unsigned int ClassID::getClassID(){ 84 return *(u nsigned int *)&data_[ _CLASSID ];84 return *(uint32_t *)(data_ + _CLASSID); 85 85 } 86 86 -
code/trunk/src/network/packet/ClassID.h
r1763 r1907 43 43 public: 44 44 ClassID( unsigned int classID, std::string className ); 45 ClassID( u nsigned char* data,int clientID );45 ClassID( uint8_t* data, unsigned int clientID ); 46 46 ~ClassID(); 47 47 … … 53 53 unsigned char *getClassName(); 54 54 private: 55 u nsigned int classNameLength_;55 uint32_t classNameLength_; 56 56 }; 57 57 -
code/trunk/src/network/packet/Gamestate.cc
r1767 r1907 46 46 #define HEADER GAMESTATE_HEADER(data_) 47 47 48 49 #define PACKET_FLAG_GAMESTATE ENET_PACKET_FLAG_RELIABLE 50 48 51 Gamestate::Gamestate() 49 52 { 50 } 51 52 Gamestate::Gamestate(unsigned char *data, int clientID): 53 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 54 } 55 56 Gamestate::Gamestate(uint8_t *data, unsigned int clientID): 53 57 Packet(data, clientID) 54 58 { 59 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 60 } 61 62 Gamestate::Gamestate(uint8_t *data) 63 { 64 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 65 data_=data; 55 66 } 56 67 … … 74 85 return false; 75 86 } 76 87 88 #ifndef NDEBUG 89 std::list<Synchronisable*> slist; 90 std::list<Synchronisable*>::iterator iit; 91 #endif 77 92 //start collect data synchronisable by synchronisable 78 u nsigned char*mem=data_;93 uint8_t *mem=data_; 79 94 mem+=sizeof(GamestateHeader); 80 95 orxonox::ObjectList<Synchronisable>::iterator it; 81 96 for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it){ 82 tempsize=it->getSize 2(id, mode);97 tempsize=it->getSize(id, mode); 83 98 84 99 if(currentsize+tempsize > size){ … … 88 103 int addsize=tempsize; 89 104 while(++temp) 90 addsize+=temp->getSize 2(id, mode);91 data_ = (u nsigned char*)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);105 addsize+=temp->getSize(id, mode); 106 data_ = (uint8_t *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize); 92 107 if(!data_) 93 108 return false; … … 95 110 }// stop allocate additional memory 96 111 112 #ifndef NDEBUG 113 for(iit=slist.begin(); iit!=slist.end(); iit++) 114 assert((*iit)!=*it); 115 slist.push_back(*it); 116 #endif 117 118 //if(it->doSelection(id)) 119 dataMap_[mem-data_]=(*it); // save the mem location of the synchronisable data 97 120 if(!it->getData(mem, id, mode)) 98 121 return false; // mem pointer gets automatically increased because of call by reference … … 105 128 HEADER->packetType = ENUM::Gamestate; 106 129 assert( *(ENUM::Type *)(data_) == ENUM::Gamestate); 107 HEADER-> normsize = currentsize;130 HEADER->datasize = currentsize; 108 131 HEADER->id = id; 109 132 HEADER->diffed = false; … … 122 145 assert(!HEADER->compressed); 123 146 assert(!HEADER->diffed); 124 unsigned int size, objectID, classID; 125 unsigned char *mem=data_+sizeof(GamestateHeader); 147 uint8_t *mem=data_+sizeof(GamestateHeader); 126 148 // get the start of the Synchronisable list 127 orxonox::ObjectList<Synchronisable>::iterator it=orxonox::ObjectList<Synchronisable>::begin(); 128 129 while(mem < data_+sizeof(GamestateHeader)+HEADER->normsize){ 130 // extract synchronisable header 131 size = *(unsigned int *)mem; 132 objectID = *(unsigned int*)(mem+sizeof(unsigned int)); 133 classID = *(unsigned int*)(mem+2*sizeof(unsigned int)); 134 135 if(!it || it->objectID!=objectID || it->classID!=classID){ 136 // bad luck ;) 137 // delete the synchronisable (obviously seems to be deleted on the server) 138 while(it && it->objectID!=objectID) 139 removeObject(it); 140 141 if(!it){ 142 //fabricate the new synchronisable 143 if(!Synchronisable::fabricate(mem, mode)) 144 return false; 145 it=orxonox::ObjectList<Synchronisable>::end(); 146 }else{ 147 if(! it->updateData(mem, mode)) 148 { 149 COUT(1) << "We couldn't update objectID: " \ 150 << objectID << "; classID: " << classID << std::endl; 151 } 152 } 153 } else 149 //orxonox::ObjectList<Synchronisable>::iterator it=orxonox::ObjectList<Synchronisable>::begin(); 150 Synchronisable *s; 151 152 // update the data of the objects we received 153 while(mem < data_+sizeof(GamestateHeader)+HEADER->datasize){ 154 synchronisableHeader *objectheader = (synchronisableHeader*)mem; 155 156 s = Synchronisable::getSynchronisable( objectheader->objectID ); 157 if(!s) 154 158 { 155 // we have our object 156 if(! it->updateData(mem, mode)) 157 { 158 COUT(1) << "We couldn't update objectID: " \ 159 << objectID << "; classID: " << classID << std::endl; 160 } 161 } 162 ++it; 159 s = Synchronisable::fabricate(mem, mode); 160 assert(s); 161 // if(!s) 162 // return false; 163 } 164 else 165 { 166 bool b = s->updateData(mem, mode); 167 assert(b); 168 //if(!s->updateData(mem, mode)) 169 //return false; 170 } 163 171 } 164 172 165 173 return true; 166 174 } 175 176 167 177 168 178 int Gamestate::getID(){ … … 177 187 else 178 188 { 179 return HEADER-> normsize+sizeof(GamestateHeader);189 return HEADER->datasize+sizeof(GamestateHeader); 180 190 } 181 191 } 182 192 183 193 bool Gamestate::operator==(packet::Gamestate gs){ 184 u nsigned char*d1 = data_+sizeof(GamestateHeader);185 u nsigned char*d2 = gs.data_+sizeof(GamestateHeader);194 uint8_t *d1 = data_+sizeof(GamestateHeader); 195 uint8_t *d2 = gs.data_+sizeof(GamestateHeader); 186 196 assert(!isCompressed()); 187 197 assert(!gs.isCompressed()); 188 while(d1<data_+HEADER-> normsize)198 while(d1<data_+HEADER->datasize) 189 199 { 190 200 if(*d1!=*d2) … … 201 211 } 202 212 213 214 203 215 bool Gamestate::compressData() 204 216 { 205 217 assert(HEADER); 206 218 assert(!HEADER->compressed); 207 uLongf buffer = (uLongf)(((HEADER-> normsize + 12)*1.01)+1);219 uLongf buffer = (uLongf)(((HEADER->datasize + 12)*1.01)+1); 208 220 if(buffer==0) 209 221 return false; 210 222 211 u nsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)];212 u nsigned char*dest = GAMESTATE_START(ndata);223 uint8_t *ndata = new uint8_t[buffer+sizeof(GamestateHeader)]; 224 uint8_t *dest = GAMESTATE_START(ndata); 213 225 //unsigned char *dest = new unsigned char[buffer]; 214 u nsigned char*source = GAMESTATE_START(data_);226 uint8_t *source = GAMESTATE_START(data_); 215 227 int retval; 216 retval = compress( dest, &buffer, source, (uLong)(HEADER-> normsize) );228 retval = compress( dest, &buffer, source, (uLong)(HEADER->datasize) ); 217 229 switch ( retval ) { 218 230 case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; … … 223 235 #ifndef NDEBUG 224 236 //decompress and compare the start and the decompressed data 225 u nsigned char *rdata = new unsigned char[HEADER->normsize+sizeof(GamestateHeader)];226 u nsigned char*d2 = GAMESTATE_START(rdata);227 uLongf length2 = HEADER-> normsize;237 uint8_t *rdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)]; 238 uint8_t *d2 = GAMESTATE_START(rdata); 239 uLongf length2 = HEADER->datasize; 228 240 uncompress(d2, &length2, dest, buffer); 229 for(unsigned int i=0; i<HEADER-> normsize; i++){241 for(unsigned int i=0; i<HEADER->datasize; i++){ 230 242 assert(*(source+i)==*(d2+i)); 231 243 } … … 235 247 //copy and modify header 236 248 #ifndef NDEBUG 237 HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER-> normsize);249 HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->datasize); 238 250 #endif 239 251 *GAMESTATE_HEADER(ndata) = *HEADER; … … 245 257 HEADER->compressed = true; 246 258 assert(HEADER->compressed); 247 COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl;259 COUT(3) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl; 248 260 return true; 249 261 } … … 252 264 assert(HEADER); 253 265 assert(HEADER->compressed); 254 COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", normsize: " << HEADER->normsize << ", compsize: " << HEADER->compsize << std::endl;255 unsigned int normsize = HEADER->normsize;266 COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl; 267 unsigned int datasize = HEADER->datasize; 256 268 unsigned int compsize = HEADER->compsize; 257 269 unsigned int bufsize; 258 assert(compsize<= normsize);259 bufsize = normsize;270 assert(compsize<=datasize); 271 bufsize = datasize; 260 272 assert(bufsize!=0); 261 u nsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)];262 u nsigned char*dest = ndata + sizeof(GamestateHeader);263 u nsigned char*source = data_ + sizeof(GamestateHeader);273 uint8_t *ndata = new uint8_t[bufsize + sizeof(GamestateHeader)]; 274 uint8_t *dest = ndata + sizeof(GamestateHeader); 275 uint8_t *source = data_ + sizeof(GamestateHeader); 264 276 int retval; 265 277 uLongf length=bufsize; … … 272 284 } 273 285 #ifndef NDEBUG 274 assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER-> normsize));286 assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->datasize)); 275 287 #endif 276 288 … … 282 294 data_ = ndata; 283 295 HEADER->compressed = false; 284 assert(HEADER-> normsize==normsize);296 assert(HEADER->datasize==datasize); 285 297 assert(HEADER->compsize==compsize); 286 298 return true; … … 293 305 assert(!HEADER->diffed); 294 306 //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; 295 u nsigned char*basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);307 uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); 296 308 unsigned int of=0; // pointers offset 297 309 unsigned int dest_length=0; 298 dest_length=HEADER-> normsize;310 dest_length=HEADER->datasize; 299 311 if(dest_length==0) 300 312 return NULL; 301 u nsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)];302 u nsigned char*dest = ndata + sizeof(GamestateHeader);303 while(of < GAMESTATE_HEADER(base->data_)-> normsize && of < HEADER->normsize){313 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)]; 314 uint8_t *dest = ndata + sizeof(GamestateHeader); 315 while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){ 304 316 *(dest+of)=*(basep+of)^*(gs+of); // do the xor 305 317 ++of; 306 318 } 307 if(GAMESTATE_HEADER(base->data_)-> normsize!=HEADER->normsize){308 u nsigned charn=0;309 if(GAMESTATE_HEADER(base->data_)-> normsize < HEADER->normsize){319 if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){ 320 uint8_t n=0; 321 if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){ 310 322 while(of<dest_length){ 311 323 *(dest+of)=n^*(gs+of); … … 324 336 } 325 337 338 Gamestate* Gamestate::doSelection(unsigned int clientID){ 339 assert(data_); 340 std::map<unsigned int, Synchronisable *>::iterator it; 341 342 // allocate memory for new data 343 uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)]; 344 // create a gamestate out of it 345 Gamestate *gs = new Gamestate(gdata); 346 uint8_t *newdata = gdata + sizeof(GamestateHeader); 347 uint8_t *origdata = GAMESTATE_START(data_); 348 349 //copy the GamestateHeader 350 *(GamestateHeader*)gdata = *HEADER; 351 352 synchronisableHeader *oldobjectheader, *newobjectheader; 353 unsigned int objectOffset; 354 355 //copy in the zeros 356 for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ 357 oldobjectheader = (synchronisableHeader*)origdata; 358 newobjectheader = (synchronisableHeader*)newdata; 359 unsigned int objectsize = oldobjectheader->size; 360 assert(it->second->objectID==oldobjectheader->objectID); 361 *newobjectheader = *oldobjectheader; 362 objectOffset=sizeof(uint8_t)+sizeof(bool); //skip the size and the availableDate variables in the objectheader 363 if(it->second->doSelection(HEADER->id)){ 364 newobjectheader->dataAvailable=true; //TODO: probably not neccessary 365 while(objectOffset<objectsize){ 366 *(newdata + objectOffset)=*(origdata + objectOffset); // copy the data 367 objectOffset++; 368 } 369 }else{ 370 newobjectheader->dataAvailable=false; 371 while(objectOffset<objectsize){ 372 *(newdata+objectOffset)=0; // set to 0 373 objectOffset++; 374 } 375 assert(objectOffset==objectsize); 376 } 377 newdata += objectsize; 378 origdata += objectsize; 379 } 380 return gs; 381 } 382 383 384 Gamestate* Gamestate::intelligentDiff(Gamestate *base, unsigned int clientID){ 385 // asserts 386 assert(data_); 387 assert(base->data_); 388 assert(!GAMESTATE_HEADER(base->data_)->diffed); 389 assert(!GAMESTATE_HEADER(base->data_)->compressed); 390 assert(!HEADER->compressed); 391 assert(!HEADER->diffed); 392 393 //preparations 394 std::map<unsigned int, Synchronisable *>::iterator it; 395 uint8_t *origdata, *basedata, *destdata, *ndata; 396 unsigned int objectOffset, streamOffset=0; //data offset 397 unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; 398 synchronisableHeader *origheader; 399 synchronisableHeader *destheader; 400 401 origdata = GAMESTATE_START(this->data_); 402 basedata = GAMESTATE_START(base->data_); 403 ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)]; 404 destdata = ndata + sizeof(GamestateHeader); 405 406 // do the diff 407 for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ 408 assert(streamOffset<HEADER->datasize); 409 bool sendData = it->second->doSelection(HEADER->id); 410 origheader = (synchronisableHeader *)(origdata+streamOffset); 411 destheader = (synchronisableHeader *)(destdata+streamOffset); 412 413 //copy and partially diff the object header 414 assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool)); 415 *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff) 416 *(bool*)(destdata+sizeof(uint32_t)) = sendData; 417 if(sendData){ 418 *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+sizeof(uint32_t)+sizeof(bool)); //objectid (diff it) 419 *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+2*sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+2*sizeof(uint32_t)+sizeof(bool)); //classid (diff it) 420 }else{ 421 *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0; 422 *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0; 423 } 424 objectOffset=sizeof(synchronisableHeader); 425 streamOffset+=sizeof(synchronisableHeader); 426 427 //now handle the object data or fill with zeros 428 while(objectOffset<origheader->size ){ 429 430 if(sendData && streamOffset<minsize) 431 *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor 432 else if(sendData) 433 *(destdata+objectOffset)=((uint8_t)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short) 434 else 435 *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered 436 437 objectOffset++; 438 streamOffset++; 439 } 440 destdata+=objectOffset; 441 origdata+=objectOffset; 442 basedata+=objectOffset; 443 } 444 445 //copy over the gamestate header and set the diffed flag 446 *(GamestateHeader *)ndata = *HEADER; //copy over the header 447 Gamestate *gs = new Gamestate(ndata); 448 GAMESTATE_HEADER(ndata)->diffed=true; 449 return gs; 450 } 451 452 Gamestate* Gamestate::intelligentUnDiff(Gamestate *base){ 453 // asserts 454 assert(data_); 455 assert(base->data_); 456 assert(!GAMESTATE_HEADER(base->data_)->diffed); 457 assert(!GAMESTATE_HEADER(base->data_)->compressed); 458 assert(!HEADER->compressed); 459 assert(HEADER->diffed); 460 461 //preparations 462 std::map<unsigned int, Synchronisable *>::iterator it; 463 uint8_t *origdata, *basedata, *destdata, *ndata; 464 unsigned int objectOffset, streamOffset=0; //data offset 465 unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; 466 synchronisableHeader *origheader; 467 synchronisableHeader *destheader; 468 469 origdata = GAMESTATE_START(this->data_); 470 basedata = GAMESTATE_START(base->data_); 471 ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)]; 472 destdata = ndata + sizeof(GamestateHeader); 473 474 // do the undiff 475 for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ 476 assert(streamOffset<HEADER->datasize); 477 origheader = (synchronisableHeader *)(origdata+streamOffset); 478 destheader = (synchronisableHeader *)(destdata+streamOffset); 479 bool sendData; 480 481 //copy and partially diff the object header 482 assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool)); 483 *(unsigned int*)destdata = *(unsigned int*)origdata; //size (do not diff) 484 *(bool*)(destdata+sizeof(unsigned int)) = *(bool*)(origdata+sizeof(unsigned int)); 485 sendData = *(bool*)(origdata+sizeof(unsigned int)); 486 if(sendData){ 487 *(unsigned int*)(destdata+sizeof(unsigned int)+sizeof(bool)) = *(unsigned int*)(basedata+sizeof(unsigned int)+sizeof(bool)) ^ *(unsigned int*)(origdata+sizeof(unsigned int)+sizeof(bool)); //objectid (diff it) 488 *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = *(unsigned int*)(basedata+2*sizeof(unsigned int)+sizeof(bool)) ^ *(unsigned int*)(origdata+2*sizeof(unsigned int)+sizeof(bool)); //classid (diff it) 489 }else{ 490 *(unsigned int*)(destdata+sizeof(unsigned int)+sizeof(bool)) = 0; 491 *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = 0; 492 } 493 objectOffset=sizeof(synchronisableHeader); 494 streamOffset+=sizeof(synchronisableHeader); 495 496 //now handle the object data or fill with zeros 497 while(objectOffset<origheader->size ){ 498 499 if(sendData && streamOffset<minsize) 500 *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor 501 else if(sendData) 502 *(destdata+objectOffset)=((unsigned char)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short) 503 else 504 *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered 505 506 objectOffset++; 507 streamOffset++; 508 } 509 destdata+=objectOffset; 510 origdata+=objectOffset; 511 basedata+=objectOffset; 512 } 513 514 //copy over the gamestate header and set the diffed flag 515 *(GamestateHeader *)ndata = *HEADER; //copy over the header 516 Gamestate *gs = new Gamestate(ndata); 517 GAMESTATE_HEADER(ndata)->diffed=false; 518 return gs; 519 } 520 326 521 Gamestate *Gamestate::undiff(Gamestate *base) 327 522 { … … 330 525 assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed); 331 526 //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; 332 u nsigned char*basep = GAMESTATE_START(base->data_);333 u nsigned char*gs = GAMESTATE_START(this->data_);527 uint8_t *basep = GAMESTATE_START(base->data_); 528 uint8_t *gs = GAMESTATE_START(this->data_); 334 529 unsigned int of=0; // pointers offset 335 530 unsigned int dest_length=0; 336 dest_length=HEADER-> normsize;531 dest_length=HEADER->datasize; 337 532 if(dest_length==0) 338 533 return NULL; 339 u nsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)];340 u nsigned char*dest = ndata + sizeof(GamestateHeader);341 while(of < GAMESTATE_HEADER(base->data_)-> normsize && of < HEADER->normsize){534 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)]; 535 uint8_t *dest = ndata + sizeof(GamestateHeader); 536 while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){ 342 537 *(dest+of)=*(basep+of)^*(gs+of); // do the xor 343 538 ++of; 344 539 } 345 if(GAMESTATE_HEADER(base->data_)-> normsize!=HEADER->normsize){346 u nsigned charn=0;347 if(GAMESTATE_HEADER(base->data_)-> normsize < HEADER->normsize){540 if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){ 541 uint8_t n=0; 542 if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){ 348 543 while(of < dest_length){ 349 544 *(dest+of)=n^*(gs+of); … … 370 565 // get total size of gamestate 371 566 for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it) 372 size+=it->getSize 2(id, mode); // size of the actual data of the synchronisable567 size+=it->getSize(id, mode); // size of the actual data of the synchronisable 373 568 // size+=sizeof(GamestateHeader); 374 569 return size; -
code/trunk/src/network/packet/Gamestate.h
r1763 r1907 29 29 #include "Packet.h" 30 30 #include "network/Synchronisable.h" 31 #include <map> 31 32 #ifndef NDEBUG 32 33 #include "util/CRC32.h" 33 34 #endif 35 34 36 35 37 #ifndef NETWORK_PACKETGAMESTATE_H … … 42 44 struct GamestateHeader{ 43 45 ENUM::Type packetType; 44 int id; // id of the gamestate45 u nsigned int compsize;46 u nsigned int normsize;47 int base_id; // id of the base-gamestate diffed from48 bool diffed ; // wheter diffed or not49 bool complete ; // wheter it is a complete gamestate or only partial50 bool compressed ;46 int32_t id; // id of the gamestate 47 uint32_t compsize; 48 uint32_t datasize; 49 int32_t base_id; // id of the base-gamestate diffed from 50 bool diffed:1; // wheter diffed or not 51 bool complete:1; // wheter it is a complete gamestate or only partial 52 bool compressed:1; 51 53 #ifndef NDEBUG 52 54 uint32_t crc32; … … 55 57 56 58 /** 57 @author 59 @author Oliver Scheuss 58 60 */ 59 61 class Gamestate: public Packet{ 60 62 public: 61 63 Gamestate(); 62 Gamestate(unsigned char *data, int clientID); 64 Gamestate(uint8_t *data, unsigned int clientID); 65 Gamestate(uint8_t *data); 63 66 64 67 ~Gamestate(); … … 71 74 int getBaseID(); 72 75 Gamestate *diff(Gamestate *base); 76 Gamestate* intelligentDiff(Gamestate *base, unsigned int clientID); 73 77 Gamestate *undiff(Gamestate *base); 78 Gamestate* intelligentUnDiff(Gamestate *base); 79 Gamestate* doSelection(unsigned int clientID); 74 80 bool compressData(); 75 81 bool decompressData(); 76 82 77 83 // Packet functions 84 private: 78 85 virtual unsigned int getSize() const; 79 86 virtual bool process(); … … 83 90 unsigned int calcGamestateSize(unsigned int id, int mode=0x0); 84 91 void removeObject(orxonox::ObjectListIterator<Synchronisable> &it); 85 86 87 //Bytestream *bs_; 88 //GamestateHeader *header_; 92 std::map<unsigned int, Synchronisable*> dataMap_; 89 93 }; 90 94 -
code/trunk/src/network/packet/Packet.cc
r1763 r1907 42 42 #include "Gamestate.h" 43 43 #include "Welcome.h" 44 #include "DeleteObjects.h" 44 45 #include "network/Host.h" 45 46 #include "core/CoreIncludes.h" … … 66 67 } 67 68 68 Packet::Packet(u nsigned char *data,int clientID){69 Packet::Packet(uint8_t *data, unsigned int clientID){ 69 70 flags_ = PACKET_FLAG_DEFAULT; 70 71 packetDirection_ = ENUM::Incoming; … … 74 75 } 75 76 76 /*Packet::Packet(ENetPacket *packet, ENetPeer *peer){77 packetDirection_ = ENUM::Incoming;78 enetPacket_ = packet;79 clientID_ = ClientInformation::findClient(&peer->address)->getID();80 data_ = packet->data;81 }*/82 77 83 78 Packet::Packet(const Packet &p){ … … 87 82 clientID_ = p.clientID_; 88 83 if(p.data_){ 89 data_ = new u nsigned char[p.getSize()];84 data_ = new uint8_t[p.getSize()]; 90 85 memcpy(data_, p.data_, p.getSize()); 91 86 }else … … 125 120 case ENUM::Gamestate: 126 121 case ENUM::Welcome: 122 case ENUM::DeleteObjects: 127 123 break; 128 124 default: … … 138 134 139 135 Packet *Packet::createPacket(ENetPacket *packet, ENetPeer *peer){ 140 u nsigned char*data = packet->data;136 uint8_t *data = packet->data; 141 137 unsigned int clientID = ClientInformation::findClient(&peer->address)->getID(); 142 138 Packet *p; 143 COUT( 3) << "packet type: " << *(ENUM::Type *)&data[_PACKETID] << std::endl;139 COUT(5) << "packet type: " << *(ENUM::Type *)&data[_PACKETID] << std::endl; 144 140 switch( *(ENUM::Type *)(data + _PACKETID) ) 145 141 { 146 142 case ENUM::Acknowledgement: 147 COUT( 3) << "ack" << std::endl;143 COUT(4) << "ack" << std::endl; 148 144 p = new Acknowledgement( data, clientID ); 149 145 break; 150 146 case ENUM::Chat: 151 COUT( 3) << "chat" << std::endl;147 COUT(4) << "chat" << std::endl; 152 148 p = new Chat( data, clientID ); 153 149 break; 154 150 case ENUM::ClassID: 155 COUT( 3) << "classid" << std::endl;151 COUT(4) << "classid" << std::endl; 156 152 p = new ClassID( data, clientID ); 157 153 break; 158 154 case ENUM::Gamestate: 159 COUT( 3) << "gamestate" << std::endl;155 COUT(4) << "gamestate" << std::endl; 160 156 // TODO: remove brackets 161 157 p = new Gamestate( data, clientID ); 162 158 break; 163 159 case ENUM::Welcome: 164 COUT( 3) << "welcome" << std::endl;160 COUT(4) << "welcome" << std::endl; 165 161 p = new Welcome( data, clientID ); 162 break; 163 case ENUM::DeleteObjects: 164 COUT(4) << "deleteobjects" << std::endl; 165 p = new DeleteObjects( data, clientID ); 166 166 break; 167 167 default: -
code/trunk/src/network/packet/Packet.h
r1763 r1907 32 32 #include <enet/enet.h> 33 33 34 #include "util/Integers.h" 35 34 36 namespace network { 35 37 … … 47 49 ClassID, 48 50 Chat, 49 Welcome 51 Welcome, 52 DeleteObjects 50 53 }; 51 54 } … … 74 77 protected: 75 78 Packet(); 76 Packet(u nsigned char *data,int clientID);79 Packet(uint8_t *data, unsigned int clientID); 77 80 // Packet(ENetPacket *packet, ENetPeer *peer); 78 81 enet_uint32 flags_; 79 int clientID_;82 unsigned int clientID_; 80 83 ENUM::Direction packetDirection_; 81 u nsigned char*data_;84 uint8_t *data_; 82 85 private: 83 86 static std::map<ENetPacket *, Packet *> packetMap_; -
code/trunk/src/network/packet/Welcome.cc
r1763 r1907 42 42 #define _PACKETID 0 43 43 #define _CLIENTID _PACKETID + sizeof(ENUM::Type) 44 #define _SHIPID _CLIENTID + sizeof(u nsigned int)44 #define _SHIPID _CLIENTID + sizeof(uint32_t) 45 45 46 46 Welcome::Welcome( unsigned int clientID, unsigned int shipID ) … … 49 49 flags_ = flags_ | PACKET_FLAGS_CLASSID; 50 50 assert(getSize()); 51 data_=new u nsigned char[ getSize() ];51 data_=new uint8_t[ getSize() ]; 52 52 assert(data_); 53 53 *(packet::ENUM::Type *)(data_ + _PACKETID ) = packet::ENUM::Welcome; 54 *(u nsigned int *)&data_[ _CLIENTID ] = clientID;55 *(u nsigned int *)&data_[ _SHIPID ] = shipID;54 *(uint32_t *)&data_[ _CLIENTID ] = clientID; 55 *(uint32_t *)&data_[ _SHIPID ] = shipID; 56 56 } 57 57 58 Welcome::Welcome( u nsigned char *data,int clientID )58 Welcome::Welcome( uint8_t* data, unsigned int clientID ) 59 59 : Packet(data, clientID) 60 60 { … … 65 65 } 66 66 67 u nsigned char*Welcome::getData(){67 uint8_t *Welcome::getData(){ 68 68 return data_; 69 69 } 70 70 71 71 unsigned int Welcome::getSize() const{ 72 return sizeof(network::packet::ENUM::Type) + 2*sizeof(u nsigned int);72 return sizeof(network::packet::ENUM::Type) + 2*sizeof(uint32_t); 73 73 } 74 74 75 75 bool Welcome::process(){ 76 76 unsigned int shipID, clientID; 77 clientID = *(u nsigned int *)&data_[ _CLIENTID ];78 shipID = *(u nsigned int *)&data_[ _SHIPID ];77 clientID = *(uint32_t *)&data_[ _CLIENTID ]; 78 shipID = *(uint32_t *)&data_[ _SHIPID ]; 79 79 Host::setClientID(clientID); 80 80 Host::setShipID(shipID); -
code/trunk/src/network/packet/Welcome.h
r1763 r1907 41 41 public: 42 42 Welcome( unsigned int clientID, unsigned int shipID ); 43 Welcome( u nsigned char* data,int clientID );43 Welcome( uint8_t* data, unsigned int clientID ); 44 44 virtual ~Welcome(); 45 45 46 u nsigned char*getData();46 uint8_t *getData(); 47 47 inline unsigned int getSize() const; 48 48 bool process();
Note: See TracChangeset
for help on using the changeset viewer.