Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 31, 2008, 12:05:12 AM (16 years ago)
Author:
rgrieder
Message:

Properly took care of network::packet::Packet destruction. It depends very much whether the the data was allocated by ENet or by ourselves.

Location:
code/branches/objecthierarchy/src/network
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchy/src/network/Client.cc

    r1953 r2070  
    155155      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
    156156      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
    157158      bool b = packet->process();
    158159      assert(b);
  • code/branches/objecthierarchy/src/network/ClientConnection.cc

    r1953 r2070  
    7272  bool ClientConnection::waitEstablished(int milisec) {
    7373    for(int i=0; i<=milisec && !established; i++)
    74       usleep(1000);
     74      msleep(1);
    7575
    7676    return established;
     
    184184      case ENET_EVENT_TYPE_NONE:
    185185        //receiverThread_->yield();
    186         usleep(1000);
     186        msleep(1);
    187187        break;
    188188      }
  • code/branches/objecthierarchy/src/network/ConnectionManager.cc

    r1953 r2070  
    227227        case ENET_EVENT_TYPE_NONE:
    228228          //receiverThread_->yield();
    229           usleep(1000);
     229          msleep(1);
    230230          break;
    231231      }
  • code/branches/objecthierarchy/src/network/packet/Gamestate.cc

    r2062 r2070  
    286286  //copy over the header
    287287  *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
    290300  //set new pointers
    291301  data_ = ndata;
  • code/branches/objecthierarchy/src/network/packet/Packet.cc

    r1960 r2070  
    6161  data_=0;
    6262  enetPacket_=0;
     63  bDataENetAllocated_ = false;
    6364}
    6465
     
    7374  data_=data;
    7475  enetPacket_=0;
     76  bDataENetAllocated_ = false;
    7577}
    7678
     
    8688  }else
    8789    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*/
    9099Packet::~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.
    93116    enet_packet_destroy(enetPacket_);
    94117  }
    95   if(data_)
    96     delete[] data_;
    97118}
    98119
     
    107128      return false;
    108129    }
     130    // We deliver ENet the data address so that it doesn't memcpy everything again.
     131    // --> We have to delete data_ ourselves!
    109132    enetPacket_ = enet_packet_create(getData(), getSize(), getFlags());
    110133    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.
    112136    packetMap_[enetPacket_] = this;
    113137  }
     
    127151  }
    128152#endif
    129   ENetPacket *temp = enetPacket_;
    130   enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet
    131   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_);
    132156  return true;
    133157}
     
    170194      break;
    171195  }
     196
     197  // Data was created by ENet
     198  p->bDataENetAllocated_ = true;
     199
    172200  return p;
    173201}
    174202
    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*/
     208void 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);
    179216}
    180217
  • code/branches/objecthierarchy/src/network/packet/Packet.h

    r1916 r2070  
    8484    unsigned int clientID_;
    8585    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_ */
    8689    uint8_t *data_;
     90    /** Tells whether data_ was allocated by ENet or ourselves.
     91        data_ might no correlate with enetPacket_->data. */
     92    bool bDataENetAllocated_;
    8793  private:
    8894    static std::map<ENetPacket *, Packet *> packetMap_;
Note: See TracChangeset for help on using the changeset viewer.