简体   繁体   English

c sockets sendmsg MSG_DONTWAIT - 缓冲区重用

[英]c sockets sendmsg MSG_DONTWAIT - buffer reuse

I'm using C Sockets to send ICMP packets with the MSG_DONTWAIT flag set.我正在使用 C Sockets 发送设置了 MSG_DONTWAIT 标志的 ICMP 数据包。 My program is single threaded but it expected to send messages at high frequency so I'm setting the message send as non blocking.我的程序是单线程的,但它希望以高频率发送消息,所以我将消息发送设置为非阻塞。 Is it safe to share/modify/reuse the message buffer after each call?每次调用后共享/修改/重用消息缓冲区是否安全? (Unless EAGAIN or EWOULDBLOCK is returned). (除非返回 EAGAIN 或 EWOULDBLOCK)。

msg_control (the ancillary data) is reused and msg_control->struct in_pktinfo->ipi_ifindex (outbound interface ifindex) is modified between calls. msg_control(辅助数据)被重用,并且在调用之间修改 msg_control->struct in_pktinfo->ipi_ifindex(出站接口 ifindex)。

The iov.iov_base buffer content (not pointer.) and iov.iov_len can also change between calls. iov.iov_base 缓冲区内容(不是指针。)和 iov.iov_len 也可以在调用之间更改。 (Less likely but still possible). (不太可能但仍然可能)。

Is it OK to change ifinex and iov_base content between calsl at high frequency in non blocking mode?在非阻塞模式下高频改变calsl之间的ifinex和iov_base内容可以吗? (unless I get back EAGAIN or EWOULDBLOCK) (除非我回来 EAGAIN 或 EWOULDBLOCK)

Thanks !谢谢 !

Yes, it's safe.是的,它是安全的。 On Linux, all the data you specify gets immediately copied into a buffer in the kernel, before send returns.在 Linux 上,您指定的所有数据都会在send返回之前立即复制到 kernel 的缓冲区中。 If the kernel's buffer is full, it returns EAGAIN or EWOULDBLOCK (which are the same thing in Linux, apparently) and nothing happens.如果内核的缓冲区已满,它会返回 EAGAIN 或 EWOULDBLOCK(显然,这与 Linux 中的相同)并且没有任何反应。 You don't have to worry that the kernel will go and send the packet later after you've changed the data in the buffer.您不必担心 kernel 将 go 并在您更改缓冲区中的数据发送数据包。

On Windows, non-blocking "overlapped" operations do remember your buffer and use it later - so watch out for that if you ever do non-blocking I/O on Windows.在 Windows 上,非阻塞“重叠”操作确实会记住您的缓冲区并在以后使用它 - 所以如果您曾经在 Windows 上进行非阻塞 I/O,请注意这一点。 (You'll know if you do, because it's totally different from blocking I/O) (你会知道,因为它与阻塞 I/O 完全不同)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM