usock: msize = 0 on deconnection
parent
77120450ff
commit
cf59401ef7
|
@ -152,7 +152,7 @@ int ipc_message_format (struct ipc_message *m, char type, const char *payload, s
|
||||||
}
|
}
|
||||||
|
|
||||||
m->type = type;
|
m->type = type;
|
||||||
m->length = (short) length;
|
m->length = (unsigned int) length;
|
||||||
|
|
||||||
if (payload != NULL) {
|
if (payload != NULL) {
|
||||||
if (m->payload != NULL) {
|
if (m->payload != NULL) {
|
||||||
|
|
|
@ -53,8 +53,11 @@ int usock_recv (const int fd, char **buf, ssize_t *len)
|
||||||
do {
|
do {
|
||||||
ret = recv (fd, *buf, *len, 0);
|
ret = recv (fd, *buf, *len, 0);
|
||||||
if (msize == 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);
|
assert (msize < IPC_MAX_MESSAGE_SIZE);
|
||||||
msize_read += ret - IPC_HEADER_SIZE;
|
msize_read += ret - IPC_HEADER_SIZE;
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#define SERVICE_NAME "pongd"
|
#define SERVICE_NAME "pongd"
|
||||||
|
|
||||||
#define MAX_MESSAGE_SIZE 20000
|
#define MAX_MESSAGE_SIZE 20000
|
||||||
|
#define MESSAGE "salut ça va ?"
|
||||||
|
|
||||||
void interactive (char * service_name, char *env[])
|
void interactive (char * service_name, char *env[])
|
||||||
{
|
{
|
||||||
|
@ -117,6 +118,49 @@ void interactive (char * service_name, char *env[])
|
||||||
ipc_message_empty (&m);
|
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[])
|
int main (int argc, char *argv[], char *env[])
|
||||||
{
|
{
|
||||||
char service_name[100];
|
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]);
|
ssize_t t = strlen(argv[1]) > 100 ? 100 : strlen(argv[1]);
|
||||||
memcpy(service_name, argv[1], t);
|
memcpy(service_name, argv[1], t);
|
||||||
}
|
}
|
||||||
else {
|
else { memcpy(service_name, SERVICE_NAME, strlen(SERVICE_NAME)); }
|
||||||
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;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,10 +27,10 @@ void non_interactive (char *env[])
|
||||||
exit (EXIT_FAILURE);
|
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);
|
ipc_message_format_data (&m, MSG, strlen(MSG) +1);
|
||||||
printf ("msg to send in the client: ");
|
// printf ("msg to send in the client: ");
|
||||||
ipc_message_print (&m);
|
// ipc_message_print (&m);
|
||||||
if (ipc_application_write (&srv, &m) < 0) {
|
if (ipc_application_write (&srv, &m) < 0) {
|
||||||
handle_err("main", "application_write < 0");
|
handle_err("main", "application_write < 0");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
|
@ -45,6 +45,7 @@ void non_interactive (char *env[])
|
||||||
printf ("msg recv: %s\n", m.payload);
|
printf ("msg recv: %s\n", m.payload);
|
||||||
ipc_message_empty (&m);
|
ipc_message_empty (&m);
|
||||||
|
|
||||||
|
sleep(10);
|
||||||
if (ipc_application_close (&srv) < 0) {
|
if (ipc_application_close (&srv) < 0) {
|
||||||
handle_err("main", "application_close < 0");
|
handle_err("main", "application_close < 0");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
|
@ -58,8 +59,8 @@ void interactive (char *env[])
|
||||||
struct ipc_service srv;
|
struct ipc_service srv;
|
||||||
memset (&srv, 0, sizeof (struct ipc_service));
|
memset (&srv, 0, sizeof (struct ipc_service));
|
||||||
|
|
||||||
char buf[BUFSIZ];
|
char buf[IPC_MAX_MESSAGE_SIZE];
|
||||||
memset (buf, 0, BUFSIZ);
|
memset (buf, 0, IPC_MAX_MESSAGE_SIZE);
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
int ask_server_to_quit = 0;
|
int ask_server_to_quit = 0;
|
||||||
|
@ -77,7 +78,7 @@ void interactive (char *env[])
|
||||||
while (1) {
|
while (1) {
|
||||||
printf ("msg to send: ");
|
printf ("msg to send: ");
|
||||||
fflush (stdout);
|
fflush (stdout);
|
||||||
n = read (0, buf, BUFSIZ);
|
n = read (0, buf, IPC_MAX_MESSAGE_SIZE);
|
||||||
|
|
||||||
if (n == 0 || strncmp (buf, "exit", 4) == 0)
|
if (n == 0 || strncmp (buf, "exit", 4) == 0)
|
||||||
break;
|
break;
|
||||||
|
@ -88,7 +89,7 @@ void interactive (char *env[])
|
||||||
}
|
}
|
||||||
|
|
||||||
ipc_message_format_data (&m, buf, strlen(buf) +1);
|
ipc_message_format_data (&m, buf, strlen(buf) +1);
|
||||||
memset (buf, 0, BUFSIZ);
|
memset (buf, 0, IPC_MAX_MESSAGE_SIZE);
|
||||||
|
|
||||||
// print_msg (&m);
|
// print_msg (&m);
|
||||||
|
|
||||||
|
|
|
@ -92,7 +92,7 @@ void handle_new_msg (struct ipc_clients *clients, struct ipc_clients *clients_ta
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m.length > 0) {
|
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) {
|
if (ipc_server_write (pc, &m) < 0) {
|
||||||
handle_err( "handle_new_msg", "server_write < 0");
|
handle_err( "handle_new_msg", "server_write < 0");
|
||||||
|
|
Reference in New Issue