Tim Yao | 10e5d9b | 2020-12-30 17:38:10 -0800 | [diff] [blame] | 1 | #ifndef __IPCBUFFER_H |
| 2 | #define __IPCBUFFER_H |
| 3 | |
Tim Yao | 6017d49 | 2023-08-07 15:05:53 -0700 | [diff] [blame^] | 4 | #include <time.h> |
Tim Yao | 10e5d9b | 2020-12-30 17:38:10 -0800 | [diff] [blame] | 5 | #include <boost/interprocess/managed_shared_memory.hpp> |
| 6 | |
| 7 | using namespace boost::interprocess; |
| 8 | |
| 9 | class IpcBuffer |
| 10 | { |
| 11 | public: |
| 12 | IpcBuffer(const char *name, size_t capacity); |
| 13 | ~IpcBuffer(); |
| 14 | |
| 15 | size_t size() const { return size_; } |
| 16 | size_t capacity() const { return capacity_; } |
| 17 | size_t write(const uint8_t *data, size_t bytes); |
| 18 | size_t read(uint8_t *data, size_t bytes); |
| 19 | void write_nb(const uint8_t *data, size_t bytes); |
| 20 | void get_write_position(uint64_t& time, uint64_t& position); |
| 21 | void reset(); |
| 22 | uint8_t *start_ptr(); |
Tim Yao | 6017d49 | 2023-08-07 15:05:53 -0700 | [diff] [blame^] | 23 | uint64_t wp() { return wr_position_; } |
Tim Yao | 10e5d9b | 2020-12-30 17:38:10 -0800 | [diff] [blame] | 24 | const char *name(); |
| 25 | |
Tim Yao | 6017d49 | 2023-08-07 15:05:53 -0700 | [diff] [blame^] | 26 | uint32_t getUnderrun() { return underrun_; } |
| 27 | void incUnderrun() { underrun_++; } |
| 28 | |
| 29 | void addSilence(size_t count) { silence_inserted_ += count; } |
| 30 | size_t getSilenceInserted() { return silence_inserted_; } |
| 31 | |
| 32 | void setMeta(uint64_t meta_64, uint32_t meta_32); |
| 33 | void getMeta(struct timespec *ts, uint64_t *meta_64, uint32_t *meta_32); |
| 34 | |
Tim Yao | 10e5d9b | 2020-12-30 17:38:10 -0800 | [diff] [blame] | 35 | private: |
| 36 | // Note: members can be access from different process |
| 37 | // only keep information which are common to all processes |
| 38 | // addresses must be mapped during runtime |
| 39 | size_t begin_index_, end_index_, size_, capacity_; |
| 40 | managed_shared_memory::handle_t handle_; |
| 41 | std::string name_; |
| 42 | bool blocking_; |
Tim Yao | 6017d49 | 2023-08-07 15:05:53 -0700 | [diff] [blame^] | 43 | interprocess_mutex mutex_; |
Tim Yao | 10e5d9b | 2020-12-30 17:38:10 -0800 | [diff] [blame] | 44 | uint64_t wr_position_; |
| 45 | uint64_t wr_time_; |
Tim Yao | 6017d49 | 2023-08-07 15:05:53 -0700 | [diff] [blame^] | 46 | uint32_t underrun_; |
| 47 | size_t silence_inserted_; |
| 48 | struct timespec meta_ts_; |
| 49 | uint64_t meta_64_; |
| 50 | uint32_t meta_32_; |
Tim Yao | 10e5d9b | 2020-12-30 17:38:10 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | #endif |