-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.h
48 lines (45 loc) · 1.14 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
#pragma once
#include <string>
using namespace std;
class Buffer
{
public:
Buffer(int size);
~Buffer();
// 扩容
void extendRoom(int size);
// 得到剩余的可写的内存容量
inline int writeableSize()
{
return m_capacity - m_writePos;
}
// 得到剩余的可读的内存容量
inline int readableSize()
{
return m_writePos - m_readPos;
}
// 写内存 1. 直接写 2. 接收套接字数据
int appendString(const char* data, int size);
int appendString(const char* data);
int appendString(const string data);
int socketRead(int fd);
// 根据\r\n取出一行, 找到其在数据块中的位置, 返回该位置
char* findCRLF();
// 发送数据
int sendData(int socket); // 指向内存的指针
// 得到读数据的起始位置
inline char* data()
{
return m_data + m_readPos;
}
inline int readPosIncrease(int count)
{
m_readPos += count;
return m_readPos;
}
private:
char* m_data;
int m_capacity;
int m_readPos = 0;
int m_writePos = 0;
};