Changeset 2087 for code/trunk/src/network/packet
- Timestamp:
- Nov 1, 2008, 7:04:09 PM (16 years ago)
- Location:
- code/trunk
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/network/packet/Acknowledgement.h
r1907 r2087 29 29 #define NETWORKACKNOLEDGEMENT_H 30 30 31 #include "../NetworkPrereqs.h" 31 32 #include "Packet.h" 32 33 … … 35 36 namespace packet { 36 37 /** 37 @author 38 @author 38 39 */ 39 class Acknowledgement : public Packet40 class _NetworkExport Acknowledgement : public Packet 40 41 { 41 42 public: … … 43 44 Acknowledgement( uint8_t* data, unsigned int clientID ); 44 45 ~Acknowledgement(); 45 46 46 47 inline unsigned int getSize() const; 47 48 bool process(); 48 49 49 50 unsigned int getAckID(); 50 51 private: -
code/trunk/src/network/packet/Chat.h
r1907 r2087 2 2 #ifndef NETWORKCHAT_H 3 3 #define NETWORKCHAT_H 4 5 #include "../NetworkPrereqs.h" 4 6 5 7 #include <string> … … 11 13 namespace packet { 12 14 /** 13 @author 15 @author 14 16 */ 15 class Chat : public Packet17 class _NetworkExport Chat : public Packet 16 18 { 17 19 public: … … 19 21 Chat( uint8_t* data, unsigned int clientID ); 20 22 ~Chat(); 21 23 22 24 inline unsigned int getSize() const; 23 25 bool process(); 24 26 25 27 unsigned int getMessageLength(){ return messageLength_; }; 26 28 unsigned char *getMessage(); -
code/trunk/src/network/packet/ClassID.h
r1907 r2087 29 29 #define NETWORKCLASSID_H 30 30 31 #include "../NetworkPrereqs.h" 32 31 33 #include <string> 32 34 … … 37 39 38 40 /** 39 @author 41 @author 40 42 */ 41 class ClassID : public Packet43 class _NetworkExport ClassID : public Packet 42 44 { 43 45 public: … … 45 47 ClassID( uint8_t* data, unsigned int clientID ); 46 48 ~ClassID(); 47 49 48 50 inline unsigned int getSize() const; 49 51 bool process(); 50 52 51 53 unsigned int getClassID(); 52 54 unsigned int getClassNameLength(){ return classNameLength_; } -
code/trunk/src/network/packet/DeleteObjects.h
r1907 r2087 29 29 #define NETWORKPACKETDELETEOBJECTS_H 30 30 31 #include "../NetworkPrereqs.h" 32 31 33 #include "Packet.h" 32 34 … … 35 37 namespace packet { 36 38 /** 37 @author 39 @author 38 40 */ 39 class DeleteObjects : public Packet41 class _NetworkExport DeleteObjects : public Packet 40 42 { 41 43 public: … … 43 45 DeleteObjects( uint8_t* data, unsigned int clientID ); 44 46 ~DeleteObjects(); 45 47 46 48 bool fetchIDs(); 47 49 48 50 inline unsigned int getSize() const; 49 51 bool process(); 50 52 51 53 private: 52 54 }; -
code/trunk/src/network/packet/Gamestate.cc
r1907 r2087 46 46 #define HEADER GAMESTATE_HEADER(data_) 47 47 48 48 49 49 #define PACKET_FLAG_GAMESTATE ENET_PACKET_FLAG_RELIABLE 50 50 51 51 Gamestate::Gamestate() 52 52 { … … 85 85 return false; 86 86 } 87 87 88 88 #ifndef NDEBUG 89 89 std::list<Synchronisable*> slist; … … 115 115 slist.push_back(*it); 116 116 #endif 117 117 118 118 //if(it->doSelection(id)) 119 119 dataMap_[mem-data_]=(*it); // save the mem location of the synchronisable data … … 157 157 if(!s) 158 158 { 159 s = Synchronisable::fabricate(mem, mode); 160 assert(s); 161 // if(!s) 162 // return false; 159 Synchronisable::fabricate(mem, mode); 163 160 } 164 161 else … … 257 254 HEADER->compressed = true; 258 255 assert(HEADER->compressed); 259 COUT( 3) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl;256 COUT(4) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl; 260 257 return true; 261 258 } … … 264 261 assert(HEADER); 265 262 assert(HEADER->compressed); 266 COUT( 3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl;263 COUT(4) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl; 267 264 unsigned int datasize = HEADER->datasize; 268 265 unsigned int compsize = HEADER->compsize; 269 266 unsigned int bufsize; 270 assert(compsize<=datasize);267 // assert(compsize<=datasize); 271 268 bufsize = datasize; 272 269 assert(bufsize!=0); … … 289 286 //copy over the header 290 287 *GAMESTATE_HEADER(ndata) = *HEADER; 291 //delete old (compressed data) 292 delete[] data_; 288 289 if (this->bDataENetAllocated_){ 290 // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will 291 // deallocated it anyway. So data and packet stay together. 292 this->bDataENetAllocated_ = false; 293 } 294 else{ 295 // We allocated the memory in the first place (unlikely). So we destroy the old data 296 // and overwrite it with the new decompressed data. 297 delete[] this->data_; 298 } 299 293 300 //set new pointers 294 301 data_ = ndata; … … 339 346 assert(data_); 340 347 std::map<unsigned int, Synchronisable *>::iterator it; 341 348 342 349 // allocate memory for new data 343 350 uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)]; … … 346 353 uint8_t *newdata = gdata + sizeof(GamestateHeader); 347 354 uint8_t *origdata = GAMESTATE_START(data_); 348 355 349 356 //copy the GamestateHeader 350 357 *(GamestateHeader*)gdata = *HEADER; 351 358 352 359 synchronisableHeader *oldobjectheader, *newobjectheader; 353 360 unsigned int objectOffset; 354 361 355 362 //copy in the zeros 356 363 for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ … … 360 367 assert(it->second->objectID==oldobjectheader->objectID); 361 368 *newobjectheader = *oldobjectheader; 362 objectOffset=sizeof(uint8_t)+sizeof(bool); //skip the size and the availableDat evariables in the objectheader369 objectOffset=sizeof(uint8_t)+sizeof(bool); //skip the size and the availableData variables in the objectheader 363 370 if(it->second->doSelection(HEADER->id)){ 364 371 newobjectheader->dataAvailable=true; //TODO: probably not neccessary … … 390 397 assert(!HEADER->compressed); 391 398 assert(!HEADER->diffed); 392 399 393 400 //preparations 394 401 std::map<unsigned int, Synchronisable *>::iterator it; 395 402 uint8_t *origdata, *basedata, *destdata, *ndata; 396 403 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; 404 unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; 398 405 synchronisableHeader *origheader; 399 406 synchronisableHeader *destheader; 400 407 401 408 origdata = GAMESTATE_START(this->data_); 402 409 basedata = GAMESTATE_START(base->data_); 403 410 ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)]; 404 411 destdata = ndata + sizeof(GamestateHeader); 405 412 406 413 // do the diff 407 414 for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ … … 410 417 origheader = (synchronisableHeader *)(origdata+streamOffset); 411 418 destheader = (synchronisableHeader *)(destdata+streamOffset); 412 419 413 420 //copy and partially diff the object header 414 421 assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool)); … … 420 427 }else{ 421 428 *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0; 422 *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0; 429 *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0; 423 430 } 424 431 objectOffset=sizeof(synchronisableHeader); 425 432 streamOffset+=sizeof(synchronisableHeader); 426 433 427 434 //now handle the object data or fill with zeros 428 435 while(objectOffset<origheader->size ){ 429 436 430 437 if(sendData && streamOffset<minsize) 431 438 *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor … … 434 441 else 435 442 *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered 436 443 437 444 objectOffset++; 438 445 streamOffset++; … … 442 449 basedata+=objectOffset; 443 450 } 444 451 445 452 //copy over the gamestate header and set the diffed flag 446 453 *(GamestateHeader *)ndata = *HEADER; //copy over the header … … 458 465 assert(!HEADER->compressed); 459 466 assert(HEADER->diffed); 460 467 461 468 //preparations 462 469 std::map<unsigned int, Synchronisable *>::iterator it; 463 470 uint8_t *origdata, *basedata, *destdata, *ndata; 464 471 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; 472 unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; 466 473 synchronisableHeader *origheader; 467 474 synchronisableHeader *destheader; 468 475 469 476 origdata = GAMESTATE_START(this->data_); 470 477 basedata = GAMESTATE_START(base->data_); 471 478 ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)]; 472 479 destdata = ndata + sizeof(GamestateHeader); 473 480 474 481 // do the undiff 475 482 for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ … … 478 485 destheader = (synchronisableHeader *)(destdata+streamOffset); 479 486 bool sendData; 480 487 481 488 //copy and partially diff the object header 482 489 assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool)); … … 489 496 }else{ 490 497 *(unsigned int*)(destdata+sizeof(unsigned int)+sizeof(bool)) = 0; 491 *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = 0; 498 *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = 0; 492 499 } 493 500 objectOffset=sizeof(synchronisableHeader); 494 501 streamOffset+=sizeof(synchronisableHeader); 495 502 496 503 //now handle the object data or fill with zeros 497 504 while(objectOffset<origheader->size ){ 498 505 499 506 if(sendData && streamOffset<minsize) 500 507 *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor … … 503 510 else 504 511 *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered 505 512 506 513 objectOffset++; 507 514 streamOffset++; … … 511 518 basedata+=objectOffset; 512 519 } 513 520 514 521 //copy over the gamestate header and set the diffed flag 515 522 *(GamestateHeader *)ndata = *HEADER; //copy over the header -
code/trunk/src/network/packet/Gamestate.h
r1907 r2087 27 27 */ 28 28 29 30 #ifndef NETWORK_PACKETGAMESTATE_H 31 #define NETWORK_PACKETGAMESTATE_H 32 33 #include "../NetworkPrereqs.h" 34 29 35 #include "Packet.h" 30 36 #include "network/Synchronisable.h" … … 34 40 #endif 35 41 36 37 #ifndef NETWORK_PACKETGAMESTATE_H38 #define NETWORK_PACKETGAMESTATE_H39 40 42 namespace network { 41 43 42 44 namespace packet { 43 45 44 struct GamestateHeader{46 struct _NetworkExport GamestateHeader{ 45 47 ENUM::Type packetType; 46 48 int32_t id; // id of the gamestate … … 59 61 @author Oliver Scheuss 60 62 */ 61 class Gamestate: public Packet{63 class _NetworkExport Gamestate: public Packet{ 62 64 public: 63 65 Gamestate(); -
code/trunk/src/network/packet/Packet.cc
r1907 r2087 49 49 50 50 namespace packet{ 51 51 52 52 #define PACKET_FLAG_DEFAULT ENET_PACKET_FLAG_NO_ALLOCATE 53 53 #define _PACKETID 0 54 54 55 55 std::map<ENetPacket *, Packet *> Packet::packetMap_; 56 56 57 57 Packet::Packet(){ 58 58 flags_ = PACKET_FLAG_DEFAULT; … … 61 61 data_=0; 62 62 enetPacket_=0; 63 bDataENetAllocated_ = false; 63 64 } 64 65 … … 73 74 data_=data; 74 75 enetPacket_=0; 76 bDataENetAllocated_ = false; 75 77 } 76 78 … … 86 88 }else 87 89 data_=0; 88 } 89 90 bDataENetAllocated_ = p.bDataENetAllocated_; 91 } 92 93 /** 94 @brief 95 Destroys a packet completely. 96 97 That also means destroying the ENetPacket if one exists. There 98 */ 90 99 Packet::~Packet(){ 91 if(enetPacket_){ 92 assert(enetPacket_->freeCallback==0); 100 // Deallocate data_ memory if necessary. 101 if (this->bDataENetAllocated_){ 102 // In this case ENet allocated data_ and will destroy it. 103 } 104 else if (this->data_) { 105 // This destructor was probably called as a consequence to ENet executing our callback. 106 // It simply serves us to be able to deallocate the packet content (data_) ourselves since 107 // we have created it in the first place. 108 delete[] this->data_; 109 } 110 111 // Destroy the ENetPacket if necessary. 112 // Note: For the case ENet used the callback to destroy the packet, we have already set 113 // enetPacket_ to NULL to avoid destroying it again. 114 if (this->enetPacket_){ 115 // enetPacket_->data gets destroyed too by ENet if it was allocated by it. 93 116 enet_packet_destroy(enetPacket_); 94 117 } 95 if(data_)96 delete[] data_;97 118 } 98 119 … … 107 128 return false; 108 129 } 130 // We deliver ENet the data address so that it doesn't memcpy everything again. 131 // --> We have to delete data_ ourselves! 109 132 enetPacket_ = enet_packet_create(getData(), getSize(), getFlags()); 110 133 enetPacket_->freeCallback = &Packet::deletePacket; 111 // enetPacket_->freeCallback = &blub; 134 // Add the packet to a global list so we can access it again once enet calls our 135 // deletePacket method. We can of course only give a one argument function to the ENet C library. 112 136 packetMap_[enetPacket_] = this; 113 137 } 114 #ifndef NDEBUG 138 #ifndef NDEBUG 115 139 switch( *(ENUM::Type *)(data_ + _PACKETID) ) 116 140 { … … 127 151 } 128 152 #endif 129 ENetPacket *temp = enetPacket_;130 enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet131 network::Host::addPacket( temp, clientID_);153 // ENetPacket *temp = enetPacket_; 154 // enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet 155 network::Host::addPacket( enetPacket_, clientID_); 132 156 return true; 133 157 } … … 135 159 Packet *Packet::createPacket(ENetPacket *packet, ENetPeer *peer){ 136 160 uint8_t *data = packet->data; 161 assert(ClientInformation::findClient(&peer->address)->getID() != (unsigned int)-2 || !Host::isServer()); 137 162 unsigned int clientID = ClientInformation::findClient(&peer->address)->getID(); 138 163 Packet *p; … … 169 194 break; 170 195 } 196 197 // Data was created by ENet 198 p->bDataENetAllocated_ = true; 199 171 200 return p; 172 201 } 173 202 174 void Packet::deletePacket(ENetPacket *packet){ 175 assert(packetMap_[packet]); 176 assert(packetMap_[packet]->enetPacket_==0); 177 delete packetMap_[packet]; 203 /** 204 @brief 205 ENet calls this method whenever it wants to destroy a packet that contains 206 data we allocated ourselves. 207 */ 208 void Packet::deletePacket(ENetPacket *enetPacket){ 209 // Get our Packet from a gloabal map with all Packets created in the send() method of Packet. 210 std::map<ENetPacket*, Packet*>::iterator it = packetMap_.find(enetPacket); 211 assert(it != packetMap_.end()); 212 // Make sure we don't delete it again in the destructor 213 it->second->enetPacket_ = 0; 214 delete it->second; 215 //packetMap_.erase(it); 216 COUT(4) << "PacketMap size: " << packetMap_.size() << std::endl; 178 217 } 179 218 -
code/trunk/src/network/packet/Packet.h
r1907 r2087 29 29 #define NETWORKPACKET_H 30 30 31 #include "../NetworkPrereqs.h" 32 31 33 #include <map> 32 34 #include <enet/enet.h> … … 37 39 38 40 namespace packet{ 39 41 40 42 namespace ENUM{ 41 43 enum Direction{ … … 53 55 }; 54 56 } 55 57 56 58 /** 57 59 @author Oliver Scheuss <scheusso [at] ee.ethz.ch> 58 60 */ 59 class Packet{61 class _NetworkExport Packet{ 60 62 public: 61 63 Packet(const Packet &p); … … 63 65 static Packet *createPacket(ENetPacket *packet, ENetPeer *peer); 64 66 static void deletePacket(ENetPacket *packet); 65 67 66 68 virtual unsigned char *getData(){ return data_; }; 67 69 virtual unsigned int getSize() const =0; … … 73 75 void setClientID( int id ) 74 76 { clientID_ = id; } 75 77 76 78 bool send(); 77 79 protected: … … 82 84 unsigned int clientID_; 83 85 ENUM::Direction packetDirection_; 86 /** Pointer to the data. Be careful when deleting it because it might 87 point to a location that was allocated by ENet. 88 See bDataENetAllocated_ */ 84 89 uint8_t *data_; 90 /** Tells whether data_ was allocated by ENet or ourselves. 91 data_ might no correlate with enetPacket_->data. */ 92 bool bDataENetAllocated_; 85 93 private: 86 94 static std::map<ENetPacket *, Packet *> packetMap_; -
code/trunk/src/network/packet/Welcome.h
r1907 r2087 29 29 #define NETWORKWELCOME_H 30 30 31 #include "../NetworkPrereqs.h" 32 31 33 #include "Packet.h" 32 34 … … 35 37 36 38 /** 37 @author 39 @author 38 40 */ 39 class Welcome : public Packet41 class _NetworkExport Welcome : public Packet 40 42 { 41 43 public: … … 43 45 Welcome( uint8_t* data, unsigned int clientID ); 44 46 virtual ~Welcome(); 45 47 46 48 uint8_t *getData(); 47 49 inline unsigned int getSize() const; 48 50 bool process(); 49 51 50 52 private: 51 53 };
Note: See TracChangeset
for help on using the changeset viewer.