About Store Forum Documentation Contact



Post Reply 
Connection.updateState()
Author Message
Houge Offline
Member

Post: #1
Connection.updateState()
Hello!

I faced an issue, i take the following code (it's too small to export it as a project):

Code:
Connection conn;
Thread t;

void InitPre()
{
   EE_INIT();
}

bool Init() // initialize after engine is ready
{
   t.create(connect, 0, 10000);
   return true;
}

void Shut()
{
   t.del();
}

bool Update()
{
   if(conn.receive(0))
   {
      switch(conn.data.getInt())
      {
       case 0: break;
       case 1: break;
      }
   }
  
  
   if(Kb.bp(KB_ESC))return false;
   return true;
}

void Draw()
{
   D.clear(TURQ);
   D.text (0, 0, "Hello to Esenthel Engine !");
}

Bool connect(Thread &thread_caller)
{
    if(conn.state() != CONNECT_GREETED)
    {
       SockAddr server;
       server.ip  ("127.0.0.1");
       server.port(7777);
       conn.clientConnectToServer(server);
        
       conn.updateState(5000);
    }
    
    return true;
}

I do Connection.updateState() in thread because i don't want my window to hang during connection. From time to time (sometimes at application launch time, sometimes after 40 seconds, sometimes more) application crashes - unable to allocate some memory. I think it's because main thread and secondary thread call conn.receive(0) and conn.updateState(5000) simultaneously.

Is it right to use it like that or not?

How to avoid crashes if i need to wait for connection to be greeted in thread?

Please help smile
(This post was last modified: 12-23-2014 08:58 PM by Houge.)
12-23-2014 08:53 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #2
RE: Connection.updateState()
Hi,

Since last few updates to Connection, you don't need to create a secondary Thread for connecting, you can keep calling 'updateState' with 0 timeout continuously in each frame of Update.

I recommend checking updated tutorials, there are 2 client server tutorials in "Tutorials" project, and there's also EMMO and Ineisis Online from the Store.
12-24-2014 01:35 AM
Find all posts by this user Quote this message in a reply
Houge Offline
Member

Post: #3
RE: Connection.updateState()
You won't believe me if i say that i read all network tutorials grin

My solution is to check if conn.state() == CONNECT_GREETED before performing conn.recieve(0) in main thread.

Frankly speaking i can call examples in tutorials "simple". When you need a bit more complicated solution, you perform additional actions.

For example i want to make connection in my game client like in "grown ups" MMOs, when i have fixed connection timeout when connecting to the server. If i can use conn.updateState(5000) in a thread to perform timeout check, why do i need to replace it with global variables, timers, etc only to make it work in main thread without additional threads?
12-24-2014 07:18 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #4
RE: Connection.updateState()
Hello,

If you're interested in a timeout, I recommend using Connection.life
12-24-2014 01:27 PM
Find all posts by this user Quote this message in a reply
Houge Offline
Member

Post: #5
RE: Connection.updateState()
Thanks for suggestion, but i like updateState option smile
12-24-2014 01:53 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply