Skip to content

Commit

Permalink
AI automatically translates all annotation content into English
Browse files Browse the repository at this point in the history
  • Loading branch information
alexliyu7352 committed Sep 12, 2024
1 parent 1b36624 commit 53e7dec
Show file tree
Hide file tree
Showing 51 changed files with 2,284 additions and 495 deletions.
75 changes: 50 additions & 25 deletions src/Network/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ template <typename T> struct is_pointer<std::shared_ptr<T const> > : public std:
template <typename T> struct is_pointer<T*> : public std::true_type {};
template <typename T> struct is_pointer<const T*> : public std::true_type {};

//缓存基类
//缓存基类 [AUTO-TRANSLATED:d130ab72]
//Cache base class
class Buffer : public noncopyable {
public:
using Ptr = std::shared_ptr<Buffer>;

Buffer() = default;
virtual ~Buffer() = default;

//返回数据长度
//返回数据长度 [AUTO-TRANSLATED:955f731c]
//Return data length
virtual char *data() const = 0;
virtual size_t size() const = 0;

Expand All @@ -49,7 +51,8 @@ class Buffer : public noncopyable {
}

private:
//对象个数统计
//对象个数统计 [AUTO-TRANSLATED:3b43e8c2]
//Object count statistics
ObjectStatistic<Buffer> _statistic;
};

