1
0
mirror of https://github.com/haiwen/libsearpc.git synced 2025-08-22 14:15:51 +00:00

Merge pull request #41 from haiwen/add_named_pipe_server_thread_pool

Add named pipe server thread pool.
This commit is contained in:
Jiaqiang Xu 2019-09-11 14:55:28 +08:00 committed by GitHub
commit 5b21fe6e69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 18 deletions

View File

@ -33,7 +33,7 @@ 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 *arg); static void named_pipe_client_handler(void *data, void *user_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,10 +71,27 @@ SearpcNamedPipeClient* searpc_create_named_pipe_client(const char *path)
return client; return client;
} }
SearpcNamedPipeServer* searpc_create_named_pipe_server(const char *path) SearpcNamedPipeServer* searpc_create_named_pipe_server(const char *path, int named_pipe_server_thread_pool_size)
{ {
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,
NULL,
named_pipe_server_thread_pool_size,
FALSE,
&error);
if (!server->named_pipe_server_thread_pool) {
if (error) {
g_warning ("Falied to create named pipe server thread pool : %s\n", error->message);
g_clear_error (&error);
} else {
g_warning ("Falied to create named pipe server thread pool.\n");
}
g_free (server);
return NULL;
}
return server; return server;
} }
@ -151,14 +168,10 @@ static void* named_pipe_listen(void *arg)
#if !defined(WIN32) #if !defined(WIN32)
while (1) { while (1) {
int connfd = accept (server->pipe_fd, NULL, 0); int connfd = accept (server->pipe_fd, NULL, 0);
pthread_t *handler = g_malloc(sizeof(pthread_t));
ServerHandlerData *data = g_malloc(sizeof(ServerHandlerData)); ServerHandlerData *data = g_malloc(sizeof(ServerHandlerData));
data->server = server; data->server = server;
data->connfd = connfd; data->connfd = connfd;
// TODO(low priority): Instead of using a thread to handle each client, g_thread_pool_push (server->named_pipe_server_thread_pool, data, NULL);
// use select(unix)/iocp(windows) to do it.
pthread_create(handler, NULL, named_pipe_client_handler, data);
server->handlers = g_list_append(server->handlers, handler);
} }
#else // !defined(WIN32) #else // !defined(WIN32)
@ -195,24 +208,20 @@ static void* named_pipe_listen(void *arg)
/* g_debug ("Accepted a named pipe client\n"); */ /* g_debug ("Accepted a named pipe client\n"); */
pthread_t *handler = g_malloc(sizeof(pthread_t));
ServerHandlerData *data = g_malloc(sizeof(ServerHandlerData)); ServerHandlerData *data = g_malloc(sizeof(ServerHandlerData));
data->server = server; data->server = server;
data->connfd = connfd; data->connfd = connfd;
// TODO(low priority): Instead of using a thread to handle each client, g_thread_pool_push (server->named_pipe_server_thread_pool, data, NULL);
// use select(unix)/iocp(windows) to do it.
pthread_create(handler, NULL, named_pipe_client_handler, data);
server->handlers = g_list_append(server->handlers, handler);
} }
#endif // !defined(WIN32) #endif // !defined(WIN32)
return NULL; return NULL;
} }
static void* named_pipe_client_handler(void *arg) static void named_pipe_client_handler(void *data, void *user_data)
{ {
ServerHandlerData *data = arg; ServerHandlerData *handler_data = data;
// SearpcNamedPipeServer *server = data->server; // SearpcNamedPipeServer *server = data->server;
SearpcNamedPipe connfd = data->connfd; SearpcNamedPipe connfd = handler_data->connfd;
guint32 len; guint32 len;
guint32 bufsize = 4096; guint32 bufsize = 4096;
@ -275,8 +284,6 @@ static void* named_pipe_client_handler(void *arg)
DisconnectNamedPipe(connfd); DisconnectNamedPipe(connfd);
CloseHandle(connfd); CloseHandle(connfd);
#endif // !defined(WIN32) #endif // !defined(WIN32)
return NULL;
} }

View File

@ -32,11 +32,12 @@ struct _SearpcNamedPipeServer {
pthread_t listener_thread; pthread_t listener_thread;
GList *handlers; GList *handlers;
SearpcNamedPipe pipe_fd; SearpcNamedPipe pipe_fd;
GThreadPool *named_pipe_server_thread_pool;
}; };
typedef struct _SearpcNamedPipeServer SearpcNamedPipeServer; typedef struct _SearpcNamedPipeServer SearpcNamedPipeServer;
SearpcNamedPipeServer* searpc_create_named_pipe_server(const char *path); SearpcNamedPipeServer* searpc_create_named_pipe_server(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);