Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions common/ipc/ScopedFileLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "exception.h"
#include "const.h"

#include "String.h"

namespace Exchange::Ipc {

Expand All @@ -18,23 +18,23 @@ namespace Exchange::Ipc {
*/
class ScopedFileLock {
int mFd;
std::string mPath;
Core::String mPath;
public:
/**
* The constructor ensures that Producers only fight other Producers, and Consumers only fight other Consumers
*/
ScopedFileLock(const std::string& name, bool isProducer) {
ScopedFileLock(const Core::String& name, bool isProducer) {

mPath = std::string(LOCK_BASE_PATH) + name + (isProducer ? ".prod.lock" : ".cons.lock");
mPath = Core::String(LOCK_BASE_PATH) + name + (isProducer ? ".prod.lock" : ".cons.lock");

// Open lock file (create if needs be)
// O_RDWR: read and write
// O_CREAT: create if file does not exsits
// O_CLOEXEC: close the file desciptoo when process exists
mFd = open(mPath.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0666);
mFd = open(mPath.get(), O_RDWR | O_CREAT | O_CLOEXEC, 0666);

if (mFd < 0) {
ENG_THROW("Failed to open lock file: %s", mPath.c_str());
ENG_THROW("Failed to open lock file: %s", mPath.get());
}

// LOCK_EX (Exclusive): This tells the Linux Kernel: "Only one file descriptor can hold a
Expand All @@ -43,7 +43,7 @@ namespace Exchange::Ipc {
// Fail immediately."
if (flock(mFd, LOCK_EX | LOCK_NB) < 0) {
close(mFd);
ENG_THROW("Highlander Rule Violation: Another process holds the lock %s", mPath.c_str());
ENG_THROW("Highlander Rule Violation: Another process holds the lock %s", mPath.get());
}
}

Expand Down
8 changes: 4 additions & 4 deletions common/ipc/SharedMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Exchange::Ipc {


SharedMemory::SharedMemory(const std::string& name, uint32_t capacity, bool create)
SharedMemory::SharedMemory(const Core::String& name, uint32_t capacity, bool create)
: mName("/" + name), mIsOwner(create) {

// Calculate total size
Expand All @@ -22,13 +22,13 @@ namespace Exchange::Ipc {
// reference counting. The physical RAM for that segment is not actually freed until every process (including
// any lingering Consumers) has closed it or exited. However, since the name is gone, the Producer is free to
// create a brand new segment with the same name immediately.
shm_unlink(name.c_str());
shm_unlink(name.get());

// Creates a new shared memory object.
// O_RDWR — open read/write.
// 0666 — permissions: read/write for all users.
// Returns a file descriptor in shmFd.
mFd = shm_open(name.c_str(), O_CREAT | O_RDWR, 0666);
mFd = shm_open(name.get(), O_CREAT | O_RDWR, 0666);
if (mFd == -1) {
ENG_THROW("Producer: shm_open failed (create)");
}
Expand All @@ -41,7 +41,7 @@ namespace Exchange::Ipc {
}
else {
// Consumer: Open existing
mFd = shm_open(name.c_str(), O_RDWR, 0666);
mFd = shm_open(name.get(), O_RDWR, 0666);
if (mFd == -1) {
ENG_THROW("Consumer: shm_open failed (open existing)");
}
Expand Down
15 changes: 8 additions & 7 deletions common/ipc/SharedMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "const.h"
#include "ScopedFileLock.h"
#include "String.h"

namespace Exchange::Ipc {

Expand All @@ -33,12 +34,12 @@ namespace Exchange::Ipc {
}; // class SharedHeader

// Helper to generate uuid
inline std::string generateUuid() {
inline Core::String generateUuid() {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, 15);
static const char* v = "0123456789abcdef";
std::string uuid;
Core::String uuid;
for (int i = 0; i < 36; ++i) {
if (i == 8 || i == 13 || i == 18 || i == 23) {
uuid += '-';
Expand All @@ -52,7 +53,7 @@ namespace Exchange::Ipc {

class SharedMemory {
protected:
std::string mName; ///> Name of the shared memory object
Core::String mName; ///> Name of the shared memory object
int mFd; ///> File descriptor for the shared memory object
size_t mTotalSize; ///> Total size of the shared memory segment
void* mBasePtr; ///> Base pointer to the mapped shared memory
Expand All @@ -66,7 +67,7 @@ namespace Exchange::Ipc {
* @note
* POSIX shared memory objects must start with '/'
*/
SharedMemory(const std::string& name, uint32_t capacity, bool create);
SharedMemory(const Core::String& name, uint32_t capacity, bool create);

}; // class SharedMemory

Expand All @@ -81,7 +82,7 @@ namespace Exchange::Ipc {
* SharedMemory(.., true) means this is the creator (producer) of the shared memory segment.
* mLock(true) means this is a producer lock.
*/
Producer(const std::string& name, uint32_t capacity = BUFFER_CAPACITY);
Producer(const Core::String& name, uint32_t capacity = BUFFER_CAPACITY);

/**
* @brief Write data into the ring buffer.
Expand Down Expand Up @@ -110,12 +111,12 @@ namespace Exchange::Ipc {
* SharedMemory(.., false) means this is the creator (producer) of the shared memory segment.
* mLock(false) means this is a consumer lock.
*/
Consumer(const std::string& name, uint32_t capacity = BUFFER_CAPACITY);
Consumer(const Core::String& name, uint32_t capacity = BUFFER_CAPACITY);

// Returns bytes read, or 0 if empty
uint32_t read(void* buffer, uint32_t bufferSize);

std::string getSessionUuid() const;
Core::String getSessionUuid() const;

}; // class Consumer

Expand Down
8 changes: 4 additions & 4 deletions common/ipc/SharedMemory_Consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Exchange::Ipc {

Consumer::Consumer(const std::string& name, uint32_t capacity)
Consumer::Consumer(const Core::String& name, uint32_t capacity)
: SharedMemory(name, capacity, false), mLock(name, false) {


Expand All @@ -12,7 +12,7 @@ namespace Exchange::Ipc {
ENG_THROW("Invalid IPC Header Signature");
}

const std::string uuidPath = "/tmp/"+mName+".uuid";
const Core::String uuidPath = "/tmp/" + mName +".uuid";
std::ifstream f(uuidPath);
if (!f.is_open()) {
ENG_THROW("UUID file not found");
Expand Down Expand Up @@ -51,8 +51,8 @@ namespace Exchange::Ipc {
return msgLen;
}

std::string Consumer::getSessionUuid() const {
return std::string(mHeader->uuid);
Core::String Consumer::getSessionUuid() const {
return Core::String(mHeader->uuid);
}

} // namespace Exchange::Ipc
10 changes: 5 additions & 5 deletions common/ipc/SharedMemory_Producer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

namespace Exchange::Ipc {

Producer::Producer(const std::string& name, uint32_t capacity) : SharedMemory(name, capacity, true), mLock(name, true) {
Producer::Producer(const Core::String& name, uint32_t capacity) : SharedMemory(name, capacity, true), mLock(name, true) {
// Initialize header
std::memset(mHeader, 0, sizeof(SharedHeader));
std::strncpy(mHeader->signature, MAGIC, 32); // Set magic signature

// Generate and set UUID
std::string sessionUuid = generateUuid();
Core::String sessionUuid = generateUuid();

const std::string uuidPath = "/tmp/"+mName+".uuid";
std::ofstream f(uuidPath, std::ios::trunc);
const Core::String uuidPath = "/tmp/" + mName + ".uuid";
std::ofstream f(uuidPath.get(), std::ios::trunc);
f << sessionUuid;
f.close();

std::strncpy(mHeader->uuid, sessionUuid.c_str(), 37);
std::strncpy(mHeader->uuid, sessionUuid.get(), 37);
mHeader->capacity = capacity;
mHeader->maxMsgSize = MAX_MSG_SIZE;
mHeader->writeIdx = 0;
Expand Down
Loading