2012-05-04 63 views
0

我從建造的server(服務器)例如:升壓C++線程

http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/examples.html

我做的唯一的事情 - 修改了request_handler.cpp爲:

// Decode url to path. 
std::string request_path; 
if (!url_decode(req.uri, request_path)) 
{ 
    rep = reply::stock_reply(reply::bad_request); 
    return; 
} 

// Request path must be absolute and not contain "..". 
if (request_path.empty() || request_path[0] != '/' 
    || request_path.find("..") != std::string::npos) 
{ 
    rep = reply::stock_reply(reply::bad_request); 
    return; 
} 

// Fill out the reply to be sent to the client. 
rep.status = reply::ok; 

std::string filename = "/tmp/test.mp4"; 
std::ifstream file (filename.c_str(), std::ios::in|std::ios::binary); 

char buf[1024000]; // 1MB Buffer read 
while (file.read(buf, sizeof(buf)).gcount() > 0) 
     rep.content.append(buf, file.gcount()); 

rep.headers.resize(9); 
rep.headers[0].name = "Content-Length"; 
rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size()); 
rep.headers[1].name = "Content-Type"; 
rep.headers[1].value = "video/mp4"; 

當我打開瀏覽器和點擊服務器,我可以得到視頻,沒有問題。當同樣的情況下,我打開另一個選項卡並打到服務器,沒有任何事情發生。看起來像是等到第一個標籤頁完成。

的目標是有一個處理倍數連接和發送多個文件服務器..

回答

0

服務器的響應將基於以下幾點:

  • 您在使用阻擋磁盤IO調用,所以這會在讀取數據時掛起一個線程。爲獲得最佳性能,您希望儘可能使用非阻塞。
  • 運行io_service :: run()的線程數。

對於你來說,最簡單的方法就是運行更多的線程運行io_service :: run()。我的猜測是你只運行一個線程,這就是爲什麼你沒有在第二個選項卡中得到任何響應,直到第一個選項卡完成。

更好的解決方案是查看使用非阻塞磁盤io以及。