9 Star 53 Fork 16

CageQ / knet

 / 详情

问个问题

已完成
创建于  
2020-10-23 17:21

你的代码中,调用asio::async_write的回调中,还有判断缓冲中的数据如果没有发生完,接着发送,如:
if (!ec && tcp_sock.is_open() && length > 0 ) {
connection->handle_event(EVT_SEND);
{
ilog("send out length {}",length);
std::lock_guardstd::mutex guard(m.mutex);
m.send_buffer.consume(length);
if (m.send_buffer.size() == 0){
m.is_writing = false;
return ;
}
}
self->do_async_write();
但是asio文档中说,asio::async_write要么发送完所有数据,要么出错,所以,上述异常处理代码是否必要
/// Start an asynchronous operation to write all of the supplied data to a
/// stream.
/**

  • This function is used to asynchronously write a certain number of bytes of
  • data to a stream. The function call always returns immediately. The
  • asynchronous operation will continue until one of the following conditions
  • is true:
  • @Luokery All of the data in the supplied buffers has been written. That is, the
  • bytes transferred is equal to the sum of the buffer sizes.
  • @Luokery An error occurred.

评论 (1)

zhangyy75 创建了任务
zhangyy75 关联仓库设置为CageQ/knet
展开全部操作日志

这个地方不是判断异步io是否发送完所有数据的问题,而是判断send_buffer ,重新发起do_async_write并不在锁的范围内,那么上面那个锁解锁后,发送方可能插入了数据,如果不重新发起write操作,用户的send操作就不会触发write了。这里是多加一层保障。 我这里单线程循环一个context,理论上不应该出现这个问题。

CageQ 任务状态待办的 修改为已完成

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
C++
1
https://gitee.com/cageq/knet.git
git@gitee.com:cageq/knet.git
cageq
knet
knet

搜索帮助