Jump to content
  • Advertisement
Sign in to follow this  
try_catch_this

Problem creating an enet client

This topic is 4889 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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() );
   }
}

Share this post


Link to post
Share on other sites
Advertisement
In your source you have:

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

maybe it should be:



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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!