鸿 网 互 联 www.68idc.cn

浅析hald_dbus_local_server_init()函数

来源:互联网 作者:佚名 时间:2015-10-14 08:16
gboolean hald_dbus_local_server_init ( void ) { gboolean ret ; DBusError error ; ret = FALSE ; /* setup a server listening on a socket so we can do point to point * connections for programs spawned by hald */ dbus_error_init ( error ) ; //

gboolean
hald_dbus_local_server_init (void)
{
    gboolean ret;
    DBusError error;

    ret = FALSE;

    /* setup a server listening on a socket so we can do point to point
     * connections for programs spawned by hald
     */

    dbus_error_init (&error);
    // 因为HALD_DBUS_ADDRESS定义为
    // #define HALD_DBUS_ADDRESS "unix:tmpdir=/var/run/hald"
    // 所以目录地址为/var/run/hald/ unix类型文件名由_dbus_server_listen_platform_specific
    // 自动产生,格式为"dbus-随机的10字节字符"
    // unix:abstract=/var/run/hald/dbus-FcEU3mweOz,guid=74ef46b3436f7d00fd802ba84a489a0d
    if ((local_server = dbus_server_listen (HALD_DBUS_ADDRESS, &error)) == NULL) {
        HAL_ERROR (("Cannot create D-BUS server"));
        goto out;
    }
    local_server_address = dbus_server_get_address (local_server);
    HAL_INFO (("local server is listening at %s", local_server_address));
    // 设置dbus-server对应的add_watch和add_timeout回调函数,将connection上的watches链表上的watch单元
    //
g_io_create_watch==>unix_channel_funcs.g_io_unix_create_watch,设置回调函数为io_handler_dispatch()将connection上的watches链表上的timeout单元
    // 添加到ConnectionSetup即cs的->timeouts链表上设置回调函数为timeout_handler_dispatch[luther.gliethttp].
    // 而local_server将监听HALD_DBUS_ADDRESS,即:"unix:tmpdir=/var/run/hald",当发现有人连接该unix文件时,
    // 回调函数io_handler_dispatch()调用socket_handle_watch()==>handle_new_client_fd_and_unlock()建立该用户发起的连接.
    dbus_server_setup_with_g_main (local_server, NULL); // 将local_server添件到g_main_context_default()返回的默认context上
    // 当有新的connection被添加时将调用local_server_handle_connection回调函数处理该connection连接,
    // 重新设置该connection的vtable虚函数表,然后调用dbus_connection_setup_with_g_main (new_connection, NULL);
    // 设置该connection的watch_functions为add_watch,同时将connection上的watches链表上的watch单元
    //
g_io_create_watch==>unix_channel_funcs.g_io_unix_create_watch,将该connection在
    // _dbus_transport_set_connection()==>socket_connection_set()中产生的transport监控的read_watch和write_watch,
    // 添加到transport->connection,即在handle_new_client_fd_and_unlock()中刚刚创建的connection->watches链表上,
    // read_watch和write_watch当发生数据流动时,
    // 将触发io_handler_dispatch==>_dbus_connection_handle_watch()回调函数[luther.gliethttp].
    dbus_server_set_new_connection_function (local_server, local_server_handle_connection, NULL, NULL);    

    ret = TRUE;

out:
    return ret;
}

网友评论
<