亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Boost.Asio基礎(chǔ)剖析

系統(tǒng) 2007 0

翻譯: Boost.Asio 基礎(chǔ)剖析 收藏

Basic Boost.Asio Anatomy

Boost.Asio基礎(chǔ)剖析

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對象上執(zhí)行同步和異步操作。在使用Boost.Asio之前,了解一下Boost.Asio和你的程序的各個部分的概念圖,以及它們?nèi)绾我黄鸸ぷ鳎欠浅S杏玫模?

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.

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

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.

你的程序?qū)⒅辽儆幸粋€ io_service 對象,這個 io_service 代表了你的程序到操作系統(tǒng)的

I/O服務(wù)的連接。

boost::asio::io_service io_service;

To perform I/O operations your program will need an I/O object such as a TCP socket:

為了執(zhí)行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:

當(dāng)異步連接操作執(zhí)行時,下列事件順次發(fā)生:

1. Your program initiates the connect operation by calling the I/O object :

你的程序通過調(diào)用 I/O 對象初始化連接操作,

socket.connect(server_endpoint);

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

I/O轉(zhuǎn)發(fā)請求到io_service .

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

io_service調(diào)用操作系統(tǒng)執(zhí)行連接操作

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

操作系統(tǒng)返回操作結(jié)果給 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 將任何錯誤代碼轉(zhuǎn)換為 boost::system::error_code,這個error_code可以和特定值比較,也可作為boolean測試(false表示沒有錯誤發(fā)生)。結(jié)果然后轉(zhuǎn)發(fā)回 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 將被設(shè)置為操作的結(jié)果,并且沒有異常拋出。

When an asynchronous operation is used, a different sequence of events occurs.

當(dāng)使用異步操作時,將是不同的事件產(chǎn)生順序

1. Your program initiates the connect operation by calling the I/O object :

你的程序通過調(diào)用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 (目標(biāo)識別特征)的函數(shù)或函數(shù)對象:

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 (目標(biāo)識別特征)根據(jù)執(zhí)行的異步操作的不同而不同,參考文檔為每個操作列出了正確的形式。

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

I/O 對象將請求轉(zhuǎn)發(fā)給io_service

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

io_service通知操作系統(tǒng),告訴操作系統(tǒng)啟動一個異步連接

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 .

操作系統(tǒng)通過在隊列里放置一個結(jié)果來指示連接操作已經(jīng)完成,這個結(jié)果等待 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.

為了確保結(jié)果能夠被收到,你的程序必須調(diào)用io_service::run()(或相似的io_service成員函數(shù))。當(dāng)有未完成的操作時,io_service::run()的調(diào)用被阻塞,所以你要在第一次啟動異步操作后盡快調(diào)用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()內(nèi)部, io_service 從隊列移除操作結(jié)果,轉(zhuǎn)換為 error_code ,并傳遞給 your completion handler

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

Boost.Asio基礎(chǔ)剖析


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲视频久久 | 日本三级一区 | 狠狠色丁香婷婷综合小时婷婷 | 无毒a网 | 26uuu欧美视频在线观看 | 国内精品久久久久久久999下 | 福利视频欧美一区二区三区 | 日本不卡二 | 国产精品福利资源在线 | 国产精品18| 青青青国产免费线在 | 精品新一区二区三区四区 | 91精品国产乱码在线观看 | 国产97在线 | 亚洲 | 九九在线 | 99在线热视频只有精品免费 | 日韩一级欧美一级毛片在 | 日韩精品一区二区三区中文3d | 毛片免费高清免费 | 国产精品www视频免费看 | 日本b站一卡二不卡 | 午夜在线精品不卡国产 | 欧美大成色www永久网站 | 色射综合| jizz美女| 久久骚| 五月天亚洲视频 | 日本欧美精品 | 日本大蕉香蕉大视频在线观看 | 99热久久国产精品免费看 | 波多野结衣一区 | 一本清高清dvd日本播放器 | 国产成人视屏 | 久久久久国产精品美女毛片 | 国产成人香蕉在线视频网站 | 综合图片亚洲 | 老司机永久免费网站在线观看 | 成人短视频在线观看 | 26uuu在线视频 | 呦系列视频一区二区三区 | 一本色道久久爱88av俺来也 |