1

I want to increase the throughput of my udp gameserver which uses Boost ASIO. Right now, everytime i need to send a packet, i am putting it in a queue, then checking if there is a pending async_send_to operation, if yes, do nothing, if not, call async_send_to. Then i wait for the write handler to be called and then call async_send_to for the next packet in queue, if any.

The documentation says that it is the way to do it "for TCP socket", but there is NOTHING on the whole internet about UDP socket. Try it, search it on stackoverflow, you will see nobody talks about this, and for the 2 questions you will find, the question is left ignored by users.

Why is it kept a secret? And for the 1million dollar question, can i safely call async_send_to multiple time in a row WITHOUT waiting for the write handler to be called?

Thanks in advance.

4

2 回答 2

1

这个逻辑对于 UDP 协议是没有意义的,因为它不需要阻塞发送操作。数据报要么被传送要么丢失。UDP 不必将其存储在输出缓冲区中并无限期地重新发送多次,直到它得到 ACK 数据包。

于 2017-08-10T03:26:32.547 回答
1

不,您不能async_send_to在不等待调用写入处理程序的情况下安全地连续多次调用。请参阅Asynchronous IO with Boost.Asio以了解确切原因。

但是,asio支持分散聚集,因此您可以async_send_to使用多个缓冲区调用,例如:

typedef std::deque<boost::asio::const_buffer> ConstBuffers;

std::string msg_1("Blah");
...
std::string msg_n("Blah");

ConstBuffers buffers;
buffers.push_back(msg_1);
...
buffers.push_back(msg_n);

socket_.async_send_to(buffers, tx_endpoint_, write_handler);

double buffering因此,您可以通过消息队列和使用收集的写入来增加吞吐量......

于 2017-08-10T07:54:20.157 回答