1. 序言ios
如今學的東西很容易忘記,寫這篇文章的目的是能讓我在須要時快速找回當時的感受. Let's BOOST THE WORLD . 編程
在 屬性->C/C++->預處理器->預處理定義 中加入 windows
_CRT_SECURE_NO_DEPRECATE;
來屏蔽沒必要要的警告 安全
Boost.Asio是利用當代C++的先進方法,跨平臺,異步I/O模型的C++網絡庫. 服務器
在 屬性->C/C++->命令行 中加入 網絡
-DBOOST_REGEX_NO_LIB
來防止自動鏈接. 多線程
本章介紹asio如何在定時器上進行阻塞等待(blocking wait). 併發
實現,咱們包含必要的頭文件. app
全部的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!\n"; 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!\n"; return 0; }
#include <iostream> #include <asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
asio的異步函數會在一個異步操做完成後被回調.這裏咱們定義了一個將被回調的函數.
void print(const asio::error& /*e*/) { std::cout << "Hello, world!\n"; } 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& /*e*/) { std::cout << "Hello, world!\n"; } int main() { asio::io_service io; asio::deadline_timer t(io, boost::posix_time::seconds(5)); t.async_wait(print); io.run(); return 0; }
這裏咱們將每秒回調一次,來演示如何回調函數參數的含義
#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& /*e*/, asio::deadline_timer* t, int* count) {
咱們打算讓這個函數運行6個週期,然而你會發現這裏沒有顯式的方法來終止io_service.不過,回顧上一節,你會發現當asio::io_service::run()會在全部任務完成時終止.這樣咱們當計算器的值達到5時(0爲第一次運行的值),再也不開啓一個新的異步等待就能夠了.
if (*count < 5) { std::cout << *count << "\n"; ++(*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 << "\n"; 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& /*e*/, asio::deadline_timer* t, int* count) { if (*count < 5) { std::cout << *count << "\n"; ++(*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 << "\n"; return 0; }
本例的運行結果和上一節相似
#include <iostream> #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
咱們先定義一個printer類
class printer { public:
構造函數有一個io_service參數,而且在初始化timer_時用到了它.用來計數的count_這裏一樣做爲了成員變量
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)); }
在類的折構函數中咱們輸出最後一次回調的conut的值
~printer() { std::cout << "Final count is " << count_ << "\n"; }
print函數於上一節的十分相似,可是用成員變量取代了參數.
void print() { if (count_ < 5) { std::cout << count_ << "\n"; ++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_ << "\n"; } void print() { if (count_ < 5) { std::cout << count_ << "\n"; ++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; }
本節演示了使用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_ << "\n"; }
多線程程序中,回調函數在訪問共享資源前須要同步.這裏共享資源是std::cout 和count_變量.
void print1() { if (count_ < 10) { std::cout << "Timer 1: " << count_ << "\n"; ++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_ << "\n"; ++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_ << "\n"; } void print1() { if (count_ < 10) { std::cout << "Timer 1: " << count_ << "\n"; ++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_ << "\n"; ++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; }
#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; // Connection closed cleanly by peer. else if (error) throw error; // Some other 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; // Connection closed cleanly by peer. else if (error) throw error; // Some other error. std::cout.write(buf.data(), len); } } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; }
#include <ctime> #include <iostream> #include <string> #include <asio.hpp> using asio::ip::tcp;
咱們先定義一個函數返回當前的時間的string形式.這個函數會在咱們全部的時間服務器示例上被使用.
std::string make_daytime_string() { using namespace std; // For time_t, time and ctime; 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; // For time_t, time and ctime; 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; }