gpt4 book ai didi

c - 使用带套接字的拼接可能会导致饥饿

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:28:32 25 4
gpt4 key购买 nike

我在写一个TCP代理,使用edge-triggered epoll监听fd,splice传输数据。这是问题所在:我怎么知道套接字接收缓冲区是空的?

For example, if you call read(2) by asking to read a certain amount of data and read(2) returns a lower number of bytes, you can be sure of having exhausted the read I/O space for the file descriptor.

但我发现即使 splice(sock, 0, pfd[1], 0, 65536, SPLICE_F_NONBLOCK) < 65536 有时也会导致饥饿。

O_NONBLOCK enabled, n > PIPE_BUF If the pipe is full, then write(2) fails, with errno set to EAGAIN. Otherwise, from 1 to n bytes may be written (i.e., a "partial write" may occur; the caller should check the return value from write(2) to see how many bytes were actually written), and these bytes may be interleaved with writes by other processes.

所以我应该重复调用 splice 直到 EAGAIN?但是我怎么知道套接字接收缓冲区是空的还是管道缓冲区已满呢?

最佳答案

也许你可以使用 getsockopt 系统调用和 SO_ERROR,然后你就会知道哪个套接字是真正的 EAGAIN,然后使用 epoll 监视该套接字的读/写事件。

我在向我的 Web 服务器添加反向 http 代理时也遇到了这个问题,我认为它应该可以工作,但我不确定它是否是最佳解决方案。

关于c - 使用带套接字的拼接可能会导致饥饿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30820737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com