--- a/src/PosixMQ.h Tue Mar 01 00:49:46 2022 +0100
+++ b/src/PosixMQ.h Fri Mar 04 19:43:16 2022 +0100
@@ -27,25 +27,32 @@
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_RDONLY | 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_RDONLY | 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("Unable to send message to" + queueName + " error: " + strerror(errno));
+ }
+
std::string receive() {
char buffer[MSG_SIZE + 1];
memset(buffer, 0, MSG_SIZE + 1);
@@ -55,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());
- }
-
};
}