What are the most appropriate boost::asio read/write methods to use for a game server? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this questionI am using boost::asio to make a socket network. The library has a number of different methods for sending and receiving data, each of which perfor开发者_如何学Pythonm a similar task with a slight difference.
For sending data, boost provides the following functions:
boost::asio::basic_stream_socket::async_send()
boost::asio::basic_stream_socket::async_write_some()
boost::asio::basic_stream_socket::async_write()
And for receiving data, boost provides these functions:
boost::asio::basic_stream_socket::async_receive()
boost::asio::basic_stream_socket::async_read_some()
boost::asio::basic_stream_socket::async_read()
Out of these, which one(s) are the most appropriate to use for a game server, and why?
The async_read()
and async_write()
free functions are composed operations. They are implemented in terms of zero or more calls to the stream's async_read_some()
or async_write_some()
methods.
The member functions may not transmit or receive all data before the asynchronous operation completes. You should use the free functions if you desire that behavior.
The question's already been answered, but it's also worth mentioning that async_write_some
is exactly the same function as async_send
, the naming is just there for consistency since "send" applies to sockets and "write" applies to streams.
Another important point to note is that the async_write
free function is what Chris Kohlhoff calls a "composed operation", and this means that even though it returns immediately, it's not safe to call another async_write
until the associated handler has been called - this can cause all sorts of problems if not anticipated. The async_write_some
function is lower level, and doesn't suffer from this.
Further reading: http://blog.think-async.com/2009/08/composed-operations-coroutines-and-code.html
精彩评论