Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 31, 2008, 11:48:01 AM (16 years ago)
Author:
scheusso
Message:

enet is not threadsafe (catched that now); some first step towards dedicated server

File:
1 edited

Legend:

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

    r1409 r1491  
    6565 
    6666  ConnectionManager::ConnectionManager():receiverThread_(0){}
     67  boost::recursive_mutex ConnectionManager::enet_mutex_;
    6768 
    6869  ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) {
     
    143144    if(!temp)
    144145      return false;
     146    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    145147    if(enet_peer_send(peer, (enet_uint8)temp->getID() , packet)!=0)
    146148      return false;
     
    152154    if(!temp)
    153155      return false;
     156    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    154157    if(enet_peer_send(temp->getPeer(), (enet_uint8)clientID, packet)!=0)
    155158      return false;
     
    158161
    159162  bool ConnectionManager::addPacketAll(ENetPacket *packet) {
     163    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    160164    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
    161165      if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0)
     
    177181    if(server==NULL)
    178182      return false;
     183    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    179184    enet_host_flush(server);
    180185    return true;
     
    184189    // what about some error-handling here ?
    185190    ENetEvent *event;
    186     enet_initialize();
    187191    atexit(enet_deinitialize);
    188     server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
     192    { //scope of the mutex
     193      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     194      enet_initialize();
     195      server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
     196    }
    189197    if(server==NULL){
    190198      // add some error handling here ==========================
     
    195203    while(!quit){
    196204      event = new ENetEvent;
    197       if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
    198         // we should never reach this point
    199         quit=true;
    200         // add some error handling here ========================
     205      { //mutex scope
     206        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     207        if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
     208          // we should never reach this point
     209          quit=true;
     210          continue;
     211          // add some error handling here ========================
     212        }
    201213      }
    202214      switch(event->type){
     
    231243    disconnectClients();
    232244    // if we're finishied, destroy server
    233     enet_host_destroy(server);
     245    {
     246      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     247      enet_host_destroy(server);
     248    }
    234249  }
    235250 
     
    241256    ClientInformation *temp = head_->next();
    242257    while(temp!=0){
    243       enet_peer_disconnect(temp->getPeer(), 0);
     258      {
     259        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     260        enet_peer_disconnect(temp->getPeer(), 0);
     261      }
    244262      temp = temp->next();
    245263    }
    246264    //bugfix: might be the reason why server crashes when clients disconnects
    247     //temp = temp->next();
    248265    temp = head_->next();
    249     while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
     266    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     267    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){
    250268      switch (event.type)
    251269      {
     
    273291  }
    274292
    275   /*bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
    276     COUT(4) << "removing client from list" << std::endl;
    277     return removeClient(head_->findClient(&(peer->address))->getID());
    278   }*/
    279293/**
    280294This function adds a client that connects to the clientlist of the server
     
    422436 
    423437  void ConnectionManager::disconnectClient(ClientInformation *client){
    424     enet_peer_disconnect(client->getPeer(), 0);
     438    {
     439      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     440      enet_peer_disconnect(client->getPeer(), 0);
     441    }
    425442    removeShip(client);
    426443  }
Note: See TracChangeset for help on using the changeset viewer.