Size: 7672
Comment:
|
Size: 7670
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 64: | Line 64: |
{{{ | { |
BOOST 绝对简明手册(写作中!!!!!!!) -- [email protected] TableOfContents
BOOST THE WORLD .
版本:2006.10 beta 作者:张沈鹏 电子科大大三 生物医学工程 参考:BOOST文档
[http://blog.csdn.net/zuroc 我的技术Blog] [http://www.cppblog.com/zuroc 我的C++Blog] [http://www.cnweblog.com/zuroc/ 我的文学Blog]
-- 欢迎指出错误和遗漏,提出建议和意见,请发信到[email protected] -- 欢迎转载,但请保留引用网址以获得更新
1. 序言
现在学的东西很容易忘记,写这篇文章的目的是能让我在需要时快速找回当时的感觉.
2. 编译:VC2005注意
在 属性->C/C++->预处理器->预处理定义 中加入
_CRT_SECURE_NO_DEPRECATE;
来屏蔽不必要的警告
3. Asio 网络库
Boost.Asio是利用当代C++的先进方法,跨平台,异步I/O模型的C++网络库.
3.1. 网络库:VC2005注意
在 属性->C/C++->命令行 中加入
-DBOOST_ALL_NO_LIB
来防止自动连接.
3.2. 同步Timer
本章介绍asio如何在定时器上进行阻塞等待(blocking wait).
实现,我们包含必要的头文件.
所有的asio类可以简单的通过include "asio.hpp"来调用.
#include <iostream> #include <boost/asio.hpp>
此外,这个示例用到了timer,我们还要包含Boost.Date_Time的头文件来控制时间.
#include <boost/date_time/posix_time/posix_time.hpp>
使用asio至少需要一个boost::asio::io_service对象.该类提供了访问I/O的功能.我们首先在main函数中声明它.
int main() { boost::asio::io_service io;
下一步我们声明boost::asio::deadline_timer对象.这个asio的核心类提供I/O的功能(这里更确切的说是定时功能),总是把一个io_service对象作为他的第一个构造函数,而第二个构造函数的参数设定timer会在5秒后到时(expired).
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
这个简单的示例中我们演示了定时器上的一个阻塞等待.就是说,调用boost::asio::deadline_timer::wait()的在创建后5秒内(注意:不是等待开始后),timer到时之前不会返回任何值.
一个deadline_timer只有两种状态:到时,未到时.
如果boost::asio::deadline_timer::wait()在到时的timer对象上调用,会立即return.
t.wait();
最后,我们输出理所当然的"Hello, world!"来演示timer到时了.
std::cout << "Hello, world!\n"; return 0; }
完整的代码:
#include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> int main() { boost::asio::io_service io; boost::asio::deadline_timer t(io, boost::posix_time::seconds(5)); t.wait(); std::cout << "Hello, world!\n"; return 0; }
3.3. 异步Timer
#include <iostream> #include <asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
asio的异步函数会在一个异步操作完成后被回调.这里我们定义了一个将被回调的函数.
void print(const asio::error& /*e*/) { std::cout << "Hello, world!\n"; } int main() { asio::io_service io; asio::deadline_timer t(io, boost::posix_time::seconds(5));
这里我们调用asio::deadline_timer::async_wait()来异步等待
t.async_wait(print);
最后,我们必须调用asio::io_service::run().
asio库只会调用那个正在运行的asio::io_service::run()的回调函数.
如果asio::io_service::run()不被调用,那么回调永远不会发生.
asio::io_service::run()会持续工作到点,这里就是timer到时,回调完成.
别忘了在调用 asio::io_service::run()之前设置好io_service的任务.比如,这里,如果我们忘记先调用asio::deadline_timer::async_wait()则asio::io_service::run()会在瞬间return.
io.run(); return 0; }
完整的代码:
#include <iostream> #include <asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> void print(const asio::error& /*e*/) { std::cout << "Hello, world!\n"; } int main() { asio::io_service io; asio::deadline_timer t(io, boost::posix_time::seconds(5)); t.async_wait(print); io.run(); return 0; }
3.4. 回调函数的参数
这里我们将每秒回调一次,来演示如何回调函数参数的含义
#include <iostream> #include <asio.hpp> #include <boost/bind.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
首先,调整一下timer的持续时间,开始一个异步等待.显示,回调函数需要访问timer来实现周期运行,所以我们再介绍两个新参数
- 指向timer的指针
- 一个int*来指向计数器
void print(const asio::error& /*e*/, asio::deadline_timer* t, int* count) {
我们打算让这个函数运行6个周期,然而你会发现这里没有显式的方法来终止io_service.不过,回顾上一节,你会发现当asio::io_service::run()会在所有任务完成时终止.这样我们当计算器的值达到5时(0为第一次运行的值),不再开启一个新的异步等待就可以了.
if (*count < 5) { std::cout << *count << "\n"; ++(*count);
然后,我们推迟的timer的终止时间.通过在原先的终止时间上增加延时,我们可以确保timer不会在处理回调函数所需时间内的到期.
(原文:By calculating the new expiry time relative to the old, we can ensure that the timer does not drift away from the whole-second mark due to any delays in processing the handler.)
t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
然后我们开始一个新的同步等待.如您所见,我们用把print和他的多个参数用boost::bind函数合成一个的形为void(const asio::error&)回调函数(准确的说是function object).
在这个例子中, boost::bind的asio::placeholders::error参数是为了给回调函数传入一个error对象.当开始一个异步操作并且使用boost::bind时,你必须使用它来匹配回调函数的参数表.下一节中你会学到回调函数有时会省略这个参数.
t->async_wait(boost::bind(print, asio::placeholders::error, t, count)); } } int main() { asio::io_service io; int count = 0; asio::deadline_timer t(io, boost::posix_time::seconds(1));
和上面一样,我们再一次使用了绑定asio::deadline_timer::async_wait()
t.async_wait(boost::bind(print, asio::placeholders::error, &t, &count)); io.run();
在结尾,我们打印出的最后一次没有设置timer的调用的count的值
std::cout << "Final count is " << count << "\n"; return 0; }
完整的代码:
#include <iostream> #include <asio.hpp> #include <boost/bind.hpp> #include <boost/date_time/posix_time/posix_time.hpp> void print(const asio::error& /*e*/, asio::deadline_timer* t, int* count) { if (*count < 5) { std::cout << *count << "\n"; ++(*count); t->expires_at(t->expires_at() + boost::posix_time::seconds(1)); t->async_wait(boost::bind(print, asio::placeholders::error, t, count)); } } int main() { asio::io_service io; int count = 0; asio::deadline_timer t(io, boost::posix_time::seconds(1)); t.async_wait(boost::bind(print, asio::placeholders::error, &t, &count)); io.run(); std::cout << "Final count is " << count << "\n"; return 0; }