This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleChannel.cpp
147 lines (131 loc) · 4.5 KB
/
SimpleChannel.cpp
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
144
145
146
147
//
// Created by capitalg on 2018/8/17.
//
#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include "SimpleChannel.h"
using namespace boost::interprocess;
SimpleChannel::SimpleChannel(SimpleChannelInfo *shm_channel_info) : shm_channel_info(shm_channel_info) {
shm_channel_name = shm_channel_info->shm_name;
shared_memory_object shm(open_only, shm_channel_name.c_str(), read_write);
offset_t shm_size = 0;
assert(shm.get_size(shm_size));
region_ptr = std::make_unique<mapped_region>(shm, read_write, 0, shm_size);
}
int SimpleChannel::channel_write_raw(const void *buffer, size_t buf_len) {
if (get_remaining_write_bytes() < buf_len) {
return -1;
}
uint32_t size = get_shm_size();
uint32_t write_shift = get_write_index();
uint32_t read_shift = get_read_index();
char *dest = get_shm_ptr();
if (write_shift >= read_shift) {
uint32_t write_to_tail_bytes = size - write_shift;
// 能在队列尾直接写完
if (buf_len < write_to_tail_bytes) {
memcpy(dest + write_shift, buffer, buf_len);
set_write_index(write_shift + buf_len);
return 0;
}
// 需要在空闲的队列头部写
if (buf_len < write_to_tail_bytes + read_shift) {
memcpy(dest + write_shift, buffer, write_to_tail_bytes);
memcpy(dest, static_cast<const char*>(buffer) + write_to_tail_bytes, buf_len - write_shift);
set_write_index(buf_len - write_to_tail_bytes);
return 0;
}
}
else {
uint32_t write_to_tail_bytes = read_shift - write_shift;
if (buf_len < write_to_tail_bytes) {
memcpy(dest + write_shift, buffer, buf_len);
set_write_index(write_shift + buf_len);
return 0;
}
else {
return -1;
}
}
return -1;
}
int SimpleChannel::channel_read_raw(void *buffer, size_t len) {
if (get_remaining_read_bytes() < len) {
return -1;
}
uint32_t size = get_shm_size();
uint32_t read_shift = get_read_index();
uint32_t write_shift = get_write_index();
char *src = get_shm_ptr();
if (read_shift <= write_shift) {
memcpy(buffer, src + read_shift, len);
set_read_index(read_shift + len);
return 0;
}
else {
uint32_t read_to_tail_bytes = size - read_shift;
if (len <= read_to_tail_bytes) {
memcpy(buffer, src + read_shift, len);
set_read_index(read_shift + len);
return 0;
}
else {
memcpy(buffer, src + read_shift, read_to_tail_bytes);
uint32_t remain_to_read = len - read_to_tail_bytes;
memcpy(static_cast<char*>(buffer) + read_to_tail_bytes, src, remain_to_read);
set_read_index(remain_to_read);
return 0;
}
}
return -1;
}
int SimpleChannel::channel_peek_raw(void *buffer, size_t len) {
if (get_remaining_read_bytes() < len) {
return -1;
}
uint32_t size = get_shm_size();
uint32_t read_shift = get_read_index();
uint32_t write_shift = get_write_index();
char *src = get_shm_ptr();
if (read_shift <= write_shift) {
memcpy(buffer, src + read_shift, len);
return 0;
}
else {
uint32_t read_to_tail_bytes = size - read_shift;
if (len <= read_to_tail_bytes) {
memcpy(buffer, src + read_shift, len);
return 0;
}
else {
memcpy(buffer, src + read_shift, read_to_tail_bytes);
uint32_t remain_to_read = len - read_to_tail_bytes;
memcpy(static_cast<char*>(buffer) + read_to_tail_bytes, src, remain_to_read);
return 0;
}
}
return -1;
}
int SimpleChannel::channel_send_msg(const void *msg_buffer, size_t message_len) {
if (get_remaining_write_bytes() < message_len + sizeof(uint32_t)) {
return -1;
}
uint32_t len = message_len;
channel_write_raw(reinterpret_cast<char*>(&len), sizeof(len));
channel_write_raw(msg_buffer, message_len);
return 0;
}
int SimpleChannel::channel_resv_msg(void *msg_buffer, size_t &max_msg_len) {
if (get_remaining_read_bytes() < sizeof(uint32_t)) {
return -1;
}
uint32_t len = 0;
channel_peek_raw(&len, sizeof(len));
if (get_remaining_read_bytes() < sizeof(uint32_t) + len) {
return -1;
}
max_msg_len = len;
channel_read_raw(&len, sizeof(len));
channel_read_raw(msg_buffer, len);
return 0;
}