forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmsgq_posix.c
304 lines (283 loc) · 9.05 KB
/
msgq_posix.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
/******************************************************************************
* Copyright (C) 2014-2018 Zhifeng Gong <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libraries; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "libipc.h"
#include <libmacro.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <mqueue.h>
#include <semaphore.h>
/*
* message queue IPC build flow:
*
* client (test_libipc) server (ipcd)
* step.1 create /dev/mqueue/IPC_SERVER.5555
* create thread to wait message
* step.2 create /dev/mqueue/IPC_CLIENT.$pid
* send "/IPC_CLIENT.$pid", and wait
* step.3 accept message and post sem,
* send "/IPC_CLIENT.$pid"
* step.4 post sem and strcmp recv msg and sent msg,
* message queue IPC is built
*/
#define MQ_MAXMSG 5
#define MQ_MSGSIZE 1024
#define MQ_MSG_PRIO 10
#define MAX_MQ_NAME 256
#define IPC_SERVER_NAME "/IPC_SERVER"
#define IPC_CLIENT_NAME "/IPC_CLIENT"
struct mq_posix_ctx {
mqd_t mq_wr;
mqd_t mq_rd;
char mq_wr_name[MAX_MQ_NAME];
char mq_rd_name[MAX_MQ_NAME];
ipc_role role;
sem_t sem;
void *parent;
};
typedef void (mq_notify_cb)(union sigval sv);
static ipc_recv_cb *_mq_recv_cb = NULL;
static void *_mq_recv_buf = NULL;
static int _mq_recv(struct ipc *ipc, void *buf, size_t len);
static int _mq_send(struct ipc *ipc, const void *buf, size_t len);
static int _mq_notify_update(struct mq_posix_ctx *ctx, mq_notify_cb cb)
{
struct sigevent sv;
memset(&sv, 0, sizeof(sv));
sv.sigev_notify = SIGEV_THREAD;
sv.sigev_notify_function = cb;
sv.sigev_notify_attributes = NULL;
sv.sigev_value.sival_ptr = ctx;
if (mq_notify(ctx->mq_rd, &sv) == -1) {//vagrind
printf("mq_notify failed %d: %s\n", errno, strerror(errno));
return -1;
}
return 0;
}
static int _mq_connect(struct ipc *ipc, const char *name)
{//for client
struct mq_posix_ctx *ctx = (struct mq_posix_ctx *)ipc->ctx;
struct mq_attr attr;
ctx->parent = ipc;
attr.mq_flags = 0;
attr.mq_maxmsg = MQ_MAXMSG;
attr.mq_msgsize = MQ_MSGSIZE;
attr.mq_curmsgs = 0;
ctx->mq_wr = mq_open(name, O_WRONLY|O_EXCL, S_IRWXU|S_IRWXG, &attr);
if (ctx->mq_wr < 0) {
printf("mq_open %s failed: %d:%s\n", name, errno, strerror(errno));
return -1;
}
strncpy(ctx->mq_wr_name, name, sizeof(ctx->mq_wr_name));
if (0 > _mq_send(ipc, ctx->mq_rd_name, strlen(ctx->mq_rd_name))) {
printf("_mq_send failed!\n");
return -1;
}
struct timeval now;
struct timespec abs_time;
uint32_t timeout = 5000;//msec
gettimeofday(&now, NULL);
/* Add our timeout to current time */
now.tv_usec += (timeout % 1000) * 1000;
now.tv_sec += timeout / 1000;
/* Wrap the second if needed */
if ( now.tv_usec >= 1000000 ) {
now.tv_usec -= 1000000;
now.tv_sec ++;
}
/* Convert to timespec */
abs_time.tv_sec = now.tv_sec;
abs_time.tv_nsec = now.tv_usec * 1000;
if (-1 == sem_timedwait(&ctx->sem, &abs_time)) {
printf("connect %s failed %d: %s\n", name, errno, strerror(errno));
return -1;
}
return 0;
}
static int _mq_accept(struct ipc *ipc)
{//for server
struct mq_posix_ctx *ctx = (struct mq_posix_ctx *)ipc->ctx;
ctx->parent = ipc;
struct mq_attr attr;
attr.mq_flags = 0;
attr.mq_maxmsg = MQ_MAXMSG;
attr.mq_msgsize = MQ_MSGSIZE;
attr.mq_curmsgs = 0;
sem_wait(&ctx->sem);
ctx->mq_wr = mq_open(ctx->mq_wr_name, O_WRONLY, S_IRWXU | S_IRWXG, &attr);
if (ctx->mq_wr < 0) {
printf("mq_open %s failed: %d:%s\n", ctx->mq_wr_name, errno, strerror(errno));
return -1;
}
if (0 > _mq_send(ipc, ctx->mq_wr_name, strlen(ctx->mq_wr_name))) {
printf("_mq_send failed!\n");
return -1;
}
return 0;
}
static void on_recv(union sigval sv)
{
int len;
struct mq_posix_ctx *ctx = (struct mq_posix_ctx *)sv.sival_ptr;
struct ipc *ipc = (struct ipc *)ctx->parent;
if (-1 == _mq_notify_update(ctx, on_recv)) {
printf("_mq_notify_update failed!\n");
return;
}
//recv len must greater than mq_msgsize;
len = _mq_recv(ipc, _mq_recv_buf, MAX_IPC_MESSAGE_SIZE);
if (len == -1) {
printf("_mq_recv failed!\n");
return;
}
if (_mq_recv_cb) {
_mq_recv_cb(ipc, _mq_recv_buf, len);
} else {
printf("_mq_recv_cb is NULL!\n");
}
}
static void on_connect(union sigval sv)
{
char buf[MAX_IPC_MESSAGE_SIZE];
int len;
struct mq_posix_ctx *ctx = (struct mq_posix_ctx *)sv.sival_ptr;
struct ipc *ipc = (struct ipc *)ctx->parent;
if (-1 == _mq_notify_update(ctx, on_recv)) {
printf("_mq_notify_update failed!\n");
return;
}
memset(&buf, 0, sizeof(buf));
len = _mq_recv(ipc, buf, sizeof(buf));
if (len <= 0) {
printf("_mq_recv failed!\n");
return;
}
if (ctx->role == IPC_SERVER) {
strncpy(ctx->mq_wr_name, buf, sizeof(ctx->mq_wr_name));
} else {//IPC_CLIENT
if (strcmp(buf, ctx->mq_rd_name)) {
printf("buf = %s\n", buf);
printf("connect response check failed!\n");
return;
}
}
sem_post(&ctx->sem);
}
static void *_mq_init(struct ipc *ipc, uint16_t port, enum ipc_role role)
{
struct mq_attr attr;
struct mq_posix_ctx *ctx = CALLOC(1, struct mq_posix_ctx);
if (!ctx) {
printf("malloc failed!\n");
return NULL;
}
ipc->ctx = ctx;
ctx->parent = ipc;
if (role == IPC_SERVER) {
snprintf(ctx->mq_rd_name, sizeof(ctx->mq_rd_name), "%s.%d", IPC_SERVER_NAME, port);
} else if (role == IPC_CLIENT) {
snprintf(ctx->mq_rd_name, sizeof(ctx->mq_rd_name), "%s.%d", IPC_CLIENT_NAME, port);
snprintf(ctx->mq_wr_name, sizeof(ctx->mq_wr_name), "%s.%d", IPC_SERVER_NAME, port);
}
attr.mq_flags = 0;
attr.mq_maxmsg = MQ_MAXMSG;
attr.mq_msgsize = MQ_MSGSIZE;
attr.mq_curmsgs = 0;
mq_unlink(ctx->mq_rd_name);
ctx->mq_rd = mq_open(ctx->mq_rd_name, O_RDWR|O_EXCL|O_CREAT|O_NONBLOCK, S_IRWXU|S_IRWXG, &attr);
if (ctx->mq_rd == -1) {
printf("mq_open %s failed %d:%s\n", ctx->mq_rd_name, errno, strerror(errno));
goto failed;
}
ctx->role = role;
if (-1 == sem_init(&ctx->sem, 0, 0)) {
printf("sem_init failed %d:%s\n", errno, strerror(errno));
return NULL;
}
_mq_recv_buf = calloc(1, MAX_IPC_MESSAGE_SIZE);
if (!_mq_recv_buf) {
printf("malloc failed!\n");
return NULL;
}
if (-1 == _mq_notify_update(ctx, on_connect)) {
printf("_mq_notify_update failed!\n");
return NULL;
}
if (role == IPC_CLIENT) {
_mq_connect(ipc, ctx->mq_wr_name);
} else if (role == IPC_SERVER) {
_mq_accept(ipc);
}
return ctx;
failed:
if (ctx->mq_rd) {
mq_close(ctx->mq_rd);
}
if (ctx) {
free(ctx);
}
return NULL;
}
static int _mq_set_recv_cb(struct ipc *ipc, ipc_recv_cb *cb)
{
_mq_recv_cb = cb;
return 0;
}
static void _mq_deinit(struct ipc *ipc)
{
if (!ipc) {
return;
}
struct mq_posix_ctx *ctx = (struct mq_posix_ctx *)ipc->ctx;
mq_close(ctx->mq_rd);
mq_close(ctx->mq_wr);
mq_unlink(ctx->mq_rd_name);
sem_destroy(&ctx->sem);
if (_mq_recv_buf) {
free(_mq_recv_buf);
}
free(ctx);
}
static int _mq_send(struct ipc *ipc, const void *buf, size_t len)
{
struct mq_posix_ctx *ctx = (struct mq_posix_ctx *)ipc->ctx;
if (0 != mq_send(ctx->mq_wr, (const char *)buf, len, MQ_MSG_PRIO)) {
printf("mq_send failed %d: %s\n", errno, strerror(errno));
return -1;
}
return len;
}
static int _mq_recv(struct ipc *ipc, void *buf, size_t len)
{
struct mq_posix_ctx *ctx = (struct mq_posix_ctx *)ipc->ctx;
ssize_t ret = mq_receive(ctx->mq_rd, (char *)buf, len, NULL);
if (-1 == ret) {
printf("mq_receive failed %d: %s\n", errno, strerror(errno));
return -1;
}
return ret;
}
struct ipc_ops msgq_posix_ops = {
.init = _mq_init,
.deinit = _mq_deinit,
.accept = NULL,
.connect = NULL,
.register_recv_cb = _mq_set_recv_cb,
.send = _mq_send,
.recv = _mq_recv,
};