forked from fishfishfishh/FishWebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.h
67 lines (65 loc) · 1.39 KB
/
Buffer.h
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
#pragma once
#include <vector>
#include <assert.h>
#include <iostream>
class Buffer
{
public:
static const size_t kInitialSize = 1024;
public:
explicit Buffer(size_t initialSize = kInitialSize) : charVec(initialSize),
readerIndex(0),
writerIndex(0)
{
assert(readableBytes() == 0);
assert(writableBytes() == initialSize);
}
void setSocketfd(int fd) { socketfd = fd; }
size_t readableBytes() const
{
return writerIndex - readerIndex;
}
size_t writableBytes() const
{
return charVec.size() - writerIndex;
}
char* begin()
{
return &*charVec.begin();
}
const char* begin() const
{
return &*charVec.begin();
}
char* beginRead()
{
return begin() + readerIndex;
}
char* end()
{
return &*charVec.end();
}
const char* end() const
{
return &*charVec.end();
}
const char* beginRead() const
{
return begin() + readerIndex;
}
void retrieve(size_t len) {
assert(len <= readableBytes());
if (len < readableBytes()) readerIndex += len;
else clear();
}
void append(const char* data, size_t len);
bool readFd();
void enlargeSpace(size_t len);
std::string getMessageAsString();
void clear();
private:
std::vector<char> charVec;
size_t readerIndex;
size_t writerIndex;
int socketfd;
};