mirror of
https://github.com/haiwen/libsearpc.git
synced 2025-08-19 04:47:09 +00:00
Optimize name pipe server.
This commit is contained in:
parent
fbbdafd2ab
commit
f7c20fa391
@ -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;
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user