-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuMsg.h
executable file
·143 lines (120 loc) · 2.71 KB
/
HuMsg.h
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
#ifndef __MSG_H__
#define __MSG_H__
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <linux/limits.h>
#include "hulib.h"
#define MAX_SEND_SIZE 4096
struct hu_msgbuf
{
long mtype;
char mtext[MAX_SEND_SIZE];
};
class Msg
{
public:
key_t key;
int msgqueue_id;
//hu_msgbuf qbuf;
Msg()
{
key = 0;
msgqueue_id = 0;
}
~Msg()
{
key = 0;
if(msgqueue_id)
{
close(msgqueue_id);
msgqueue_id=0;
}
}
void init_message()
{
/* Create unique key via call to ftok() */
char link[PATH_MAX];
char path[PATH_MAX];
sprintf(link, "/proc/%d/exe", getpid());
readlink(link, path, sizeof(path));
key = ftok(path, 1);
debug("\r\n init_message path[%s] key[%#x]\r\n", path, key);
/* Open the queue - create if necessary */
if ((msgqueue_id = msgget(key, IPC_CREAT | 0660)) == -1)
{
errexitf("msgget:%s:%s",path,strerror(errno));
//exit(1);
}
}
void open_message(const char * path)
{
/* Create unique key via call to ftok() */
key = ftok(path, 1);
open_message(key);
// printf("open_message key = %#x\r\n", key);
//
// /* Open the queue - create if necessary */
// if ((msgqueue_id = msgget(key, 0660)) == -1)
//
// {
// perror("msgget");
// exit(1);
// }
}
void open_message(int k)
{
key = k;
debug("open_message key = %#x\r\n", key);
/* Open the queue - create if necessary */
if ((msgqueue_id = msgget(key, 0660)) == -1)
{
errexitf("msgget:%d:%s",k,strerror(errno));
//exit(1);
}
}
void read_message(long type, struct hu_msgbuf *qbuf)
{
/* Read a message from the queue */
qbuf->mtype = type;
msgrcv(msgqueue_id, (struct msgbuf *) qbuf, MAX_SEND_SIZE, type, 0);
//printf("Type: %ld Text: %s \r\n", qbuf->mtype, qbuf->mtext);
}
void send_message(long type, const char *text)
{
/* Send a message to the queue */
struct hu_msgbuf qbuf;
qbuf.mtype = type;
strcpy(qbuf.mtext, text);
if ((msgsnd(msgqueue_id, (struct msgbuf *) &qbuf,
strlen(qbuf.mtext) + 1, 0)) == -1)
{
perror("msgsnd");
exit(1);
}
}
void remove_queue()
{
/* Remove the queue */
msgctl(msgqueue_id, IPC_RMID, 0);
}
int count()
{
struct msqid_ds myqueue_ds;
/* Get current info */
msgctl(msgqueue_id, IPC_STAT, &myqueue_ds);
return myqueue_ds.msg_qnum;
}
void change_queue_mode(char *mode)
{
struct msqid_ds myqueue_ds;
/* Get current info */
msgctl(msgqueue_id, IPC_STAT, &myqueue_ds);
/* Convert and load the mode */
sscanf(mode, "%ho", &myqueue_ds.msg_perm.mode);
/* Update the mode */
msgctl(msgqueue_id, IPC_SET, &myqueue_ds);
}
};
#endif //__MSG_H__