本文主要是介绍boost.asio 学习笔记06——其他,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
asnyc_read VS. async_read_some VS. async_receive
async_read是一个全局函数;后面两个则于ip::tcp::socket的成员个函数;都可以用来异步读取操作,他们有什么样的差别呢。先来看async_read_some和async_receive,他们的文档说明如下:
- async_read_some: Start an asynchronous read. This function is used to asynchronously read data from the stream socket. The function call always returns immediately.
- async_receive: Start an asynchronous receive. This function is used to asynchronously receive data from the stream socket. The function call always returns immediately.
从文档来看,只有一个单词的差别,一个是read,一个是receive;反正都是从socket中获取数据,这两个词有什么差别呢?我是看不出他们的差别,那就看代码吧:
template <typename MutableBufferSequence, typename ReadHandler> void async_read_some(const MutableBufferSequence& buffers, BOOST_ASIO_MOVE_ARG(ReadHandler) handler) { // If you get an error on the following line it means that your handler does // not meet the documented type requirements for a ReadHandler. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check; this->get_service().async_receive(this->get_implementation(), buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); } template <typename MutableBufferSequence, typename ReadHandler> void async_receive(const MutableBufferSequence& buffers, BOOST_ASIO_MOVE_ARG(ReadHandler) handler) { // If you get an error on the following line it means that your handler does // not meet the documented type requirements for a ReadHandler. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check; this->get_service().async_receive(this->get_implementation(), buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); } |
好了,有了源代码,就无从狡辩了吧。他们都是使用底层服务的async_recevie来读取数据,因此他们的功能是一样的,都是从socket获取一定的数据,但是该函数并不保证能够从获得指定长度的数据——也即不保证填满缓冲区;
如果想要保证异步操作完成时获取到指定数量(缓冲区的长度)的数据,那么使用全局函数async_read。该函数内部启动一个 read_op ,该op内部,会多次调用stream.async_read_some,直到缓冲区填满,或者读操作结束;
这既是这三个函数的异同之所在。
妖怪async_read
前面一节提到过,全局函数 async_read 会启动read_op并在该op内部反腐调用async _read_some()来读取数据直到缓冲区慢,或者EOF出现;如果要来看一下read_op的operator(),你多半会感觉看到妖怪了。乖,别怕,来看看吧:
void operator()(const boost::system::error_code& ec, std::size_t bytes_transferred, int start = 0) { std::size_t n = 0; switch (start) { case 1: n = this->check_for_completion(ec, total_transferred_); for (;;) { stream_.async_read_some( boost::asio::buffer(buffer_ + total_transferred_, n), BOOST_ASIO_MOVE_CAST(read_op)(*this)); return; default: total_transferred_ += bytes_transferred; if ((!ec && bytes_transferred == 0) || (n = this->check_for_completion(ec, total_transferred_)) == 0 || total_transferred_ == boost::asio::buffer_size(buffer_)) break; } handler_(ec, static_cast<const std::size_t&>(total_transferred_)); } } |
不知道你是否会和我一样感觉毛骨悚然,惊叫switch-case的邪乎。是的,说他妖怪,就妖怪在switch的那两个case标签,第一个 “case 1” 还算人性,第二个标签default就完全是妖怪了,它放置在for循环的内部——是的,编译器是允许的,假设程序流程首先跳转到default标签,在执行完default分支后,他会像正常的for循环流程一样,继续for loop的,这就是这个妖怪。
自己写的一个简单的测试及输出:
void jumpinto( int state ) { int i = 0; switch( state ) { case 1: cout << "case 1: begin for()" << endl; for( ; ; ) { cout << " in for loop, i = " << i++ << endl; //return; default: cout << "case default " << endl; if( i > 2 ) { cout << " i > 2, break" << endl; break; } }// end for };//end switch } int main() { jumpinto(0); return 0; } 输出如下: D:\studyprograms\test>caseblock.exe case default in for loop, i = 0 case default in for loop, i = 1 case default in for loop, i = 2 case default i > 2, break |
关于为何作者要这样写,暂时还没理解到。更多信息移驾翊坤宫再议:http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
优雅退出
使用asio开发的应用要中途退出,或者开发的server要停止,咱如何优优雅雅地清理asio组件呢?呵呵,肯定可以,asio已经提供了一些手段:signal。下面是从asio示例代码中提取出的一些代码,以示说明。
class server : private boost::noncopyable { public: explicit server(const std::string& address, const std::string& port, const std::string& doc_root, std::size_t thread_pool_size) : signals_(io_service_pool_.get_io_service()), acceptor_(...) { signals_.add(SIGINT); signals_.add(SIGTERM);
#if defined(SIGQUIT) signals_.add(SIGQUIT); #endif // defined(SIGQUIT)
signals_.async_wait(boost::bind(&server::handle_stop, this)); } private: void handle_stop() { io_service_.stop(); } boost::asio::io_service io_service_; boost::asio::signal_set signals_; boost::asio::ip::tcp::acceptor acceptor_; }; |
这篇关于boost.asio 学习笔记06——其他的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!