锻炼精神,首先要锻炼肉体
全部博文(104)
分类: C/C++
2015-06-09 14:32:51
Your program will have at least one io_service object. The io_service represents your program's link to the operating system's I/O services.
在你所编写的程序中至少需要创建一个 io_service 对象. io_service 对象用来作为你的程序与操作系统 I/O 服务二者之间的链接。
boost::asio::io_service io_service ;
To perform I/O operations your program will need an I/O object such as a TCP socket :
执行 I/O 读写操作的话,你的程序中需要创建一个类似于 TCP 套接字对象的 I/O读写对象才行.
boost::asio::ip::tcp::socket socket(io_service) ;
When a synchronous connect operation is performed,the following sequence of event occurs:
同步连接操作的执行,将会触发下面一系列的事件的发生:
1. Your program initiates the connect operation by calling the I/O object :
1. 你的应用程序通过调用 I/O 对象(中的成员方法)来启动连接操作:
socket.connect( server_endpoint) ;
2. The I/O object forwards the request to the io_service .
2. I/O 对象将会把(连接)请求信息发送至 io_service 对象.
3. The io_service calls on the operating system to perform the connect operation.
3. io_service 将会调用操作系统中的方法来完成连接操作.
4. The operating system returns the result of the operation of the io_service.
4. 系统将会把执行系统调用之后的返回值发送给 io_service 对象.
5. The io_service translates any error resulting from the operation into an object of type boost::system::error_code.
io_service 对象将会把系统传递给它的任何错误返回信息转化成 boost::system::error_code 的对象实例.
An error_code may be compared with specific values, or tested as a boolean (where a false result means that no error occured).
每一个 error_code 都可以用来和一个特定的数值进行比较(类似于进行位运算),当比较所得到的是一个布尔类型的数值(如果该布尔类型的变量是 假 的话表示的是上述的系统调用没有任何错误)
The result is then forward back up to the I/O object.
这个由 io_service 所转换(翻译)的结果,随后便会被反向传递给 I/O 读写对象.
6. The I/O object throws an exception of type boost::system::system_error if the operation failed . If the code to initiate the operation had instead been written as :
6.如果系统调用失败的话, I/O 对象会抛出一个类型为 boost::system::system_error 的异常出来. 当用于启动的代码被写成如下的形式的话:
boost::system::error_code ec ;
socket.connect(server_endpoint , ec ) ;
then the error_code variable ec would be set to the result of the operation, and no exception would be thrown .
在 error_code 的实例化变量 ec 中存放的便是上述系统调用的执行结果,(这种情况下)是不会有异常被抛出的.
boost::system::error_code ec ;
socket.connect( server_endpoint, ec ) ;
1. Your program initiates the connect operation by calling the I/O object socket.async_connect(server_endpoint, your_completion_handler) ;
1. 在你的程序中, 通过调用 I/O 这个读写对象中的 socket.async_connect(server_endpoint, your_completion_handler) ; 方法来启动连接操作
where your_completion_handler is a function or function object with the signature :
在方法中的 ‘your_completion_handler’ 对象实例是一个有着如下签名的方法或是方法实例对象
void your_completion_handler( const boost::system::error_code &ec ) ;
The exact signature required depends on the asynchronous operation being performed. The reference documentation indicates the appropriate form for each operation.
确切的签名要遵照即将执行的异步操作而定的. 相关参考文档中详细描述了适合不同类型的异步操作的签名模式.
2. The I/O object forwards the request to the io_service.
2. I/O 读写对象实体会将这一请求交付该 io_service 对象.
3. The io_service signals to the operating system that is should start any asynchronous connect.
3. io_service 将信号发送给操作系统,让其开展异步连接的操作.
Time passes. ( In the synchronous case this wait would have been contained entirely within the duration of the connect operation.)
在随后的时间中,(在同步操作中,将会进入等待状态,直到连接操作完成)
4. The operating system indicates that the connection operation has completed by placing the result on a queue, ready to be picked up by the io_service.
4.操作将系统调用的返回结果放置到等待 io_service 访问队列中,借此,来告知一个连接操作的完成.
5. Your program must make a call to io_service::run () (or to one of the similari io_service member functions) in order for the result to be retrieved.
在你的程序中必须要调用 io_service 对象中的 run() 成员方法(或是 io_service 对象中与之相似的成员方法)来获取刚刚通过 io_service 所执行系统调用的运行结果.
6. While inside the call to io_service::run(), the io_service duqueues the result of the operation , translates it into an error_code , and then passes it to your completion handler.
6. 当调用 io_service::run() 这个方法的时候,其方法内部会将刚刚执行系统调用的返回结果从队列中取出,并将取出的结果转换(翻译)成一个 error_code 实例对象,然后将该 error_code
对象传递给连接完成句柄对象.
This is a simplified picture of how Boost.Asio operates . You will want to delve further into the documentation if your needs are more advanced, such as extending Boost.Asio
to perform other types of asynchronous operations.
这便是通过简单示例图展示 Boost.Asio 的运行机理. 如果你需要实现,靠扩展 Boost.Asio 来实现其他类型的异步操作,这样高级的应用的话,你可以进一步钻研文档.