-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.cpp
More file actions
143 lines (112 loc) · 3.06 KB
/
Buffer.cpp
File metadata and controls
143 lines (112 loc) · 3.06 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
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
#include "Buffer.h"
#include <iostream>
#include <algorithm>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
const char Buffer::kCRLF[] = "\r\n";
Buffer::Buffer(size_t initialSize)
: buffer_(kCheapPrepend+initialSize),
readIndex_(kCheapPrepend),
writeIndex_(kCheapPrepend) {}
size_t Buffer::readableBytes() const {
return writeIndex_ - readIndex_;
}
size_t Buffer::writableBytes() const {
return buffer_.size() - writeIndex_;
}
size_t Buffer::prependableBytes() const {
return readIndex_;
}
const char* Buffer::peek() const {
return begin_() + readIndex_;
}
const char* Buffer::findCRLF() const {
const char* crlf = std::search(peek(), beginWrite(), kCRLF, kCRLF+2);
return crlf == beginWrite() ? NULL : crlf;
}
void Buffer::retrieve(size_t len) {
if (len < readableBytes()) {
readIndex_ += len;
} else {
retrieveAll();
}
}
void Buffer::retrieveUtil(const char* end) {
retrieve(end - peek());
}
void Buffer::retrieveAll() {
readIndex_ = kCheapPrepend;
writeIndex_ = kCheapPrepend;
}
void Buffer::append(const char* data, size_t len) {
ensureWritableBytes(len);
copy(data, data+len, beginWrite());
writeIndex_ += len;
}
void Buffer::ensureWritableBytes(size_t len) {
if (writableBytes() < len) {
makeSpace_(len);
}
}
ssize_t Buffer::readFd(int fd) {
// saved an ioctl()/FIONREAD call to tell how much to read
char extrabuf[65536];
struct iovec vec[2];
const size_t writable = writableBytes();
vec[0].iov_base = begin_()+writeIndex_;
vec[0].iov_len = writable;
vec[1].iov_base = extrabuf;
vec[1].iov_len = sizeof(extrabuf);
// when there is enough space in this buffer, don't read into extrabuf.
// when extrabuf is used, we read 128k-1 bytes at most.
const int iovcnt = (writable < sizeof extrabuf) ? 2 : 1;
const ssize_t n = readv(fd, vec, iovcnt);
if (n < 0) {
// FIXME: 停止程序
exit(1);
}
else if (static_cast<size_t>(n) <= writable)
{
writeIndex_ += n;
}
else
{
writeIndex_ = buffer_.size();
append(extrabuf, n - writable);
}
return n;
}
char* Buffer::beginWrite() {
return begin_() + writeIndex_;
}
const char* Buffer::beginWrite() const {
return begin_() + writeIndex_;
}
char* Buffer::beginRead() {
return begin_() + readIndex_;
}
const char* Buffer::beginRead() const {
return begin_() + readIndex_;
}
char* Buffer::begin_() {
return &*buffer_.begin();
}
const char* Buffer::begin_() const {
return &*buffer_.begin();
}
void Buffer::makeSpace_(size_t len) {
if (writableBytes() + prependableBytes() < len + kCheapPrepend) {
buffer_.resize(writeIndex_+len);
moveReadableToFront_();
} else {
moveReadableToFront_();
}
}
void Buffer::moveReadableToFront_() {
size_t readable = readableBytes();
copy(begin_()+readIndex_, begin_()+writeIndex_, begin_()+kCheapPrepend);
readIndex_ = kCheapPrepend;
writeIndex_ = kCheapPrepend + readable;
}