libp2p / cpp-libp2p

C++17 implementation of libp2p
Apache License 2.0
346 stars 97 forks source link

使用 example/02-kademlia/rendezvous_chat.cpp 遇到的问题 #231

Closed Phypeng closed 5 months ago

Phypeng commented 5 months ago

Hi, 我是第一次使用 libp2p, 在测试 rendezvous_chat 时, FindProviders 总是获取不到 Provider.

所以我修改了日志级别为Debug, 发现 src/protocol/kademlia/impl/session.cpp 中的 Session::onMessageWritten 函数, 在写完业务数据包后, 立即尝试调用 close(), 导致 Client 认为链接被重置.

请评估是否可以按如下方法进行修改?


void Session::onMessageWritten(
    outcome::result<size_t> res,
    const std::shared_ptr<ResponseHandler> &response_handler) {
  if (not res) {
    close(res.as_failure());
    return;
  }

  --writing_;

  if (not response_handlers_.empty()) {
    read();
  }

  // if (canBeClosed()) {
  //   close();
  // }
}
Phypeng commented 5 months ago

我想了一下, 这样修改可能会导致连接不能及时断开. 这种问题的出现, 有以下几种可能: 1, 帧发生了乱序, 导致提前处理了FIN; //这种低级错误不至于现在才发现 2, 处理数据帧的使用了异步方法, 导致数据帧处理滞后; //这种处理是常规操作 3, 流状态处理存在问题. //大概率是这里的问题

所以我继续看了一下代码, 发现 YamuxStream::onFINReceived() 函数中, 接收到 FIN, 立刻将 isreadable 置为 false. 此时, 如果数据帧和FIN帧到达的时间间隔太小, 便会出现此问题.