-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_message_queue.hpp
120 lines (99 loc) · 2.97 KB
/
system_message_queue.hpp
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
#ifndef RCO_UTILITY_SYSTEM_MESSAGE_QUEUE_HPP
#define RCO_UTILITY_SYSTEM_MESSAGE_QUEUE_HPP
/**
*** Author: R-CO
*** E-mail: [email protected]
*** Date: 2021-01-22
**/
// unix system header
#include <errno.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
namespace rco {
namespace sysmsgq {
/*
* To inherit this structure for calling template function send/receive
* ex:
* struct MyData : public rco::sysmsgq::MessageBufferBase {
* struct TYPE_OF_CONTIGUOUS_MEMORY_BLOCK {
* // ...
* };
* };
* rco::SystemMessageQueue smq;
* smq.createQueueViaKey(key);
* MyData data;
* data.mtype = 1;
* // data.actual_data = XXXXXX;
* smq.sendToQueue(data, msgflg);
*/
struct MessageBufferBase {
long mtype;
MessageBufferBase() : mtype(0) {}
explicit MessageBufferBase(const long message_type) : mtype(message_type) {}
};
} // end of namespace sysmsgq
class SystemMessageQueue {
public:
enum class SystemMessageFlag : int {
open_existed,
create_or_open_existed,
create_new_only
};
// these values is "NOT" hex
enum class Permission : int {
owner_only = 0600,
owner_and_group = 0660,
all = 0666
};
SystemMessageQueue() = default;
// SystemMessageQueue(const key_t key, const int msgflg); // TODO:
// No default copy constructor
SystemMessageQueue(const SystemMessageQueue& SystemMessageQueeu) = delete;
// No default operator=
SystemMessageQueue& operator=(
const SystemMessageQueue& system_message_queue) = delete;
~SystemMessageQueue();
// default permission: Permission::all
bool createQueue(const key_t key);
bool createQueueWithPermission(const key_t key, const Permission permission);
bool bindQueueViaKey(const key_t key);
bool bindQueueViaQueueId(const int queue_id);
bool removeQueue();
/*
* structure should be somthing like
* struct {
* long mtype; // Structure must have this member(1st), and it's type must
* be 'long int'. char mtext[1] // message data
* }
*
* Please refer to `int msgsnd(int msqid, const void *msgp, size_t msgsz, int
* msgflg)` in <sys/msg.h>
*/
bool sendToQueue(const void* msgp, const size_t msgsz, const int msgflg);
ssize_t receiveFromQueue(void* msgp, const size_t msgsz, const long msgtyp,
const int msgflg);
template <typename T>
bool sendToQueue(const T& msgst, const int msgflg) {
const sysmsgq::MessageBufferBase& msg_buffer = msgst;
enum ReturnCode { ok = 0, fail = -1 };
if (msgsnd(this->getQueueId(), &msgst,
sizeof(msgst) - sizeof(msg_buffer.mtype),
msgflg) == static_cast<int>(ReturnCode::fail)) {
this->error_no_ = errno;
return false;
}
return true;
}
int getQueueId() const;
int getErrorNo() const;
protected:
void reset();
private:
key_t key_ = 0;
int msgflg_ = 0;
int queue_id_ = -1;
int error_no_ = 0;
};
} // end of namespace rco
#endif // end of define RCO_UTILITY_SYSTEM_MESSAGE_QUEUE_HPP