user_type
This commit is contained in:
parent
75c00d229e
commit
a8d0a82adf
@ -35,7 +35,7 @@ void non_interactive (char *env[])
|
||||
}
|
||||
|
||||
printf ("msg to send (%ld): %.*s\n", (ssize_t) strlen(MSG) +1, (int) strlen(MSG), MSG);
|
||||
ret = ipc_message_format_data (&m, MSG, (ssize_t) strlen(MSG) +1);
|
||||
ret = ipc_message_format_data (&m, 42, MSG, (ssize_t) strlen(MSG) +1);
|
||||
if (ret != IPC_ERROR_NONE) {
|
||||
PRINTERR(ret, "message format data");
|
||||
exit (EXIT_FAILURE);
|
||||
@ -58,7 +58,7 @@ void non_interactive (char *env[])
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf ("msg recv: %s\n", m.payload);
|
||||
printf ("msg recv (type: %u): %s\n", m.user_type, m.payload);
|
||||
ipc_message_empty (&m);
|
||||
|
||||
ret = ipc_close (&srv);
|
||||
|
@ -1,4 +0,0 @@
|
||||
#ifndef __IPC_COMMUNICATION_H__
|
||||
#define __IPC_COMMUNICATION_H__
|
||||
|
||||
#endif
|
10
src/ipc.h
10
src/ipc.h
@ -11,7 +11,7 @@
|
||||
|
||||
#define RUNDIR "/run/ipc/"
|
||||
#define PATH_MAX 4096
|
||||
#define IPC_HEADER_SIZE 5
|
||||
#define IPC_HEADER_SIZE 6
|
||||
#define IPC_MAX_MESSAGE_SIZE 8000000-IPC_HEADER_SIZE
|
||||
// #define IPC_MAX_MESSAGE_SIZE 100-IPC_HEADER_SIZE
|
||||
// #include "queue.h"
|
||||
@ -146,6 +146,7 @@ struct ipc_connection_infos {
|
||||
|
||||
struct ipc_message {
|
||||
char type;
|
||||
char user_type;
|
||||
uint32_t length;
|
||||
char *payload;
|
||||
};
|
||||
@ -211,7 +212,6 @@ enum ipc_errors ipc_connection (char **env, struct ipc_connection_info *srv, c
|
||||
|
||||
enum ipc_errors ipc_server_close (struct ipc_connection_info *srv);
|
||||
enum ipc_errors ipc_close (struct ipc_connection_info *p);
|
||||
enum ipc_errors ipc_accept (struct ipc_connection_info *srv, struct ipc_connection_info *p);
|
||||
|
||||
enum ipc_errors ipc_read (const struct ipc_connection_info *, struct ipc_message *m);
|
||||
enum ipc_errors ipc_write (const struct ipc_connection_info *, const struct ipc_message *m);
|
||||
@ -259,8 +259,10 @@ enum ipc_errors ipc_message_read (int32_t fd, struct ipc_message *m);
|
||||
// write a structure msg to fd
|
||||
enum ipc_errors ipc_message_write (int32_t fd, const struct ipc_message *m);
|
||||
|
||||
enum ipc_errors ipc_message_format (struct ipc_message *m, char type, const char *payload, ssize_t length);
|
||||
enum ipc_errors ipc_message_format_data (struct ipc_message *m, const char *payload, ssize_t length);
|
||||
enum ipc_errors ipc_message_format (struct ipc_message *m
|
||||
, char type, char utype, const char *payload, ssize_t length);
|
||||
enum ipc_errors ipc_message_format_data (struct ipc_message *m
|
||||
, char utype, const char *payload, ssize_t length);
|
||||
enum ipc_errors ipc_message_format_server_close (struct ipc_message *m);
|
||||
|
||||
enum ipc_errors ipc_message_empty (struct ipc_message *m);
|
||||
|
@ -56,8 +56,11 @@ enum ipc_errors ipc_message_format_read (struct ipc_message *m, const char *buf,
|
||||
return IPC_ERROR_MESSAGE_FORMAT_READ__MESSAGE_SIZE;
|
||||
}
|
||||
|
||||
// message format:
|
||||
// Type (1 B) | Length (4 B) | UserType (1 B) | Payload (Length B)
|
||||
m->type = buf[0];
|
||||
memcpy (&m->length, buf+1, sizeof m->length);
|
||||
m->user_type = buf[1 + sizeof m->length];
|
||||
|
||||
assert (m->length <= IPC_MAX_MESSAGE_SIZE);
|
||||
#if defined(IPC_WITH_ERRORS) && IPC_WITH_ERRORS > 2
|
||||
@ -106,6 +109,7 @@ enum ipc_errors ipc_message_format_write (const struct ipc_message *m, char **bu
|
||||
|
||||
buffer[0] = m->type;
|
||||
memcpy (buffer + 1, &m->length, sizeof m->length);
|
||||
buffer[1 + sizeof m->length] = m->user_type;
|
||||
if (m->payload != NULL) {
|
||||
memcpy (buffer + IPC_HEADER_SIZE, m->payload, m->length);
|
||||
}
|
||||
@ -185,7 +189,8 @@ enum ipc_errors ipc_message_write (int32_t fd, const struct ipc_message *m)
|
||||
|
||||
// MSG FORMAT
|
||||
|
||||
enum ipc_errors ipc_message_format (struct ipc_message *m, char type, const char *payload, ssize_t length)
|
||||
enum ipc_errors ipc_message_format (struct ipc_message *m
|
||||
, char type, char utype, const char *payload, ssize_t length)
|
||||
{
|
||||
assert (m != NULL);
|
||||
assert (length <= IPC_MAX_MESSAGE_SIZE);
|
||||
@ -206,6 +211,7 @@ enum ipc_errors ipc_message_format (struct ipc_message *m, char type, const char
|
||||
}
|
||||
|
||||
m->type = type;
|
||||
m->user_type = utype;
|
||||
m->length = (uint32_t) length;
|
||||
|
||||
if (payload != NULL) {
|
||||
@ -226,14 +232,14 @@ enum ipc_errors ipc_message_format (struct ipc_message *m, char type, const char
|
||||
return IPC_ERROR_NONE;
|
||||
}
|
||||
|
||||
enum ipc_errors ipc_message_format_data (struct ipc_message *m, const char *payload, ssize_t length)
|
||||
enum ipc_errors ipc_message_format_data (struct ipc_message *m, char utype, const char *payload, ssize_t length)
|
||||
{
|
||||
return ipc_message_format (m, MSG_TYPE_DATA, payload, length);
|
||||
return ipc_message_format (m, MSG_TYPE_DATA, utype, payload, length);
|
||||
}
|
||||
|
||||
enum ipc_errors ipc_message_format_server_close (struct ipc_message *m)
|
||||
{
|
||||
return ipc_message_format (m, MSG_TYPE_SERVER_CLOSE, NULL, 0);
|
||||
return ipc_message_format (m, MSG_TYPE_SERVER_CLOSE, 0, NULL, 0);
|
||||
}
|
||||
|
||||
enum ipc_errors ipc_message_empty (struct ipc_message *m)
|
||||
|
Reference in New Issue
Block a user