forked from douban/libmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferWriter.cpp
More file actions
94 lines (71 loc) · 1.79 KB
/
Copy pathBufferWriter.cpp
File metadata and controls
94 lines (71 loc) · 1.79 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <inttypes.h>
#include <cstdio>
#include <vector>
#include "BufferWriter.h"
#include "Utility.h"
namespace douban{
namespace mc {
namespace io {
BufferWriter::BufferWriter() :m_readIdx(0), m_msgIovlen(0) {
}
BufferWriter::~BufferWriter() {
reset();
}
void BufferWriter::reset() {
m_iovec.clear();
for (std::vector<char*>::const_iterator it = m_unsignedStringList.begin();
it != m_unsignedStringList.end(); ++it) {
delete[] *it;
}
m_unsignedStringList.clear();
m_readIdx = 0;
m_msgIovlen = 0;
}
void BufferWriter::reserve(size_t n) {
m_iovec.reserve(n);
}
void BufferWriter::takeBuffer(const char* const buf, size_t buf_len) {
struct iovec iov;
iov.iov_base = const_cast<char*>(buf);
iov.iov_len = buf_len;
m_iovec.push_back(iov);
m_msgIovlen += 1;
}
void BufferWriter::takeNumber(int64_t val) {
m_unsignedStringList.push_back(new char[32]);
char* buf = m_unsignedStringList.back();
struct iovec iov;
iov.iov_base = buf;
iov.iov_len = douban::mc::utility::int64ToCharArray(val, buf);
m_iovec.push_back(iov);
m_msgIovlen += 1;
}
#ifdef __APPLE__
const struct iovec* const BufferWriter::getReadPtr(int &n) {
#else
const struct iovec* const BufferWriter::getReadPtr(size_t &n) {
#endif
n = m_msgIovlen;
if (n > 0) {
return &m_iovec[m_readIdx];
}
return NULL;
}
void BufferWriter::commitRead(size_t nSent) {
while (m_msgIovlen > 0 && nSent >= m_iovec[m_readIdx].iov_len) {
nSent -= m_iovec[m_readIdx].iov_len;
++m_readIdx;
--m_msgIovlen;
}
if (nSent > 0) {
struct iovec* iovPtr = &m_iovec[m_readIdx];
iovPtr->iov_base = static_cast<char*>(iovPtr->iov_base) + nSent;
iovPtr->iov_len -= nSent;
}
}
size_t BufferWriter::msgIovlen() {
return m_msgIovlen;
}
} // namespace io
} // namespace mc
} // namespace douban