2222
2323#include < cpp_redis/logger.hpp>
2424#include < cpp_redis/network/redis_connection.hpp>
25+ #include < cpp_redis/network/tcp_client.hpp>
2526#include < cpp_redis/redis_error.hpp>
2627
28+ #include < tacopie/tacopie>
29+
2730namespace cpp_redis {
2831
2932namespace network {
3033
34+ std::function<std::shared_ptr<tcp_client_iface>()> get_tcp_client = []() -> std::shared_ptr<tcp_client_iface> {
35+ return std::make_shared<tcp_client>();
36+ };
37+
3138redis_connection::redis_connection (void )
32- : m_reply_callback(nullptr )
39+ : m_client(get_tcp_client())
40+ , m_reply_callback(nullptr )
3341, m_disconnection_handler(nullptr ) {
3442 __CPP_REDIS_LOG (debug, " cpp_redis::network::redis_connection created" );
3543}
3644
3745redis_connection::~redis_connection (void ) {
38- m_client. disconnect (true );
46+ m_client-> disconnect (true );
3947 __CPP_REDIS_LOG (debug, " cpp_redis::network::redis_connection destroyed" );
4048}
4149
@@ -46,14 +54,17 @@ redis_connection::connect(const std::string& host, std::size_t port,
4654 try {
4755 __CPP_REDIS_LOG (debug, " cpp_redis::network::redis_connection attempts to connect" );
4856
49- // ! connect client and start to read asynchronously
50- m_client.connect (host, port);
51- m_client.async_read ({__CPP_REDIS_READ_SIZE, std::bind (&redis_connection::tcp_client_receive_handler, this , std::placeholders::_1)});
52- m_client.set_on_disconnection_handler (std::bind (&redis_connection::tcp_client_disconnection_handler, this ));
57+ // ! connect client
58+ m_client->connect (host, port);
59+ m_client->set_on_disconnection_handler (std::bind (&redis_connection::tcp_client_disconnection_handler, this ));
60+
61+ // ! start to read asynchronously
62+ tcp_client_iface::read_request request = {__CPP_REDIS_READ_SIZE, std::bind (&redis_connection::tcp_client_receive_handler, this , std::placeholders::_1)};
63+ m_client->async_read (request);
5364
5465 __CPP_REDIS_LOG (debug, " cpp_redis::network::redis_connection connected" );
5566 }
56- catch (const tacopie::tacopie_error & e) {
67+ catch (const std::exception & e) {
5768 __CPP_REDIS_LOG (error, std::string (" cpp_redis::network::redis_connection " ) + e.what ());
5869 throw redis_error (e.what ());
5970 }
@@ -65,13 +76,13 @@ redis_connection::connect(const std::string& host, std::size_t port,
6576void
6677redis_connection::disconnect (bool wait_for_removal) {
6778 __CPP_REDIS_LOG (debug, " cpp_redis::network::redis_connection attempts to disconnect" );
68- m_client. disconnect (wait_for_removal);
79+ m_client-> disconnect (wait_for_removal);
6980 __CPP_REDIS_LOG (debug, " cpp_redis::network::redis_connection disconnected" );
7081}
7182
7283bool
7384redis_connection::is_connected (void ) {
74- return m_client. is_connected ();
85+ return m_client-> is_connected ();
7586}
7687
7788std::string
@@ -104,9 +115,10 @@ redis_connection::commit(void) {
104115 std::string buffer = std::move (m_buffer);
105116
106117 try {
107- m_client.async_write ({std::vector<char >{buffer.begin (), buffer.end ()}, nullptr });
118+ tcp_client_iface::write_request request = {std::vector<char >{buffer.begin (), buffer.end ()}, nullptr };
119+ m_client->async_write (request);
108120 }
109- catch (const tacopie::tacopie_error & e) {
121+ catch (const std::exception & e) {
110122 __CPP_REDIS_LOG (error, std::string (" cpp_redis::network::redis_connection " ) + e.what ());
111123 throw redis_error (e.what ());
112124 }
@@ -125,7 +137,7 @@ redis_connection::call_disconnection_handler(void) {
125137}
126138
127139void
128- redis_connection::tcp_client_receive_handler (const tacopie::tcp_client ::read_result& result) {
140+ redis_connection::tcp_client_receive_handler (const tcp_client_iface ::read_result& result) {
129141 if (!result.success ) { return ; }
130142
131143 try {
@@ -151,9 +163,10 @@ redis_connection::tcp_client_receive_handler(const tacopie::tcp_client::read_res
151163 }
152164
153165 try {
154- m_client.async_read ({__CPP_REDIS_READ_SIZE, std::bind (&redis_connection::tcp_client_receive_handler, this , std::placeholders::_1)});
166+ tcp_client_iface::read_request request = {__CPP_REDIS_READ_SIZE, std::bind (&redis_connection::tcp_client_receive_handler, this , std::placeholders::_1)};
167+ m_client->async_read (request);
155168 }
156- catch (const tacopie::tacopie_error &) {
169+ catch (const std::exception &) {
157170 // ! Client disconnected in the meantime
158171 }
159172}
0 commit comments