posix_quic-master接受收据

本文深入探讨了QUIC协议在POSIX环境下的数据读取过程,详细跟踪了QuicRead()和QuicReadv()函数,尤其是对Readv()函数的多次调用和跟进分析,揭示了QUIC在接收数据时的底层机制。

读数据

res = QuicRead(fd, buf, sizeof(buf));//读事件

跟踪一下这个QuicRead()函数

ssize_t QuicRead(QuicStream stream, void* data, size_t length)
{
    struct iovec iov;
    iov.iov_base = data;
    iov.iov_len = length;
    return QuicReadv(stream, &iov, 1);
}

跟踪这个QuicReadv()函数

ssize_t QuicRead(QuicStream stream, void* data, size_t length)
{
    struct iovec iov;
    iov.iov_base = data;
    iov.iov_len = length;
    return QuicReadv(stream, &iov, 1);
}

//跟进这个QuicReadv()函数

ssize_t QuicReadv(QuicStream stream, const struct iovec* iov, int iov_count)
{
    auto streamPtr = EntryBase::GetFdManager().Get(stream);
    if (!streamPtr || streamPtr->Category() != EntryCategory::Stream) {
        DebugPrint(dbg_api, "stream = %d, return = -1, errno = EBADF", stream);
        errno = EBADF;
        return -1;
    }

    ssize_t res = ((QuicStreamEntry*)streamPtr.get())->Readv(iov, iov_count);
    DebugPrint(dbg_api, "stream = %d, return = %ld, errno = %d", stream, res, errno);
    return res;
}

//跟进这个Readv()函数

ssize_t QuicStreamEntry::Readv(const struct iovec* iov, size_t iov_count)
{
    if (Error()) {
        errno = Error();
        return -1;
    }

    if (finRead_) {
        errno = 0;
        return 0;
    }

    auto stream = GetQuartcStream();
    if (!stream) {
        errno = EBADF;
        return -1;
    }

    int res = stream->Readv(iov, iov_count);
    if (res == 0) {
        if (Error()) {
            errno = Error();
            return -1;
        }

        if (finRead_) {
            errno = 0;
            return 0;
        }

        errno = EAGAIN;
        return -1;
    }

    errno = 0;
    return res;
}

//跟进这个Readv()函数

int QuartcStream::Readv(const struct iovec* iov, size_t iov_len) {
  int res = sequencer()->Readv(iov, iov_len);
  if (sequencer()->IsClosed())
    OnFinRead();
  return res;
}

//Readv()函数

int QuicStreamSequencer::Readv(const struct iovec* iov, size_t iov_len) {
  DCHECK(!blocked_);
  QuicString error_details;
  size_t bytes_read;
  QuicErrorCode read_error =
      buffered_frames_.Readv(iov, iov_len, &bytes_read, &error_details);
  if (read_error != QUIC_NO_ERROR) {
    QuicString details =
        QuicStrCat("Stream ", stream_->id(), ": ", error_details);
    stream_->CloseConnectionWithDetails(read_error, details);
    return static_cast<int>(bytes_read);
  }

  stream_->AddBytesConsumed(bytes_read);
  return static_cast<int>(bytes_read);
}

//跟进这个Readv函数

QuicErrorCode QuicStreamSequencerBuffer::Readv(const iovec* dest_iov,
                                               size_t dest_count,
                                               size_t* bytes_read,
                                               QuicString* error_details) {
//接受对象是否损坏
  CHECK_EQ(destruction_indicator_, 123456) << "This object has been destructed";

  *bytes_read = 0;
  for (size_t i = 0; i < dest_count && ReadableBytes() > 0; ++i) {
    char* dest = reinterpret_cast<char*>(dest_iov[i].iov_base);
    CHECK_NE(dest, nullptr);
    size_t dest_remaining = dest_iov[i].iov_len;
    while (dest_remaining > 0 && ReadableBytes() > 0) {
      size_t block_idx = NextBlockToRead();
      size_t start_offset_in_block = ReadOffset();
      size_t block_capacity = GetBlockCapacity(block_idx);
      size_t bytes_available_in_block = std::min<size_t>(
          ReadableBytes(), block_capacity - start_offset_in_block);
      size_t bytes_to_copy =
          std::min<size_t>(bytes_available_in_block, dest_remaining);
      DCHECK_GT(bytes_to_copy, 0UL);
      if (blocks_[block_idx] == nullptr || dest == nullptr) {
        *error_details = QuicStrCat(
            "QuicStreamSequencerBuffer error:"
            " Readv() dest == nullptr: ",
            (dest == nullptr), " blocks_[", block_idx,
            "] == nullptr: ", (blocks_[block_idx] == nullptr),
            " Gaps: ", GapsDebugString(),
            " Remaining frames: ", ReceivedFramesDebugString(),
            " total_bytes_read_ = ", total_bytes_read_);
        return QUIC_STREAM_SEQUENCER_INVALID_STATE;
      }
      memcpy(dest, blocks_[block_idx]->buffer + start_offset_in_block,
             bytes_to_copy);
      dest += bytes_to_copy;
      dest_remaining -= bytes_to_copy;
      num_bytes_buffered_ -= bytes_to_copy;
      total_bytes_read_ += bytes_to_copy;
      *bytes_read += bytes_to_copy;

      // Retire the block if all the data is read out and no other data is
      // stored in this block.
      // In case of failing to retire a block which is ready to retire, return
      // immediately.
      if (bytes_to_copy == bytes_available_in_block) {
        bool retire_successfully = RetireBlockIfEmpty(block_idx);
        if (!retire_successfully) {
          *error_details = QuicStrCat(
              "QuicStreamSequencerBuffer error: fail to retire block ",
              block_idx,
              " as the block is already released, total_bytes_read_ = ",
              total_bytes_read_, " Gaps: ", GapsDebugString());
          return QUIC_STREAM_SEQUENCER_INVALID_STATE;
        }
      }
    }
  }

  if (*bytes_read > 0) {
    UpdateFrameArrivalMap(total_bytes_read_);
  }
  return QUIC_NO_ERROR;
}

