Advertisement

Problem creating an enet client

Started by January 05, 2005 12:47 AM
4 comments, last by Fil 20 years, 1 month ago
I need to be able to test my game on one computer right now. When I create an enet server aand set it to listen I cannot create a client. There is no error I get, it simply stalls.

void MainWindow::CreateClient()
{
// here is the problem
   m_Client = enet_host_create( NULL, // create a client host
                                1,    // only allow 1 outgoing connection
                                57600 / 8,   // 56K modem with 56 Kbps downstream bandwidth
                                14400 / 8 ); // 56K modem with 14 Kbps upstream bandwidth 

   if( m_Client == NULL )
   {
      m_Mutex.Lock();
      m_Console.AddLine( "An error occurred while trying to create an ENet client host." );
      m_Mutex.Release();
   }
   else
   {
      m_Mutex.Lock();
      m_Console.AddLine( "An enet client was successfully created." );
      m_Mutex.Release();
   }
}

void MainWindow::CreateServer()
{
   ENetAddress  address;

   // Bind the m_Server to the default localhost.
   // A specific host address can be specified by
   
//   enet_address_set_host( &address, "x.x.x.x" );

   m_Mutex.Lock();
   EnumerateLocalAddresses();
   m_Mutex.Lock();

   if( m_Ipaddresses.size() > 1 )
   {
      enet_address_set_host( &address, m_Ipaddresses[1].c_str() );
//      enet_address_set_host( &address, "localhost" );
   }
   else if( m_Ipaddresses.size() > 0 )
   {
      enet_address_set_host( &address, m_Ipaddresses[0].c_str() );
//      enet_address_set_host( &address, "localhost" );
   }
   else
   {
      address.host = ENET_HOST_ANY;
//      enet_address_set_host( &address, "localhost" );
   }

   // Bind the m_Server to a port
   address.port = m_Port;

   m_Server = enet_host_create( &address, // the address to bind the m_Server host to 
                                32,       // allow up to 32 clients and/or outgoing connections 
                                0,        // assume any amount of incoming bandwidth 
                                0 );      // assume any amount of outgoing bandwidth

   if( m_Server == NULL )
   {
      m_Console.AddLine( "An error occurred while trying to create an ENet m_Server host.\n" );
   }
   else
   {
      std::ostringstream tmp, tmp2;
      tmp << m_Port;
      tmp2 << address.host;
      m_Console.AddLine( std::string( "Server successfully created on " ) + tmp2.str() + ":" + tmp.str() );
   }
}

In your source you have:

m_Mutex.Lock();
EnumerateLocalAddresses();
m_Mutex.Lock();

maybe it should be:



m_Mutex.Lock();
EnumerateLocalAddresses();
m_Mutex.Release(); // <-

Fil (il genio)
Advertisement
I realisez that the problem was with the mutex I was using for the thread and server both had the same name.

#ifndef __C_Mutex_H#define __C_Mutex_H#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <string>class C_Mutex{   public:      C_Mutex( void ) : m_Mutex( NULL ) {}      ~C_Mutex(){}      operator HANDLE (void) const      {         return m_Mutex;      }      operator std::string (void)      {         return m_Name;      }      bool Create( bool bThisThreadisIiitialOwner, std::string name )      {         m_Name  = name;         m_Mutex = ::CreateMutex( NULL, bThisThreadisIiitialOwner, (LPCTSTR) name.c_str() );         if( !m_Mutex )            return false;         return true;      }      DWORD Lock( DWORD time = INFINITE )      {         return ::WaitForSingleObject( m_Mutex, time );      }      bool Release( void )      {         return (bool) ::ReleaseMutex( m_Mutex );      }      protected:      HANDLE      m_Mutex;      std::string m_Name;   private:};#endif


Now my question is if I am using mutexes the correct way.

// thread 1
C_Mutex m_Mutex;
m_Mutex.Create( true, "enetservermutex" );
m_Mutex.Release();

// thread 2
m_Mutex.Lock(); // program freezes

What am I doing wrong here?
It's likely that thread 2 do Lock for a mutex that is not yet created.
You should check what is returned by Release and Lock functions ;)
I usually create the mutex before starting any thread that will use it.

Hope that helps.
Fil (il genio)
thanks I got the mutex problems sorted out :)
Happy to have been of help! [cool]
Fil (il genio)

This topic is closed to new replies.

Advertisement