tcpBuffer 初步封装
This commit is contained in:
parent
87203cc26f
commit
40503d68ce
55
includes/net/tcp/tcp_buffer.hpp
Normal file
55
includes/net/tcp/tcp_buffer.hpp
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstring>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
namespace tinyrpc {
|
||||||
|
class TcpBuffer {
|
||||||
|
public:
|
||||||
|
TcpBuffer(std::size_t size) : m_buffer(size) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
~TcpBuffer() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
std::size_t getReadable() const {
|
||||||
|
return m_write_index - m_read_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t getWriteable() const {
|
||||||
|
return m_buffer.size() - m_write_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* getWriteAddress() {
|
||||||
|
return m_buffer.data() + m_write_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* getReadAddress() {
|
||||||
|
return m_buffer.data() + m_read_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
void adjustBuffer();
|
||||||
|
|
||||||
|
bool readOffset(std::size_t offset) ;
|
||||||
|
bool writeOffset(std::size_t offset) ;
|
||||||
|
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
int readFromBuffer(void* buf, std::size_t size);
|
||||||
|
int writeToBuffer(const void* buf, std::size_t size);
|
||||||
|
|
||||||
|
void resize(std::size_t size);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::size_t m_read_index{0};
|
||||||
|
std::size_t m_write_index{0};
|
||||||
|
|
||||||
|
std::vector<char> m_buffer{};
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
100
src/net/tcp/tcp_buffer.cc
Normal file
100
src/net/tcp/tcp_buffer.cc
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
#include "tcp_buffer.hpp"
|
||||||
|
#include "logger.hpp"
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstring>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace tinyrpc {
|
||||||
|
|
||||||
|
void TcpBuffer::adjustBuffer() {
|
||||||
|
|
||||||
|
std::vector<char> newBuffer(m_buffer.size());
|
||||||
|
std::memcpy(newBuffer.data(), m_buffer.data() + m_read_index, getReadable());
|
||||||
|
m_buffer.swap(newBuffer);
|
||||||
|
m_write_index -= m_read_index;
|
||||||
|
m_read_index = 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool TcpBuffer::readOffset(std::size_t offset) {
|
||||||
|
int newReadIdx = m_read_index + offset;
|
||||||
|
if(newReadIdx > m_write_index) {
|
||||||
|
logger() << "read index overflow write index";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
m_read_index = newReadIdx;
|
||||||
|
if(getWriteable() < m_read_index) {
|
||||||
|
adjustBuffer();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TcpBuffer::writeOffset(std::size_t offset) {
|
||||||
|
int newWriteIdx = m_write_index + offset;
|
||||||
|
|
||||||
|
if(newWriteIdx > m_buffer.size()) {
|
||||||
|
logger() << "newReadIdx overflow buffer size";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_write_index = newWriteIdx;
|
||||||
|
|
||||||
|
if(getWriteable() < m_read_index) {
|
||||||
|
adjustBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TcpBuffer::clear() {
|
||||||
|
m_buffer.clear();
|
||||||
|
m_read_index = m_write_index = 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
int TcpBuffer::readFromBuffer(void* buf, std::size_t size) {
|
||||||
|
|
||||||
|
int cnt = std::min(getReadable(), size);
|
||||||
|
if(cnt == 0) return 0;
|
||||||
|
memcpy(buf, getReadAddress(), cnt);
|
||||||
|
m_read_index += cnt;
|
||||||
|
|
||||||
|
if(getWriteable() < m_read_index) {
|
||||||
|
adjustBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
return cnt;
|
||||||
|
|
||||||
|
}
|
||||||
|
int TcpBuffer::writeToBuffer(const void* buf, std::size_t size) {
|
||||||
|
|
||||||
|
if(getWriteable() < size && m_read_index == 0) {
|
||||||
|
resize((m_buffer.size() + size) * 1.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(getWriteable() < size) {
|
||||||
|
adjustBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(getWriteable() < size) {
|
||||||
|
resize((m_buffer.size() + size) * 1.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
int cnt = std::min(getWriteable(), size);
|
||||||
|
if(cnt == 0) return 0;
|
||||||
|
memcpy(getWriteAddress(), buf, cnt);
|
||||||
|
m_write_index += cnt;
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TcpBuffer::resize(std::size_t size) {
|
||||||
|
std::vector<char> newBuffer(size);
|
||||||
|
int cnt = std::min(size, getReadable());
|
||||||
|
memcpy(newBuffer.data(), getReadAddress(), cnt);
|
||||||
|
m_write_index = cnt;
|
||||||
|
m_read_index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user