mirror of
https://github.com/commaai/msgq.git
synced 2026-06-11 23:44:58 +08:00
* Implementation of FakeSubSocket and FakePubSocket using eventfd with support for one-in/one-out synchronization * Expose FakeEvent to Python * Add demo showcasing synchronization between processes * Fix linter errors * Expose more FakeEvent APIs in Python bindings * Add FakePoller implementation * Remove suffix from poll env vars * Set poller timeout to zero when events are enabled * Replace poll with ppoll. Add invalidation methods * Fix lint issues * Fix comment indent * Remove fake_demo * Remove FakePubSocket. Simpler FakePoller implementation. Ability to wait for multiple events * Rename FakeEvent to Event and move it to event.cc * Rename event purpose constants in py * Add support for timeout in wait methods * Add tests for events and fake sockets * Fix lint errors * Add zmq sleeps * Temporarly disable TestFakeSockets on ZMQ * Add exception type specifiers to test_fake * Event Manager implementation * Fix fake sockets tests * Update EventManager API * Add test for enable/disable * Add tests for cereal prefix * Remove EventPurpose from python bindings * Fix lint issues * event_state_shm_mmap implementation shared by EventManager and FakeSubSocket * Rename EventManager to SocketEventHandle * More renames
59 lines
1.2 KiB
C++
59 lines
1.2 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#define CEREAL_EVENTS_PREFIX std::string("cereal_events")
|
|
|
|
void event_state_shm_mmap(std::string endpoint, std::string identifier, char **shm_mem, std::string *shm_path);
|
|
|
|
enum EventPurpose {
|
|
RECV_CALLED,
|
|
RECV_READY
|
|
};
|
|
|
|
struct EventState {
|
|
int fds[2];
|
|
bool enabled;
|
|
};
|
|
|
|
class Event {
|
|
private:
|
|
int event_fd = -1;
|
|
|
|
inline void throw_if_invalid() const {
|
|
if (!this->is_valid()) {
|
|
throw std::runtime_error("Event does not have valid file descriptor.");
|
|
}
|
|
}
|
|
public:
|
|
Event(int fd = -1);
|
|
|
|
void set() const;
|
|
int clear() const;
|
|
void wait(int timeout_sec = -1) const;
|
|
bool peek() const;
|
|
bool is_valid() const;
|
|
int fd() const;
|
|
|
|
static int wait_for_one(const std::vector<Event>& events, int timeout_sec = -1);
|
|
};
|
|
|
|
class SocketEventHandle {
|
|
private:
|
|
std::string shm_path;
|
|
EventState* state;
|
|
public:
|
|
SocketEventHandle(std::string endpoint, std::string identifier = "", bool override = true);
|
|
~SocketEventHandle();
|
|
|
|
bool is_enabled();
|
|
void set_enabled(bool enabled);
|
|
Event recv_called();
|
|
Event recv_ready();
|
|
|
|
static void toggle_fake_events(bool enabled);
|
|
static void set_fake_prefix(std::string prefix);
|
|
static std::string fake_prefix();
|
|
};
|