你列出的是 `x86_64-w64-mingw32` 工具链在 Ubuntu 上安装后提供的完整命令集合,这些工具来自 **MinGW-w64** 的 GCC 编译器套件,用于交叉编译 Windows 程序(64 位)。 下面我将 **逐一解释这些命令的用途和区别**,并说明如何正确使用它们来构建 C/C++ 程序。 --- ## 🧰 MinGW-w64 工具链详解(针对 `x86_64-w64-mingw32`) ### ✅ 基本命名规则 格式为: ``` <target-triplet>-<tool-name>[-(posix|win32)] ``` - `x86_64-w64-mingw32`: 目标平台三元组 - 架构:x86_64(64位) - 供应商:w64(支持 Win64 的 MinGW 分支) - 系统:Windows - `posix` / `win32`: 表示线程模型或异常处理方式 - `posix`: 使用 POSIX 风格的线程和异常处理(推荐,更现代、稳定) - `win32`: 使用 Windows 原生线程模型(旧版兼容) > ⚠️ 如果没有后缀,默认行为取决于你的系统配置。通常建议使用 `-posix` 版本。 --- ## 🔍 各命令详细说明 | 命令 | 作用 | 推荐使用 | |------|------|----------| | `x86_64-w64-mingw32-gcc` | C 编译器(主入口) | ✅ 是 | | `x86_64-w64-mingw32-g++` | C++ 编译器(主入口) | ✅ 是 | | `x86_64-w64-mingw32-gcc-posix` | 使用 POSIX 线程模型的 GCC | ✅ 强烈推荐 | | `x86_64-w64-mingw32-g++-posix` | 使用 POSIX 线程模型的 G++ | ✅ 强烈推荐 | | `x86_64-w64-mingw32-gcc-win32` | 使用 Win32 线程模型的 GCC | ❌ 不推荐(易崩溃) | | `x86_64-w64-mingw32-g++-win32` | 使用 Win32 线程模型的 G++ | ❌ 不推荐 | 📌 **最佳实践:优先使用 `-posix` 结尾的版本** ```bash # 推荐写法(C++) x86_64-w64-mingw32-g++-posix -static -o myapp.exe main.cpp # 或简写(如果软链接已设置) x86_64-w64-mingw32-g++ -static -o myapp.exe main.cpp ``` --- ### 🛠 辅助工具说明 | 命令 | 作用 | |------|------| | `x86_64-w64-mingw32-gcc-ar` / `...-ar-posix` / `...-ar-win32` | 归档工具,打包静态库 `.a` 文件(相当于 Linux 的 ar) | | `x86_64-w64-mingw32-gcc-ranlib` / `...-ranlib-posix` / `...-ranlib-win32` | 为静态库生成索引(提高链接速度) | | `x86_64-w64-mingw32-gcc-nm` / `...-nm-posix` / `...-nm-win32` | 查看目标文件符号表(调试用) | | `x86_64-w64-mingw32-gcc-dumpmachine` | 输出目标机器类型(调试工具链时有用) | | `x86_64-w64-mingw32-gcov` / `...-gcov-dump` / `...-gcov-tool` | 代码覆盖率分析工具(配合 `-fprofile-arcs -ftest-coverage` 使用) | | `x86_64-w64-mingw32-gprof` | 性能分析工具(需编译时加 `-pg`) | --- ### 📦 示例:编译一个简单的 C++ 程序 #### 创建 `main.cpp` ```cpp #include <iostream> using namespace std; int main() { cout << "Hello from MinGW-w64 on Linux!" << endl; return 0; } ``` #### 使用推荐工具编译 ```bash x86_64-w64-mingw32-g++-posix -static -s -o hello.exe main.cpp ``` 参数说明: - `-static`: 静态链接运行时库,避免依赖 `libstdc++-6.dll` 等 - `-s`: strip 符号信息,减小体积 - `-o hello.exe`: 输出 Windows 可执行文件 #### 检查输出 ```bash file hello.exe ``` 输出应为: ``` hello.exe: PE32+ executable (console) x86-64, for MS Windows ``` --- ### 💡 如何选择正确的工具? | 场景 | 推荐命令 | |------|---------| | 编译普通 C 程序 | `x86_64-w64-mingw32-gcc-posix` | | 编译 C++ 程序 | `x86_64-w64-mingw32-g++-posix` | | 创建静态库 | `x86_64-w64-mingw32-gcc-ar-posix` | | 查看符号 | `x86_64-w64-mingw32-gcc-nm` | | 调试覆盖率 | `x86_64-w64-mingw32-gcov` | > 实际上,大多数情况下你可以直接使用不带后缀的 `g++` 或 `gcc`,因为它们通常是符号链接到 `-posix` 版本。 你可以检查一下: ```bash ls -l $(which x86_64-w64-mingw32-g++) ``` 很可能指向 `x86_64-w64-mingw32-g++-posix` --- ### ⚠️ 注意事项 1. **不要混用 `posix` 和 `win32` 编译的对象文件** 它们使用不同的异常处理机制(SEH vs SJLJ),混合会导致运行时崩溃。 2. **推荐始终使用 `-static` 编译 GUI 程序** 避免用户缺少 MinGW 运行时 DLL。 3. **Qt 程序必须使用与 Qt 构建时一致的线程模型** 若 Qt 是用 `posix` 编译的,则你也必须用 `g++-posix` 编译你的程序。 --- ### ✅ 总结:你应该怎么用? ```bash # ✅ 推荐做法(C++) x86_64-w64-mingw32-g++-posix -static -O2 -s -o app.exe main.cpp # 或者简写(等效) x86_64-w64-mingw32-g++ -static -O2 -s -o app.exe main.cpp ``` 只要确保你的工具链默认使用的是 `posix` 模型即可。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值