确定我可以向文件句柄写入多少内容;将数据从一个 FH 复制到另一个 FH
如何确定是否可以将给定数量的字节写入文件句柄(实际上是套接字)? (或者,如何“取消读取”我从其他文件句柄读取的数据?)
我想要类似的内容:
n = how_much_can_I_write(w_handle); n = read(r_handle, buf, n); assert(n==write(w_handle, buf, n));
两个文件句柄(r_handle 和 w_handle)都已从 epoll_wait 接收到就绪状态。
我希望将 r_handle 中的所有数据复制到 w_handle 而不使用“写债务”缓冲区。
一般来说,如何简单可靠地将数据从一个文件句柄复制到另一个文件句柄?
How to determine if I can write the given number of bytes to a filehandle (socket actually)? (Alternatively, how to "unread" the data I had read from other filehandle?)
I want something like:
n = how_much_can_I_write(w_handle); n = read(r_handle, buf, n); assert(n==write(w_handle, buf, n));
Both filehandles (r_handle and w_handle) have received ready status from epoll_wait.
I want all data from r_handle to be copied to w_handle without using a "write debt" buffer.
In general, how to copy the data from one filehandle to the other simply and reliably?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能这样做 - 一旦数据被写入,它就被写入 - 该操作是不可逆的或提前可预测的。你需要重新思考你的程序逻辑。
You can't do that - once the data is written, it's written - the operation is not reversible or predictable in advance. You need to rethink your program logic.
我认为没有任何界面可以让您访问该信息,而且一旦您获得它,它就会过时。
我建议将两个文件描述符设置为非阻塞,然后读/写 1K(可能更大)块,直到获得 EAGAIN/EWOULDBLOCK,这时您应该缓存一个块,直到下一次写入 fd 准备好为止。
无论如何,您都需要有一个缓冲区来执行读/写周期,因此为写债务保留缓冲区应该是一个太大的问题?
I don't think there's any interface that allows you access to that information, and it would be stale as soon as you got it anyway.
I'd suggest setting both file descriptors to non-blocking, then reading/writing 1K (maybe larger) blocks until you get EAGAIN/EWOULDBLOCK, when you should cache one block until the next time the write fd is ready.
You need to have a buffer for doing the read/write cycle anyway, so keeping the buffer for the write-debt should be too much of a problem?