Expand Down Expand Up @@ -107,7 +110,8 @@ class BufferOffset : public Buffer {

using BufferString = BufferOffset<std::string>;

//指针式缓存对象,
//指针式缓存对象, [AUTO-TRANSLATED:c8403290]
//Pointer-style cache object,
class BufferRaw : public Buffer {
public:
using Ptr = std::shared_ptr<BufferRaw>;
Expand All @@ -120,32 +124,38 @@ class BufferRaw : public Buffer {
}
}

//在写入数据时请确保内存是否越界
//在写入数据时请确保内存是否越界 [AUTO-TRANSLATED:5602043e]
//When writing data, please ensure that the memory does not overflow
char *data() const override {
return _data;
}

//有效数据大小
//有效数据大小 [AUTO-TRANSLATED:b8dcbda7]
//Effective data size
size_t size() const override {
return _size;
}

//分配内存大小
//分配内存大小 [AUTO-TRANSLATED:cce87adf]
//Allocated memory size
void setCapacity(size_t capacity) {
if (_data) {
do {
if (capacity > _capacity) {
//请求的内存大于当前内存,那么重新分配
//请求的内存大于当前内存,那么重新分配 [AUTO-TRANSLATED:65306424]
//If the requested memory is greater than the current memory, reallocate
break;
}

if (_capacity < 2 * 1024) {
//2K以下,不重复开辟内存,直接复用
//2K以下,不重复开辟内存,直接复用 [AUTO-TRANSLATED:056416c0]
//Less than 2K, do not repeatedly allocate memory, reuse directly
return;
}

if (2 * capacity > _capacity) {
//如果请求的内存大于当前内存的一半,那么也复用
//如果请求的内存大于当前内存的一半,那么也复用 [AUTO-TRANSLATED:c189d660]
//If the requested memory is greater than half of the current memory, also reuse
return;
}
} while (false);
Expand All @@ -156,15 +166,17 @@ class BufferRaw : public Buffer {
_capacity = capacity;
}

//设置有效数据大小
//设置有效数据大小 [AUTO-TRANSLATED:efc4fb3e]
//Set valid data size
virtual void setSize(size_t size) {
if (size > _capacity) {
throw std::invalid_argument("Buffer::setSize out of range");
}
_size = size;
}

//赋值数据
//赋值数据 [AUTO-TRANSLATED:0b91b213]
//Assign data
void assign(const char *data, size_t size = 0) {
if (size <= 0) {
size = strlen(data);
Expand Down Expand Up @@ -196,7 +208,8 @@ class BufferRaw : public Buffer {
size_t _size = 0;
size_t _capacity = 0;
char *_data = nullptr;
//对象个数统计
//对象个数统计 [AUTO-TRANSLATED:3b43e8c2]
//Object count statistics
ObjectStatistic<BufferRaw> _statistic;
};

Expand Down Expand Up @@ -275,39 +288,48 @@ class BufferLikeString : public Buffer {

BufferLikeString &erase(size_t pos = 0, size_t n = std::string::npos) {
if (pos == 0) {
//移除前面的数据
//移除前面的数据 [AUTO-TRANSLATED:b025d3c5]
//Remove data from the front
if (n != std::string::npos) {
//移除部分
//移除部分 [AUTO-TRANSLATED:a650bef2]
//Remove part
if (n > size()) {
//移除太多数据了
//移除太多数据了 [AUTO-TRANSLATED:64460d15]
//Removed too much data
throw std::out_of_range("BufferLikeString::erase out_of_range in head");
}
//设置起始便宜量
//设置起始便宜量 [AUTO-TRANSLATED:7a0250bd]
//Set starting offset
_erase_head += n;
data()[size()] = '\0';
return *this;
}
//移除全部数据
//移除全部数据 [AUTO-TRANSLATED:3d016f79]
//Remove all data
_erase_head = 0;
_erase_tail = _str.size();
data()[0] = '\0';
return *this;
}

if (n == std::string::npos || pos + n >= size()) {
//移除末尾所有数据
//移除末尾所有数据 [AUTO-TRANSLATED:efaf1165]
//Remove all data from the end
if (pos >= size()) {
//移除太多数据
//移除太多数据 [AUTO-TRANSLATED:dc9347c3]
//Removed too much data
throw std::out_of_range("BufferLikeString::erase out_of_range in tail");
}
_erase_tail += size() - pos;
data()[size()] = '\0';
return *this;
}

//移除中间的
//移除中间的 [AUTO-TRANSLATED:fd25344c]
//Remove the middle
if (pos + n > size()) {
//超过长度限制
//超过长度限制 [AUTO-TRANSLATED:9ae84929]
//Exceeds the length limit
throw std::out_of_range("BufferLikeString::erase out_of_range in middle");
}
_str.erase(_erase_head + pos, n);
Expand Down Expand Up @@ -415,14 +437,16 @@ class BufferLikeString : public Buffer {

std::string substr(size_t pos, size_t n = std::string::npos) const {
if (n == std::string::npos) {
//获取末尾所有的
//获取末尾所有的 [AUTO-TRANSLATED:8a0b92b6]
//Get all at the end
if (pos >= size()) {
throw std::out_of_range("BufferLikeString::substr out_of_range");
}
return _str.substr(_erase_head + pos, size() - pos);
}

//获取部分
//获取部分 [AUTO-TRANSLATED:d01310a4]
//Get part
if (pos + n > size()) {
throw std::out_of_range("BufferLikeString::substr out_of_range");
}
Expand All @@ -441,7 +465,8 @@ class BufferLikeString : public Buffer {
size_t _erase_head;
size_t _erase_tail;
std::string _str;
//对象个数统计
//对象个数统计 [AUTO-TRANSLATED:3b43e8c2]
//Object count statistics
ObjectStatistic<BufferLikeString> _statistic;
};

Expand Down
75 changes: 50 additions & 25 deletions src/Network/BufferSock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ class BufferCallBack {

void sendCompleted(bool flag) {
if (_cb) {
//全部发送成功或失败回调
//全部发送成功或失败回调 [AUTO-TRANSLATED:6b9a9abf]
//All send success or failure callback
while (!_pkt_list.empty()) {
_cb(_pkt_list.front().first, flag);
_pkt_list.pop_front();
Expand All @@ -109,7 +110,8 @@ class BufferCallBack {

void sendFrontSuccess() {
if (_cb) {
//发送成功回调
//发送成功回调 [AUTO-TRANSLATED:52759efc]
//Send success callback
_cb(_pkt_list.front().first, true);
}
_pkt_list.pop_front();
Expand Down Expand Up @@ -183,19 +185,22 @@ ssize_t BufferSendMsg::send_l(int fd, int flags) {
#endif

if (n >= (ssize_t)_remain_size) {
//全部写完了
//全部写完了 [AUTO-TRANSLATED:c990f48a]
//All written
_remain_size = 0;
sendCompleted(true);
return n;
}

if (n > 0) {
//部分发送成功
//部分发送成功 [AUTO-TRANSLATED:4c240905]
//Partial send success
reOffset(n);
return n;
}

//一个字节都未发送
//一个字节都未发送 [AUTO-TRANSLATED:c33c611b]
//Not a single byte sent
return n;
}

Expand All @@ -205,10 +210,12 @@ ssize_t BufferSendMsg::send(int fd, int flags) {

ssize_t sent = remain_size - _remain_size;
if (sent > 0) {
//部分或全部发送成功
//部分或全部发送成功 [AUTO-TRANSLATED:a3f5e70e]
//Partial or all send success
return sent;
}
//一个字节都未发送成功
//一个字节都未发送成功 [AUTO-TRANSLATED:858b63e5]
//Not a single byte sent successfully
return -1;
}

Expand All @@ -223,18 +230,21 @@ void BufferSendMsg::reOffset(size_t n) {
offset += ref.len;
#endif
if (offset < n) {
//此包发送完毕
//此包发送完毕 [AUTO-TRANSLATED:759b9f0e]
//This package is sent
sendFrontSuccess();
continue;
}
_iovec_off = i;
if (offset == n) {
//这是末尾发送完毕的一个包
//这是末尾发送完毕的一个包 [AUTO-TRANSLATED:6a3b77e4]
//This is the last package sent
++_iovec_off;
sendFrontSuccess();
break;
}
//这是末尾发送部分成功的一个包
//这是末尾发送部分成功的一个包 [AUTO-TRANSLATED:64645cef]
//This is the last package partially sent
size_t remain = offset - n;
#if !defined(_WIN32)
ref.iov_base = (char *)ref.iov_base + ref.iov_len - remain;
Expand Down Expand Up @@ -323,12 +333,15 @@ ssize_t BufferSendTo::send(int fd, int flags) {
continue;
}

//n == -1的情况
//n == -1的情况 [AUTO-TRANSLATED:305fb5bc]
//n == -1 case
if (get_uv_error(true) == UV_EINTR) {
//被打断,需要继续发送
//被打断,需要继续发送 [AUTO-TRANSLATED:6ef0b34d]
//interrupted, need to continue sending
continue;
}
//其他原因导致的send返回-1
//其他原因导致的send返回-1 [AUTO-TRANSLATED:299cddb7]
//other reasons causing send to return -1
break;
}
return sent ? sent : -1;
Expand Down Expand Up @@ -372,12 +385,14 @@ ssize_t BufferSendMMsg::send_l(int fd, int flags) {
} while (-1 == n && UV_EINTR == get_uv_error(true));

if (n > 0) {
//部分或全部发送成功
//部分或全部发送成功 [AUTO-TRANSLATED:a3f5e70e]
//partially or fully sent successfully
reOffset(n);
return n;
}

//一个字节都未发送
//一个字节都未发送 [AUTO-TRANSLATED:c33c611b]
//not a single byte sent
return n;
}

Expand All @@ -386,10 +401,12 @@ ssize_t BufferSendMMsg::send(int fd, int flags) {
while (_remain_size && send_l(fd, flags) != -1);
ssize_t sent = remain_size - _remain_size;
if (sent > 0) {
//部分或全部发送成功
//部分或全部发送成功 [AUTO-TRANSLATED:a3f5e70e]
//partially or fully sent successfully
return sent;
}
//一个字节都未发送成功
//一个字节都未发送成功 [AUTO-TRANSLATED:858b63e5]
//not a single byte sent successfully
return -1;
}

Expand All @@ -400,12 +417,14 @@ void BufferSendMMsg::reOffset(size_t n) {
assert(hdr.msg_len <= io.iov_len);
_remain_size -= hdr.msg_len;
if (hdr.msg_len == io.iov_len) {
//这个udp包全部发送成功
//这个udp包全部发送成功 [AUTO-TRANSLATED:fce1cc86]
//this UDP packet sent successfully
it = _hdrvec.erase(it);
sendFrontSuccess();
continue;
}
//部分发送成功
//部分发送成功 [AUTO-TRANSLATED:4c240905]
//partially sent successfully
io.iov_base = (char *)io.iov_base + hdr.msg_len;
io.iov_len -= hdr.msg_len;
break;
Expand Down Expand Up @@ -444,24 +463,30 @@ BufferSendMMsg::BufferSendMMsg(List<std::pair<Buffer::Ptr, bool>> list, SendResu
BufferList::Ptr BufferList::create(List<std::pair<Buffer::Ptr, bool> > list, SendResult cb, bool is_udp) {
#if defined(_WIN32)
if (is_udp) {
// sendto/send 方案,待优化
// sendto/send 方案,待优化 [AUTO-TRANSLATED:e94184aa]
//sendto/send scheme, to be optimized
return std::make_shared<BufferSendTo>(std::move(list), std::move(cb), is_udp);
}
// WSASend方案
// WSASend方案 [AUTO-TRANSLATED:9ac7bb81]
//WSASend scheme
return std::make_shared<BufferSendMsg>(std::move(list), std::move(cb));
#elif defined(__linux__) || defined(__linux)
if (is_udp) {
// sendmmsg方案
// sendmmsg方案 [AUTO-TRANSLATED:4596c2c4]
//sendmmsg scheme
return std::make_shared<BufferSendMMsg>(std::move(list), std::move(cb));
}
// sendmsg方案
// sendmsg方案 [AUTO-TRANSLATED:8846f9c4]
//sendmsg scheme
return std::make_shared<BufferSendMsg>(std::move(list), std::move(cb));
#else
if (is_udp) {
// sendto/send 方案, 可优化?
// sendto/send 方案, 可优化? [AUTO-TRANSLATED:21dbae7c]
//sendto/send scheme, can be optimized?
return std::make_shared<BufferSendTo>(std::move(list), std::move(cb), is_udp);
}
// sendmsg方案
// sendmsg方案 [AUTO-TRANSLATED:8846f9c4]
//sendmsg scheme
return std::make_shared<BufferSendMsg>(std::move(list), std::move(cb));
#endif
}
Expand Down
Loading

0 comments on commit 53e7dec

Please sign in to comment.