Tim Yao | 10e5d9b | 2020-12-30 17:38:10 -0800 | [diff] [blame] | 1 | #ifndef __IPCBUFFER_H |
| 2 | #define __IPCBUFFER_H |
| 3 | |
| 4 | #include <boost/interprocess/managed_shared_memory.hpp> |
| 5 | |
| 6 | using namespace boost::interprocess; |
| 7 | |
| 8 | class IpcBuffer |
| 9 | { |
| 10 | public: |
| 11 | IpcBuffer(const char *name, size_t capacity); |
| 12 | ~IpcBuffer(); |
| 13 | |
| 14 | size_t size() const { return size_; } |
| 15 | size_t capacity() const { return capacity_; } |
| 16 | size_t write(const uint8_t *data, size_t bytes); |
| 17 | size_t read(uint8_t *data, size_t bytes); |
| 18 | void write_nb(const uint8_t *data, size_t bytes); |
| 19 | void get_write_position(uint64_t& time, uint64_t& position); |
| 20 | void reset(); |
| 21 | uint8_t *start_ptr(); |
| 22 | const char *name(); |
| 23 | |
| 24 | private: |
| 25 | // Note: members can be access from different process |
| 26 | // only keep information which are common to all processes |
| 27 | // addresses must be mapped during runtime |
| 28 | size_t begin_index_, end_index_, size_, capacity_; |
| 29 | managed_shared_memory::handle_t handle_; |
| 30 | std::string name_; |
| 31 | bool blocking_; |
| 32 | interprocess_mutex wr_position_mutex_; |
| 33 | uint64_t wr_position_; |
| 34 | uint64_t wr_time_; |
| 35 | }; |
| 36 | |
| 37 | #endif |