Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 242

xiongying/cpp-tbox

forked from cpp-master/cpp-tbox
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (12)
Tags (3)
master
develop
fix-ActionFinishIssue
feature-DBus
feature-epoll
feature-update-readme
feature-cmake
feature-Dns
refactory-Flow
feature-Broker
feature-autotools
feature-TcpSSL
2.2
v2.1
v2.0
master
Branches (12)
Tags (3)
master
develop
fix-ActionFinishIssue
feature-DBus
feature-epoll
feature-update-readme
feature-cmake
feature-Dns
refactory-Flow
feature-Broker
feature-autotools
feature-TcpSSL
2.2
v2.1
v2.0
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (12)
Tags (3)
master
develop
fix-ActionFinishIssue
feature-DBus
feature-epoll
feature-update-readme
feature-cmake
feature-Dns
refactory-Flow
feature-Broker
feature-autotools
feature-TcpSSL
2.2
v2.1
v2.0
cpp-tbox
/
modules
/
network
/
buffer.cpp
cpp-tbox
/
modules
/
network
/
buffer.cpp
buffer.cpp 4.64 KB
Copy Edit Raw Blame History
海卫哥 authored 2023年07月25日 00:13 +08:00 . tidy: 添加文件头
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include <cstring>
#include <utility>
#include <tbox/base/assert.h>
#include <tbox/base/defines.h>
#include "buffer.h"
namespace tbox {
namespace network {
Buffer::Buffer(size_t reverse_size)
{
//! 如果预留空间大小,那么要为缓冲分配空间
if (reverse_size > 0) {
uint8_t *p_buff = new uint8_t [reverse_size];
TBOX_ASSERT(p_buff != nullptr);
buffer_ptr_ = p_buff;
buffer_size_ = reverse_size;
}
}
Buffer::Buffer(const Buffer &other)
{
cloneFrom(other);
}
Buffer::Buffer(Buffer &&other)
{
swap(other);
}
Buffer::~Buffer()
{
CHECK_DELETE_RESET_ARRAY(buffer_ptr_);
}
Buffer& Buffer::operator = (const Buffer &other)
{
if (this != &other)
cloneFrom(other);
return *this;
}
Buffer& Buffer::operator = (Buffer &&other)
{
if (this != &other) {
reset();
swap(other);
}
return *this;
}
void Buffer::swap(Buffer &other)
{
if (&other == this)
return;
std::swap(other.buffer_ptr_, buffer_ptr_);
std::swap(other.buffer_size_, buffer_size_);
std::swap(other.read_index_, read_index_);
std::swap(other.write_index_, write_index_);
}
void Buffer::reset()
{
Buffer tmp(0);
swap(tmp);
}
bool Buffer::ensureWritableSize(size_t write_size)
{
if (write_size == 0)
return true;
//! 空间足够
if (writableSize() >= write_size)
return true;
//! 检查是否可以通过将数据往前挪是否可以满足空间要求
if ((writableSize() + read_index_) >= write_size) {
//! 将 readable 区的数据往前移
::memmove(buffer_ptr_, (buffer_ptr_ + read_index_), (write_index_ - read_index_));
write_index_ -= read_index_;
read_index_ = 0;
return true;
} else { //! 只有重新分配更多的空间才可以
size_t new_size = (write_index_ + write_size) << 1; //! 两倍扩展
uint8_t *p_buff = new uint8_t[new_size];
if (p_buff == nullptr)
return false;
if (buffer_ptr_ != nullptr) {
//! 只需要复制 readable 部分数据
::memcpy((p_buff + read_index_), (buffer_ptr_ + read_index_), (write_index_ - read_index_));
delete [] buffer_ptr_;
}
buffer_ptr_ = p_buff;
buffer_size_ = new_size;
return true;
}
}
void Buffer::hasWritten(size_t write_size)
{
if (write_index_ + write_size > buffer_size_) {
write_index_ = buffer_size_;
} else {
write_index_ += write_size;
}
}
size_t Buffer::append(const void *p_data, size_t data_size)
{
if (ensureWritableSize(data_size)) {
::memcpy(writableBegin(), p_data, data_size);
hasWritten(data_size);
return data_size;
}
return 0;
}
void Buffer::hasRead(size_t read_size)
{
if (read_index_ + read_size > write_index_) {
read_index_ = write_index_ = 0;
} else {
read_index_ += read_size;
if (read_index_ == write_index_)
read_index_ = write_index_ = 0;
}
}
void Buffer::hasReadAll()
{
read_index_ = write_index_ = 0;
}
size_t Buffer::fetch(void *p_buff, size_t buff_size)
{
size_t read_size = (buff_size > readableSize()) ? readableSize() : buff_size;
::memcpy(p_buff, readableBegin(), read_size);
hasRead(read_size);
return read_size;
}
//! 不是完全复制,只复制有效的数据
void Buffer::cloneFrom(const Buffer &other)
{
CHECK_DELETE_RESET_ARRAY(buffer_ptr_);
//! 如果 other 有可读数据,则要根据可读大小分配空间
if (other.readableSize() > 0) {
uint8_t *p_buff = new uint8_t[other.readableSize()];
TBOX_ASSERT(p_buff != nullptr);
::memcpy(p_buff, other.readableBegin(), other.readableSize());
buffer_ptr_ = p_buff;
buffer_size_ = write_index_ = other.readableSize();
} else {
buffer_ptr_ = nullptr;
buffer_size_ = write_index_ = 0;
}
read_index_ = 0;
}
void Buffer::shrink()
{
Buffer tmp(*this); //! 将自己复制给 tmp,其间会缩减空间
swap(tmp); //! 与 tmp 交换
}
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

C++的百宝箱,是一个完备的Linux应用l软件开发工具库与运行框架。 它有通信库(TCP/UDP/串口)、HTTP、线程池、定时器池、协程、日志、命令终端、状态机、行为树等非常实用的开发组件,它还有完备实用的启动框架。 它可以让应用开发者从实现细节中解放出来,专注于功能逻辑。
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/VisionDeveloper/cpp-tbox.git
git@gitee.com:VisionDeveloper/cpp-tbox.git
VisionDeveloper
cpp-tbox
cpp-tbox
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /