From dc34299915f685775aff28bc99abbbc101ed001f Mon Sep 17 00:00:00 2001 From: Minje Ahn Date: Thu, 16 Apr 2020 10:08:55 +0900 Subject: [PATCH] Improve mainloop Change-Id: I0b585910f60e11428df241a7c4d594fa9cd539eb Signed-off-by: Minje Ahn --- svc/dcm_svc_main.c | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/svc/dcm_svc_main.c b/svc/dcm_svc_main.c index 78844ae..42654aa 100755 --- a/svc/dcm_svc_main.c +++ b/svc/dcm_svc_main.c @@ -26,15 +26,7 @@ static GMainLoop *g_dcm_svc_mainloop; -static void __dcm_svc_kill_service(void) -{ - if (g_dcm_svc_mainloop) { - dcm_warn("Shutting down DCM Service"); - g_main_loop_quit(g_dcm_svc_mainloop); - } -} - -static gboolean __dcm_service_recieve_request(GIOChannel *src, GIOCondition condition, gpointer data) +static gboolean __dcm_service_recieve_request(GIOChannel *channel, GIOCondition condition, gpointer data) { dcmMsg recv_msg; int sock = -1; @@ -44,7 +36,7 @@ static gboolean __dcm_service_recieve_request(GIOChannel *src, GIOCondition cond memset((void *)&recv_msg, 0, sizeof(recv_msg)); - sock = g_io_channel_unix_get_fd(src); + sock = g_io_channel_unix_get_fd(channel); dcm_retvm_if(sock < 0, G_SOURCE_CONTINUE, "sock fd is invalid!"); /* Accept tcp client socket */ @@ -60,9 +52,13 @@ static gboolean __dcm_service_recieve_request(GIOChannel *src, GIOCondition cond if (recv_msg.msg_type == DCM_MSG_REQUEST_KILL_SERVER) { dcm_warn("Receive DCM_MSG_REQUEST_KILL_SERVER"); - __dcm_svc_kill_service(); + dcm_ipc_close_socket(client_sock); + g_main_loop_quit(g_dcm_svc_mainloop); - } else if (recv_msg.msg_type == DCM_MSG_REQUEST_MEDIA) { + return G_SOURCE_REMOVE; + } + + if (recv_msg.msg_type == DCM_MSG_REQUEST_MEDIA) { ret = dcm_scan_single(recv_msg.msg, recv_msg.uid, &face_count); dcm_debug("Scan single result: ret[%d] face_count[%d]", ret, face_count); if (ret == MS_MEDIA_ERR_NONE) @@ -86,9 +82,7 @@ ERROR: int main(int argc, char *argv[]) { int sockfd = -1; - GSource *source = NULL; GIOChannel *channel = NULL; - GMainContext *context = NULL; /* Create and bind new socket to main_loop */ if (dcm_ipc_create_socket(&sockfd, DCM_IPC_PORT_DCM_RECV) != MS_MEDIA_ERR_NONE) { @@ -96,16 +90,12 @@ int main(int argc, char *argv[]) return -1; } - g_dcm_svc_mainloop = g_main_loop_new(context, FALSE); - context = g_main_loop_get_context(g_dcm_svc_mainloop); + g_dcm_svc_mainloop = g_main_loop_new(NULL, FALSE); - /* Create new channel to watch new socket for main_loop */ + /* Create new channel to watch tcp socket */ channel = g_io_channel_unix_new(sockfd); - source = g_io_create_watch(channel, G_IO_IN); - - /* Set callback to be called when socket is readable */ - g_source_set_callback(source, (GSourceFunc)__dcm_service_recieve_request, NULL, NULL); - g_source_attach(source, context); + g_io_add_watch(channel, G_IO_IN, __dcm_service_recieve_request, NULL); + g_io_channel_unref(channel); if (dcm_face_detect_initialize() != MS_MEDIA_ERR_NONE) dcm_error("Failed to dcm_face_detect_initialize"); @@ -119,12 +109,8 @@ int main(int argc, char *argv[]) g_main_loop_run(g_dcm_svc_mainloop); dcm_info("DCM Service is shutting down"); - - g_io_channel_shutdown(channel, FALSE, NULL); - g_io_channel_unref(channel); dcm_ipc_close_socket(sockfd); g_main_loop_unref(g_dcm_svc_mainloop); - dcm_face_detect_finalize(); return 0; -- 2.34.1