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/ClientConnection.cc

    r1409 r1491  
    5050{
    5151  //static boost::thread_group network_threads;
     52
     53  boost::recursive_mutex ClientConnection::enet_mutex_;
    5254
    5355  ClientConnection::ClientConnection(int port, std::string address) {
     
    124126      return false;
    125127    }
     128    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    126129    if(enet_peer_send(server, 0, packet)<0)
    127130      return false;
     
    143146    if(server==NULL)
    144147      return false;
     148    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    145149    enet_host_flush(client);
    146150    return true;
     
    149153  void ClientConnection::receiverThread() {
    150154    // what about some error-handling here ?
    151     enet_initialize();
    152155    atexit(enet_deinitialize);
    153156    ENetEvent *event;
    154     client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
     157    {
     158      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     159      enet_initialize();
     160      client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
     161    }
    155162    if(client==NULL) {
    156163      COUT(2) << "ClientConnection: could not create client host" << std::endl;
     
    168175      event = new ENetEvent;
    169176      //std::cout << "connection loop" << std::endl;
    170       if(enet_host_service(client, event, NETWORK_CLIENT_TIMEOUT)<0){
    171         // we should never reach this point
    172         quit=true;
    173         // add some error handling here ========================
     177      {
     178        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     179        if(enet_host_service(client, event, NETWORK_CLIENT_TIMEOUT)<0){
     180          // we should never reach this point
     181          quit=true;
     182          continue;
     183          // add some error handling here ========================
     184        }
    174185      }
    175186      switch(event->type){
     
    196207    if(!disconnectConnection())
    197208      // if disconnecting failed destroy conn.
     209      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    198210      enet_peer_reset(server);
    199211    return;
     
    202214  bool ClientConnection::disconnectConnection() {
    203215    ENetEvent event;
     216    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    204217    enet_peer_disconnect(server, 0);
    205218    while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){
     
    222235    ENetEvent event;
    223236    // connect to peer (server is type ENetPeer*)
     237    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    224238    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
    225239    if(server==NULL) {
Note: See TracChangeset for help on using the changeset viewer.