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

Boost.Asio基礎(chǔ)剖析

系統(tǒng) 2216 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對(duì)象上執(zhí)行同步和異步操作。在使用Boost.Asio之前,了解一下Boost.Asio和你的程序的各個(gè)部分的概念圖,以及它們?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.

作為一個(gè)入門例子,讓我們了解一下,當(dāng)你在執(zhí)行一個(gè)socket連接時(shí),發(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ū)⒅辽儆幸粋€(gè) io_service 對(duì)象,這個(gè) 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操作,你的程序需要一個(gè) I/O 對(duì)象,如 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í)行時(shí),下列事件順次發(fā)生:

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

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

socket.connect(server_endpoint);

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

I/O轉(zhuǎn)發(fā)請(qǐng)求到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 將任何錯(cuò)誤代碼轉(zhuǎn)換為 boost::system::error_code,這個(gè)error_code可以和特定值比較,也可作為boolean測(cè)試(false表示沒有錯(cuò)誤發(fā)生)。結(jié)果然后轉(zhuǎn)發(fā)回 I/O 對(duì)象

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 對(duì)象拋出 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)使用異步操作時(shí),將是不同的事件產(chǎn)生順序

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

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

socket.async_connect(server_endpoint, your_completion_handler);

where your_completion_handleris a function or function object with the signature:

這里your_completion_handler是一個(gè)帶有signature (目標(biāo)識(shí)別特征)的函數(shù)或函數(shù)對(duì)象:

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

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

I/O 對(duì)象將請(qǐng)求轉(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)啟動(dòng)一個(gè)異步連接

Time passes. (In the synchronous case this wait would have been contained entirely within the duration of the connect operation.)

過一段時(shí)間。(在同步操作的情況下,這個(gè)等待是整個(gè)連接操作的時(shí)間)

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)通過在隊(duì)列里放置一個(gè)結(jié)果來指示連接操作已經(jīng)完成,這個(gè)結(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)有未完成的操作時(shí),io_service::run()的調(diào)用被阻塞,所以你要在第一次啟動(dòng)異步操作后盡快調(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 從隊(duì)列移除操作結(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號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 亚洲99在线的 | 精品国产欧美一区二区三区成人 | 国产成人免费高清在线观看 | 久久性网 | 一级片视频免费看 | 二级毛片在线观看 | 亚洲图片综合区另类图片 | 免费毛片网 | 国产成人精品亚洲777图片 | 国产99精品在线观看 | 免费乱理伦片在线观看老妇 | 亚洲男人的天堂久久香蕉网 | 奇米777四色影视在线看 | 国产激情一区二区三区在线观看 | 日韩中文字幕在线不卡 | 99久久久国产精品免费牛牛四川 | 久草性视频 | 亚洲瑟瑟| 九九视频免费观看 | 99热精品在线观看 | 殴美一级视频 | 成人精品一区二区三区校园激情 | 草草伊人 | 日韩在线视频中文字幕 | 97色精品视频在线观看免费 | 奇米在线777在线视频 | 特级女人十八毛片a级 | 日韩精品福利视频一区二区三区 | 日韩第三页 | 国产亚洲精品一区二区在线观看 | 日本a在线视频 | 亚洲一区免费视频 | 成年人色视频 | 久久网站免费 | 亚洲精品久久久久网站 | 亚洲精品国产第一区二区多人 | 欧美性xxxx另类 | 全黄一级裸片视频在线观看 | 亚洲一级毛片免费在线观看 | 亚洲酷色综合 | 亚洲精品中文字幕乱码三区一二 |