Files
CProxy/lib/Buffer.h
linzhaosheng c682805f71 命名规范
2022-08-21 04:43:21 +08:00

30 lines
860 B
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include <iostream>
#include <memory>
class Buffer {
public:
// write_index_是可写的所以长度为size的buffer最多只能存储size-1的数据
Buffer(int capacity = 8, int max_capacity = 1024)
: capacity_(capacity), data_size_(capacity + 1) {
max_capacity_ = max_capacity > capacity ? max_capacity : capacity;
data_ = (char *)malloc(data_size_);
}
~Buffer() { free(data_); }
size_t read(char *data, int expect_len);
size_t WriteToBuffer(char *data, int expect_len);
size_t WriteToSock(int fd);
int GetUnreadSize();
private:
void ensureInsert(int insert_len);
int getFreeSize();
void resize(int new_size);
int capacity_;
int data_size_;
int max_capacity_;
char *data_;
int read_index_ = 0;
int write_index_ = 0; // write_index_处可写
};
typedef std::shared_ptr<Buffer> SP_Buffer;