cpp_redis  4.0.0
cpp_redis is a C++11 Asynchronous Multi-Platform Lightweight Redis Client, with support for synchronous operations and pipelining.
redis_connection.hpp
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #pragma once
24 
25 #include <functional>
26 #include <memory>
27 #include <mutex>
28 #include <string>
29 #include <vector>
30 
31 #include <cpp_redis/builders/reply_builder.hpp>
32 #include <cpp_redis/network/tcp_client_iface.hpp>
33 
34 #ifndef __CPP_REDIS_READ_SIZE
35 #define __CPP_REDIS_READ_SIZE 4096
36 #endif /* __CPP_REDIS_READ_SIZE */
37 
38 namespace cpp_redis {
39 
40 namespace network {
41 
46 public:
47 #ifndef __CPP_REDIS_USE_CUSTOM_TCP_CLIENT
48  redis_connection(void);
50 #endif /* __CPP_REDIS_USE_CUSTOM_TCP_CLIENT */
51 
57  explicit redis_connection(const std::shared_ptr<tcp_client_iface>& tcp_client);
58 
60  ~redis_connection(void);
61 
63  redis_connection(const redis_connection&) = delete;
66 
67 public:
71  typedef std::function<void(redis_connection&)> disconnection_handler_t;
72 
76  typedef std::function<void(redis_connection&, reply&)> reply_callback_t;
77 
87  void connect(
88  const std::string& host = "127.0.0.1",
89  std::size_t port = 6379,
90  const disconnection_handler_t& disconnection_handler = nullptr,
91  const reply_callback_t& reply_callback = nullptr,
92  std::uint32_t timeout_msecs = 0);
93 
99  void disconnect(bool wait_for_removal = false);
100 
104  bool is_connected(void) const;
105 
114  redis_connection& send(const std::vector<std::string>& redis_cmd);
115 
122  redis_connection& commit(void);
123 
124 private:
131  void tcp_client_receive_handler(const tcp_client_iface::read_result& result);
132 
137  void tcp_client_disconnection_handler(void);
138 
143  std::string build_command(const std::vector<std::string>& redis_cmd);
144 
145 private:
149  void call_disconnection_handler(void);
150 
151 private:
155  std::shared_ptr<cpp_redis::network::tcp_client_iface> m_client;
156 
160  reply_callback_t m_reply_callback;
161 
165  disconnection_handler_t m_disconnection_handler;
166 
170  builders::reply_builder m_builder;
171 
175  std::string m_buffer;
176 
180  std::mutex m_buffer_mutex;
181 };
182 
183 } // namespace network
184 
185 } // namespace cpp_redis
void disconnect(bool wait_for_removal=false)
Definition: redis_connection.hpp:45
std::function< void(redis_connection &)> disconnection_handler_t
Definition: redis_connection.hpp:71
std::function< void(redis_connection &, reply &)> reply_callback_t
Definition: redis_connection.hpp:76
redis_connection & send(const std::vector< std::string > &redis_cmd)
redis_connection & operator=(const redis_connection &)=delete
assignment operator
void connect(const std::string &host="127.0.0.1", std::size_t port=6379, const disconnection_handler_t &disconnection_handler=nullptr, const reply_callback_t &reply_callback=nullptr, std::uint32_t timeout_msecs=0)
Definition: reply_builder.hpp:40
redis_connection & commit(void)
Definition: tcp_client.hpp:37
Definition: tcp_client_iface.hpp:70
Definition: array_builder.hpp:29