黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

Boost.Asio基礎剖析

系統 2379 0

翻譯: Boost.Asio 基礎剖析 收藏

Basic Boost.Asio Anatomy

Boost.Asio基礎剖析

Boost.Asio may be used to perform both synchronous and asynchronous operations on I/O objects such as sockets. Before using Boost.Asio it may be useful to get a conceptual picture of the various parts of Boost.Asio, your program, and how they work together.

Boost.Asio可用于在諸如socket對象上執行同步和異步操作。在使用Boost.Asio之前,了解一下Boost.Asio和你的程序的各個部分的概念圖,以及它們如何一起工作,是非常有用的,

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.

作為一個入門例子,讓我們了解一下,當你在執行一個socket連接時,發生了什么情況。這里我們將開始研究 同步 操作。

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操作,你的程序需要一個 I/O 對象,如 TCP socket:。

boost::asio::ip::tcp::socket socket(io_service);

When a synchronous connect operation is performed, the following sequence of events occurs:

當異步連接操作執行時,下列事件順次發生:

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 io_service .

I/O轉發請求到io_service .

3. The io_service calls on the operating system to perform the connect operation.

io_service調用操作系統執行連接操作

4. The operating system returns the result of the operation to the io_service .

操作系統返回操作結果給 io_service

5. The io_service translates any error resulting from the operation into a boost::system::error_code. Anerror_codemay be compared with specific values, or tested as a boolean (where afalseresult means that no error occurred). The result is then forwarded back up to the I/O object .

io_service 將任何錯誤代碼轉換為 boost::system::error_code,這個error_code可以和特定值比較,也可作為boolean測試(false表示沒有錯誤發生)。結果然后轉發回 I/O 對象

6. The I/O object throws an exception of typeboost::system::system_errorif the operation failed. If the code to initiate the operation had instead been written as:

如果操作失敗, I/O 對象拋出 boost::system::system_error類型的異常。如果初始化操作的代碼按下面的方式書寫:

boost::system::error_code ec;

socket.connect(server_endpoint, ec);

then the error_codevariableecwould 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.

當使用異步操作時,將是不同的事件產生順序

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_handleris a function or function object with the signature:

這里your_completion_handler是一個帶有signature (目標識別特征)的函數或函數對象:

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.

確切的signature (目標識別特征)根據執行的異步操作的不同而不同,參考文檔為每個操作列出了正確的形式。

2. The I/O object forwards the request to the io_service .

I/O 對象將請求轉發給io_service

3. The io_service signals to the operating system that it should start an asynchronous connect.

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 connect operation has completed by placing the result on a queue, ready to be picked up by the io_service .

操作系統通過在隊列里放置一個結果來指示連接操作已經完成,這個結果等待 io_service來取。

5. Your program must make a call toio_service::run()(or to one of the similar io_service member functions) in order for the result to be retrieved. A call toio_service::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_service::run()(或相似的io_service成員函數)。當有未完成的操作時,io_service::run()的調用被阻塞,所以你要在第一次啟動異步操作后盡快調用io_service::run()。

6. While inside the call to io_service::run(), the io_service dequeues the result of the operation, translates it into anerror_code, and then passes it to your completion handler .

在io_service::run()內部, io_service 從隊列移除操作結果,轉換為 error_code ,并傳遞給 your completion handler

源文檔 < http://blog.csdn.net/asmc51/archive/2009/09/09/4536287.aspx >

Boost.Asio基礎剖析


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論