mirror of https://github.com/dnomd343/gdbus-demo
dnomd343
3 years ago
9 changed files with 228 additions and 0 deletions
@ -0,0 +1,7 @@ |
|||
cmake_minimum_required(VERSION 3.16) |
|||
set(CMAKE_CXX_STANDARD 14) |
|||
project(gdbus_multi_signal_demo) |
|||
|
|||
add_subdirectory(gdbus) |
|||
add_subdirectory(client) |
|||
add_subdirectory(server) |
@ -0,0 +1,13 @@ |
|||
cmake_minimum_required(VERSION 3.16) |
|||
|
|||
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/includes) |
|||
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/gdbus) |
|||
INCLUDE_DIRECTORIES(/usr/include/glib-2.0) |
|||
INCLUDE_DIRECTORIES(/usr/include/gio-unix-2.0) |
|||
INCLUDE_DIRECTORIES(/usr/lib/glib-2.0/include) |
|||
INCLUDE_DIRECTORIES(/usr/lib/i386-linux-gnu/glib-2.0/include) |
|||
INCLUDE_DIRECTORIES(/usr/lib/x86_64-linux-gnu/glib-2.0/include) |
|||
INCLUDE_DIRECTORIES(/usr/lib/aarch64-linux-gnu/glib-2.0/include) |
|||
|
|||
add_executable(client client.c ${PROJECT_SOURCE_DIR}/gdbus/gdbus_demo_gen.c) |
|||
target_link_libraries(client rt pthread gio-2.0 glib-2.0 gobject-2.0 gmodule-2.0 gthread-2.0) |
@ -0,0 +1,55 @@ |
|||
#include "client.h" |
|||
|
|||
// 进程主循环函数
|
|||
static void* start_main_loop(void* args) { |
|||
g_main_loop_run(main_loop); |
|||
} |
|||
|
|||
// 接收信号1
|
|||
static gboolean receive_signal_1(ComGdbusDemo *object, const gint *arg, gpointer userdata) { |
|||
g_print("Get Signal-1: %d\n", arg); |
|||
return true; |
|||
} |
|||
|
|||
// 接收信号2
|
|||
static gboolean receive_signal_2(ComGdbusDemo *object, const gdouble *arg, gpointer userdata) { |
|||
g_print("Get Signal-2: %f\n", arg); |
|||
return true; |
|||
} |
|||
|
|||
// 接收信号3
|
|||
static gboolean receive_signal_3(ComGdbusDemo *object, const gchar *arg, gpointer userdata) { |
|||
g_print("Get Signal-3: %s\n", arg); |
|||
return true; |
|||
} |
|||
|
|||
// 初始化DBus连接
|
|||
bool init_dbus_client() { |
|||
GError *proxy_error = NULL; |
|||
GError *connect_error = NULL; |
|||
main_loop= g_main_loop_new(NULL,FALSE); // 创建主循环对象
|
|||
connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &connect_error); // 建立连接
|
|||
if (connect_error != NULL) { // 连接失败
|
|||
g_print("Failed to connect dbus: %s.\n", connect_error->message); |
|||
g_error_free(connect_error); |
|||
return false; |
|||
} |
|||
proxy = com_gdbus_demo_proxy_new_sync(connection, G_DBUS_PROXY_FLAGS_NONE, "com.gdbus.demo", |
|||
"/com/gdbus/object", NULL, &proxy_error); // 创建Proxy
|
|||
if (proxy == 0) { // 创建Proxy失败
|
|||
g_print("Failed to create proxy: %s.\n", proxy_error->message); |
|||
g_error_free(proxy_error); |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
int main() { |
|||
pthread_t tid; |
|||
init_dbus_client(); // 初始化DBus
|
|||
g_signal_connect(proxy, "send-signal1", G_CALLBACK(receive_signal_1), NULL); // 监听信号1
|
|||
g_signal_connect(proxy, "send-signal2", G_CALLBACK(receive_signal_2), NULL); // 监听信号2
|
|||
g_signal_connect(proxy, "send-signal3", G_CALLBACK(receive_signal_3), NULL); // 监听信号3
|
|||
pthread_create(&tid, NULL, start_main_loop, NULL); // 创建线程 进入主循环
|
|||
for (;;) {} // 死循环
|
|||
} |
@ -0,0 +1,4 @@ |
|||
cmake_minimum_required(VERSION 3.16) |
|||
|
|||
EXECUTE_PROCESS(COMMAND gdbus-codegen --generate-c-code |
|||
${PROJECT_SOURCE_DIR}/gdbus/gdbus_demo_gen ${PROJECT_SOURCE_DIR}/gdbus/interface.xml) |
@ -0,0 +1,14 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<node> |
|||
<interface name="com.gdbus.demo"> |
|||
<signal name="SendSignal1"> |
|||
<arg name="sig" type="i"/> |
|||
</signal> |
|||
<signal name="SendSignal2"> |
|||
<arg name="sig" type="d"/> |
|||
</signal> |
|||
<signal name="SendSignal3"> |
|||
<arg name="sig" type="s"/> |
|||
</signal> |
|||
</interface> |
|||
</node> |
@ -0,0 +1,22 @@ |
|||
#ifndef _CLIENT_H_ |
|||
#define _CLIENT_H_ |
|||
|
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <ctype.h> |
|||
#include <stdbool.h> |
|||
#include <gio/gio.h> |
|||
#include <glib.h> |
|||
#include "gdbus_demo_gen.h" |
|||
|
|||
static ComGdbusDemo *proxy = NULL; |
|||
static GMainLoop *main_loop = NULL; |
|||
static GDBusConnection *connection = NULL; |
|||
|
|||
bool init_dbus_client(); |
|||
static void* start_main_loop(void*); |
|||
static gboolean receive_signal_1(ComGdbusDemo*, const gint*, gpointer); |
|||
static gboolean receive_signal_2(ComGdbusDemo*, const gdouble*, gpointer); |
|||
static gboolean receive_signal_3(ComGdbusDemo*, const gchar*, gpointer); |
|||
|
|||
#endif |
@ -0,0 +1,24 @@ |
|||
#ifndef _SERVER_H_ |
|||
#define _SERVER_H_ |
|||
|
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <ctype.h> |
|||
#include <stdbool.h> |
|||
#include <gio/gio.h> |
|||
#include <glib.h> |
|||
#include "gdbus_demo_gen.h" |
|||
|
|||
static GMainLoop *main_loop = NULL; |
|||
static ComGdbusDemo *skeleton = NULL; |
|||
|
|||
void init_dbus_server(); |
|||
static void* start_main_loop(void*); |
|||
static gboolean send_signal_1(gconstpointer); |
|||
static gboolean send_signal_2(gconstpointer); |
|||
static gboolean send_signal_3(gconstpointer); |
|||
static void gdbus_acquired_callback(GDBusConnection*, const gchar*, gpointer); |
|||
static void gdbus_name_acquired_callback(GDBusConnection*, const gchar*, gpointer); |
|||
static void gdbus_name_lost_callback(GDBusConnection*, const gchar*, gpointer); |
|||
|
|||
#endif |
@ -0,0 +1,13 @@ |
|||
cmake_minimum_required(VERSION 3.16) |
|||
|
|||
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/includes) |
|||
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/gdbus) |
|||
INCLUDE_DIRECTORIES(/usr/include/glib-2.0) |
|||
INCLUDE_DIRECTORIES(/usr/include/gio-unix-2.0) |
|||
INCLUDE_DIRECTORIES(/usr/lib/glib-2.0/include) |
|||
INCLUDE_DIRECTORIES(/usr/lib/i386-linux-gnu/glib-2.0/include) |
|||
INCLUDE_DIRECTORIES(/usr/lib/x86_64-linux-gnu/glib-2.0/include) |
|||
INCLUDE_DIRECTORIES(/usr/lib/aarch64-linux-gnu/glib-2.0/include) |
|||
|
|||
add_executable(server server.c ${PROJECT_SOURCE_DIR}/gdbus/gdbus_demo_gen.c) |
|||
target_link_libraries(server rt pthread gio-2.0 glib-2.0 gobject-2.0 gmodule-2.0 gthread-2.0) |
@ -0,0 +1,76 @@ |
|||
#include "server.h" |
|||
|
|||
// 进程主循环函数
|
|||
static void* start_main_loop(void* args) { |
|||
g_main_loop_run(main_loop); // 进入主循环
|
|||
} |
|||
|
|||
// 发送信号1
|
|||
static gboolean send_signal_1(gconstpointer p) { |
|||
com_gdbus_demo_emit_send_signal1(skeleton, 111); |
|||
g_print("Signal-1 sent.\n"); |
|||
return true; |
|||
} |
|||
|
|||
// 发送信号2
|
|||
static gboolean send_signal_2(gconstpointer p) { |
|||
com_gdbus_demo_emit_send_signal2(skeleton, 343e7); |
|||
g_print("Signal-2 sent.\n"); |
|||
return true; |
|||
} |
|||
|
|||
// 发送信号3
|
|||
static gboolean send_signal_3(gconstpointer p) { |
|||
com_gdbus_demo_emit_send_signal3(skeleton, "dnomd343"); |
|||
g_print("Signal-3 sent.\n"); |
|||
return true; |
|||
} |
|||
|
|||
// 如果无法建立与总线的连接会调用 name_lost_handler()
|
|||
// 如果无法获取 bus-name,会先调用 bus_acquired_handler(),再调用 name_lost_handler()
|
|||
// 如果已获得 bus-name ,会先调用 bus_acquired_handler(),再调用 name_acquired_handler()
|
|||
|
|||
// GDBus Acquired 回调
|
|||
static void gdbus_acquired_callback(GDBusConnection *callback_connection, const gchar *name, gpointer user_data) { |
|||
GError *error = NULL; |
|||
skeleton = com_gdbus_demo_skeleton_new(); |
|||
g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(skeleton), callback_connection, |
|||
"/com/gdbus/object", &error); // 绑定到对象上
|
|||
if(error != 0) { |
|||
g_print("Failed to export object: %s.\n", error->message); |
|||
g_error_free(error); |
|||
g_main_loop_quit(main_loop); |
|||
return; |
|||
} |
|||
g_print("Skeleton load successfully.\n"); |
|||
} |
|||
|
|||
// GDBus Name Acquired 回调
|
|||
static void gdbus_name_acquired_callback(GDBusConnection *callback_connection, const gchar *name, gpointer user_data) { |
|||
g_print("Acquired bus-name success.\n"); |
|||
} |
|||
|
|||
// GDBus Name Lost 回调
|
|||
static void gdbus_name_lost_callback(GDBusConnection *callback_connection, const gchar *name, gpointer user_data) { |
|||
g_print("Name lost error: Failed to get bus-name.\n"); |
|||
g_main_loop_quit(main_loop); // 退出主循环
|
|||
} |
|||
|
|||
// 初始化DBus连接
|
|||
void init_dbus_server() { |
|||
main_loop = g_main_loop_new(NULL, FALSE); // 创建主循环对象
|
|||
// 申请 bus-name 并将结果给到回调函数
|
|||
g_bus_own_name(G_BUS_TYPE_SESSION, "com.gdbus.demo", G_BUS_NAME_OWNER_FLAGS_NONE, |
|||
&gdbus_acquired_callback, &gdbus_name_acquired_callback, |
|||
&gdbus_name_lost_callback, NULL, NULL); |
|||
} |
|||
|
|||
int main() { |
|||
pthread_t tid; |
|||
init_dbus_server(); // 初始化DBus连接
|
|||
g_timeout_add(2000, (GSourceFunc)send_signal_1, NULL); // 定时发送信号
|
|||
g_timeout_add(4000, (GSourceFunc)send_signal_2, NULL); // 定时发送信号
|
|||
g_timeout_add(8000, (GSourceFunc)send_signal_3, NULL); // 定时发送信号
|
|||
pthread_create(&tid, NULL, start_main_loop, NULL); // 创建线程 进入主循环
|
|||
for (;;) {} // 死循环
|
|||
} |
Loading…
Reference in new issue