2

There is a dbus C api example http://www.matthew.ath.cx/misc/dbus . It is also the on github with improvements https://github.com/wware/stuff/blob/master/dbus-example/dbus-example.c . In the query() function which makes a call and gets a reply, do you need to handle timeout like I'm showing below by adding lines 19 to 23? If we do it, should we call ...unref on pending line at the line 20? By reading the flow of this example I think that is what we should do.

// send message and get a handle for a reply
if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) { // -1 is default timeout
    fprintf(stderr, "Out Of Memory!\n"); 
    exit(1);
}
if (NULL == pending) { 
    fprintf(stderr, "Pending Call Null\n"); 
    exit(1); 
}
dbus_connection_flush(conn);

printf("Request Sent\n");

// free message
dbus_message_unref(msg);

// block until we recieve a reply
dbus_pending_call_block(pending);

/*do we need to handle the timeout case? -- line 20*/
if ( ! dbus_pending_call_get_completed(pending) ) {
    /*do we need to unref pending?  -- line 22 */
    dbus_pending_call_unref(pending); /* --line 23 */
    fprintf(stderr, "Reply timeout\n"); 
    return(1); 
}
minghua
  • 5,981
  • 6
  • 45
  • 71
  • When posting, please do not post line numbers. It makes it more difficult for people to help you by preventing cut/paste of your code. – David C. Rankin Jul 30 '14 at 23:48

1 Answers1

0

After some reading and experimenting, here is the answer:

if ( ! dbus_pending_call_get_completed(pending) ) {
    /* always safe to call cancel */
    dbus_pending_call_cancel(pending);

    /*do we need to unref pending? yes. if not it would be a memory leak */
    dbus_pending_call_unref(pending);
    fprintf(stderr, "Reply failed to complete\n"); 
    return(1); 
}
/* timeout notes:
 *   it always reaches here, _completed() always returns true.
 *   if destination name does not exist, it consumes 0 time and returns
 *           a string indicating the possible error.
 *   if destination replies late, it consumes full timeout duration and
 *           returns a string about the possible error.
 * to abort before complete, use the _cancel() call. it is safe to call 
 * _cancel() even if it has been completed. 
 */
minghua
  • 5,981
  • 6
  • 45
  • 71