Skip to content

Commit

Permalink
Merge pull request #40 from haiwen/fix-named-pipe-transport
Browse files Browse the repository at this point in the history
Fix named pipe transport
  • Loading branch information
killing authored Jul 19, 2019
2 parents c161cb9 + 2e1f32b commit 2803f8d
Showing 1 changed file with 17 additions and 41 deletions.
58 changes: 17 additions & 41 deletions lib/searpc-named-pipe-transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ int searpc_named_pipe_server_start(SearpcNamedPipeServer *server)
}

if (g_file_test (un_path, G_FILE_TEST_EXISTS)) {
g_debug ("socket file exists, delete it anyway\n");
g_message ("socket file exists, delete it anyway\n");
if (g_unlink (un_path) < 0) {
g_warning ("delete socket file failed : %s\n", strerror(errno));
goto failed;
Expand Down Expand Up @@ -193,7 +193,7 @@ static void* named_pipe_listen(void *arg)
break;
}

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));
Expand All @@ -218,17 +218,17 @@ static void* named_pipe_client_handler(void *arg)
guint32 bufsize = 4096;
char *buf = g_malloc(bufsize);

g_debug ("start to serve on pipe client\n");
g_message ("start to serve on pipe client\n");

while (1) {
len = 0;
if (pipe_read_n(connfd, &len, sizeof(guint32)) < 0) {
g_warning("failed to read rpc request size: %s", strerror(errno));
g_warning("failed to read rpc request size: %s\n", strerror(errno));
break;
}

if (len == 0) {
g_debug("EOF reached, pipe connection lost");
/* g_debug("EOF reached, pipe connection lost"); */
break;
}

Expand All @@ -238,7 +238,7 @@ static void* named_pipe_client_handler(void *arg)
}

if (pipe_read_n(connfd, buf, len) < 0 || len == 0) {
g_warning("failed to read rpc request: %s", strerror(errno));
g_warning("failed to read rpc request: %s\n", strerror(errno));
g_free (buf);
break;
}
Expand All @@ -255,13 +255,13 @@ static void* named_pipe_client_handler(void *arg)

len = (guint32)ret_len;
if (pipe_write_n(connfd, &len, sizeof(guint32)) < 0) {
g_warning("failed to send rpc response(%s): %s", ret_str, strerror(errno));
g_warning("failed to send rpc response(%s): %s\n", ret_str, strerror(errno));
g_free (ret_str);
break;
}

if (pipe_write_n(connfd, ret_str, ret_len) < 0) {
g_warning("failed to send rpc response: %s", strerror(errno));
g_warning("failed to send rpc response: %s\n", strerror(errno));
g_free (ret_str);
break;
}
Expand Down Expand Up @@ -320,7 +320,7 @@ int searpc_named_pipe_client_connect(SearpcNamedPipeClient *client)

#endif // !defined(WIN32)

g_debug ("pipe client connected to server\n");
/* g_debug ("pipe client connected to server\n"); */
return 0;
}

Expand All @@ -341,36 +341,36 @@ void searpc_free_client_with_pipe_transport (SearpcClient *client)
char *searpc_named_pipe_send(void *arg, const gchar *fcall_str,
size_t fcall_len, size_t *ret_len)
{
g_debug ("searpc_named_pipe_send is called\n");
/* g_debug ("searpc_named_pipe_send is called\n"); */
ClientTransportData *data = arg;
SearpcNamedPipeClient *client = data->client;

char *json_str = request_to_json(data->service, fcall_str, fcall_len);
guint32 len = (guint32)strlen(json_str);

if (pipe_write_n(client->pipe_fd, &len, sizeof(guint32)) < 0) {
g_warning("failed to send rpc call: %s", strerror(errno));
g_warning("failed to send rpc call: %s\n", strerror(errno));
free (json_str);
return NULL;
}

if (pipe_write_n(client->pipe_fd, json_str, len) < 0) {
g_warning("failed to send rpc call: %s", strerror(errno));
g_warning("failed to send rpc call: %s\n", strerror(errno));
free (json_str);
return NULL;
}

free (json_str);

if (pipe_read_n(client->pipe_fd, &len, sizeof(guint32)) < 0) {
g_warning("failed to read rpc response: %s", strerror(errno));
g_warning("failed to read rpc response: %s\n", strerror(errno));
return NULL;
}

char *buf = g_malloc(len);

if (pipe_read_n(client->pipe_fd, buf, len) < 0) {
g_warning("failed to read rpc response: %s", strerror(errno));
g_warning("failed to read rpc response: %s\n", strerror(errno));
g_free (buf);
return NULL;
}
Expand Down Expand Up @@ -529,31 +529,6 @@ ssize_t pipe_write_n(SearpcNamedPipe fd, const void *vptr, size_t n)
return 0;
}

static char *locale_to_utf8 (const gchar *src)
{
if (!src)
return NULL;

gsize bytes_read = 0;
gsize bytes_written = 0;
GError *error = NULL;
gchar *dst = NULL;

dst = g_locale_to_utf8
(src, /* locale specific string */
strlen(src), /* len of src */
&bytes_read, /* length processed */
&bytes_written, /* output length */
&error);

if (error) {
return NULL;
}

return dst;
}


// http://stackoverflow.com/questions/3006229/get-a-text-from-the-error-code-returns-from-the-getlasterror-function
// The caller is responsible to free the returned message.
char* formatErrorMessage()
Expand All @@ -566,11 +541,12 @@ char* formatErrorMessage()
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
/* EN_US */
MAKELANGID(LANG_ENGLISH, 0x01),
buf,
sizeof(buf) - 1,
NULL);
return locale_to_utf8(buf);
return g_strdup(buf);
}

#endif // !defined(WIN32)

0 comments on commit 2803f8d

Please sign in to comment.