Changeset 1751 for code/trunk/src/network/packet
- Timestamp:
- Sep 9, 2008, 4:31:34 PM (16 years ago)
- Location:
- code/trunk/src/network/packet
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/packet/Acknowledgement.cc
r1735 r1751 45 45 data_=new unsigned char[ getSize() ]; 46 46 *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::Acknowledgement; 47 *(unsigned int *) &data_[ _ACKID ]= id;47 *(unsigned int *)(data_ + _ACKID ) = id; 48 48 clientID_=clientID; 49 49 } -
code/trunk/src/network/packet/Gamestate.cc
r1747 r1751 41 41 namespace packet { 42 42 43 44 #define GAMESTATE_START(data) data + sizeof(GamestateHeader) 43 #define GAMESTATE_START(data) (data + sizeof(GamestateHeader)) 45 44 #define GAMESTATE_HEADER(data) ((GamestateHeader *)data) 46 45 #define HEADER GAMESTATE_HEADER(data_) … … 63 62 { 64 63 int tempsize=0, currentsize=0; 65 assert(data_==0 /*&& bs_==0*/);66 int size = calcGamestateSize( mode);64 assert(data_==0); 65 int size = calcGamestateSize(id, mode); 67 66 68 67 COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl; 69 //retval->data = (unsigned char*)malloc(size);70 68 if(size==0) 71 69 return false; 72 70 data_ = new unsigned char[size + sizeof(GamestateHeader)]; 73 //bs_ = new Bytestream(data_+sizeof(GamestateHeader), size);74 71 if(!data_){ 75 72 COUT(2) << "GameStateManager: could not allocate memory" << std::endl; … … 82 79 orxonox::ObjectList<Synchronisable>::iterator it; 83 80 for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it){ 84 tempsize=it->getSize2( mode);81 tempsize=it->getSize2(id, mode); 85 82 86 83 if(currentsize+tempsize > size){ … … 90 87 int addsize=tempsize; 91 88 while(++temp) 92 addsize+=temp->getSize2( mode);89 addsize+=temp->getSize2(id, mode); 93 90 data_ = (unsigned char *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize); 94 91 if(!data_) … … 97 94 }// stop allocate additional memory 98 95 99 if(!it->getData 2(mem, mode))96 if(!it->getData(mem, id, mode)) 100 97 return false; // mem pointer gets automatically increased because of call by reference 101 98 // increase size counter by size of current synchronisable … … 121 118 bool Gamestate::spreadData(int mode) 122 119 { 123 assert(data_ && !HEADER->compressed && !HEADER->diffed); 120 assert(data_); 121 assert(!HEADER->compressed); 122 assert(!HEADER->diffed); 124 123 unsigned int size, objectID, classID; 125 124 unsigned char *mem=data_+sizeof(GamestateHeader); … … 142 141 //fabricate the new synchronisable 143 142 if(!Synchronisable::fabricate(mem, mode)) 144 /*return false*/;143 return false; 145 144 it=orxonox::ObjectList<Synchronisable>::end(); 146 145 } … … 175 174 } 176 175 176 bool Gamestate::operator==(packet::Gamestate gs){ 177 unsigned char *d1 = data_+sizeof(GamestateHeader); 178 unsigned char *d2 = gs.data_+sizeof(GamestateHeader); 179 assert(!isCompressed()); 180 assert(!gs.isCompressed()); 181 while(d1<data_+HEADER->normsize) 182 { 183 if(*d1!=*d2) 184 return false; 185 d1++; 186 d2++; 187 } 188 return true; 189 } 190 177 191 bool Gamestate::process() 178 192 { … … 183 197 { 184 198 assert(HEADER); 185 uLongf buffer = (uLongf)((HEADER->normsize + 12)*1.01)+1; 199 assert(!HEADER->compressed); 200 uLongf buffer = (uLongf)(((HEADER->normsize + 12)*1.01)+1); 186 201 if(buffer==0) 187 202 return false; … … 189 204 unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)]; 190 205 unsigned char *dest = GAMESTATE_START(ndata); 206 //unsigned char *dest = new unsigned char[buffer]; 207 unsigned char *source = GAMESTATE_START(data_); 191 208 int retval; 192 retval = compress( dest, &buffer, GAMESTATE_START(data_), (uLong)(HEADER->normsize) );209 retval = compress( dest, &buffer, source, (uLong)(HEADER->normsize) ); 193 210 switch ( retval ) { 194 211 case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; 195 case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; 196 return false; 197 case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; 198 return false; 199 case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; 200 return false; 201 } 212 case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false; 213 case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false; 214 case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false; 215 } 216 #ifndef NDEBUG 217 //decompress and compare the start and the decompressed data 218 unsigned char *rdata = new unsigned char[HEADER->normsize+sizeof(GamestateHeader)]; 219 unsigned char *d2 = GAMESTATE_START(rdata); 220 uLongf length2 = HEADER->normsize; 221 uncompress(d2, &length2, dest, buffer); 222 for(unsigned int i=0; i<HEADER->normsize; i++){ 223 assert(*(source+i)==*(d2+i)); 224 } 225 delete[] rdata; 226 #endif 202 227 203 228 //copy and modify header 204 HEADER->compsize = buffer; 205 HEADER->compressed = true; 229 #ifndef NDEBUG 230 HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->normsize); 231 #endif 206 232 *GAMESTATE_HEADER(ndata) = *HEADER; 207 233 //delete old data … … 209 235 //save new data 210 236 data_ = ndata; 237 HEADER->compsize = buffer; 238 HEADER->compressed = true; 211 239 assert(HEADER->compressed); 212 240 COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl; … … 215 243 bool Gamestate::decompressData() 216 244 { 245 assert(HEADER); 217 246 assert(HEADER->compressed); 218 //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl; 219 int normsize = HEADER->normsize; 220 int compsize = HEADER->compsize; 221 int bufsize; 222 if(normsize < compsize) 223 bufsize = compsize; 224 else 225 bufsize = normsize; 226 if(bufsize==0) 227 return false; 247 COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", normsize: " << HEADER->normsize << ", compsize: " << HEADER->compsize << std::endl; 248 unsigned int normsize = HEADER->normsize; 249 unsigned int compsize = HEADER->compsize; 250 unsigned int bufsize; 251 assert(compsize<=normsize); 252 bufsize = normsize; 253 assert(bufsize!=0); 228 254 unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)]; 229 255 unsigned char *dest = ndata + sizeof(GamestateHeader); 256 unsigned char *source = data_ + sizeof(GamestateHeader); 230 257 int retval; 231 uLongf length= normsize;232 retval = uncompress( dest, &length, data_+sizeof(GamestateHeader), (uLong)compsize );258 uLongf length=bufsize; 259 retval = uncompress( dest, &length, source, (uLong)compsize ); 233 260 switch ( retval ) { 234 261 case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break; … … 237 264 case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false; 238 265 } 239 240 HEADER->compressed = false; 266 #ifndef NDEBUG 267 assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->normsize)); 268 #endif 269 241 270 //copy over the header 242 271 *GAMESTATE_HEADER(ndata) = *HEADER; 243 272 //delete old (compressed data) 244 273 delete[] data_; 245 //set new pointers and create bytestream274 //set new pointers 246 275 data_ = ndata; 247 //bs_ = new Bytestream(getGs(), GAMESTATE_HEADER->normsize); 248 276 HEADER->compressed = false; 277 assert(HEADER->normsize==normsize); 278 assert(HEADER->compsize==compsize); 249 279 return true; 250 280 } … … 252 282 Gamestate *Gamestate::diff(Gamestate *base) 253 283 { 284 assert(HEADER); 285 assert(!HEADER->compressed); 286 assert(!HEADER->diffed); 254 287 //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; 255 288 unsigned char *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); … … 277 310 *GAMESTATE_HEADER(ndata) = *HEADER; 278 311 GAMESTATE_HEADER(ndata)->diffed = true; 279 Gamestate *g = new Gamestate(ndata, 0); 312 GAMESTATE_HEADER(ndata)->base_id = base->getID(); 313 Gamestate *g = new Gamestate(ndata, getClientID()); 314 g->flags_=flags_; 315 g->packetDirection_ = packetDirection_; 280 316 return g; 281 317 } … … 283 319 Gamestate *Gamestate::undiff(Gamestate *base) 284 320 { 285 assert(this && base); 321 assert(this && base);assert(HEADER); 322 assert(HEADER->diffed); 286 323 assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed); 287 324 //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; … … 310 347 *GAMESTATE_HEADER(ndata) = *HEADER; 311 348 GAMESTATE_HEADER(ndata)->diffed = false; 312 Gamestate *g = new Gamestate(ndata, 0); 349 Gamestate *g = new Gamestate(ndata, getClientID()); 350 g->flags_=flags_; 351 g->packetDirection_ = packetDirection_; 352 assert(!g->isDiffed()); 353 assert(!g->isCompressed()); 313 354 return g; 314 355 } 315 356 316 357 317 unsigned int Gamestate::calcGamestateSize( int mode)358 unsigned int Gamestate::calcGamestateSize(unsigned int id, int mode) 318 359 { 319 360 int size=0; … … 322 363 // get total size of gamestate 323 364 for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it) 324 size+=it->getSize2( mode); // size of the actual data of the synchronisable365 size+=it->getSize2(id, mode); // size of the actual data of the synchronisable 325 366 // size+=sizeof(GamestateHeader); 326 367 return size; … … 342 383 } 343 384 385 bool Gamestate::isCompressed(){ 386 return HEADER->compressed; 387 } 388 344 389 int Gamestate::getBaseID(){ 345 390 return HEADER->base_id; -
code/trunk/src/network/packet/Gamestate.h
r1747 r1751 29 29 #include "Packet.h" 30 30 #include "network/Synchronisable.h" 31 //#include "util/Bytestream.h" 31 #ifndef NDEBUG 32 #include "util/CRC32.h" 33 #endif 32 34 #include "core/CoreIncludes.h" 33 35 … … 48 50 bool complete; // wheter it is a complete gamestate or only partial 49 51 bool compressed; 52 #ifndef NDEBUG 53 uint32_t crc32; 54 #endif 50 55 }; 51 56 … … 64 69 int getID(); 65 70 bool isDiffed(); 71 bool isCompressed(); 66 72 int getBaseID(); 67 73 Gamestate *diff(Gamestate *base); … … 74 80 virtual bool process(); 75 81 76 82 bool operator ==(packet::Gamestate gs); 77 83 private: 78 unsigned int calcGamestateSize( int mode=0x0);79 void removeObject(orxonox::ObjectList Iterator<Synchronisable>&it);84 unsigned int calcGamestateSize(unsigned int id, int mode=0x0); 85 void removeObject(orxonox::ObjectList<Synchronisable>::iterator &it); 80 86 81 87 -
code/trunk/src/network/packet/Packet.cc
r1735 r1751 92 92 93 93 Packet::~Packet(){ 94 if(enetPacket_) 94 if(enetPacket_){ 95 assert(enetPacket_->freeCallback==0); 95 96 enet_packet_destroy(enetPacket_); 97 } 96 98 if(data_) 97 99 delete[] data_; … … 109 111 } 110 112 enetPacket_ = enet_packet_create(getData(), getSize(), getFlags()); 111 //enetPacket_->freeCallback = &Packet::deletePacket;112 enetPacket_->freeCallback = &blub;113 enetPacket_->freeCallback = &Packet::deletePacket; 114 // enetPacket_->freeCallback = &blub; 113 115 packetMap_[enetPacket_] = this; 114 116 } 115 /*switch( *(ENUM::Type *)(data_ + _PACKETID) ) 117 #ifndef NDEBUG 118 switch( *(ENUM::Type *)(data_ + _PACKETID) ) 116 119 { 117 120 case ENUM::Acknowledgement: … … 120 123 case ENUM::Gamestate: 121 124 case ENUM::Welcome: 122 COUT(3) << "welcome" << std::endl; 123 p = new Welcome( data, clientID ); 125 break; 124 126 default: 125 assert(0); // TODO: repair this127 assert(0); //there was some error, if this is the case 126 128 break; 127 }*/ 128 network::Host::addPacket( enetPacket_, clientID_); 129 } 130 #endif 131 ENetPacket *temp = enetPacket_; 129 132 enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet 133 network::Host::addPacket( temp, clientID_); 130 134 return true; 131 135 } … … 168 172 void Packet::deletePacket(ENetPacket *packet){ 169 173 assert(packetMap_[packet]); 174 assert(packetMap_[packet]->enetPacket_==0); 170 175 delete packetMap_[packet]; 171 176 } -
code/trunk/src/network/packet/Packet.h
r1735 r1751 78 78 enet_uint32 flags_; 79 79 int clientID_; 80 ENUM::Direction packetDirection_; 80 81 unsigned char *data_; 81 82 private: 82 83 static std::map<ENetPacket *, Packet *> packetMap_; 83 84 ENetPacket *enetPacket_; 84 ENUM::Direction packetDirection_;85 85 }; 86 86 -
code/trunk/src/network/packet/Welcome.cc
r1735 r1751 32 32 #include "Welcome.h" 33 33 #include "network/Host.h" 34 #include "network/Synchronisable.h" 34 35 #include "core/CoreIncludes.h" 35 36 #include <assert.h> … … 79 80 Host::setShipID(shipID); 80 81 COUT(3) << "Welcome set clientId: " << clientID << " shipID: " << shipID << std::endl; 82 Synchronisable::setClient(true); 81 83 delete this; 82 84 return true;
Note: See TracChangeset
for help on using the changeset viewer.