-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcshm.h
143 lines (111 loc) · 3.63 KB
/
cshm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* CSHM
*
* Created on: Mar 8, 2009
* Desc : This contains the code for a Circular SHM implementation
*/
#ifndef CSHM_H_
#define CSHM_H_
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define CSHM_VERSION 1
typedef enum {
ACH_OK = 0,
ACH_OVERFLOW,
ACH_INVALID_NAME,
ACH_INVALID_BUFFER,
ACH_BAD_SHM_FILE,
ACH_FAILED_SYSCALL,
ACH_STALE_FRAMES,
ACH_MISSED_FRAME,
ACH_TIMEOUT,
ACH_READER_SLOW,
ACH_READER_FAST
} cshm_error;
void cshm_error_cstr(cshm_error err);
typedef struct {
uint32_t total_frames; //< number of entries in index
size_t frame_size; //< size of data frame
uint32_t last_frame;
uint64_t last_seq_num;
pthread_rwlock_t lock;
} cshm_header_t, cshm_stat_t;
typedef struct {
uint64_t seq_num; //< number of frame
size_t size_written; //< size of frame
size_t offset; //< byte offset of entry from beginning of data array
double time; //< last written at this time
int valid;
} cshm_buffer_index_t, cshm_buffer_info_t;
class cshm {
private:
int fd;
struct stat sbuf;
char* fd_name;
size_t total_size;
size_t header_size;
size_t index_size;
size_t frame_size;
int total_frames;
char* head_ptr;
char* index_ptr;
char* frame_ptr;
uint32_t last_index_set;
uint64_t last_seq_set;
int is_open;
cshm_header_t* shm_header;
cshm_buffer_index_t* shm_index;
private:
void getIndex(uint32_t index);
void setIndex(uint32_t index);
void getFrame(uint32_t index, void* buffer, size_t buf_size);
void setFrame(uint32_t index, void* buffer, size_t buf_size);
void updateHeader();
void updateFromHeader();
public:
/// Checks whether a channel exists or not. And if it does, returns the total number of frames and frame size. Calculate total size.
/// \param channel_name Input channel_name to query
/// \param _total_frame Returns total number of circular buffer frames
/// \param _frame_size Returns size of a single frame
static int ChannelInfo(const char* channel_name, int* _total_frame, size_t* _frame_size);
/// Get latest index and sequence number from the CSHM header
void getLastWrittenBufferInfo(uint32_t *_index, uint64_t *_sequence);
/// Get all the info on the current channel
void getChannelStats(cshm_stat_t *stats);
/// Create an instance of the CSHM
/// \param channel_name Input channel_name
/// \param _total_frames Input total number of frames
/// \param _frame_size Input maximum size of each frame
cshm(const char* channel_name, int _total_frames, size_t _frame_size);
/// Try to open file and map it to our pointers
cshm_error Open();
/// Write buffer to the CHSM
/// \param buffer Input buffer address
/// \param buffer_size Input size of buffer
/// \param c_time Input current time
cshm_error Write(void* buffer, size_t buffer_size, double c_time);
/// Read buffer from the CSHM
/// \param buffer Input buffer address
/// \param buffer_size Size of input buffer after read
/// \param index Input index to read from
/// \param sequence Sequence of buffer is CSHM
cshm_error Read(void* buffer, cshm_buffer_info_t* info, uint32_t index);
/// Read latest buffer written to the CSHM
/// \param buffer Input buffer address
/// \param buffer_size Size of input buffer after read
/// \param index Input Index of buffer after read
/// \param sequence Sequence of buffer is CSHM
cshm_error ReadLatest(void* buffer, cshm_buffer_info_t* info);
/// Close the connection to CSHM instance
void close_cshm();
/// Close the connection to CSHM instance. Same as close_ach()
~cshm(void);
};
#endif /* CSHM_H_ */