From: Sangchul Lee Date: Thu, 27 Apr 2023 02:46:14 +0000 (+0900) Subject: Pass client handle address and utilize it with pid as a key X-Git-Tag: accepted/tizen/7.0/unified/20231025.122158~65 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1a8c8a4b729bfabced65fa43f8feeb9e684fb131;p=platform%2Fcore%2Fmultimedia%2Fespp-service.git Pass client handle address and utilize it with pid as a key [Version] 0.1.4 Signed-off-by: Sangchul Lee --- diff --git a/packaging/espp-service.spec b/packaging/espp-service.spec index 539b66a..d7853c7 100644 --- a/packaging/espp-service.spec +++ b/packaging/espp-service.spec @@ -1,6 +1,6 @@ Name: espp-service Summary: ESPP service package which contains client lib. and daemon binary -Version: 0.1.3 +Version: 0.1.4 Release: 0 Group: Multimedia/Libraries License: Apache-2.0 diff --git a/src/client/espp_service_client_event_handler.c b/src/client/espp_service_client_event_handler.c index c453ff3..c21ded0 100644 --- a/src/client/espp_service_client_event_handler.c +++ b/src/client/espp_service_client_event_handler.c @@ -30,7 +30,11 @@ gpointer espp_service_client_event_handler_thread_func(gpointer data) while (!espp->event_handler.exit) { LOG_DEBUG("wait for read.."); - if ((ret = read(espp->event_fd, &event, sizeof(espp_service_event_from_server_s))) < 0) { + if ((ret = read(espp->event_fd, &event, sizeof(espp_service_event_from_server_s))) <= 0) { + if (errno == EAGAIN) { + LOG_DEBUG("socket might be closed"); + goto exit; + } strerror_r(errno, str_error, sizeof(str_error)); LOG_ERROR("failed to read(), fd[%d], err: %s", espp->event_fd, str_error); goto exit; diff --git a/src/client/espp_service_client_socket.c b/src/client/espp_service_client_socket.c index dfe4416..69ef49c 100644 --- a/src/client/espp_service_client_socket.c +++ b/src/client/espp_service_client_socket.c @@ -189,6 +189,7 @@ int espp_service_client_socket_request_init_event(espp_s *espp) RET_VAL_IF(fd == -1, -1, "failed to espp_service_client_socket_fd_new()"); FILL_SOCKET_MSG_REQUEST(data, ESPP_SERVICE_REQUEST_INIT_EVENT); + FILL_SOCKET_MSG_PARAMS(data, ESPP_SERVICE_REQUEST_INIT_EVENT, "handle", (long)espp); if (send_data(fd, &data, &result) != 0) goto error; if (result.ret != 0) { @@ -256,6 +257,7 @@ int espp_service_client_socket_request_create(espp_s *espp) RET_VAL_IF(fd == -1, -1, "failed to espp_service_client_socket_fd_new()"); FILL_SOCKET_MSG_REQUEST(data, ESPP_SERVICE_REQUEST_CREATE); + FILL_SOCKET_MSG_PARAMS(data, ESPP_SERVICE_REQUEST_CREATE, "handle", (long)espp); if (send_data(fd, &data, &result) != 0) goto error; if (result.ret != 0) { diff --git a/src/common/espp_service_common.c b/src/common/espp_service_common.c index 5d593c5..0cc98a7 100644 --- a/src/common/espp_service_common.c +++ b/src/common/espp_service_common.c @@ -17,8 +17,8 @@ #include "espp_service_common.h" espp_service_request_s requests[] = { /* str, param_formats - 'b':bool, 'i':int, 'l':int64, 'u':uint, 'k':uint64, 'd':double, 's':string) */ - [ESPP_SERVICE_REQUEST_INIT_EVENT] = { "InitEvent", NULL }, - [ESPP_SERVICE_REQUEST_CREATE] = { "Create", NULL }, + [ESPP_SERVICE_REQUEST_INIT_EVENT] = { "InitEvent", "i" }, + [ESPP_SERVICE_REQUEST_CREATE] = { "Create", "i" }, [ESPP_SERVICE_REQUEST_DESTROY] = {"Destroy", NULL }, [ESPP_SERVICE_REQUEST_OPEN] = { "Open", NULL }, [ESPP_SERVICE_REQUEST_CLOSE] = { "Close", NULL }, @@ -38,4 +38,4 @@ const char *data_type_strs[] = { [ESPP_SERVICE_DATA_TYPE_UINT64] = "uint64", [ESPP_SERVICE_DATA_TYPE_DOUBLE] = "double", [ESPP_SERVICE_DATA_TYPE_STRING] = "string", -}; \ No newline at end of file +}; diff --git a/src/daemon/espp_service.c b/src/daemon/espp_service.c index a03638f..775ad73 100644 --- a/src/daemon/espp_service.c +++ b/src/daemon/espp_service.c @@ -78,7 +78,7 @@ static void __sa_handler(int signal) sigfillset(&all_mask); sigprocmask(SIG_BLOCK, &all_mask, &old_mask); - g_hash_table_destroy(g_svc->espp_handles); + g_hash_table_destroy(g_svc->fd_table); espp_service_deinit_socket(g_svc); sigprocmask(SIG_SETMASK, &old_mask, NULL); @@ -187,11 +187,11 @@ int main(int argc, char *argv[]) if (espp_service_init_socket(&svc) != 0) goto exit; - svc.espp_handles = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, espp_service_handle_destroy_cb); + svc.fd_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, espp_service_handle_destroy_cb); run(&svc); - g_hash_table_destroy(svc.espp_handles); + g_hash_table_destroy(svc.fd_table); espp_service_deinit_socket(&svc); deinitialize_signals(); diff --git a/src/daemon/espp_service_handler.c b/src/daemon/espp_service_handler.c index 79f9a22..5a7d7d6 100644 --- a/src/daemon/espp_service_handler.c +++ b/src/daemon/espp_service_handler.c @@ -17,39 +17,69 @@ #include "espp_service_priv.h" #include -typedef void (*func_handler) (espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result); +typedef void (*func_handler) (handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result); -static void __handle_init_event(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +typedef struct { + int fd; + int event_fd; + esplusplayer_handle espp; +} tb_data_s; + +static void __handle_init_event(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { - ASSERT(svc); - ASSERT(fd >= 0); + int ret; + + ASSERT(hdata); ASSERT(data); ASSERT(result); + ASSERT(hdata->svc); + ASSERT(hdata->fd >= 0); - LOG_DEBUG("event initialization requested"); + result->ret = -1; + + ret = espp_service_msg_parse_params(data->params, data->request, &hdata->client.handle_addr); + if (ret != 0) + return; + + LOG_INFO("fd[%d], client[pid:%u, handle:0x%x]", hdata->fd, hdata->client.pid, hdata->client.handle_addr); result->ret = 0; } -static void __handle_create(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +static void __handle_create(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { + int ret; esplusplayer_handle espp; + tb_data_s *tbs; - ASSERT(svc); - ASSERT(fd >= 0); + ASSERT(hdata); ASSERT(data); ASSERT(result); + ASSERT(hdata->svc); + ASSERT(hdata->fd >= 0); result->ret = -1; + ret = espp_service_msg_parse_params(data->params, data->request, &hdata->client.handle_addr); + if (ret != 0) + return; + espp = esplusplayer_create(); ASSERT(espp); - LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_create() success", fd, espp); + hdata->espp = espp; + hdata->key = g_strdup_printf("%u_%x", hdata->client.pid, hdata->client.handle_addr); + + LOG_INFO("fd[%d], client[pid:%u, handle:0x%x], ESPP[%p]: esplusplayer_create() success", + hdata->fd, hdata->client.pid, hdata->client.handle_addr, espp); + + tbs = g_new0(tb_data_s, 1); + tbs->fd = hdata->fd; + tbs->espp = hdata->espp; - if (!g_hash_table_insert(svc->espp_handles, GINT_TO_POINTER(fd), (gpointer)espp)) { - LOG_ERROR("should not be reached here, fd[%d] already exist, ESPP[%p] will be removed", fd, espp); - g_hash_table_remove(svc->espp_handles, GINT_TO_POINTER(fd)); + if (!g_hash_table_insert(hdata->svc->fd_table, hdata->key, (gpointer)tbs)) { + LOG_ERROR("should not be reached here, key[%s] already exist", hdata->key); + g_hash_table_remove(hdata->svc->fd_table, hdata->key); esplusplayer_destroy(espp); return; } @@ -57,151 +87,151 @@ static void __handle_create(espp_service_s *svc, int fd, espp_service_data_from_ result->ret = 0; } -static void __handle_destroy(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +static void __handle_destroy(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { - esplusplayer_handle espp; - - ASSERT(svc); - ASSERT(fd >= 0); + ASSERT(hdata); ASSERT(data); ASSERT(result); + ASSERT(hdata->svc); + ASSERT(hdata->fd >= 0); result->ret = -1; - RET_IF(!(espp = g_hash_table_lookup(svc->espp_handles, GINT_TO_POINTER(fd))), "could not find ESPP by fd[%d]", fd); - RET_IF(esplusplayer_destroy(espp) != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_destroy()"); + RET_IF(!g_hash_table_lookup(hdata->svc->fd_table, hdata->key), "failed to g_hash_table_lookup(), key[%s]", hdata->key); - LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_destroy() success", fd, espp); + RET_IF(esplusplayer_destroy((esplusplayer_handle)hdata->espp) != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_destroy()"); - ASSERT(g_hash_table_steal(svc->espp_handles, GINT_TO_POINTER(fd))); + LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_destroy() success", hdata->fd, hdata->espp); + + ASSERT(g_hash_table_steal(hdata->svc->fd_table, hdata->key)); result->ret = 0; } -static void __handle_open(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +static void __handle_open(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { int ret; - esplusplayer_handle espp; - ASSERT(svc); - ASSERT(fd >= 0); + ASSERT(hdata); ASSERT(data); ASSERT(result); + ASSERT(hdata->svc); + ASSERT(hdata->fd >= 0); result->ret = -1; - RET_IF(!(espp = g_hash_table_lookup(svc->espp_handles, GINT_TO_POINTER(fd))), "failed to g_hash_table_lookup(), fd[%d]", fd); + RET_IF(!g_hash_table_lookup(hdata->svc->fd_table, hdata->key), "failed to g_hash_table_lookup(), key[%s]", hdata->key); - ret = esplusplayer_open(espp); - RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_open(), ESPP[%p]", espp); + ret = esplusplayer_open((esplusplayer_handle)hdata->espp); + RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_open(), ESPP[%p]", hdata->espp); - LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_open() success", fd, espp); + LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_open() success", hdata->fd, hdata->espp); result->ret = 0; } -static void __handle_close(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +static void __handle_close(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { int ret; - esplusplayer_handle espp; - ASSERT(svc); - ASSERT(fd >= 0); + ASSERT(hdata); ASSERT(data); ASSERT(result); + ASSERT(hdata->svc); + ASSERT(hdata->fd >= 0); result->ret = -1; - RET_IF(!(espp = g_hash_table_lookup(svc->espp_handles, GINT_TO_POINTER(fd))), "failed to g_hash_table_lookup(), fd[%d]", fd); + RET_IF(!g_hash_table_lookup(hdata->svc->fd_table, hdata->key), "failed to g_hash_table_lookup(), key[%s]", hdata->key); - ret = esplusplayer_close(espp); - RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_close(), ESPP[%p]", espp); + ret = esplusplayer_close((esplusplayer_handle)hdata->espp); + RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_close(), ESPP[%p]", hdata->espp); - LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_close() success", fd, espp); + LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_close() success", hdata->fd, hdata->espp); result->ret = 0; } -static void __handle_start(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +static void __handle_start(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { int ret; - esplusplayer_handle espp; - ASSERT(svc); - ASSERT(fd >= 0); + ASSERT(hdata); ASSERT(data); ASSERT(result); + ASSERT(hdata->svc); + ASSERT(hdata->fd >= 0); result->ret = -1; - RET_IF(!(espp = g_hash_table_lookup(svc->espp_handles, GINT_TO_POINTER(fd))), "failed to g_hash_table_lookup(), fd[%d]", fd); + RET_IF(!g_hash_table_lookup(hdata->svc->fd_table, hdata->key), "failed to g_hash_table_lookup(), key[%s]", hdata->key); - ret = esplusplayer_start(espp); - RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_start(), ESPP[%p]", espp); + ret = esplusplayer_start((esplusplayer_handle)hdata->espp); + RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_start(), ESPP[%p]", hdata->espp); - LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_start() success", fd, espp); + LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_start() success", hdata->fd, hdata->espp); result->ret = 0; } -static void __handle_stop(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +static void __handle_stop(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { int ret; - esplusplayer_handle espp; - ASSERT(svc); - ASSERT(fd >= 0); + ASSERT(hdata); ASSERT(data); ASSERT(result); + ASSERT(hdata->svc); + ASSERT(hdata->fd >= 0); result->ret = -1; - RET_IF(!(espp = g_hash_table_lookup(svc->espp_handles, GINT_TO_POINTER(fd))), "failed to g_hash_table_lookup(), fd[%d]", fd); + RET_IF(!g_hash_table_lookup(hdata->svc->fd_table, hdata->key), "failed to g_hash_table_lookup(), key[%s]", hdata->key); - ret = esplusplayer_stop(espp); - RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_stop(), ESPP[%p]", espp); + ret = esplusplayer_stop((esplusplayer_handle)hdata->espp); + RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_stop(), ESPP[%p]", hdata->espp); - LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_stop() success", fd, espp); + LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_stop() success", hdata->fd, hdata->espp); result->ret = 0; } -static void __handle_prepare_async(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +static void __handle_prepare_async(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { int ret; - esplusplayer_handle espp; - ASSERT(svc); - ASSERT(fd >= 0); + ASSERT(hdata); ASSERT(data); ASSERT(result); + ASSERT(hdata->svc); + ASSERT(hdata->fd >= 0); result->ret = -1; - RET_IF(!(espp = g_hash_table_lookup(svc->espp_handles, GINT_TO_POINTER(fd))), "failed to g_hash_table_lookup(), fd[%d]", fd); + RET_IF(!g_hash_table_lookup(hdata->svc->fd_table, hdata->key), "failed to g_hash_table_lookup(), key[%s]", hdata->key); - ret = esplusplayer_prepare_async(espp); - RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_prepare_async(), ESPP[%p]", espp); + ret = esplusplayer_prepare_async((esplusplayer_handle)hdata->espp); + RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_prepare_async(), ESPP[%p]", hdata->espp); - LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_prepare_async() success", fd, espp); + LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_prepare_async() success", hdata->fd, hdata->espp); result->ret = 0; } -static void __handle_set_audio_stream_info(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +static void __handle_set_audio_stream_info(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { int ret; - esplusplayer_handle espp; esplusplayer_audio_stream_info info; - ASSERT(svc); - ASSERT(fd >= 0); + ASSERT(hdata); ASSERT(data); ASSERT(result); + ASSERT(hdata->svc); + ASSERT(hdata->fd >= 0); result->ret = -1; - RET_IF(!(espp = g_hash_table_lookup(svc->espp_handles, GINT_TO_POINTER(fd))), "failed to g_hash_table_lookup(), fd[%d]", fd); + RET_IF(!g_hash_table_lookup(hdata->svc->fd_table, hdata->key), "failed to g_hash_table_lookup(), key[%s]", hdata->key); ret = espp_service_msg_parse_params(data->params, data->request, &info.codec_data, &info.codec_data_length, &info.mime_type, @@ -209,28 +239,28 @@ static void __handle_set_audio_stream_info(espp_service_s *svc, int fd, espp_ser if (ret != 0) return; - ret = esplusplayer_set_audio_stream_info(espp, &info); - RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_set_audio_stream_info(), ESPP[%p]", espp); + ret = esplusplayer_set_audio_stream_info((esplusplayer_handle)hdata->espp, &info); + RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_set_audio_stream_info(), ESPP[%p]", hdata->espp); - LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_set_audio_stream_info() success", fd, espp); + LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_set_audio_stream_info() success", hdata->fd, hdata->espp); result->ret = 0; } -static void __handle_set_video_stream_info(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +static void __handle_set_video_stream_info(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { int ret; - esplusplayer_handle espp; esplusplayer_video_stream_info info; - ASSERT(svc); - ASSERT(fd >= 0); + ASSERT(hdata); ASSERT(data); ASSERT(result); + ASSERT(hdata->svc); + ASSERT(hdata->fd >= 0); result->ret = -1; - RET_IF(!(espp = g_hash_table_lookup(svc->espp_handles, GINT_TO_POINTER(fd))), "failed to g_hash_table_lookup(), fd[%d]", fd); + RET_IF(!g_hash_table_lookup(hdata->svc->fd_table, hdata->key), "failed to g_hash_table_lookup(), key[%s]", hdata->key); ret = espp_service_msg_parse_params(data->params, data->request, &info.codec_data, &info.codec_data_length, &info.mime_type, @@ -239,34 +269,34 @@ static void __handle_set_video_stream_info(espp_service_s *svc, int fd, espp_ser if (ret != 0) return; - ret = esplusplayer_set_video_stream_info(espp, &info); - RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_set_video_stream_info(), ESPP[%p]", espp); + ret = esplusplayer_set_video_stream_info((esplusplayer_handle)hdata->espp, &info); + RET_IF(ret != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_set_video_stream_info(), ESPP[%p]", hdata->espp); - LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_set_video_stream_info() success", fd, espp); + LOG_INFO("fd[%d], ESPP[%p]: esplusplayer_set_video_stream_info() success", hdata->fd, hdata->espp); result->ret = 0; } -static void __handle_set_callback(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +static void __handle_set_callback(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { int ret; - esplusplayer_handle espp; espp_service_cb_type_e cb_type; - ASSERT(svc); - ASSERT(fd >= 0); + ASSERT(hdata); ASSERT(data); ASSERT(result); + ASSERT(hdata->svc); + ASSERT(hdata->fd >= 0); result->ret = -1; - RET_IF(!(espp = g_hash_table_lookup(svc->espp_handles, GINT_TO_POINTER(fd))), "failed to g_hash_table_lookup(), fd[%d]", fd); + RET_IF(!g_hash_table_lookup(hdata->svc->fd_table, hdata->key), "failed to g_hash_table_lookup(), key[%s]", hdata->key); ret = espp_service_msg_parse_params(data->params, data->request, &cb_type); if (ret != 0) return; - LOG_INFO("fd[%d], ESPP[%p]: cb_type[%d]", fd, espp, cb_type); + LOG_INFO("fd[%d], ESPP[%p]: cb_type[%d]", hdata->fd, hdata->espp, cb_type); result->ret = 0; } @@ -285,27 +315,36 @@ static func_handler handlers[] = { [ESPP_SERVICE_REQUEST_SET_CALLBACK] = __handle_set_callback, }; -int espp_service_func_handler(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) +int espp_service_func_handler(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result) { - ASSERT(svc); - ASSERT(fd >= 0); + ASSERT(hdata); + ASSERT(hdata->fd >= 0); + ASSERT(hdata->svc); ASSERT(data); ASSERT(result); - LOG_WARNING("fd[%d] data[%p, request:%s]", fd, data, requests[data->request].str); + LOG_WARNING("fd[%d] data[%p, request:%s]", hdata->fd, data, requests[data->request].str); - handlers[data->request](svc, fd, data, result); + handlers[data->request](hdata, data, result); return 0; } void espp_service_handle_destroy_cb(gpointer data) { - esplusplayer_handle espp = (esplusplayer_handle)data; + tb_data_s *tbs = (tb_data_s *)data; - ASSERT(data); + ASSERT(tbs); + + LOG_INFO("destroy tbs[%p, fd:%d, event_fd:%d, ESPP:%p]", + tbs, tbs->fd, tbs->event_fd, tbs->espp); - LOG_INFO("destroy ESPP[%p]", espp); + if (esplusplayer_stop(tbs->espp) != ESPLUSPLAYER_ERROR_TYPE_NONE) + LOG_ERROR("failed to esplusplayer_stop()"); + if (esplusplayer_close(tbs->espp) != ESPLUSPLAYER_ERROR_TYPE_NONE) + LOG_ERROR("failed to esplusplayer_close()"); + if (esplusplayer_destroy(tbs->espp) != ESPLUSPLAYER_ERROR_TYPE_NONE) + LOG_ERROR("failed to esplusplayer_destroy()"); - RET_IF(esplusplayer_destroy(espp) != ESPLUSPLAYER_ERROR_TYPE_NONE, "failed to esplusplayer_destroy()"); -} \ No newline at end of file + g_free(tbs); +} diff --git a/src/daemon/espp_service_priv.h b/src/daemon/espp_service_priv.h index cc256d3..8a841d1 100644 --- a/src/daemon/espp_service_priv.h +++ b/src/daemon/espp_service_priv.h @@ -32,12 +32,23 @@ typedef struct { GMainLoop *mainloop; int fd; pthread_t thread_id; - GHashTable *espp_handles; + GHashTable *fd_table; } espp_service_s; +typedef struct { + espp_service_s *svc; + int fd; + struct { + int handle_addr; + unsigned int pid; + } client; + void *espp; + gchar *key; +} handler_userdata_s; + int espp_service_init_socket(espp_service_s *svc); void espp_service_deinit_socket(espp_service_s *svc); -int espp_service_func_handler(espp_service_s *svc, int fd, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result); +int espp_service_func_handler(handler_userdata_s *hdata, espp_service_data_from_client_s *data, espp_service_data_from_server_s *result); void espp_service_handle_destroy_cb(gpointer data); int espp_service_msg_parse_params(const char *params, espp_service_request_e request, ...); diff --git a/src/daemon/espp_service_socket.c b/src/daemon/espp_service_socket.c index 335e02d..942c431 100644 --- a/src/daemon/espp_service_socket.c +++ b/src/daemon/espp_service_socket.c @@ -35,20 +35,18 @@ do { \ } \ } while (0) -typedef struct { - espp_service_s *svc; - int fd; - unsigned int pid; -} work_thread_userdata_s; - -static int __event_ret_msg_handling(int fd, espp_service_s *svc) +static int __event_ret_msg_handling(handler_userdata_s *hdata) { int ret; + int fd; char str_error[MAX_ERROR_LEN] = {'\0',}; espp_service_data_from_client_s rx_data; - ASSERT(fd >= 0); - ASSERT(svc); + ASSERT(hdata); + ASSERT(hdata->fd >= 0); + ASSERT(hdata->svc); + + fd = hdata->fd; memset(&rx_data, 0x00, sizeof(espp_service_data_from_client_s)); if ((ret = read(fd, &rx_data, sizeof(espp_service_data_from_client_s))) < 0) { @@ -77,17 +75,21 @@ exit: return -1; } -static int __request_msg_handling(int fd, espp_service_s *svc, bool *event_mode) +static int __request_msg_handling(handler_userdata_s *hdata, bool *event_mode) { int ret; + int fd; char str_error[MAX_ERROR_LEN] = {'\0',}; espp_service_data_from_client_s rx_data; espp_service_data_from_server_s tx_data; - ASSERT(fd >= 0); - ASSERT(svc); + ASSERT(hdata); + ASSERT(hdata->fd >= 0); + ASSERT(hdata->svc); ASSERT(event_mode); + fd = hdata->fd; + memset(&rx_data, 0x00, sizeof(espp_service_data_from_client_s)); if ((ret = read(fd, &rx_data, sizeof(espp_service_data_from_client_s))) < 0) { strerror_r(errno, str_error, sizeof(str_error)); @@ -99,7 +101,7 @@ static int __request_msg_handling(int fd, espp_service_s *svc, bool *event_mode) LOG_DEBUG("<<<<< from fd[%d]: request[%s]", fd, requests[rx_data.request].str); memset(&tx_data, 0x00, sizeof(espp_service_data_from_server_s)); - if (espp_service_func_handler(svc, fd, &rx_data, &tx_data) != 0) + if (espp_service_func_handler(hdata, &rx_data, &tx_data) != 0) LOG_ERROR("failed to espp_service_func_handler()"); LOG_DEBUG(">>>>>> to fd[%d]: ret[%d]", fd, tx_data.ret); @@ -124,33 +126,28 @@ exit: static void *__work_thread_func(void *data) { - int fd; bool event_mode = false; - work_thread_userdata_s *userdata = (work_thread_userdata_s *)data; - espp_service_s *svc; - - ASSERT(userdata); - ASSERT(userdata->fd != -1); + handler_userdata_s *hdata = (handler_userdata_s *)data; - fd = userdata->fd; - svc = userdata->svc; + ASSERT(hdata); + ASSERT(hdata->fd != -1); - LOG_WARNING("entrance, fd[%d]", fd); + LOG_WARNING("entrance, fd[%d]", hdata->fd); do { if (!event_mode) { - if (__request_msg_handling(fd, svc, &event_mode) == -1) + if (__request_msg_handling(hdata, &event_mode) == -1) goto exit; } else { - if (__event_ret_msg_handling(fd, svc) == -1) + if (__event_ret_msg_handling(hdata) == -1) goto exit; } } while (1); exit: - LOG_WARNING("exit, fd[%d]", fd); - g_free(userdata); - close(fd); + LOG_WARNING("exit, fd[%d]", hdata->fd); + close(hdata->fd); + g_free(hdata); pthread_exit(NULL); } @@ -179,7 +176,7 @@ static void *__listen_thread_func(void *data) int client_fd = -1; pthread_attr_t attr; char str_error[MAX_ERROR_LEN] = {'\0',}; - work_thread_userdata_s *userdata; + handler_userdata_s *userdata = NULL; pthread_t work_thread_id; ASSERT(svc); @@ -215,16 +212,16 @@ static void *__listen_thread_func(void *data) LOG_ERROR("failed to accept(), err: %s", str_error); goto exit; } - LOG_DEBUG("client_fd[%d]", client_fd); - userdata = g_new0(work_thread_userdata_s, 1); + userdata = g_new0(handler_userdata_s, 1); userdata->svc = svc; userdata->fd = client_fd; - userdata->pid = get_pid_from_fd(client_fd); + userdata->client.pid = get_pid_from_fd(client_fd); + + LOG_DEBUG("client[fd:%d, pid:%u]", client_fd, userdata->client.pid); if (pthread_create(&work_thread_id, &attr, (void *)__work_thread_func, (void *)userdata)) { LOG_ERROR("failed to pthread_create(), client_fd[%d]", client_fd); - g_free(userdata); goto exit; } @@ -234,6 +231,9 @@ exit: if (client_fd >= 0) close(client_fd); + if (userdata) + g_free(userdata); + LOG_DEBUG("pthread_exit()"); pthread_attr_destroy(&attr); pthread_exit(NULL); @@ -325,4 +325,4 @@ void espp_service_deinit_socket(espp_service_s *svc) LOG_DEBUG("pthread_join() done"); svc->thread_id = 0; } -} \ No newline at end of file +}