1
0
mirror of https://github.com/haiwen/libsearpc.git synced 2025-08-19 12:57:01 +00:00

Merge pull request #47 from haiwen/optimize_named_pipe_server

Optimize name pipe server.
This commit is contained in:
Jiaqiang Xu 2019-10-19 14:28:44 +08:00 committed by GitHub
commit 501fa31d03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 8 deletions

View File

@ -33,7 +33,9 @@ static char* formatErrorMessage();
#endif // defined(WIN32) #endif // defined(WIN32)
static void* named_pipe_listen(void *arg); static void* named_pipe_listen(void *arg);
static void named_pipe_client_handler(void *data, void *user_data); static void* handle_named_pipe_client_with_thread (void *arg);
static void handle_named_pipe_client_with_threadpool(void *data, void *user_data);
static void named_pipe_client_handler (void *data);
static char* searpc_named_pipe_send(void *arg, const gchar *fcall_str, size_t fcall_len, size_t *ret_len); static char* searpc_named_pipe_send(void *arg, const gchar *fcall_str, size_t fcall_len, size_t *ret_len);
static char * request_to_json(const char *service, const char *fcall_str, size_t fcall_len); static char * request_to_json(const char *service, const char *fcall_str, size_t fcall_len);
@ -71,13 +73,21 @@ SearpcNamedPipeClient* searpc_create_named_pipe_client(const char *path)
return client; return client;
} }
SearpcNamedPipeServer* searpc_create_named_pipe_server(const char *path, int named_pipe_server_thread_pool_size) SearpcNamedPipeServer* searpc_create_named_pipe_server(const char *path)
{
SearpcNamedPipeServer *server = g_malloc0(sizeof(SearpcNamedPipeServer));
memcpy(server->path, path, strlen(path) + 1);
return server;
}
SearpcNamedPipeServer* searpc_create_named_pipe_server_with_threadpool (const char *path, int named_pipe_server_thread_pool_size)
{ {
GError *error = NULL; GError *error = NULL;
SearpcNamedPipeServer *server = g_malloc0(sizeof(SearpcNamedPipeServer)); SearpcNamedPipeServer *server = g_malloc0(sizeof(SearpcNamedPipeServer));
memcpy(server->path, path, strlen(path) + 1); memcpy(server->path, path, strlen(path) + 1);
server->named_pipe_server_thread_pool = g_thread_pool_new (named_pipe_client_handler, server->named_pipe_server_thread_pool = g_thread_pool_new (handle_named_pipe_client_with_threadpool,
NULL, NULL,
named_pipe_server_thread_pool_size, named_pipe_server_thread_pool_size,
FALSE, FALSE,
@ -92,6 +102,7 @@ SearpcNamedPipeServer* searpc_create_named_pipe_server(const char *path, int nam
g_free (server); g_free (server);
return NULL; return NULL;
} }
return server; return server;
} }
@ -169,7 +180,15 @@ static void* named_pipe_listen(void *arg)
int connfd = accept (server->pipe_fd, NULL, 0); int connfd = accept (server->pipe_fd, NULL, 0);
ServerHandlerData *data = g_malloc(sizeof(ServerHandlerData)); ServerHandlerData *data = g_malloc(sizeof(ServerHandlerData));
data->connfd = connfd; data->connfd = connfd;
g_thread_pool_push (server->named_pipe_server_thread_pool, data, NULL); if (server->named_pipe_server_thread_pool)
g_thread_pool_push (server->named_pipe_server_thread_pool, data, NULL);
else {
pthread_t handler;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&handler, &attr, handle_named_pipe_client_with_thread, data);
}
} }
#else // !defined(WIN32) #else // !defined(WIN32)
@ -208,13 +227,33 @@ static void* named_pipe_listen(void *arg)
ServerHandlerData *data = g_malloc(sizeof(ServerHandlerData)); ServerHandlerData *data = g_malloc(sizeof(ServerHandlerData));
data->connfd = connfd; data->connfd = connfd;
g_thread_pool_push (server->named_pipe_server_thread_pool, data, NULL); if (server->named_pipe_server_thread_pool)
g_thread_pool_push (server->named_pipe_server_thread_pool, data, NULL);
else {
pthread_t handler;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&handler, &attr, handle_named_pipe_client_with_thread, data);
}
} }
#endif // !defined(WIN32) #endif // !defined(WIN32)
return NULL; return NULL;
} }
static void named_pipe_client_handler(void *data, void *user_data) static void* handle_named_pipe_client_with_thread(void *arg)
{
named_pipe_client_handler(arg);
return NULL;
}
static void handle_named_pipe_client_with_threadpool(void *data, void *user_data)
{
named_pipe_client_handler(data);
}
static void named_pipe_client_handler(void *data)
{ {
ServerHandlerData *handler_data = data; ServerHandlerData *handler_data = data;
SearpcNamedPipe connfd = handler_data->connfd; SearpcNamedPipe connfd = handler_data->connfd;

View File

@ -36,7 +36,9 @@ struct _SearpcNamedPipeServer {
typedef struct _SearpcNamedPipeServer SearpcNamedPipeServer; typedef struct _SearpcNamedPipeServer SearpcNamedPipeServer;
SearpcNamedPipeServer* searpc_create_named_pipe_server(const char *path, int named_pipe_server_thread_pool_size); SearpcNamedPipeServer* searpc_create_named_pipe_server(const char *path);
SearpcNamedPipeServer* searpc_create_named_pipe_server_with_threadpool(const char *path, int named_pipe_server_thread_pool_size);
int searpc_named_pipe_server_start(SearpcNamedPipeServer *server); int searpc_named_pipe_server_start(SearpcNamedPipeServer *server);

View File

@ -547,7 +547,7 @@ test_searpc__initialize (void)
client->async_send = sample_async_send; client->async_send = sample_async_send;
client->async_arg = "test_async"; client->async_arg = "test_async";
SearpcNamedPipeServer *pipe_server = searpc_create_named_pipe_server(pipe_path, NAMED_PIPE_SERVER_THREAD_POOL_SIZE); SearpcNamedPipeServer *pipe_server = searpc_create_named_pipe_server_with_threadpool(pipe_path, NAMED_PIPE_SERVER_THREAD_POOL_SIZE);
cl_must_pass_(searpc_named_pipe_server_start(pipe_server), "named pipe server failed to start"); cl_must_pass_(searpc_named_pipe_server_start(pipe_server), "named pipe server failed to start");
#if defined(WIN32) #if defined(WIN32)
// Wait for the server thread to start // Wait for the server thread to start