Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2164


Ignore:
Timestamp:
Nov 9, 2008, 2:39:15 PM (16 years ago)
Author:
rgrieder
Message:

Added a mutex to the static packetMap_ in Packet. This could resolve two issues with that map.
Also added the packetMap_.erase(.) call. So Fabian, if it doesn't work anymore, just comment line 220 in Packet.cc.

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

Legend:

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

    r2112 r2164  
    138138
    139139  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) {
    140     boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex);
     140    boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex);
    141141    if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0)
    142142      return false;
     
    156156    if(!instance_)
    157157      return false;
    158     boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex);
     158    boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex);
    159159    for(ClientInformation *i=ClientInformation::getBegin()->next(); i!=0; i=i->next()){
    160160      COUT(3) << "adding broadcast packet for client: " << i->getID() << std::endl;
     
    169169    if(server==NULL || !instance_)
    170170      return false;
    171     boost::recursive_mutex::scoped_lock lock(enet_mutex);
     171    boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex);
    172172    enet_host_flush(server);
    173173    lock.unlock();
     
    180180    atexit(enet_deinitialize);
    181181    { //scope of the mutex
    182       boost::recursive_mutex::scoped_lock lock(enet_mutex);
     182      boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex);
    183183      enet_initialize();
    184184      server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
     
    194194    while(!quit){
    195195      { //mutex scope
    196         boost::recursive_mutex::scoped_lock lock(enet_mutex);
     196        boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex);
    197197        if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
    198198          // we should never reach this point
     
    236236    // if we're finishied, destroy server
    237237    {
    238       boost::recursive_mutex::scoped_lock lock(enet_mutex);
     238      boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex);
    239239      enet_host_destroy(server);
    240240      lock.unlock();
     
    250250    while(temp!=0){
    251251      {
    252         boost::recursive_mutex::scoped_lock lock(enet_mutex);
     252        boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex);
    253253        enet_peer_disconnect(temp->getPeer(), 0);
    254254        lock.unlock();
     
    258258    //bugfix: might be the reason why server crashes when clients disconnects
    259259    temp = ClientInformation::getBegin()->next();
    260     boost::recursive_mutex::scoped_lock lock(enet_mutex);
     260    boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex);
    261261    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){
    262262      switch (event.type)
     
    332332  void ConnectionManager::disconnectClient(ClientInformation *client){
    333333    {
    334       boost::recursive_mutex::scoped_lock lock(enet_mutex);
     334      boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex);
    335335      enet_peer_disconnect(client->getPeer(), 0);
    336336      lock.unlock();
  • code/branches/objecthierarchy/src/network/packet/Packet.cc

    r2132 r2164  
    5454
    5555std::map<size_t, Packet *> Packet::packetMap_;
     56boost::recursive_mutex Packet::packetMap_mutex;
    5657
    5758Packet::Packet(){
     
    128129      return false;
    129130    }
     131    // Assures we don't create a packet and destroy it right after in another thread
     132    // without having a reference in the packetMap_
     133    boost::recursive_mutex::scoped_lock lock(Packet::packetMap_mutex);
    130134    // We deliver ENet the data address so that it doesn't memcpy everything again.
    131135    // --> We have to delete data_ ourselves!
     
    207211*/
    208212void Packet::deletePacket(ENetPacket *enetPacket){
     213  boost::recursive_mutex::scoped_lock lock(Packet::packetMap_mutex);
    209214  // Get our Packet from a gloabal map with all Packets created in the send() method of Packet.
    210215  std::map<size_t, Packet*>::iterator it = packetMap_.find((size_t)enetPacket);
     
    213218  it->second->enetPacket_ = 0;
    214219  delete it->second;
    215   //packetMap_.erase(it);
     220  packetMap_.erase(it);
    216221  COUT(4) << "PacketMap size: " << packetMap_.size() << std::endl;
    217222}
  • code/branches/objecthierarchy/src/network/packet/Packet.h

    r2132 r2164  
    2929#define NETWORKPACKET_H
    3030
    31 #include "../NetworkPrereqs.h"
     31#include "network/NetworkPrereqs.h"
    3232
    3333#include <map>
    3434#include <enet/enet.h>
     35#include <boost/thread/recursive_mutex.hpp>
    3536
    3637#include "util/Integers.h"
     
    9394  private:
    9495    static std::map<size_t, Packet *> packetMap_;
     96    //! Static mutex for any packetMap_ access
     97    static boost::recursive_mutex packetMap_mutex;
    9598    ENetPacket *enetPacket_;
    9699};
Note: See TracChangeset for help on using the changeset viewer.