Changeset 2070 for code/branches/objecthierarchy/src/network
- Timestamp:
- Oct 31, 2008, 12:05:12 AM (16 years ago)
- Location:
- code/branches/objecthierarchy/src/network
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/objecthierarchy/src/network/Client.cc
r1953 r2070 155 155 COUT(5) << "tick packet size " << event->packet->dataLength << std::endl; 156 156 packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer); 157 // note: packet commits suicide here except for the GameState. That is then deleted by a GamestateHandler 157 158 bool b = packet->process(); 158 159 assert(b); -
code/branches/objecthierarchy/src/network/ClientConnection.cc
r1953 r2070 72 72 bool ClientConnection::waitEstablished(int milisec) { 73 73 for(int i=0; i<=milisec && !established; i++) 74 usleep(1000);74 msleep(1); 75 75 76 76 return established; … … 184 184 case ENET_EVENT_TYPE_NONE: 185 185 //receiverThread_->yield(); 186 usleep(1000);186 msleep(1); 187 187 break; 188 188 } -
code/branches/objecthierarchy/src/network/ConnectionManager.cc
r1953 r2070 227 227 case ENET_EVENT_TYPE_NONE: 228 228 //receiverThread_->yield(); 229 usleep(1000);229 msleep(1); 230 230 break; 231 231 } -
code/branches/objecthierarchy/src/network/packet/Gamestate.cc
r2062 r2070 286 286 //copy over the header 287 287 *GAMESTATE_HEADER(ndata) = *HEADER; 288 //delete old (compressed data) 289 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 290 300 //set new pointers 291 301 data_ = ndata; -
code/branches/objecthierarchy/src/network/packet/Packet.cc
r1960 r2070 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 } … … 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 } … … 170 194 break; 171 195 } 196 197 // Data was created by ENet 198 p->bDataENetAllocated_ = true; 199 172 200 return p; 173 201 } 174 202 175 void Packet::deletePacket(ENetPacket *packet){ 176 assert(packetMap_[packet]); 177 assert(packetMap_[packet]->enetPacket_==0); 178 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); 179 216 } 180 217 -
code/branches/objecthierarchy/src/network/packet/Packet.h
r1916 r2070 84 84 unsigned int clientID_; 85 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_ */ 86 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_; 87 93 private: 88 94 static std::map<ENetPacket *, Packet *> packetMap_;
Note: See TracChangeset
for help on using the changeset viewer.