Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitec4689a

Browse files
committed
Test for Less Copy Connection Write
This commit basically is a test for the case where the range passed tothe call to connection::write is actually a Boost.Asio const_bufferderivative. This removes the requirement to make copies of the rangepassed into the call to connection::write. This is a backward-compatiblechange which means code that used to work with the range-based solutionwill still be supported while Boost.Asio const_buffer derivatives passedin will be supported as first-class elements that get used be theimplementation directly.Note: Boost.Asio const_buffers explicitly do not own the memory that hasbeen wrapped to it. Care should be taken when passing in Boost.Asioconst_buffer sequences into the connection::write interface to make surethat the data is still accessible until at least the provided callbackis provided.
1 parentddc8bdf commitec4689a

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

‎boost/network/protocol/http/server/async_connection.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,13 @@ namespace boost { namespace network { namespace http {
644644
if (error_encountered)
645645
boost::throw_exception(boost::system::system_error(*error_encountered));
646646

647+
boost::function<void(boost::system::error_code)> callback_function =
648+
callback;
649+
647650
boost::function<void()> continuation =boost::bind(
648-
&async_connection<Tag,Handler>::write_vec_impl<ConstBufferSeq,Callback>
651+
&async_connection<Tag,Handler>::write_vec_impl<ConstBufferSeq,boost::function<void(boost::system::error_code)>>
649652
,async_connection<Tag,Handler>::shared_from_this()
650-
,seq,callback, temporaries, buffers
653+
,seq,callback_function, temporaries, buffers
651654
);
652655

653656
if (!headers_already_sent && !headers_in_progress) {
@@ -664,7 +667,7 @@ namespace boost { namespace network { namespace http {
664667
,boost::bind(
665668
&async_connection<Tag,Handler>::handle_write
666669
,async_connection<Tag,Handler>::shared_from_this()
667-
,callback
670+
,callback_function
668671
,temporaries
669672
,buffers
670673
,asio::placeholders::error

‎libs/network/test/http/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ if (Boost_FOUND)
6969
set (SERVER_TESTS
7070
server_hello_world
7171
server_async
72+
server_async_less_copy
7273
)
7374
foreach (test${SERVER_TESTS} )
7475
if (${CMAKE_CXX_COMPILER_ID}MATCHESGNU)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
// Copyright 2010 Dean Michael Berris.
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#defineBOOST_TEST_MODULE HTTP Asynchronous Server Tests
8+
9+
#include<boost/config/warning_disable.hpp>
10+
#include<boost/network/include/http/server.hpp>
11+
#include<boost/network/utils/thread_pool.hpp>
12+
#include<boost/range/algorithm/find_if.hpp>
13+
14+
namespacenet= boost::network;
15+
namespacehttp= boost::network::http;
16+
namespaceutils= boost::network::utils;
17+
18+
structasync_hello_world;
19+
typedef http::async_server<async_hello_world> server;
20+
21+
structasync_hello_world {
22+
23+
structis_content_length {
24+
template<classHeader>
25+
booloperator()(Headerconst & header) {
26+
returnboost::iequals(name(header),"content-length");
27+
}
28+
};
29+
30+
voidoperator()(server::requestconst & request, server::connection_ptr connection) {
31+
static server::response_header headers[] = {
32+
{"Connection","close"}
33+
, {"Content-Type","text/plain"}
34+
, {"Server","cpp-netlib/0.9-devel"}
35+
};
36+
if (request.method =="HEAD") {
37+
connection->set_status(server::connection::ok);
38+
connection->set_headers(boost::make_iterator_range(headers, headers+3));
39+
}else {
40+
if (request.method =="PUT" || request.method =="POST") {
41+
static std::stringbad_request("Bad Request.");
42+
server::request::headers_container_type::iterator found =
43+
boost::find_if(request.headers,is_content_length());
44+
if (found == request.headers.end()) {
45+
connection->set_status(server::connection::bad_request);
46+
connection->set_headers(boost::make_iterator_range(headers, headers+3));
47+
connection->write(bad_request);
48+
return;
49+
}
50+
}
51+
staticcharconst * hello_world ="Hello, World!";
52+
connection->set_status(server::connection::ok);
53+
connection->set_headers(boost::make_iterator_range(headers, headers+3));
54+
connection->write(
55+
boost::asio::const_buffers_1(hello_world,13)
56+
,boost::bind(&async_hello_world::error,this, _1));
57+
}
58+
}
59+
60+
voiderror(boost::system::error_codeconst & ec) {
61+
// do nothing here.
62+
}
63+
};
64+
65+
intmain(int argc,char * argv[]) {
66+
utils::thread_poolthread_pool(2);
67+
async_hello_world handler;
68+
serverinstance("127.0.0.1","8000", handler, thread_pool, http::_reuse_address=true);
69+
instance.run();
70+
return0;
71+
}
72+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp