PtConnectionWaitForName()

Try to connect to the server with a given name

Synopsis:

typedef int PtConnectionWaitFunc_t(
        PtConnectionClient_t *connection, 
        void *user_data, 
        unsigned n );

int PtConnectionWaitForName(
        const char *name,
        int subtype,
        unsigned flags,
        PtConnectionWaitFunc_t *cb,
        void *usrdata );

Library:

ph

Description:

This function lets you set up a timer that periodically tries to connect to a server. This mechanism is meant to be used when you might need to spawn the server and then wait until it's ready to accept a connection.

Each attempt consists of a call to PtConnectionFindName(), followed by a call to your callback. The callback should check its connection argument: if it's non-NULL, it means that this attempt was successful. The callback should then set up the connection object and return zero.

If the connection argument to your callback is NULL, the connection couldn't been established this time. The callback should return the number of milliseconds to wait before making another attempt. Returning zero means “don't retry.”

The n argument to the callback is just a serial number that gets incremented after each failed attempt. It can be useful if you want to spawn the server after the first attempt fails:

int connection_callback(
        PtConnectionClient_t *connection,
        void *user_data, unsigned n ) {

    if ( connection )
        PtConnectionAddEventHandlers( connection, tab, N );
    else
        switch ( n ) {
              default :
                  return 200; /* retry after 1/5 of a second */
              case 0 :
                  /* First attempt failed -- spawn the server */
                  if ( PtSpawn( ... ) > 0 )
                     return 1000; /* Retry after one second */
                  else
                     warn( "Couldn't spawn the server" );
              case 9 :
                  /* ten attempts failed -- give up */
                  ;
              }
        return 0;
    }

(A better way would be to let it keep trying until the spawned server terminates rather than just have a fixed number of retries — but we'll leave that as an exercise for the reader.)

Returns:

0
Success.
-1
An error occurred.

Classification:

Photon

Safety:
Interrupt handler No
Signal handler No
Thread No

See also:

PtConnectionFindId(), PtConnectionFindName(), PtConnectionTmpName(),

Connections in the Interprocess Communication chapter of the Photon Programmer's Guide