Skip to content

Commit d5b65ee

Browse files
committed
Refactored client connections into a client_connection namespace.
1 parent 6441091 commit d5b65ee

File tree

9 files changed

+343
-332
lines changed

9 files changed

+343
-332
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ namespace network {
5353
client_options options_;
5454
boost::asio::io_service io_service_;
5555
boost::asio::io_service::strand strand_;
56-
std::unique_ptr<async_resolver> resolver_;
57-
std::unique_ptr<async_connection> connection_;
56+
std::unique_ptr<client_connection::async_resolver> resolver_;
57+
std::unique_ptr<client_connection::async_connection> connection_;
5858
std::unique_ptr<boost::asio::io_service::work> sentinel_;
5959
std::thread lifetime_thread_;
6060

@@ -70,8 +70,8 @@ namespace network {
7070
client::impl::impl(client_options options)
7171
: options_(options)
7272
, strand_(io_service_)
73-
, resolver_(new tcp_resolver(io_service_, options.cache_resolved()))
74-
, connection_(new normal_connection(io_service_))
73+
, resolver_(new client_connection::tcp_resolver(io_service_, options.cache_resolved()))
74+
, connection_(new client_connection::normal_connection(io_service_))
7575
, sentinel_(new boost::asio::io_service::work(io_service_))
7676
, lifetime_thread_([=] () { io_service_.run(); }) {
7777

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

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,83 +16,85 @@
1616
namespace network {
1717
namespace http {
1818
namespace v2 {
19-
/**
20-
* \class async_connection network/http/v2/client/connection/async_connection.hpp
21-
* \brief Manages a connection through a socket.
22-
*/
23-
class async_connection {
24-
25-
async_connection(const async_connection &) = delete;
26-
async_connection &operator = (const async_connection &) = delete;
27-
28-
public:
29-
30-
/**
31-
* \typedef connect_callback
32-
*/
33-
typedef std::function<void (const boost::system::error_code &)> connect_callback;
34-
35-
/**
36-
* \typedef write_callback
37-
*/
38-
typedef std::function<void (const boost::system::error_code &, std::size_t)> write_callback;
39-
40-
/**
41-
* \typedef read_callback
42-
*/
43-
typedef std::function<void (const boost::system::error_code &, std::size_t)> read_callback;
44-
19+
namespace client_connection {
4520
/**
46-
* \brief Constructor.
21+
* \class async_connection network/http/v2/client/connection/async_connection.hpp
22+
* \brief Manages a connection through a socket.
4723
*/
48-
async_connection() = default;
49-
50-
/**
51-
* \brief Destructor.
52-
*/
53-
virtual ~async_connection() noexcept { }
54-
55-
/**
56-
* \brief Asynchronously creates a connection to an endpoint.
57-
* \param endpoint The endpoint to which to connect.
58-
* \param callback A callback handler.
59-
*/
60-
virtual void async_connect(const boost::asio::ip::tcp::endpoint &endpoint,
61-
connect_callback callback) = 0;
62-
63-
/**
64-
* \brief Asynchronously writes data across the connection.
65-
* \param command_streambuf
66-
* \param callback A callback handler.
67-
*/
68-
virtual void async_write(boost::asio::streambuf &command_streambuf,
69-
write_callback callback) = 0;
70-
71-
/**
72-
* \brief Asynchronously reads some data from the connection.
73-
* \param command_streambuf
74-
* \param delim A delimiter string which, if found, the socket
75-
* will stop reading.
76-
* \param callback A callback handler.
77-
*/
78-
virtual void async_read_until(boost::asio::streambuf &command_streambuf,
79-
const std::string &delim,
80-
read_callback callback) = 0;
81-
82-
/**
83-
* \brief Asynchronously reads some data from the connection.
84-
* \param command_streambuf
85-
* \param callback A callback handler.
86-
*/
87-
virtual void async_read(boost::asio::streambuf &command_streambuf,
88-
read_callback callback) = 0;
89-
90-
/**
91-
* \brief Cancels an operation on a connection.
92-
*/
93-
virtual void cancel() = 0;
94-
95-
};
24+
class async_connection {
25+
26+
async_connection(const async_connection &) = delete;
27+
async_connection &operator = (const async_connection &) = delete;
28+
29+
public:
30+
31+
/**
32+
* \typedef connect_callback
33+
*/
34+
typedef std::function<void (const boost::system::error_code &)> connect_callback;
35+
36+
/**
37+
* \typedef write_callback
38+
*/
39+
typedef std::function<void (const boost::system::error_code &, std::size_t)> write_callback;
40+
41+
/**
42+
* \typedef read_callback
43+
*/
44+
typedef std::function<void (const boost::system::error_code &, std::size_t)> read_callback;
45+
46+
/**
47+
* \brief Constructor.
48+
*/
49+
async_connection() = default;
50+
51+
/**
52+
* \brief Destructor.
53+
*/
54+
virtual ~async_connection() noexcept { }
55+
56+
/**
57+
* \brief Asynchronously creates a connection to an endpoint.
58+
* \param endpoint The endpoint to which to connect.
59+
* \param callback A callback handler.
60+
*/
61+
virtual void async_connect(const boost::asio::ip::tcp::endpoint &endpoint,
62+
connect_callback callback) = 0;
63+
64+
/**
65+
* \brief Asynchronously writes data across the connection.
66+
* \param command_streambuf
67+
* \param callback A callback handler.
68+
*/
69+
virtual void async_write(boost::asio::streambuf &command_streambuf,
70+
write_callback callback) = 0;
71+
72+
/**
73+
* \brief Asynchronously reads some data from the connection.
74+
* \param command_streambuf
75+
* \param delim A delimiter string which, if found, the socket
76+
* will stop reading.
77+
* \param callback A callback handler.
78+
*/
79+
virtual void async_read_until(boost::asio::streambuf &command_streambuf,
80+
const std::string &delim,
81+
read_callback callback) = 0;
82+
83+
/**
84+
* \brief Asynchronously reads some data from the connection.
85+
* \param command_streambuf
86+
* \param callback A callback handler.
87+
*/
88+
virtual void async_read(boost::asio::streambuf &command_streambuf,
89+
read_callback callback) = 0;
90+
91+
/**
92+
* \brief Cancels an operation on a connection.
93+
*/
94+
virtual void cancel() = 0;
95+
96+
};
97+
} // namespace client_connection
9698
} // namespace v2
9799
} // namespace http
98100
} // namespace network

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

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,64 @@
1414
namespace network {
1515
namespace http {
1616
namespace v2 {
17-
/**
18-
* \class async_resolver network/http/v2/client/connection/async_resolver.hpp
19-
* \brief Resolves and maintains a cache of hosts.
20-
*/
21-
class async_resolver {
22-
23-
async_resolver(const async_resolver &) = delete;
24-
async_resolver &operator = (const async_resolver &) = delete;
25-
26-
public:
27-
28-
/**
29-
* \brief resolver
30-
*/
31-
typedef boost::asio::ip::tcp::resolver resolver;
32-
33-
/**
34-
* \brief resolver_iterator
35-
*/
36-
typedef resolver::iterator resolver_iterator;
37-
38-
/**
39-
* \typedef resolve_callback
40-
*/
41-
typedef std::function<void (const boost::system::error_code &,
42-
resolver_iterator)> resolve_callback;
43-
17+
namespace client_connection {
4418
/**
45-
* \brief Constructor.
19+
* \class async_resolver network/http/v2/client/connection/async_resolver.hpp
20+
* \brief Resolves and maintains a cache of hosts.
4621
*/
47-
async_resolver() {
48-
49-
}
50-
51-
/**
52-
* \brief Destructor.
53-
*/
54-
virtual ~async_resolver() noexcept {
55-
56-
}
57-
58-
/**
59-
* \brief Resolves a host asynchronously.
60-
* \param host The hostname to resolve.
61-
* \param port The port number.
62-
* \param callback A callback handler.
63-
*/
64-
virtual void async_resolve(const std::string &host, std::uint16_t port,
65-
resolve_callback handler) = 0;
66-
67-
/**
68-
* \brief Clears the cache of already resolved endpoints.
69-
*/
70-
virtual void clear_resolved_cache() = 0;
71-
72-
};
22+
class async_resolver {
23+
24+
async_resolver(const async_resolver &) = delete;
25+
async_resolver &operator = (const async_resolver &) = delete;
26+
27+
public:
28+
29+
/**
30+
* \brief resolver
31+
*/
32+
typedef boost::asio::ip::tcp::resolver resolver;
33+
34+
/**
35+
* \brief resolver_iterator
36+
*/
37+
typedef resolver::iterator resolver_iterator;
38+
39+
/**
40+
* \typedef resolve_callback
41+
*/
42+
typedef std::function<void (const boost::system::error_code &,
43+
resolver_iterator)> resolve_callback;
44+
45+
/**
46+
* \brief Constructor.
47+
*/
48+
async_resolver() {
49+
50+
}
51+
52+
/**
53+
* \brief Destructor.
54+
*/
55+
virtual ~async_resolver() noexcept {
56+
57+
}
58+
59+
/**
60+
* \brief Resolves a host asynchronously.
61+
* \param host The hostname to resolve.
62+
* \param port The port number.
63+
* \param callback A callback handler.
64+
*/
65+
virtual void async_resolve(const std::string &host, std::uint16_t port,
66+
resolve_callback handler) = 0;
67+
68+
/**
69+
* \brief Clears the cache of already resolved endpoints.
70+
*/
71+
virtual void clear_resolved_cache() = 0;
72+
73+
};
74+
} // namespace client_connection
7375
} // namespace v2
7476
} // namespace http
7577
} // namespace network

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

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,52 @@
1414
namespace network {
1515
namespace http {
1616
namespace v2 {
17+
namespace client_connection {
1718

18-
class endpoint_cache {
19+
class endpoint_cache {
1920

20-
typedef boost::asio::ip::tcp::resolver resolver;
21+
typedef boost::asio::ip::tcp::resolver resolver;
2122

22-
typedef resolver::iterator resolver_iterator;
23+
typedef resolver::iterator resolver_iterator;
2324

24-
typedef std::unordered_map<std::string,
25-
resolver_iterator> cache_type;
25+
typedef std::unordered_map<std::string,
26+
resolver_iterator> cache_type;
2627

27-
public:
28+
public:
2829

29-
typedef cache_type::iterator iterator;
30+
typedef cache_type::iterator iterator;
3031

31-
cache_type::iterator begin() {
32-
return endpoints_.begin();
33-
}
32+
cache_type::iterator begin() {
33+
return endpoints_.begin();
34+
}
3435

35-
cache_type::iterator end() {
36-
return endpoints_.end();
37-
}
36+
cache_type::iterator end() {
37+
return endpoints_.end();
38+
}
3839

39-
void insert(const std::string &host,
40-
resolver_iterator endpoint_iterator) {
41-
endpoints_.insert(std::make_pair(host, endpoint_iterator));
42-
}
40+
void insert(const std::string &host,
41+
resolver_iterator endpoint_iterator) {
42+
endpoints_.insert(std::make_pair(host, endpoint_iterator));
43+
}
4344

44-
iterator find(const std::string &host) {
45-
return endpoints_.find(boost::to_lower_copy(host));
46-
}
45+
iterator find(const std::string &host) {
46+
return endpoints_.find(boost::to_lower_copy(host));
47+
}
4748

48-
void clear() {
49-
endpoint_cache().swap(*this);
50-
}
49+
void clear() {
50+
endpoint_cache().swap(*this);
51+
}
5152

52-
void swap(endpoint_cache &other) noexcept {
53-
endpoints_.swap(other.endpoints_);
54-
}
53+
void swap(endpoint_cache &other) noexcept {
54+
endpoints_.swap(other.endpoints_);
55+
}
5556

56-
private:
57+
private:
5758

58-
cache_type endpoints_;
59-
60-
};
59+
cache_type endpoints_;
6160

61+
};
62+
} // namespace client_connection
6263
} // namespace v2
6364
} // namespace http
6465
} // namespace network

0 commit comments

Comments
 (0)