Asio Basic Asio Anatomy

基本Asio解剖

Asio may be used to perform both synchronous and asynchronous operations on I/O objects such as sockets. Before using Asio it may be useful to get a conceptual picture of the various parts of Asio, your program, and how they work together.
As an introductory example, let’s consider what happens when you perform a connect operation on a socket. We shall start by examining synchronous operations.

Asio 可用于对 I/O 对象如套接字执行同步和异步操作。在使用 Asio 之前,了解 Asio 的各个部分、您的程序以及它们如何协同工作的概念图可能会很有用。
作为一个介绍性示例,让我们考虑在套接字上执行连接操作时会发生什么。我们将从检查同步操作开始。
sync_op
Your program will have at least one I/O execution context, such as an asio::io_context object, asio::thread_pool object, or asio::system_context. This I/O execution context represents your program’s link to the operating system’s I/O services.
你的程序将至少有一个I/O执行上下文,例如一个asio::io_context对象,asio::thread_pool 对象,或者asio::system_contex。这个I/O执行上下文标识你的程序与操作系统I/O服务的链接。

asio::io_context io_context;

To perform I/O operations your program will need an I/O object such as a TCP socket:
要执行I/O操作,你的程序需要一个I/O对象,例如TCP socket:

asio::ip::tcp::socket socket(io_context);

When a synchronous connect operation is performed, the following sequence of events occurs:
当执行一个同步连接操作时,以下event序列将发生:

  1. Your program initiates the connect operation by calling the I/O object:
    你的程序通过调用I/O对象来启动连接操作:

    socket.connect(server_endpoint);
    
  2. The I/O object forwards the request to the I/O execution context.
    I/O对象将请求转发到I/O执行上下文

  3. The I/O execution context calls on the operating system to perform the connect operation.
    I/O执行上下文调用操作系统来执行连接操作

  4. The operating system returns the result of the operation to the I/O execution context.
    操作系统将操作结果返回给I/O执行上下文

  5. The I/O execution context translates any error resulting from the operation into an object of type asio::error_code. An error_code may be compared with specific values, or tested as a boolean (where a false result means that no error occurred). The result is then forwarded back up to the I/O object.
    I/O执行上下文将操作结果的任何错误转换为asio::error_code类型的对象。一个error_code可以与特定值进行比较,或者作为bool值进行测试(false表示没有错误发生)。然后结果将被转发到I/O对象。

  6. The I/O object throws an exception of type asio::system_error if the operation failed. If the code to initiate the operation had instead been written as:
    如果操作失败,I/O对象将抛出一个asio::system_error类型的异常。如果改为以下代码启动操作:

    asio::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将被设置为操作的结果,不会抛出异常。

When an asynchronous operation is used, a different sequence of events occurs.
当使用异步操作时,会发生不同的事件序列。
async_op1

1.Your program initiates the connect operation by calling the I/O object:
你的程序通过调用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 asio::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 I/O execution context.
I/O对象将请求转发到I/O执行上下文

3.The I/O execution context signals to the operating system that it should start an asynchronous connect.
I/O执行上下文向操作系统发信号,它应该启动一个异步连接。

Time passes. (In the synchronous case this wait would have been contained entirely within the duration of the connect operation.)
时间流逝。(在同步场景下,此等待将完全包含在连接操作期间内)
async_op2

4.The operating system indicates that the connect operation has completed by placing the result on a queue, ready to be picked up by the I/O execution context.
操作系统通过将结果放入队列来指明连接操作已完成,准备好被I/O执行上下文获取。

5.When using an io_context as the I/O execution context, your program must make a call to io_context::run() (or to one of the similar io_context member functions) in order for the result to be retrieved. A call to io_context::run() blocks while there are unfinished asynchronous operations, so you would typically call it as soon as you have started your first asynchronous operation.
当使用io_context作为I/O执行上下文,你的程序必须调用io_context::run()(或者类似的io_context成员函数)以便检索结果。在有未完成的异步操作时,io_context::run()的调用会被阻塞,所以你通常会在开始你的第一个异步操作后立即调用它。

6.While inside the call to io_context::run(), the I/O execution context dequeues the result of the operation, translates it into an error_code, and then passes it to your completion handler.
在io_context::run()调用内部,I/O执行上下文将操作结果出列,将其转换为一个error_code,然后将其传递给your_completion_handler。

This is a simplified picture of how Asio operates. You will want to delve further into the documentation if your needs are more advanced, such as extending Asio to perform other types of asynchronous operations.
这是Asio运行的简化图。如果你有更高需求,例如扩展Asion以执行其他类型的异步操作,你需要更深入地研究文档

Asio - Basic Asio Anatomy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值