--- a/src/PosixMQ.h Fri Mar 04 01:40:50 2022 +0100
+++ b/src/PosixMQ.h Fri Mar 04 21:30:08 2022 +0100
@@ -27,28 +27,30 @@
class PosixMQ {
private:
- size_t MSG_SIZE = 8192; // TODO: configurable/dynamic
+ const static size_t MSG_SIZE = 8192; // TODO: configurable/dynamic
std::string queueName;
mqd_t handle = -2;
+ bool unlinkOnClose = false;
- PosixMQ(std::string queueName, mqd_t handle) : queueName(queueName), handle(handle) {
+ PosixMQ(std::string queueName, mqd_t handle, bool unlinkOnClose) : queueName(queueName), handle(handle), unlinkOnClose(unlinkOnClose) {
}
public:
virtual ~PosixMQ() {
if (handle >= 0) mq_close(handle);
+ if (unlinkOnClose) mq_unlink(queueName.c_str());
}
- static PosixMQ* open(std::string queueName) {
- mqd_t handle = mq_open(queueName.c_str(), O_RDWR | O_CREAT);
- if (handle >= 0) return new PosixMQ(queueName, handle);
+ static PosixMQ* open(std::string queueName, bool unlinkOnClose = false) {
+ mqd_t handle = mq_open(queueName.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+ if (handle >= 0) return new PosixMQ(queueName, handle, unlinkOnClose);
else throw std::logic_error("Unable to open PosixMQ: " + queueName + " error: " + strerror(errno));
}
void send(std::string message) {
int result = mq_send(handle, message.c_str(), message.size(), 0);
- if (result) throw std::logic_error("mq_send() = " + std::to_string(result) + " error: " + strerror(errno));
+ if (result) throw std::logic_error("Unable to send message to" + queueName + " error: " + strerror(errno));
}
std::string receive() {
@@ -60,10 +62,6 @@
else throw std::logic_error("Unable to receive PosixMQ message from " + queueName + " error: " + strerror(errno));
}
- void unlink() {
- mq_unlink(queueName.c_str());
- }
-
};
}