【问题标题】:dbus c api example handle timeout case and unref the pending variabledbus c api 示例处理超时情况并取消引用挂起的变量
【发布时间】:2014-07-30 23:02:40
【问题描述】:

有一个 dbus C api 示例 http://www.matthew.ath.cx/misc/dbus 。它也是 github 上的改进 https://github.com/wware/stuff/blob/master/dbus-example/dbus-example.c 。在发出呼叫并获得回复的 query() 函数中,您是否需要像我在下面显示的那样通过添加第 19 行到第 23 行来处理超时?如果我们这样做,我们应该在第 20 行的待处理行上调用 ...unref 吗?通过阅读这个例子的流程,我认为这是我们应该做的。

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

【问题讨论】:

  • 发帖时请不要贴行号。通过防止剪切/粘贴代码,人们更难帮助您。

标签: c dbus c-api


【解决方案1】:

经过一番阅读和实验,答案如下:

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. 
 */

【讨论】:

    猜你喜欢
    • 2014-03-03
    • 2018-08-07
    • 1970-01-01
    • 2016-01-21
    • 2021-08-02
    • 2012-02-12
    • 2012-03-08
    • 2018-02-18
    • 1970-01-01
    相关资源
    最近更新 更多