-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_message_queue.cpp
152 lines (117 loc) · 3.73 KB
/
system_message_queue.cpp
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
#include "system_message_queue.hpp"
#include <errno.h>
// #define DEBUG_RCO
#ifdef DEBUG_RCO
#include <chrono>
#include <iostream>
#endif // end of ifdef DEBUG_RCO
namespace rco {
// SystemMessageQueue::SystemMessageQueue(const key_t key, const int msgflg)
// : key_(key), msgflg_(msgflg) {
// // TODO:
// }
SystemMessageQueue::~SystemMessageQueue() {
// TODO:
}
// default permission: Permission::all
bool SystemMessageQueue::createQueue(const key_t key) {
static const int kMessageFlag =
IPC_CREAT | IPC_EXCL | static_cast<int>(Permission::all);
this->key_ = key;
this->msgflg_ = kMessageFlag;
this->queue_id_ = msgget(key, kMessageFlag);
if (this->queue_id_ == -1) {
this->error_no_ = errno;
return false;
}
return true;
}
bool SystemMessageQueue::createQueueWithPermission(
const key_t key, const Permission permission) {
// TODO:
return false;
}
bool SystemMessageQueue::bindQueueViaKey(const key_t key) {
static const int kGetMessageflg = 0;
this->queue_id_ = msgget(key, kGetMessageflg);
if (this->queue_id_ == -1) {
this->error_no_ = errno;
return false;
}
return true;
}
bool SystemMessageQueue::bindQueueViaQueueId(const int queue_id) {
// check existed and accessible
msqid_ds ds;
if (msgctl(queue_id, IPC_STAT, &ds) == -1) {
this->queue_id_ = -1;
this->error_no_ = errno;
return false;
}
this->queue_id_ = queue_id;
return true;
}
bool SystemMessageQueue::removeQueue() {
if (msgctl(this->queue_id_, IPC_RMID, nullptr) == -1) {
this->error_no_ = errno;
return false;
}
reset();
return true;
}
bool SystemMessageQueue::sendToQueue(const void *msgp, const size_t msgsz,
const int msgflg) {
enum ReturnCode { ok = 0, fail = -1 };
#ifdef DEBUG_RCO
auto t1 = std::chrono::high_resolution_clock::now();
#endif // end of ifdef DEBUG_RCO
if (msgsnd(this->getQueueId(), msgp, msgsz, msgflg) ==
static_cast<int>(ReturnCode::fail)) {
#ifdef DEBUG_RCO
auto t2 = std::chrono::high_resolution_clock::now();
auto time_span =
std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1);
std::cout << "Time span of msgsnd() = [" << time_span.count()
<< "] microseconds." << std::endl;
#endif // end of ifdef DEBUG_RCO
this->error_no_ = errno;
return false;
}
#ifdef DEBUG_RCO
auto t2 = std::chrono::high_resolution_clock::now();
auto time_span =
std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1);
std::cout << "Time span of msgsnd() = [" << time_span.count()
<< "] microseconds." << std::endl;
#endif // end of ifdef DEBUG_RCO
return true;
}
ssize_t SystemMessageQueue::receiveFromQueue(void *msgp, const size_t msgsz,
const long msgtyp,
const int msgflg) {
#ifdef DEBUG_RCO
auto t1 = std::chrono::high_resolution_clock::now();
#endif // end of ifdef DEBUG_RCO
const auto return_value =
msgrcv(this->getQueueId(), msgp, msgsz, msgtyp, msgflg);
if (return_value == static_cast<ssize_t>(-1)) {
this->error_no_ = errno;
}
#ifdef DEBUG_RCO
auto t2 = std::chrono::high_resolution_clock::now();
auto time_span =
std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1);
std::cout << "Time span of msgrcv() = [" << time_span.count()
<< "] microseconds." << std::endl;
#endif // end of ifdef DEBUG_RCO
return return_value;
}
int SystemMessageQueue::getQueueId() const { return this->queue_id_; }
int SystemMessageQueue::getErrorNo() const { return this->error_no_; }
void SystemMessageQueue::reset() {
this->key_ = 0;
this->msgflg_ = 0;
this->queue_id_ = -1;
this->error_no_ = 0;
}
} // end of namespace rco