From cf59401ef77b39aef23ef6d68a153c17e84d8d62 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Fri, 12 Oct 2018 18:06:16 +0200
Subject: [PATCH] usock: msize = 0 on deconnection
---
core/message.c | 2 +-
core/usocket.c | 5 +++-
pong/app/ipc-debug.c | 59 +++++++++++++++++++++++++++++++++++++++++---
pong/app/pong.c | 15 +++++------
pong/app/pongd.c | 2 +-
5 files changed, 69 insertions(+), 14 deletions(-)
diff --git a/core/message.c b/core/message.c
index 089fe0d..25b50c7 100644
--- a/core/message.c
+++ b/core/message.c
@@ -152,7 +152,7 @@ int ipc_message_format (struct ipc_message *m, char type, const char *payload, s
}
m->type = type;
- m->length = (short) length;
+ m->length = (unsigned int) length;
if (payload != NULL) {
if (m->payload != NULL) {
diff --git a/core/usocket.c b/core/usocket.c
index 1e20c27..cf45b4c 100644
--- a/core/usocket.c
+++ b/core/usocket.c
@@ -53,8 +53,11 @@ int usock_recv (const int fd, char **buf, ssize_t *len)
do {
ret = recv (fd, *buf, *len, 0);
if (msize == 0) {
- memcpy (&msize, *buf + 1, sizeof msize);
+ if (ret != 0) {
+ memcpy (&msize, *buf + 1, sizeof msize);
+ }
}
+ printf("msize = %u\n", msize);
assert (msize < IPC_MAX_MESSAGE_SIZE);
msize_read += ret - IPC_HEADER_SIZE;
diff --git a/pong/app/ipc-debug.c b/pong/app/ipc-debug.c
index 800ecf0..8bd16c3 100644
--- a/pong/app/ipc-debug.c
+++ b/pong/app/ipc-debug.c
@@ -11,6 +11,7 @@
#define SERVICE_NAME "pongd"
#define MAX_MESSAGE_SIZE 20000
+#define MESSAGE "salut ça va ?"
void interactive (char * service_name, char *env[])
{
@@ -117,6 +118,49 @@ void interactive (char * service_name, char *env[])
ipc_message_empty (&m);
}
+void non_interactive (char msg_type, char *msg, char * service_name, char *env[])
+{
+ struct ipc_message m;
+ memset (&m, 0, sizeof (struct ipc_message));
+ struct ipc_service srv;
+ memset (&srv, 0, sizeof (struct ipc_service));
+
+ // index and version should be filled
+ srv.index = 0;
+ srv.version = 0;
+
+ // init service
+ if (ipc_application_connection (env, &srv, service_name) < 0) {
+ handle_err ("main", "ipc_application_connection < 0");
+ exit (EXIT_FAILURE);
+ }
+
+ ipc_message_format (&m, msg_type, msg, strlen(msg) + 1);
+ // print_msg (&m);
+
+ if (ipc_application_write (&srv, &m) < 0) {
+ handle_err("main", "application_write < 0");
+ exit (EXIT_FAILURE);
+ }
+ ipc_message_empty (&m);
+
+ if (ipc_application_read (&srv, &m) < 0) {
+ handle_err("main", "application_read < 0");
+ exit (EXIT_FAILURE);
+ }
+
+ if (m.length > 0) {
+ printf ("msg recv: %.*s\n", m.length, m.payload);
+ }
+ ipc_message_empty (&m);
+
+ if (ipc_application_close (&srv) < 0) {
+ handle_err("main", "application_close < 0");
+ exit (EXIT_FAILURE);
+ }
+ ipc_message_empty (&m);
+}
+
int main (int argc, char *argv[], char *env[])
{
char service_name[100];
@@ -126,11 +170,18 @@ int main (int argc, char *argv[], char *env[])
ssize_t t = strlen(argv[1]) > 100 ? 100 : strlen(argv[1]);
memcpy(service_name, argv[1], t);
}
- else {
- memcpy(service_name, SERVICE_NAME, strlen(SERVICE_NAME));
- }
+ else { memcpy(service_name, SERVICE_NAME, strlen(SERVICE_NAME)); }
- interactive (service_name, env);
+ char mtype = 2;
+ if (argc > 2) { mtype = atoi(argv[2]); }
+
+ char msg[IPC_MAX_MESSAGE_SIZE];
+ memset(msg, 0, IPC_MAX_MESSAGE_SIZE);
+
+ if (argc > 3) { memcpy(msg, argv[3], strlen(argv[3])); }
+ else { memcpy(msg, MESSAGE, strlen(MESSAGE)); }
+
+ non_interactive (mtype, msg, service_name, env);
return EXIT_SUCCESS;
}
diff --git a/pong/app/pong.c b/pong/app/pong.c
index 39196c8..c4b747f 100644
--- a/pong/app/pong.c
+++ b/pong/app/pong.c
@@ -27,10 +27,10 @@ void non_interactive (char *env[])
exit (EXIT_FAILURE);
}
- printf ("msg to send: %s\n", MSG);
+ printf ("msg to send: %.*s\n", (int) strlen(MSG), MSG);
ipc_message_format_data (&m, MSG, strlen(MSG) +1);
- printf ("msg to send in the client: ");
- ipc_message_print (&m);
+ // printf ("msg to send in the client: ");
+ // ipc_message_print (&m);
if (ipc_application_write (&srv, &m) < 0) {
handle_err("main", "application_write < 0");
exit (EXIT_FAILURE);
@@ -45,6 +45,7 @@ void non_interactive (char *env[])
printf ("msg recv: %s\n", m.payload);
ipc_message_empty (&m);
+ sleep(10);
if (ipc_application_close (&srv) < 0) {
handle_err("main", "application_close < 0");
exit (EXIT_FAILURE);
@@ -58,8 +59,8 @@ void interactive (char *env[])
struct ipc_service srv;
memset (&srv, 0, sizeof (struct ipc_service));
- char buf[BUFSIZ];
- memset (buf, 0, BUFSIZ);
+ char buf[IPC_MAX_MESSAGE_SIZE];
+ memset (buf, 0, IPC_MAX_MESSAGE_SIZE);
int n;
int ask_server_to_quit = 0;
@@ -77,7 +78,7 @@ void interactive (char *env[])
while (1) {
printf ("msg to send: ");
fflush (stdout);
- n = read (0, buf, BUFSIZ);
+ n = read (0, buf, IPC_MAX_MESSAGE_SIZE);
if (n == 0 || strncmp (buf, "exit", 4) == 0)
break;
@@ -88,7 +89,7 @@ void interactive (char *env[])
}
ipc_message_format_data (&m, buf, strlen(buf) +1);
- memset (buf, 0, BUFSIZ);
+ memset (buf, 0, IPC_MAX_MESSAGE_SIZE);
// print_msg (&m);
diff --git a/pong/app/pongd.c b/pong/app/pongd.c
index 97312a5..4c69d82 100644
--- a/pong/app/pongd.c
+++ b/pong/app/pongd.c
@@ -92,7 +92,7 @@ void handle_new_msg (struct ipc_clients *clients, struct ipc_clients *clients_ta
}
if (m.length > 0) {
- printf ("new message : %.*s", m.length, m.payload);
+ printf ("new message : %.*s\n", m.length, m.payload);
}
if (ipc_server_write (pc, &m) < 0) {
handle_err( "handle_new_msg", "server_write < 0");