2. 同步Timer
本章介紹asio如何在定時器上進行阻塞等待(
blocking wait
).
實現,我們包含必要的頭文件.
所有的asio類可以簡單的通過
include "asio.hpp"
來調用.
- #include <iostream>
- #include <boost/asio.hpp>
此外,這個示例用到了
timer
,我們還要包含
Boost.Date_Time
的頭文件來控制時間.
- #include <boost/date_time/posix_time/posix_time.hpp>
使用asio至少需要一個
boost::asio::io_service
對象.該類提供了訪問I/O的功能.我們首先在main函數中聲明它.
- int main()
- {
- boost::asio::io_service io;
下一步我們聲明
boost::asio::deadline_timer
對象.這個asio的核心類提供I/O的功能(這里更確切的說是定時功能),總是把一個
io_service
對象作為他的第一個構造函數,而第二個構造函數的參數設定timer會在5秒后到時(expired).
- boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
這個簡單的示例中我們演示了定時器上的一個阻塞等待.就是說,調用
boost::asio::deadline_timer::wait()
的在創建后5秒內(注意:不是等待開始后),timer到時之前不會返回任何值.
一個
deadline_timer
只有兩種狀態:
到時
,
未到時
.
如果
boost::asio::deadline_timer::wait()
在到時的timer對象上調用,會立即return.
- t.wait();
最后,我們輸出理所當然的"Hello, world!"來演示timer到時了.
- std::cout << "Hello, world! ";
- return 0;
- }
完整的代碼:
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- int main()
- {
- boost::asio::io_service io;
- boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
- t.wait();
- std::cout << "Hello, world! ";
- return 0;
- }
3. 異步Timer
- #include <iostream>
- #include <asio.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
asio的異步函數會在一個異步操作完成后被回調.這里我們定義了一個將被回調的函數.
- void print(const asio::error& )
- {
- std::cout << "Hello, world! ";
- }
- int main()
- {
- asio::io_service io;
- asio::deadline_timer t(io, boost::posix_time::seconds(5));
這里我們調用
asio::deadline_timer::async_wait()
來異步等待
- t.async_wait(print);
最后,我們必須調用
asio::io_service::run()
.
asio庫只會調用那個正在運行的
asio::io_service::run()
的回調函數.
如果
asio::io_service::run()
不被調用,那么回調永遠不會發生.
asio::io_service::run()
會持續工作到點,這里就是timer到時,回調完成.
別忘了在調用
asio::io_service::run()
之前設置好io_service的任務.比如,這里,如果我們忘記先調用
asio::deadline_timer::async_wait()
則
asio::io_service::run()
會在瞬間return.
- io.run();
- return 0;
- }
完整的代碼:
- #include <iostream>
- #include <asio.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- void print(const asio::error& )
- {
- std::cout << "Hello, world! ";
- }
- int main()
- {
- asio::io_service io;
- asio::deadline_timer t(io, boost::posix_time::seconds(5));
- t.async_wait(print);
- io.run();
- return 0;
- }
4. 回調函數的參數
這里我們將每秒回調一次,來演示如何回調函數參數的含義
- #include <iostream>
- #include <asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
首先,調整一下timer的持續時間,開始一個異步等待.顯示,回調函數需要訪問timer來實現周期運行,所以我們再介紹兩個新參數
- void print(const asio::error& ,
- asio::deadline_timer* t, int* count)
- {
我們打算讓這個函數運行6個周期,然而你會發現這里沒有顯式的方法來終止io_service.不過,回顧上一節,你會發現當 asio::io_service::run()會在所有任務完成時終止.這樣我們當計算器的值達到5時(0為第一次運行的值),不再開啟一個新的異步等待就可以了.
- if (*count < 5)
- {
- std::cout << *count << " ";
- ++(*count);
- ...
然后,我們推遲的timer的終止時間.通過在原先的終止時間上增加延時,我們可以確保timer不會在處理回調函數所需時間內的到期.
(原文:By calculating the new expiry time relative to the old, we can ensure that the timer does not drift away from the whole-second mark due to any delays in processing the handler.)
- t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
然后我們開始一個新的同步等待.如您所見,我們用把print和他的多個參數用boost::bind函數合成一個的形為void(const asio::error&)回調函數(准確的說是function object).
在這個例子中, boost::bind的asio::placeholders::error參數是為了給回調函數傳入一個error對象.當進行一個異步操作,開始 boost::bind時,你需要使用它來匹配回調函數的參數表.下一節中你會學到回調函數不需要error參數時可以省略它.
- t->async_wait(boost::bind(print,
- asio::placeholders::error, t, count));
- }
- }
- int main()
- {
- asio::io_service io;
- int count = 0;
- asio::deadline_timer t(io, boost::posix_time::seconds(1));
和上面一樣,我們再一次使用了綁定asio::deadline_timer::async_wait()
- t.async_wait(boost::bind(print,
- asio::placeholders::error, &t, &count));
- io.run();
在結尾,我們打印出的最后一次沒有設置timer的調用的count的值
- std::cout << "Final count is " << count << " ";
- return 0;
- }
完整的代碼:
- #include <iostream>
- #include <asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- void print(const asio::error& ,
- bsp; asio::deadline_timer* t, int* count)
- {
- if (*count < 5)
- {
- std::cout << *count << " ";
- ++(*count);
- t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
- t->async_wait(boost::bind(print,
- asio::placeholders::error, t, count));
- }
- }
- int main()
- {
- asio::io_service io;
- int count = 0;
- asio::deadline_timer t(io, boost::posix_time::seconds(1));
- t.async_wait(boost::bind(print,
- asio::placeholders::error, &t, &count));
- io.run();
- std::cout << "Final count is " << count << " ";
- return 0;
- }
5. 成員函數作為回調函數
本例的運行結果和上一節類似
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
我們先定義一個printer類
- class printer
- {
- public:
- printer(boost::asio::io_service& io)
- : timer_(io, boost::posix_time::seconds(1)),
- count_(0)
- {
boost::bind 同樣可以出色的工作在成員函數上.眾所周知,所有的非靜態成員函數都有一個隱式的this參數,我們需要把this作為參數bind到成員函數上.和上一節類似,我們再次用bind構造出void(const boost::asio::error&)形式的函數.
注意,這里沒有指定boost::asio::placeholders::error占位符,因為這個print成員函數沒有接受一個error對象作為參數.
- timer_.async_wait(boost::bind(&printer::print, this));
-
在類的折構函數中我們輸出最后一次回調的count的值
- ~printer()
- {
- std::cout << "Final count is " << count_ << " ";
- }
print函數於上一節的十分類似,但是用成員變量取代了參數.
- void print()
- {
- if (count_ < 5)
- {
- std::cout << count_ << " ";
- ++count_;
- timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
- timer_.async_wait(boost::bind(&printer::print, this));
- }
- }
- private:
- boost::asio::deadline_timer timer_;
- int count_;
- };
-
現在main函數清爽多了,在運行io_service之前只需要簡單的定義一個printer對象.
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- io.run();
- return 0;
- }
完整的代碼:
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- class printer
- {
- public:
- printer(boost::asio::io_service& io)
- : timer_(io, boost::posix_time::seconds(1)),
- count_(0)
- {
- timer_.async_wait(boost::bind(&printer::print, this));
- }
- ~printer()
- {
- std::cout << "Final count is " << count_ << " ";
- }
- void print()
- {
- if (count_ < 5)
- {
- std::cout << count_ << " ";
- ++count_;
- timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
- timer_.async_wait(boost::bind(&printer::print, this));
- }
- }
- private:
- boost::asio::deadline_timer timer_;
- int count_;
- };
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- io.run();
- return 0;
- }
6. 多線程回調同步
本節演示了使用
boost::asio::strand在多線程程序中進行回調同步(synchronise).
先前的幾節闡明了如何在單線程程序中用
boost::asio::io_service::run()進行同步.如您所見,asio庫確保 僅當當前線程調用
boost::asio::io_service::run()時產生回調.顯然,僅在一個線程中調用
boost::asio::io_service::run() 來確保回調是適用於並發編程的.
一個基於asio的程序最好是從單線程入手,但是單線程有如下的限制,這一點在服務器上尤其明顯:
- 當回調耗時較長時,反應遲鈍.
- 在多核的系統上無能為力
如果你發覺你陷入了這種困擾,可以替代的方法是建立一個
boost::asio::io_service::run()的線程池.然而這樣就允許回調函數並發執行.所以,當回調函數需要訪問一個共享,線程不安全的資源時,我們需要一種方式來同步操作.
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/thread.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
在上一節的基礎上我們定義一個printer類,此次,它將並行運行兩個timer
- class printer
- {
- public:
除了聲明了一對
boost::asio::deadline_timer,構造函數也初始化了類型為
boost::asio::strand的
strand_成員.
boost::asio::strand 可以分配的回調函數.它保證無論有多少線程調用了
boost::asio::io_service::run(),下一個回調函數僅在前一個回調函數完成后開始,當然回調函數仍然可以和那些不使用
boost::asio::strand
分配,或是使用另一個
boost::asio::strand分配的回調函數一起並發執行.
- printer(boost::asio::io_service& io)
- : strand_(io),
- timer1_(io, boost::posix_time::seconds(1)),
- timer2_(io, boost::posix_time::seconds(1)),
- count_(0)
- {
當一個異步操作開始時,用
boost::asio::strand來 "wrapped(包裝)"回調函數.
boost::asio::strand::wrap()會返回一個由
boost::asio::strand分配的新的handler(句柄),這樣,我們可以確保它們不會同時運行.
- timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
- timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
- }
- ~printer()
- {
- std::cout << "Final count is " << count_ << " ";
- }
多線程程序中,回調函數在訪問共享資源前需要同步.這里共享資源是
std::cout 和
count_變量.
- void print1()
- {
- if (count_ < 10)
- {
- std::cout << "Timer 1: " << count_ << " ";
- ++count_;
- timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
- timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
- }
- }
- void print2()
- {
- if (count_ < 10)
- {
- std::cout << "Timer 2: " << count_ << " ";
- ++count_;
- timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
- timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
- }
- }
- private:
- boost::asio::strand strand_;
- boost::asio::deadline_timer timer1_;
- boost::asio::deadline_timer timer2_;
- int count_;
- };
main函數中
boost::asio::io_service::run()在兩個線程中被調用:主線程、一個
boost::thread線程.
正如單線程中那樣,並發的
boost::asio::io_service::run()會一直運行直到完成任務.后台的線程將在所有異步線程完成后終結.
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
- io.run();
- t.join();
- return 0;
- }
完整的代碼:
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/thread.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- class printer
- {
- public:
- printer(boost::asio::io_service& io)
- : strand_(io),
- timer1_(io, boost::posix_time::seconds(1)),
- timer2_(io, boost::posix_time::seconds(1)),
- count_(0)
- {
- timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
- timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
- }
- ~printer()
- {
- std::cout << "Final count is " << count_ << " ";
- }
- void print1()
- {
- if (count_ < 10)
- {
- std::cout << "Timer 1: " << count_ << " ";
- ++count_;
- timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
- timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
- }
- }
- void print2()
- {
- if (count_ < 10)
- {
- std::cout << "Timer 2: " << count_ << " ";
- ++count_;
- timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
- timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
- }
- }
- private:
- boost::asio::strand strand_;
- boost::asio::deadline_timer timer1_;
- boost::asio::deadline_timer timer2_;
- int count_;
- };
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
- io.run();
- t.join();
- return 0;
- }
7. TCP客戶端:對准時間
- #include <iostream>
- #include <boost/array.hpp>
- #include <boost/asio.hpp>
本程序的目的是訪問一個時間同步服務器,我們需要用戶指定一個服務器(如time-nw.nist.gov),用IP亦可.
(譯者注:日期查詢協議,這種時間傳輸協議不指定固定的傳輸格式,只要求按照ASCII標准發送數據。)
- using boost::asio::ip::tcp;
- int main(int argc, char* argv[])
- {
- try
- {
- if (argc != 2)
- {
- std::cerr << "Usage: client <host>" << std::endl;
- return 1;
- }
用asio進行網絡連接至少需要一個boost::asio::io_service對象
- boost::asio::io_service io_service;
我們需要把在命令行參數中指定的服務器轉換為TCP上的節點.完成這項工作需要boost::asio::ip::tcp::resolver對象
- tcp::resolver resolver(io_service);
一個resolver對象查詢一個參數,並將其轉換為TCP上節點的列表.這里我們把argv[1]中的sever的名字和要查詢字串daytime關聯.
- tcp::resolver::query query(argv[1], "daytime");
節點列表可以用 boost::asio::ip::tcp::resolver::iterator 來進行迭代.iterator默認的構造函數生成一個end iterator.
- tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
- tcp::resolver::iterator end;
現在我們建立一個連接的sockert,由於獲得節點既有IPv4也有IPv6的.所以,我們需要依次嘗試他們直到找到一個可以正常工作的.這步使得我們的程序獨立於IP版本
- tcp::socket socket(io_service);
- boost::asio::error error = boost::asio::error::host_not_found;
- while (error && endpoint_iterator != end)
- {
- socket.close();
- socket.connect(*endpoint_iterator++, boost::asio::assign_error(error));
- }
- if (error)
- throw error;
連接完成,我們需要做的是讀取daytime服務器的響應.
我們用boost::array來保存得到的數據,boost::asio::buffer()會自動根據array的大小暫停工作,來防止緩沖溢出.除了使用boost::array,也可以使用char [] 或std::vector.
- for (;;)
- {
- boost::array<char, 128> buf;
- boost::asio::error error;
- size_t len = socket.read_some(
- boost::asio::buffer(buf), boost::asio::assign_error(error));
當服務器關閉連接時,boost::asio::ip::tcp::socket::read_some()會用boost::asio::error::eof標志完成, 這時我們應該退出讀取循環了.
- if (error == boost::asio::error::eof)
- break;
- else if (error)
- throw error;
- std::cout.write(buf.data(), len);
-
如果發生了什么異常我們同樣會拋出它
- }
- catch (std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- }
運行示例:在windowsXP的cmd窗口下
輸入:upload.exe time-a.nist.gov
輸出:54031 06-10-23 01:50:45 07 0 0 454.2 UTC(NIST) *
完整的代碼:
- #include <iostream>
- #include <boost/array.hpp>
- #include <asio.hpp>
- using asio::ip::tcp;
- int main(int argc, char* argv[])
- {
- try
- {
- if (argc != 2)
- {
- std::cerr << "Usage: client <host>" << std::endl;
- return 1;
- }
- asio::io_service io_service;
- tcp::resolver resolver(io_service);
- tcp::resolver::query query(argv[1], "daytime");
- tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
- tcp::resolver::iterator end;
- tcp::socket socket(io_service);
- asio::error error = asio::error::host_not_found;
- while (error && endpoint_iterator != end)
- {
- socket.close();
- socket.connect(*endpoint_iterator++, asio::assign_error(error));
- }
- if (error)
- throw error;
- for (;;)
- {
- boost::array<char, 128> buf;
- asio::error error;
- size_t len = socket.read_some(
- asio::buffer(buf), asio::assign_error(error));
- if (error == asio::error::eof)
- break;
- else if (error)
- throw error;
- std::cout.write(buf.data(), len);
- }
- }
- catch (std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- }
- return 0;
- }
8. TCP同步時間服務器
- #include <ctime>
- #include <iostream>
- #include <string>
- #include <asio.hpp>
- using asio::ip::tcp;
我們先定義一個函數返回當前的時間的string形式.這個函數會在我們所有的時間服務器示例上被使用.
- std::string make_daytime_string()
- {
- using namespace std;
- time_t now = time(0);
- return ctime(&now);
- }
- int main()
- {
- try
- {
- asio::io_service io_service;
新建一個asio::ip::tcp::acceptor對象來監聽新的連接.我們監聽TCP端口13,IP版本為V4
- tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
這是一個iterative server,也就是說同一時間只能處理一個連接.建立一個socket來表示一個和客戶端的連接, 然后等待客戶端的連接.
- for (;;)
- {
- tcp::socket socket(io_service);
- acceptor.accept(socket);
當客戶端訪問服務器時,我們獲取當前時間,然后返回它.
- std::string message = make_daytime_string();
- asio::write(socket, asio::buffer(message),
- asio::transfer_all(), asio::ignore_error());
- }
- }
最后處理異常
- catch (std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- }
- return 0;
-
運行示例:運行服務器,然后運行上一節的客戶端,在windowsXP的cmd窗口下
輸入:client.exe 127.0.0.1
輸出:Mon Oct 23 09:44:48 2006
完整的代碼:
- #include <ctime>
- #include <iostream>
- #include <string>
- #include <asio.hpp>
- using asio::ip::tcp;
- std::string make_daytime_string()
- {
- using namespace std;
- time_t now = time(0);
- return ctime(&now);
- }
- int main()
- {
- try
- {
- asio::io_service io_service;
- tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
- for (;;)
- {
- tcp::socket socket(io_service);
- acceptor.accept(socket);
- std::string message = make_daytime_string();
- asio::write(socket, asio::buffer(message),
- asio::transfer_all(), asio::ignore_error());
- }
- }
- catch (std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- }
- return 0;
- }
2. 同步Timer
本章介紹asio如何在定時器上進行阻塞等待(blocking wait).
實現,我們包含必要的頭文件.
所有的asio類可以簡單的通過include "asio.hpp"來調用.
- #include <iostream>
- #include <boost/asio.hpp>
此外,這個示例用到了timer,我們還要包含Boost.Date_Time的頭文件來控制時間.
- #include <boost/date_time/posix_time/posix_time.hpp>
使用asio至少需要一個boost::asio::io_service對象.該類提供了訪問I/O的功能.我們首先在main函數中聲明它.
- int main()
- {
- boost::asio::io_service io;
下一步我們聲明boost::asio::deadline_timer對象.這個asio的核心類提供I/O的功能(這里更確切的說是定時功能),總是把一個io_service對象作為他的第一個構造函數,而第二個構造函數的參數設定timer會在5秒后到時(expired).
- boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
這個簡單的示例中我們演示了定時器上的一個阻塞等待.就是說,調用boost::asio::deadline_timer::wait()的在創建后5秒內(注意:不是等待開始后),timer到時之前不會返回任何值.
一個deadline_timer只有兩種狀態:到時,未到時.
如果boost::asio::deadline_timer::wait()在到時的timer對象上調用,會立即return.
- t.wait();
最后,我們輸出理所當然的"Hello, world!"來演示timer到時了.
- std::cout << "Hello, world! ";
- return 0;
- }
完整的代碼:
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- int main()
- {
- boost::asio::io_service io;
- boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
- t.wait();
- std::cout << "Hello, world! ";
- return 0;
- }
3. 異步Timer
- #include <iostream>
- #include <asio.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
asio的異步函數會在一個異步操作完成后被回調.這里我們定義了一個將被回調的函數.
- void print(const asio::error& )
- {
- std::cout << "Hello, world! ";
- }
- int main()
- {
- asio::io_service io;
- asio::deadline_timer t(io, boost::posix_time::seconds(5));
這里我們調用asio::deadline_timer::async_wait()來異步等待
- t.async_wait(print);
最后,我們必須調用asio::io_service::run().
asio庫只會調用那個正在運行的asio::io_service::run()的回調函數.
如果asio::io_service::run()不被調用,那么回調永遠不會發生.
asio::io_service::run()會持續工作到點,這里就是timer到時,回調完成.
別忘了在調用 asio::io_service::run()之前設置好io_service的任務.比如,這里,如果我們忘記先調用asio::deadline_timer::async_wait()則asio::io_service::run()會在瞬間return.
- io.run();
- return 0;
- }
完整的代碼:
- #include <iostream>
- #include <asio.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- void print(const asio::error& )
- {
- std::cout << "Hello, world! ";
- }
- int main()
- {
- asio::io_service io;
- asio::deadline_timer t(io, boost::posix_time::seconds(5));
- t.async_wait(print);
- io.run();
- return 0;
- }
4. 回調函數的參數
這里我們將每秒回調一次,來演示如何回調函數參數的含義
- #include <iostream>
- #include <asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
首先,調整一下timer的持續時間,開始一個異步等待.顯示,回調函數需要訪問timer來實現周期運行,所以我們再介紹兩個新參數
- void print(const asio::error& ,
- asio::deadline_timer* t, int* count)
- {
我們打算讓這個函數運行6個周期,然而你會發現這里沒有顯式的方法來終止io_service.不過,回顧上一節,你會發現當 asio::io_service::run()會在所有任務完成時終止.這樣我們當計算器的值達到5時(0為第一次運行的值),不再開啟一個新的異步等待就可以了.
- if (*count < 5)
- {
- std::cout << *count << " ";
- ++(*count);
- ...
然后,我們推遲的timer的終止時間.通過在原先的終止時間上增加延時,我們可以確保timer不會在處理回調函數所需時間內的到期.
(原文:By calculating the new expiry time relative to the old, we can ensure that the timer does not drift away from the whole-second mark due to any delays in processing the handler.)
- t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
然后我們開始一個新的同步等待.如您所見,我們用把print和他的多個參數用boost::bind函數合成一個的形為void(const asio::error&)回調函數(准確的說是function object).
在這個例子中, boost::bind的asio::placeholders::error參數是為了給回調函數傳入一個error對象.當進行一個異步操作,開始 boost::bind時,你需要使用它來匹配回調函數的參數表.下一節中你會學到回調函數不需要error參數時可以省略它.
- t->async_wait(boost::bind(print,
- asio::placeholders::error, t, count));
- }
- }
- int main()
- {
- asio::io_service io;
- int count = 0;
- asio::deadline_timer t(io, boost::posix_time::seconds(1));
和上面一樣,我們再一次使用了綁定asio::deadline_timer::async_wait()
- t.async_wait(boost::bind(print,
- asio::placeholders::error, &t, &count));
- io.run();
在結尾,我們打印出的最后一次沒有設置timer的調用的count的值
- std::cout << "Final count is " << count << " ";
- return 0;
- }
完整的代碼:
- #include <iostream>
- #include <asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- void print(const asio::error& ,
- bsp; asio::deadline_timer* t, int* count)
- {
- if (*count < 5)
- {
- std::cout << *count << " ";
- ++(*count);
- t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
- t->async_wait(boost::bind(print,
- asio::placeholders::error, t, count));
- }
- }
- int main()
- {
- asio::io_service io;
- int count = 0;
- asio::deadline_timer t(io, boost::posix_time::seconds(1));
- t.async_wait(boost::bind(print,
- asio::placeholders::error, &t, &count));
- io.run();
- std::cout << "Final count is " << count << " ";
- return 0;
- }
5. 成員函數作為回調函數
本例的運行結果和上一節類似
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
我們先定義一個printer類
- class printer
- {
- public:
- printer(boost::asio::io_service& io)
- : timer_(io, boost::posix_time::seconds(1)),
- count_(0)
- {
boost::bind 同樣可以出色的工作在成員函數上.眾所周知,所有的非靜態成員函數都有一個隱式的this參數,我們需要把this作為參數bind到成員函數上.和上一節類似,我們再次用bind構造出void(const boost::asio::error&)形式的函數.
注意,這里沒有指定boost::asio::placeholders::error占位符,因為這個print成員函數沒有接受一個error對象作為參數.
- timer_.async_wait(boost::bind(&printer::print, this));
-
在類的折構函數中我們輸出最后一次回調的count的值
- ~printer()
- {
- std::cout << "Final count is " << count_ << " ";
- }
print函數於上一節的十分類似,但是用成員變量取代了參數.
- void print()
- {
- if (count_ < 5)
- {
- std::cout << count_ << " ";
- ++count_;
- timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
- timer_.async_wait(boost::bind(&printer::print, this));
- }
- }
- private:
- boost::asio::deadline_timer timer_;
- int count_;
- };
-
現在main函數清爽多了,在運行io_service之前只需要簡單的定義一個printer對象.
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- io.run();
- return 0;
- }
完整的代碼:
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- class printer
- {
- public:
- printer(boost::asio::io_service& io)
- : timer_(io, boost::posix_time::seconds(1)),
- count_(0)
- {
- timer_.async_wait(boost::bind(&printer::print, this));
- }
- ~printer()
- {
- std::cout << "Final count is " << count_ << " ";
- }
- void print()
- {
- if (count_ < 5)
- {
- std::cout << count_ << " ";
- ++count_;
- timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
- timer_.async_wait(boost::bind(&printer::print, this));
- }
- }
- private:
- boost::asio::deadline_timer timer_;
- int count_;
- };
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- io.run();
- return 0;
- }
6. 多線程回調同步
本節演示了使用boost::asio::strand在多線程程序中進行回調同步(synchronise).
先前的幾節闡明了如何在單線程程序中用boost::asio::io_service::run()進行同步.如您所見,asio庫確保 僅當當前線程調用boost::asio::io_service::run()時產生回調.顯然,僅在一個線程中調用 boost::asio::io_service::run() 來確保回調是適用於並發編程的.
一個基於asio的程序最好是從單線程入手,但是單線程有如下的限制,這一點在服務器上尤其明顯:
- 當回調耗時較長時,反應遲鈍.
- 在多核的系統上無能為力
如果你發覺你陷入了這種困擾,可以替代的方法是建立一個boost::asio::io_service::run()的線程池.然而這樣就允許回調函數並發執行.所以,當回調函數需要訪問一個共享,線程不安全的資源時,我們需要一種方式來同步操作.
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/thread.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
在上一節的基礎上我們定義一個printer類,此次,它將並行運行兩個timer
- class printer
- {
- public:
除了聲明了一對boost::asio::deadline_timer,構造函數也初始化了類型為boost::asio::strand的strand_成員.
boost::asio::strand 可以分配的回調函數.它保證無論有多少線程調用了boost::asio::io_service::run(),下一個回調函數僅在前一個回調函數完成后開始,當然回調函數仍然可以和那些不使用boost::asio::strand分配,或是使用另一個boost::asio::strand分配的回調函數一起並發執行.
- printer(boost::asio::io_service& io)
- : strand_(io),
- timer1_(io, boost::posix_time::seconds(1)),
- timer2_(io, boost::posix_time::seconds(1)),
- count_(0)
- {
當一個異步操作開始時,用boost::asio::strand來 "wrapped(包裝)"回調函數.boost::asio::strand::wrap()會返回一個由boost::asio::strand分配的新的handler(句柄),這樣,我們可以確保它們不會同時運行.
- timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
- timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
- }
- ~printer()
- {
- std::cout << "Final count is " << count_ << " ";
- }
多線程程序中,回調函數在訪問共享資源前需要同步.這里共享資源是std::cout 和count_變量.
- void print1()
- {
- if (count_ < 10)
- {
- std::cout << "Timer 1: " << count_ << " ";
- ++count_;
- timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
- timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
- }
- }
- void print2()
- {
- if (count_ < 10)
- {
- std::cout << "Timer 2: " << count_ << " ";
- ++count_;
- timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
- timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
- }
- }
- private:
- boost::asio::strand strand_;
- boost::asio::deadline_timer timer1_;
- boost::asio::deadline_timer timer2_;
- int count_;
- };
main函數中boost::asio::io_service::run()在兩個線程中被調用:主線程、一個boost::thread線程.
正如單線程中那樣,並發的boost::asio::io_service::run()會一直運行直到完成任務.后台的線程將在所有異步線程完成后終結.
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
- io.run();
- t.join();
- return 0;
- }
完整的代碼:
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/thread.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- class printer
- {
- public:
- printer(boost::asio::io_service& io)
- : strand_(io),
- timer1_(io, boost::posix_time::seconds(1)),
- timer2_(io, boost::posix_time::seconds(1)),
- count_(0)
- {
- timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
- timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
- }
- ~printer()
- {
- std::cout << "Final count is " << count_ << " ";
- }
- void print1()
- {
- if (count_ < 10)
- {
- std::cout << "Timer 1: " << count_ << " ";
- ++count_;
- timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
- timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
- }
- }
- void print2()
- {
- if (count_ < 10)
- {
- std::cout << "Timer 2: " << count_ << " ";
- ++count_;
- timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
- timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
- }
- }
- private:
- boost::asio::strand strand_;
- boost::asio::deadline_timer timer1_;
- boost::asio::deadline_timer timer2_;
- int count_;
- };
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
- io.run();
- t.join();
- return 0;
- }
7. TCP客戶端:對准時間
- #include <iostream>
- #include <boost/array.hpp>
- #include <boost/asio.hpp>
本程序的目的是訪問一個時間同步服務器,我們需要用戶指定一個服務器(如time-nw.nist.gov),用IP亦可.
(譯者注:日期查詢協議,這種時間傳輸協議不指定固定的傳輸格式,只要求按照ASCII標准發送數據。)
- using boost::asio::ip::tcp;
- int main(int argc, char* argv[])
- {
- try
- {
- if (argc != 2)
- {
- std::cerr << "Usage: client <host>" << std::endl;
- return 1;
- }
用asio進行網絡連接至少需要一個boost::asio::io_service對象
- boost::asio::io_service io_service;
我們需要把在命令行參數中指定的服務器轉換為TCP上的節點.完成這項工作需要boost::asio::ip::tcp::resolver對象
- tcp::resolver resolver(io_service);
一個resolver對象查詢一個參數,並將其轉換為TCP上節點的列表.這里我們把argv[1]中的sever的名字和要查詢字串daytime關聯.
- tcp::resolver::query query(argv[1], "daytime");
節點列表可以用 boost::asio::ip::tcp::resolver::iterator 來進行迭代.iterator默認的構造函數生成一個end iterator.
- tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
- tcp::resolver::iterator end;
現在我們建立一個連接的sockert,由於獲得節點既有IPv4也有IPv6的.所以,我們需要依次嘗試他們直到找到一個可以正常工作的.這步使得我們的程序獨立於IP版本
- tcp::socket socket(io_service);
- boost::asio::error error = boost::asio::error::host_not_found;
- while (error && endpoint_iterator != end)
- {
- socket.close();
- socket.connect(*endpoint_iterator++, boost::asio::assign_error(error));
- }
- if (error)
- throw error;
連接完成,我們需要做的是讀取daytime服務器的響應.
我們用boost::array來保存得到的數據,boost::asio::buffer()會自動根據array的大小暫停工作,來防止緩沖溢出.除了使用boost::array,也可以使用char [] 或std::vector.
- for (;;)
- {
- boost::array<char, 128> buf;
- boost::asio::error error;
- size_t len = socket.read_some(
- boost::asio::buffer(buf), boost::asio::assign_error(error));
當服務器關閉連接時,boost::asio::ip::tcp::socket::read_some()會用boost::asio::error::eof標志完成, 這時我們應該退出讀取循環了.
- if (error == boost::asio::error::eof)
- break;
- else if (error)
- throw error;
- std::cout.write(buf.data(), len);
-
如果發生了什么異常我們同樣會拋出它
- }
- catch (std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- }
運行示例:在windowsXP的cmd窗口下
輸入:upload.exe time-a.nist.gov輸出:54031 06-10-23 01:50:45 07 0 0 454.2 UTC(NIST) *
完整的代碼:
- #include <iostream>
- #include <boost/array.hpp>
- #include <asio.hpp>
- using asio::ip::tcp;
- int main(int argc, char* argv[])
- {
- try
- {
- if (argc != 2)
- {
- std::cerr << "Usage: client <host>" << std::endl;
- return 1;
- }
- asio::io_service io_service;
- tcp::resolver resolver(io_service);
- tcp::resolver::query query(argv[1], "daytime");
- tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
- tcp::resolver::iterator end;
- tcp::socket socket(io_service);
- asio::error error = asio::error::host_not_found;
- while (error && endpoint_iterator != end)
- {
- socket.close();
- socket.connect(*endpoint_iterator++, asio::assign_error(error));
- }
- if (error)
- throw error;
- for (;;)
- {
- boost::array<char, 128> buf;
- asio::error error;
- size_t len = socket.read_some(
- asio::buffer(buf), asio::assign_error(error));
- if (error == asio::error::eof)
- break;
- else if (error)
- throw error;
- std::cout.write(buf.data(), len);
- }
- }
- catch (std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- }
- return 0;
- }
8. TCP同步時間服務器
- #include <ctime>
- #include <iostream>
- #include <string>
- #include <asio.hpp>
- using asio::ip::tcp;
我們先定義一個函數返回當前的時間的string形式.這個函數會在我們所有的時間服務器示例上被使用.
- std::string make_daytime_string()
- {
- using namespace std;
- time_t now = time(0);
- return ctime(&now);
- }
- int main()
- {
- try
- {
- asio::io_service io_service;
新建一個asio::ip::tcp::acceptor對象來監聽新的連接.我們監聽TCP端口13,IP版本為V4
- tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
這是一個iterative server,也就是說同一時間只能處理一個連接.建立一個socket來表示一個和客戶端的連接, 然后等待客戶端的連接.
- for (;;)
- {
- tcp::socket socket(io_service);
- acceptor.accept(socket);
當客戶端訪問服務器時,我們獲取當前時間,然后返回它.
- std::string message = make_daytime_string();
- asio::write(socket, asio::buffer(message),
- asio::transfer_all(), asio::ignore_error());
- }
- }
最后處理異常
- catch (std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- }
- return 0;
-
運行示例:運行服務器,然后運行上一節的客戶端,在windowsXP的cmd窗口下
輸入:client.exe 127.0.0.1
輸出:Mon Oct 23 09:44:48 2006完整的代碼:
- #include <ctime>
- #include <iostream>
- #include <string>
- #include <asio.hpp>
- using asio::ip::tcp;
- std::string make_daytime_string()
- {
- using namespace std;
- time_t now = time(0);
- return ctime(&now);
- }
- int main()
- {
- try
- {
- asio::io_service io_service;
- tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
- for (;;)
- {
- tcp::socket socket(io_service);
- acceptor.accept(socket);
- std::string message = make_daytime_string();
- asio::write(socket, asio::buffer(message),
- asio::transfer_all(), asio::ignore_error());
- }
- }
- catch (std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- }
- return 0;
- }