-
Notifications
You must be signed in to change notification settings - Fork 1
/
RingBuf.cpp
202 lines (178 loc) · 5.36 KB
/
RingBuf.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// Created by wg on 2021/1/30.
//
#include <cassert>
#include <algorithm>
#include <cstring>
#include "RingBuf.h"
RingBuf::RingBuf(ssize_t size) : mSize(size) {
assert(size > 0);
mBuf = new char[size];
mRIndex = 0;
mWIndex = 0;
mPending = 0;
}
ssize_t RingBuf::write(const char *buf, ssize_t size) {
ssize_t maxCopyLen = std::min(mSize - mPending, size);
assert(maxCopyLen >= 0);
if (maxCopyLen == 0) {
return 0;
}
ssize_t maxCopyLenTmp = maxCopyLen;
while (maxCopyLenTmp > 0) {
ssize_t copyLen = std::min(mSize - mWIndex, maxCopyLenTmp);
memcpy(mBuf + mWIndex, buf + maxCopyLen - maxCopyLenTmp, copyLen);
mPending += copyLen;
maxCopyLenTmp -= copyLen;
mWIndex = myMod(mWIndex + copyLen, mSize);
}
return maxCopyLen;
}
ssize_t RingBuf::read(char *buf, ssize_t size) {
ssize_t maxCopyLen = std::min(mPending, size);
assert(maxCopyLen >= 0);
if (maxCopyLen == 0) {
return 0;
}
ssize_t maxCopyLenTmp = maxCopyLen;
while (maxCopyLenTmp > 0) {
ssize_t copyLen = std::min(mSize - mRIndex, maxCopyLenTmp);
memcpy(buf + maxCopyLen - maxCopyLenTmp, mBuf + mRIndex, copyLen);
mPending -= copyLen;
assert(mPending >= 0);
maxCopyLenTmp -= copyLen;
mRIndex = myMod(mRIndex + copyLen, mSize);
}
return maxCopyLen;
}
bool RingBuf::empty() const {
return mPending == mSize;
}
ssize_t RingBuf::getWriteIoVec(struct iovec *out, ssize_t size) {
memset(out, 0, sizeof(iovec) * size);
ssize_t maxCopyLen = mSize - mPending;
assert(maxCopyLen >= 0);
if (maxCopyLen == 0) {
return 0;
}
ssize_t maxCopyLenTmp = maxCopyLen;
ssize_t i = 0, tmpWindex = mWIndex;
while (maxCopyLenTmp > 0 && i < size) {
ssize_t copyLen = std::min(mSize - tmpWindex, maxCopyLenTmp);
out[i].iov_base = mBuf + tmpWindex;
out[i].iov_len = copyLen;
maxCopyLenTmp -= copyLen;
tmpWindex = myMod(tmpWindex + copyLen, mSize);
i++;
}
return maxCopyLen;
}
ssize_t RingBuf::dataSize() const {
return mPending;
}
ssize_t RingBuf::freeSize() const {
return mSize - mPending;
}
ssize_t RingBuf::getReadIoVec(struct iovec *out, ssize_t size) {
memset(out, 0, sizeof(iovec) * size);
assert(mPending >= 0);
if (mPending == 0) {
return 0;
}
ssize_t maxCopyLenTmp = mPending;
ssize_t i = 0, tmpRindex = mRIndex;
while (maxCopyLenTmp > 0 && i < size) {
ssize_t copyLen = std::min(mSize - tmpRindex, maxCopyLenTmp);
out[i].iov_base = mBuf + tmpRindex;
out[i].iov_len = copyLen;
maxCopyLenTmp -= copyLen;
tmpRindex = myMod(tmpRindex + copyLen, mSize);
i++;
}
return mPending;
}
ssize_t RingBuf::readv(const struct iovec *out, int size) {
struct iovec src[2]{};
auto len = getReadIoVec(src, 2);
if (len == 0) {
return 0;
}
auto rsize = copyIoVec(src, 2, out, size);
setReadSize(rsize);
return rsize;
}
ssize_t RingBuf::writev(const struct iovec *src, int size) {
struct iovec out[2]{};
auto len = getWriteIoVec(out, 2);
if (len == 0) {
return 0;
}
auto wSize = copyIoVec(src, size, out, 2);
setWriteSize(wSize);
return wSize;
}
ssize_t RingBuf::copyIoVec(const struct iovec *src, int srcSize, const struct iovec *dst, int dstSize) {
// 分别表示i j 对应的已拷贝的数据长度
ssize_t copyLenI = 0, copyLenJ = 0;
ssize_t totalCopyLen = 0;
// i表示src的下标,j表示dst的下标
for (int i = 0, j = 0; i < srcSize && j < dstSize;) {
assert(src[i].iov_len >= copyLenI);
assert(dst[j].iov_len >= copyLenJ);
if (src[i].iov_base == nullptr || src[i].iov_len - copyLenI == 0) {
i++;
copyLenI = 0;
continue;
}
if (src[j].iov_base == nullptr || dst[j].iov_len - copyLenJ == 0) {
j++;
copyLenJ = 0;
continue;
}
ssize_t minCopyLen = std::min(src[i].iov_len - copyLenI, dst[j].iov_len - copyLenJ);
memcpy((char *) dst[j].iov_base + copyLenJ, (char *) src[i].iov_base + copyLenI, minCopyLen);
copyLenI += minCopyLen;
copyLenJ += minCopyLen;
totalCopyLen += minCopyLen;
}
return totalCopyLen;
}
void RingBuf::setWriteSize(ssize_t ssize) {
mPending += ssize;
assert(mPending <= mSize);
mWIndex = myMod(mWIndex + ssize, mSize);
}
void RingBuf::setReadSize(ssize_t ssize) {
mPending -= ssize;
assert(mPending >= 0);
mRIndex = myMod(mRIndex + ssize, mSize);
}
RingBuf::~RingBuf() {
delete mBuf;
mBuf = nullptr;
}
ssize_t RingBuf::myMod(ssize_t left, ssize_t right) {
while (left >= right) {
left -= right;
}
return left;
}
ssize_t RingBuf::peek(char *buf, ssize_t size) const {
ssize_t maxCopyLen = std::min(mPending, size);
assert(maxCopyLen >= 0);
if (maxCopyLen == 0) {
return 0;
}
ssize_t maxCopyLenTmp = maxCopyLen;
auto tmpRIndex = mRIndex;
while (maxCopyLenTmp > 0) {
ssize_t copyLen = std::min(mSize - tmpRIndex, maxCopyLenTmp);
memcpy(buf + maxCopyLen - maxCopyLenTmp, mBuf + tmpRIndex, copyLen);
maxCopyLenTmp -= copyLen;
tmpRIndex = myMod(tmpRIndex + copyLen, mSize);
}
return maxCopyLen;
}
ssize_t RingBuf::totalSize() const {
return mSize;
}