-
Notifications
You must be signed in to change notification settings - Fork 43
/
buffer.cpp
173 lines (147 loc) · 3.33 KB
/
buffer.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
// encode UTF-8
// @Author : Aged_cat
// @Date : 2021-05-11
#include "buffer.h"
Buffer::Buffer(int initBuffersize):buffer_(initBuffersize),readPos_(0),writePos_(0){}
size_t Buffer::readableBytes() const
{
return writePos_-readPos_;
}
size_t Buffer::writeableBytes() const
{
return buffer_.size()-writePos_;
}
size_t Buffer::readBytes() const
{
return readPos_;
}
const char* Buffer::curReadPtr() const
{
return BeginPtr_()+readPos_;
}
const char* Buffer::curWritePtrConst() const
{
return BeginPtr_()+writePos_;
}
char* Buffer::curWritePtr()
{
return BeginPtr_()+writePos_;
}
void Buffer::updateReadPtr(size_t len)
{
assert(len<=readableBytes());
readPos_+=len;
}
void Buffer::updateReadPtrUntilEnd(const char* end)
{
assert(end>=curReadPtr());
updateReadPtr(end-curReadPtr());
}
void Buffer::updateWritePtr(size_t len)
{
assert(len<=writeableBytes());
writePos_+=len;
}
void Buffer::initPtr()
{
bzero(&buffer_[0],buffer_.size());
readPos_=0;
writePos_=0;
}
void Buffer::allocateSpace(size_t len)
{
//如果buffer_里面剩余的空间有len就进行调整,否则需要申请空间。
//剩余空间包括write指针之前的空间和可写的空间
if(writeableBytes()+readBytes()<len)
{
buffer_.resize(writePos_+len+1);
}
else{
//将读指针置为0,调整一下
size_t readable=readableBytes();
std::copy(BeginPtr_()+readPos_,BeginPtr_()+writePos_,BeginPtr_());
readPos_=0;
writePos_=readable;
assert(readable==readableBytes());
}
}
void Buffer::ensureWriteable(size_t len)
{
if(writeableBytes()<len)
{
allocateSpace(len);
}
assert(writeableBytes()>=len);
}
void Buffer::append(const char* str,size_t len)
{
assert(str);
ensureWriteable(len);
std::copy(str,str+len,curWritePtr());
updateWritePtr(len);
}
void Buffer::append(const std::string& str)
{
append(str.data(),str.length());
}
void Buffer::append(const void* data,size_t len)
{
assert(data);
append(static_cast<const char*>(data),len);
}
void Buffer::append(const Buffer& buffer)
{
append(buffer.curReadPtr(),buffer.readableBytes());
}
ssize_t Buffer::readFd(int fd,int* Errno)
{
char buff[65535];//暂时的缓冲区
struct iovec iov[2];
const size_t writable=writeableBytes();
iov[0].iov_base=BeginPtr_()+writePos_;
iov[0].iov_len=writable;
iov[1].iov_base=buff;
iov[1].iov_len=sizeof(buff);
const ssize_t len=readv(fd,iov,2);
if(len<0)
{
//std::cout<<"从fd读取数据失败!"<<std::endl;
*Errno=errno;
}
else if(static_cast<size_t>(len)<=writable)
{
writePos_+=len;
}
else{
writePos_=buffer_.size();
append(buff,len-writable);
}
return len;
}
ssize_t Buffer::writeFd(int fd,int* Errno)
{
size_t readSize=readableBytes();
ssize_t len=write(fd,curReadPtr(),readSize);
if(len<0)
{
//std::cout<<"往fd写入数据失败!"<<std::endl;
*Errno=errno;
return len;
}
readPos_+=len;
return len;
}
std::string Buffer::AlltoStr()
{
std::string str(curReadPtr(),readableBytes());
initPtr();
return str;
}
char* Buffer::BeginPtr_()
{
return &*buffer_.begin();
}
const char* Buffer::BeginPtr_() const
{
return &*buffer_.begin();
}