-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo_queue.h
More file actions
76 lines (58 loc) · 1.39 KB
/
fifo_queue.h
File metadata and controls
76 lines (58 loc) · 1.39 KB
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
#ifndef FIFO_QUEUE_H_
#define FIFO_QUEUE_H_
#include<stdint.h>
#ifdef PTHREAD
#include"mutex.h"
using namespace Mutex;
#else
#include<thread>
#include<mutex>
#include<condition_variable>
#endif
#define MAX_PACK_SIZE 65535
#define INIT_BUF_SIZE 10000000
#define END_FLAG 0x0000
#define PACK_LEN_SIZE 2
#define USE_LOCK
struct Cell{
uint32_t cell_id;
uint32_t unicast_ip; //unicast ip, this is not necessary, 4 bytes len
uint16_t data_len; //data length of cell_body
int body_start_pos; //the cell body's start position
char cell_data[900]; //pointer to the buffer where store cell body
Cell():unicast_ip(0), data_len(0), body_start_pos(0){
};
~Cell(){
}
};
class FifoQueue{
public:
FifoQueue(int buf_size = INIT_BUF_SIZE);
~FifoQueue();
void Reset();
uint16_t PeekNextPackLen();
bool Update(char*& recv_pos, int recv_len);
//push a complete packet into buffer
bool PushPacket(char* packet, uint16_t len);
//pop a complete packet from buffer
bool PopPacket(char* pacet, uint16_t * len);
private:
void Init(int);
char* buffer_;
char* buf_end_;
volatile char* read_pos_;
volatile char* write_pos_;
volatile uint64_t read_bytes_;
volatile uint64_t write_bytes_;
int buf_size_;
#ifdef USE_LOCK
#ifdef PTHREAD
MutexLock mutex_lock_;
Condition condition_;
#else
std::mutex mutex_lock_;
std::condition_variable condition_;
#endif
#endif
};
#endif