boost asio stateful server design

﹥>﹥吖頭↗ 提交于 2019-12-05 19:16:37

The overall design looks okay, but there are a few implementation errors:

  • Session::handler(): message, the underlying buffer provided async_write(), may go out of scope before async_write() is invoked. This is a violation of the guarantee required by the API. Consider making message a member variable for Session, or make it static within the member function. This is the relevant excerpt from the documentation:

    Although the buffers object may be copied as necessary, ownership of the underlying memory blocks is retained by the caller, which must guarantee that they remain valid until the handler is called. Here is the relevant excerpt from the documentation:

  • For objects inheriting from boost::enable_shared_from_this, use shared_from_this() instead of the this pointer when passing the instance handle to boost::bind. Otherwise, it is possible that the object instance pointed to by this may be deleted prior to the handler running.

Also, to address the question in the code comment within the Server::handler(), if an error occurs, the Session will be deleted once the handler returns since it is managed via a boost::shared_ptr.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!