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

Commit83cca78

Browse files
committed
A few updates to the client - next step is to test the components individually.
1 parent5e68836 commit83cca78

File tree

11 files changed

+128
-49
lines changed

11 files changed

+128
-49
lines changed

‎http/src/http/v2/client/client.cpp‎

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,91 @@
66
#include<network/http/v2/client/client.hpp>
77
#include<network/http/v2/method.hpp>
88
#include<network/http/v2/client/connection/async_resolver_delegate.hpp>
9+
#include<thread>
910

1011
namespacenetwork {
1112
namespacehttp {
1213
namespacev2 {
14+
structclient::impl {
15+
16+
explicitimpl(client_options options);
17+
18+
~impl()noexcept;
19+
20+
std::future<response>do_request(method method_, request request_, request_options options);
21+
22+
client_options options_;
23+
boost::asio::io_service io_service_;
24+
std::unique_ptr<boost::asio::io_service::work> sentinel_;
25+
std::thread lifetime_thread_;
26+
async_resolver_delegate resolver_;
27+
28+
};
29+
30+
client::impl::impl(client_options options)
31+
: options_(options)
32+
, sentinel_(new boost::asio::io_service::work(io_service_))
33+
, lifetime_thread_([=] () { io_service_.run(); })
34+
, resolver_(io_service_, options.cache_resolved()) {
35+
36+
}
37+
38+
client::impl::~impl()noexcept {
39+
sentinel_.reset();
40+
lifetime_thread_.join();
41+
}
42+
43+
std::future<response>client::impl::do_request(method method_,
44+
request request_,
45+
request_options options) {
46+
request_.set_method(method_);
47+
48+
std::future<response> response;
49+
resolver_.resolve(request_.host(), request_.port(),
50+
[&response] (const boost::system::error_code &ec,
51+
boost::iterator_range<async_resolver_delegate::resolver_iterator> resolvers) {
52+
if (ec) {
53+
return;
54+
}
55+
56+
// make TCP connection
57+
});
58+
59+
60+
return response;
61+
}
62+
1363
client::client(client_options options)
14-
: options_(options) {
64+
: pimpl_(new impl(options)) {
65+
66+
}
1567

68+
client::~client()noexcept {
69+
delete pimpl_;
1670
}
1771

1872
std::future<response>client::get(request request_, request_options options) {
19-
returndo_request(method::GET, request_, options);
73+
returnpimpl_->do_request(method::GET, request_, options);
2074
}
2175

2276
std::future<response>client::post(request request_, request_options options) {
23-
returndo_request(method::POST, request_, options);
77+
returnpimpl_->do_request(method::POST, request_, options);
2478
}
2579

2680
std::future<response>client::put(request request_, request_options options) {
27-
returndo_request(method::PUT, request_, options);
81+
returnpimpl_->do_request(method::PUT, request_, options);
2882
}
2983

3084
std::future<response>client::delete_(request request_, request_options options) {
31-
returndo_request(method::DELETE, request_, options);
85+
returnpimpl_->do_request(method::DELETE, request_, options);
3286
}
3387

3488
std::future<response>client::head(request request_, request_options options) {
35-
returndo_request(method::HEAD, request_, options);
89+
returnpimpl_->do_request(method::HEAD, request_, options);
3690
}
3791

3892
std::future<response>client::options(request request_, request_options options) {
39-
returndo_request(method::OPTIONS, request_, options);
40-
}
41-
42-
std::future<response>client::do_request(method method_, request request_, request_options options) {
43-
request_.set_method(method_);
44-
45-
boost::asio::io_service io_service;
46-
async_resolver_delegateresolver(io_service,async_resolver_delegate::cache_resolved());
47-
resolver.resolve(request_.host(), request_.port(),
48-
[=] (const boost::system::error_code &ec, boost::iterator_range<async_resolver_delegate::resolver_iterator> resolvers) {
49-
if (ec) {
50-
return;
51-
}
52-
53-
// make TCP connection
54-
});
55-
io_service.run();
56-
57-
return std::future<response>();
93+
return pimpl_->do_request(method::OPTIONS, request_, options);
5894
}
5995
}// namespace v2
6096
}// namespace network

‎http/src/network/http/v2/client/client.hpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
#include<network/http/v2/client/request.hpp>
1111
#include<network/http/v2/client/request_options.hpp>
1212
#include<network/http/v2/client/response.hpp>
13+
#include<boost/asio/io_service.hpp>
1314
#include<future>
15+
#include<memory>
1416

1517
namespacenetwork {
1618
namespacehttp {
1719
namespacev2 {
20+
1821
classclient {
1922

2023
public:
@@ -24,6 +27,7 @@ namespace network {
2427
explicitclient(client_options options = client_options());
2528
client(clientconst &) =delete;
2629
client(client &&) =delete;
30+
~client()noexcept;
2731

2832
std::future<response>get(request request_, request_options options = request_options());
2933

@@ -39,9 +43,8 @@ namespace network {
3943

4044
private:
4145

42-
std::future<response>do_request(method method_, request request_, request_options options);
43-
44-
client_options options_;
46+
structimpl;
47+
impl *pimpl_;
4548

4649
};
4750
}// namespace v2

‎http/src/network/http/v2/client/client_options.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include<string>
1212
#include<vector>
1313
#include<chrono>
14+
#include<boost/asio/io_service.hpp>
1415

1516
namespacenetwork {
1617
namespacehttp {

‎http/src/network/http/v2/client/connection.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace network {
3131

3232
connection &operator = (const connection &) =delete;
3333

34-
virtualconnection()= default;
34+
virtual~connection()noexcept;
3535

3636
virtual responsesend_request(std::string method,
3737
request req,

‎http/src/network/http/v2/client/connection/async_resolver_delegate.hpp‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,13 @@ namespace network {
3232

3333
public:
3434

35-
structcache_resolved { };
36-
37-
/*!
38-
* \brief Constructor.
39-
*/
40-
explicitasync_resolver_delegate(boost::asio::io_service &service)
41-
: resolver_(service)
42-
, cache_resolved_(false)
43-
, resolver_strand_(new boost::asio::io_service::strand(service)) {
44-
45-
}
46-
4735
/*!
4836
* \brief Constructor.
4937
*/
50-
async_resolver_delegate(boost::asio::io_service &service, cache_resolved)
38+
async_resolver_delegate(boost::asio::io_service &service,boolcache_resolved =false)
5139
: resolver_(service)
52-
,cache_resolved_(true)
53-
,resolver_strand_(new boost::asio::io_service::strand(service)) {
40+
,resolver_strand_(new boost::asio::io_service::strand(service))
41+
,cache_resolved_(cache_resolved) {
5442

5543
}
5644

@@ -94,7 +82,7 @@ namespace network {
9482
* \brief Clears the cache of already resolved endpoints.
9583
*/
9684
voidclear_resolved_cache() {
97-
endpoint_cache_.clear();
85+
endpoint_cache().swap(endpoint_cache_);
9886
}
9987

10088
private:

‎http/src/network/http/v2/client/connection_manager.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace network {
2828

2929
connection_manager &operator = (const connection_manager &) =delete;
3030

31-
virtualconnection_manager()= defaultnoexcept;
31+
virtualconnection_manager()noexcept = 0;
3232

3333
virtual std::shared_ptr<connection>get_connection(boost::asio::io_service &io_service,
3434
const request &req,

‎http/src/network/http/v2/client/request.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ namespace network {
168168
}
169169

170170
voidclear_headers() {
171-
headers_.clear();
171+
headers_type().swap(headers_);
172172
}
173173

174174
voidset_method(network::http::v2::method method) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
7+
#ifndef __NETWORK_HTTP_V2_CLIENT_SIMPLE_CONNECTION_MANAGER_INC__
8+
#define__NETWORK_HTTP_V2_CLIENT_SIMPLE_CONNECTION_MANAGER_INC__
9+
10+
#include"network/http/v2/client/connection/simple_connection_manager.hpp"
11+
#include<network/http/v2/client/connection/async_resolver_delegate.hpp>
12+
13+
namespacenetwork {
14+
namespacehttp {
15+
namespacev2 {
16+
17+
classrequest;
18+
classclient_connection;
19+
classclient_options;
20+
21+
classsimple_connection_manager {
22+
23+
public:
24+
25+
typedef std::shared_ptr<connection> connection_ptr;
26+
27+
simple_connection_manager() = simple;
28+
29+
virtualsimple_connection_manager()noexcept;
30+
31+
virtual connection_ptrget_connection(boost::asio::io_service &io_service,
32+
const request &req,
33+
const client_options &options);
34+
35+
virtualvoidclear_resolved_cache();
36+
37+
virtualvoidreset();
38+
39+
private:
40+
41+
async_resolver_delegate resolver_;
42+
43+
};
44+
}// namespace v2
45+
}// namespace http
46+
}// namespace network
47+
48+
49+
#endif// __NETWORK_HTTP_V2_CLIENT_SIMPLE_CONNECTION_MANAGER_INC__

‎http/test/v2/client/async_resolver_test.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ TEST(async_resolver_test, resolve_localhost) {
1919
[] (const boost::system::error_code &ec,
2020
const boost::iterator_range<http::async_resolver_delegate::resolver_iterator> &endpoints) {
2121
for (auto endpoint : endpoints) {
22-
std::cout <<"Endpoint" << std::endl;
22+
//std::cout <<endpoint << std::endl;
2323
}
2424
});
2525

‎http/test/v2/client/normal_connection_test.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88
#include"network/http/v2/client/connection/normal_connection_delegate.hpp"
99
#include<iostream>
1010

11+
using boost::asio::ip::tcp;
1112
namespacehttp= network::http::v2;
13+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp