-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc.c
368 lines (325 loc) · 10.5 KB
/
ipc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/******************************************************************************
* Copyright (C) Dean Zou 2016-2017
* file: libipc.c
* author: Dean Zou <[email protected]>
* created: 2017-08-02 16:29:41
* updated: 2017-08-02 16:29:41
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "log.h"
#include "libipc.h"
#include "ipc_private.h"
#include "msgq.h"
list_node *ipc_ctx_list = NULL; //Global value for IPC list.
/**
* msg_init() - init
* @module:
* @cflag:
* @receive:
* Return: 0 on success, a negative error code on failure.
*/
ipc_ctx *msg_init(const char *module,int cflag,recv_cb receive)
{
ipc_ctx *ipc_ctx = NULL;
char shm_rm_cmd[128] ={};
if(strlen(module) < 1){
log_error("Module name must be greater then one character.\n");
return NULL;
}
log_info("Module: %s %d initializing...\n",module,cflag);
if(ipc_ctx_list != NULL){
if(list_find(ipc_ctx_list,list_node_compare,module) == NULL){
log_error("%s has already initialized.\n",module);
return NULL;
}
}
else{
ipc_ctx = (struct ipc_ctx*)calloc(1,sizeof(struct ipc_ctx));
if(ipc_ctx == NULL){
log_error("Module: %s init failed, memory is not enough.\n",module);
return NULL;
}
}
snprintf(ipc_ctx->module, sizeof(ipc_ctx->module), "/%s", module);
//Remove shared memory from system before starting message queues.
snprintf(shm_rm_cmd,sizeof(shm_rm_cmd),"rm -rf /dev/shm/%s.*",module);
system(shm_rm_cmd);
ipc_ctx->mq_ctx = _mq_init(ipc_ctx->module,(mq_recv_cb)receive,msg_recv_callback,IPC_RECEIVE);
if(ipc_ctx->mq_ctx == NULL){
free(ipc_ctx);
log_error("Module: %s init failed, message queues init failed.\n",module);
return NULL;
}
if(ipc_ctx->mq_ctx->mqdes == -1){
free(ipc_ctx);
log_error("Module: %s init failed, message queues init failed.\n",module);
}
ipc_ctx->msg_prio = 10;
ipc_ctx_list = list_create(ipc_ctx);
log_info("Module: %s initializing successfully...\n",module);
return ipc_ctx;
}
/**
* msg_wait() - init
* @module:
* @timespec:
*
*
*
* Return: 0 on success, a negative error code on failure.
*/
int msg_wait(ipc_ctx *ipc_ctx,const char *module,const struct timespec *abs_timeout)
{
return 0;
}
/**
* msg_send() - init
* @module:
* @timespec:
*
*
*
* Return: 0 on success, a negative error code on failure.
*/
int msg_send_free(ipc_ctx *ipc_ctx,const char *module,struct msg_st **msg)
{
int ret = -1;
if(msg == NULL && ipc_ctx == NULL)
return ret;
if(*msg != NULL){
ret = msg_send(ipc_ctx,module,*msg);
if((*msg)->msg_shmdata != NULL)
free((*msg)->msg_shmdata);
free(*msg);
*msg = NULL;
}
return ret;
}
int msg_send(ipc_ctx *ipc_ctx,const char *module,struct msg_st *msg)
{
int ret = -1;
int max_len = strlen(module);
struct ipc_ctx *dest_ipc_ctx = NULL;
char real_module_name[MAX_MODULE_NAME_SIZE]= {};
if(max_len < 1 || max_len > MAX_MODULE_NAME_SIZE){
log_error("Module name must be greater then one character.\n");
return -1;
}
if(msg == NULL)
return -1;
snprintf(real_module_name,sizeof(real_module_name),"/%s",module);
list_node *node = list_find(ipc_ctx_list,list_node_compare,real_module_name);
if(node == NULL){
dest_ipc_ctx = (struct ipc_ctx* )malloc(sizeof(struct ipc_ctx));
if(dest_ipc_ctx == NULL)
return -1;
snprintf(dest_ipc_ctx->module, sizeof(dest_ipc_ctx->module), "/%s", module);
//Saving this handle for sending messages,so no need receive callback.
dest_ipc_ctx->mq_ctx = _mq_init(ipc_ctx->module,NULL,NULL,IPC_SEND);
if(dest_ipc_ctx->mq_ctx == NULL){
free(dest_ipc_ctx);
return -1;
}
list_insert_after(ipc_ctx_list,dest_ipc_ctx);
}else{
dest_ipc_ctx= (struct ipc_ctx *)node->data;
}
if(dest_ipc_ctx != NULL){
if(ipc_ctx != NULL)
snprintf(msg->msg_module,sizeof(ipc_ctx->module),"%s",ipc_ctx->module);
ret = _mq_send(dest_ipc_ctx->mq_ctx,(const char *)msg,sizeof(struct msg_st),ipc_ctx->msg_prio);
return ret;
}
return 0;
}
/**
* msg_setattr() - init
* @module:
* @timespec:
*
*
*
* Return: 0 on success, a negative error code on failure.
*/
int msg_setattr(const char*module,long msgsize,long maxmsg)
{
char real_module_name[MAX_MODULE_NAME_SIZE]= {};
snprintf(real_module_name,sizeof(real_module_name),"/%s",module);
list_node *node = list_find(ipc_ctx_list,list_node_compare,real_module_name);
ipc_ctx *ipc_ctx= (struct ipc_ctx *)node->data;
if(ipc_ctx != NULL){
ipc_ctx->mq_ctx->msgsize = msgsize;
ipc_ctx->mq_ctx->maxmsg = maxmsg;
}
return _mq_setattr(ipc_ctx->mq_ctx);
}
int msg_ctx_free(list_node *list,void *data)
{
ipc_ctx *ipc_ctx= (struct ipc_ctx *)list->data;
_mq_deinit(ipc_ctx->mq_ctx,ipc_ctx->module);
free(ipc_ctx);
return 0;
}
void msg_deinit(const char *module)
{
list_foreach(ipc_ctx_list,msg_ctx_free,NULL);
list_destroy(&ipc_ctx_list);
}
void msg_shmrm(msg_st *received_msg)
{
//Clear MSG_IPC_SHM flag then send back.
received_msg->msg_type &= ~MSG_IPC_SHM;
received_msg->msg_type |= MSG_IPC_SHM_RELEASE;
msg_send(NULL,received_msg->msg_module,received_msg);
}
#define msg_create_private(name, data,type) \
struct msg_st *msg = (struct msg_st *)calloc(1,sizeof(struct msg_st)); \
if(msg == NULL) return NULL; \
SET_DATA_TYPE(type,msg->msg_type); \
snprintf(msg->msg_name, sizeof(msg->msg_name), "%s", msg_name); \
memcpy(&msg->msg_data,&data,sizeof(data)); \
msg->msg_size = sizeof(data) \
/**
* msg_create_int() - init
* @module:
* @timespec:
*
*
*
* Return: 0 on success, a negative error code on failure.
*/
msg_st *msg_create_int(const char *msg_name,int data)
{
msg_create_private(msg_name,data,DATA_TYPE_INT);
return msg;
}
msg_st *msg_create_double(const char *msg_name,double data)
{
msg_create_private(msg_name,data,DATA_TYPE_DOUBLE);
return msg;
}
msg_st *msg_create_short(const char *msg_name,short data)
{
msg_create_private(msg_name,data,DATA_TYPE_SHORT);
return msg;
}
msg_st *msg_create_char(const char *msg_name,char data)
{
msg_create_private(msg_name,data,DATA_TYPE_CHAR);
return msg;
}
msg_st *msg_create_long(const char *msg_name,long data)
{
msg_create_private(msg_name,data,DATA_TYPE_LONG);
return msg;
}
msg_st *msg_create_float(const char *msg_name,float data)
{
msg_create_private(msg_name,data,DATA_TYPE_FLOAT);
return msg;
}
msg_st *msg_create_string(const char *msg_name,const char *data)
{
long size = 0;
if(data == NULL) return NULL;
size = strlen(data)+1;
struct msg_st *msg = (struct msg_st *)calloc(1,sizeof(struct msg_st));
if(msg == NULL) return NULL;
SET_DATA_TYPE(DATA_TYPE_STRING,msg->msg_type);
snprintf(msg->msg_name, sizeof(msg->msg_name), "%s", msg_name);
memcpy(msg->msg_data._buffer,data,size);
msg->msg_size = size;
return msg;
}
msg_st *msg_create_unsigned_int(const char *msg_name,unsigned int data)
{
msg_create_private(msg_name,data,DATA_TYPE_UINT);
return msg;
}
msg_st *msg_create_unsigned_char(const char *msg_name,unsigned char data)
{
msg_create_private(msg_name,data,DATA_TYPE_UCHAR);
return msg;
}
msg_st *msg_create_unsigned_short(const char *msg_name,unsigned short data)
{
msg_create_private(msg_name,data,DATA_TYPE_USHORT);
return msg;
}
msg_st *msg_create_llong(const char *msg_name,long long data)
{
msg_create_private(msg_name,data,DATA_TYPE_LLONG);
return msg;
}
msg_st *msg_create_unsigned_long(const char *msg_name,unsigned long data)
{
msg_create_private(msg_name,data,DATA_TYPE_ULONG);
return msg;
}
msg_st *msg_create_unsigned_llong(const char *msg_name,unsigned long long data)
{
msg_create_private(msg_name,data,DATA_TYPE_ULLONG);
return msg;
}
/**
* msg_create_json() - init
* @module:
* @timespec:
*
*
*
* Return: 0 on success, a negative error code on failure.
*/
struct msg_st *msg_create_json(struct ipc_ctx *ipc_ctx,const char *msg_name,const char *data,size_t size)
{
struct msg_st *msg = msg_create_binary(ipc_ctx,msg_name,data,size);
if(msg != NULL)
SET_DATA_TYPE(DATA_TYPE_JSON,msg->msg_type);
return msg;
}
struct msg_st *msg_create_binary(ipc_ctx *ipc_ctx,const char *msg_name,const void *data,size_t size)
{
struct msg_st *msg = (struct msg_st *)calloc(1,sizeof(struct msg_st));
SET_DATA_TYPE(DATA_TYPE_BINARY,msg->msg_type);
snprintf(msg->msg_name, sizeof(msg->msg_name), "%s", msg_name);
msg->msg_size = size;
if(size > sizeof(msg->msg_data)){
char shm_name[MAX_SHM_NAME_SIZE] ={};
struct shm_ctx *shm_ctx = NULL;
static int sed = 0;
sed = sed > 10000 ? 0: sed;
snprintf(shm_name,sizeof(shm_name),"%s.%s_%d",ipc_ctx->module,msg_name,sed++);
shm_ctx = shm_init(shm_name,size,SHM_ID_UNIQUE);
if(shm_ctx != NULL){
snprintf(msg->msg_shmid,sizeof(shm_name),"%s",shm_name);
shm_write(shm_ctx,data,size,0);
msg->msg_type |= MSG_IPC_SHM;
//shm_close does not remove the shared memory from system.
//So it must be closed after sending remove message back from receiver.
//This case is very critical.
shm_close(shm_ctx);
msg->msg_shmdata = (void *)data;
msg->msg_data._long = size;
}else{
free(msg);
return NULL;
}
}else{
memcpy(msg->msg_data._buffer,data,size);
}
return msg;
}
int list_node_compare(list_node *list,const void *data)
{
struct ipc_ctx *ipc_ctx = (struct ipc_ctx*)list->data;
return strcmp(ipc_ctx->module,(char *)data) ? 0:1;
}
void msg_recv_callback(mq_ctx *mq_ctx,msg_st *msg)
{
if(mq_ctx->recv_cb)
mq_ctx->recv_cb(msg);
if(IS_DEFINED(msg->msg_type,MSG_IPC_SHM))
msg_shmrm(msg);
}