Skip to content
This repository was archived by the owner on Apr 6, 2019. It is now read-only.

Commit 38321da

Browse files
authored
Release 2.2 (#18)
1 parent 8643ee7 commit 38321da

20 files changed

+388
-562
lines changed

CMakeLists.txt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,25 @@ ELSE ()
9090
target_link_libraries(${PROJECT} pthread)
9191
ENDIF (WIN32)
9292

93+
# __CPP_REDIS_READ_SIZE
9394
IF (READ_SIZE)
94-
set_target_properties(${PROJECT}
95-
PROPERTIES
96-
COMPILE_DEFINITIONS "__CPP_REDIS_READ_SIZE=${READ_SIZE}")
95+
set_target_properties(${PROJECT} PROPERTIES COMPILE_DEFINITIONS "__CPP_REDIS_READ_SIZE=${READ_SIZE}")
9796
ENDIF (READ_SIZE)
9897

99-
IF (NO_LOGGING)
100-
set_target_properties(${PROJECT}
101-
PROPERTIES
102-
COMPILE_DEFINITIONS "__CPP_REDIS_NO_LOGGING=${NO_LOGGING}")
103-
ENDIF (NO_LOGGING)
98+
# __CPP_REDIS_LOGGING_ENABLED
99+
IF (LOGGING_ENABLED)
100+
set_target_properties(${PROJECT} PROPERTIES COMPILE_DEFINITIONS "__CPP_REDIS_LOGGING_ENABLED=${LOGGING_ENABLED}")
101+
ENDIF (LOGGING_ENABLED)
102+
103+
# _CPP_REDIS_MAX_NB_FDS
104+
IF (MAX_NB_FDS)
105+
set_target_properties(${PROJECT} PROPERTIES COMPILE_DEFINITIONS "_CPP_REDIS_MAX_NB_FDS=${MAX_NB_FDS}")
106+
ENDIF (MAX_NB_FDS)
107+
108+
# __CPP_REDIS_DEFAULT_NB_IO_SERVICE_WORKERS
109+
IF (DEFAULT_NB_IO_SERVICE_WORKERS)
110+
set_target_properties(${PROJECT} PROPERTIES COMPILE_DEFINITIONS "__CPP_REDIS_DEFAULT_NB_IO_SERVICE_WORKERS=${DEFAULT_NB_IO_SERVICE_WORKERS}")
111+
ENDIF (DEFAULT_NB_IO_SERVICE_WORKERS)
104112

105113

106114
###

includes/cpp_redis/logger.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ void warn(const std::string& msg, const std::string& file, unsigned int line);
6767
void error(const std::string& msg, const std::string& file, unsigned int line);
6868

6969
//! convenience macro to log with file and line information
70-
//! if __CPP_REDIS_NO_LOGGING, all logging related lines are removed from source code
71-
#ifdef __CPP_REDIS_NO_LOGGING
72-
#define __CPP_REDIS_LOG(level, msg)
73-
#else
70+
#ifdef __CPP_REDIS_LOGGING_ENABLED
7471
#define __CPP_REDIS_LOG(level, msg) cpp_redis::level(msg, __FILE__, __LINE__);
75-
#endif /* __CPP_REDIS_NO_LOGGING */
72+
#else
73+
#define __CPP_REDIS_LOG(level, msg)
74+
#endif /* __CPP_REDIS_LOGGING_ENABLED */
7675

7776
} //! cpp_redis
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
#include <functional>
5+
#include <memory>
6+
#include <vector>
7+
8+
#include <cpp_redis/network/socket.hpp>
9+
10+
#ifndef __CPP_REDIS_DEFAULT_NB_IO_SERVICE_WORKERS
11+
#define __CPP_REDIS_DEFAULT_NB_IO_SERVICE_WORKERS 16
12+
#endif /* __CPP_REDIS_DEFAULT_NB_IO_SERVICE_WORKERS */
13+
14+
namespace cpp_redis {
15+
16+
namespace network {
17+
18+
class io_service {
19+
public:
20+
//! get default global instance
21+
static const std::shared_ptr<network::io_service>& get_global_instance(void);
22+
//! set default global instance
23+
static void set_global_instance(const std::shared_ptr<network::io_service>& instance);
24+
25+
public:
26+
//! ctor & dtor
27+
io_service(size_t nb_workers);
28+
virtual ~io_service(void) = default;
29+
30+
//! copy ctor & assignment operator
31+
io_service(const io_service&) = default;
32+
io_service& operator=(const io_service&) = default;
33+
34+
public:
35+
//! disconnection handler declaration
36+
typedef std::function<void(io_service&)> disconnection_handler_t;
37+
38+
//! add or remove a given fd from the io service
39+
//! untrack should never be called from inside a callback
40+
virtual void track(_sock_t sock, const disconnection_handler_t& handler) = 0;
41+
virtual void untrack(_sock_t sock) = 0;
42+
43+
//! asynchronously read read_size bytes and append them to the given buffer
44+
//! on completion, call the read_callback to notify of the success or failure of the operation
45+
//! return false if another async_read operation is in progress or fd is not registered
46+
typedef std::function<void(std::size_t)> read_callback_t;
47+
virtual bool async_read(_sock_t sock, std::vector<char>& buffer, std::size_t read_size, const read_callback_t& callback) = 0;
48+
49+
//! asynchronously write write_size bytes from buffer to the specified fd
50+
//! on completion, call the write_callback to notify of the success or failure of the operation
51+
//! return false if another async_write operation is in progress or fd is not registered
52+
typedef std::function<void(std::size_t)> write_callback_t;
53+
virtual bool async_write(_sock_t sock, const std::vector<char>& buffer, std::size_t write_size, const write_callback_t& callback) = 0;
54+
55+
public:
56+
size_t get_nb_workers(void) const;
57+
58+
private:
59+
//! listen for incoming events and notify
60+
virtual void process_io(void) = 0;
61+
62+
private:
63+
size_t m_nb_workers;
64+
};
65+
66+
//! multi-platform instance builder
67+
std::shared_ptr<network::io_service> create_io_service(size_t nb_workers = __CPP_REDIS_DEFAULT_NB_IO_SERVICE_WORKERS);
68+
69+
} //! network
70+
71+
} //! cpp_redis

includes/cpp_redis/network/redis_connection.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55
#include <string>
66
#include <vector>
77

8-
#ifdef _MSC_VER
9-
#include <cpp_redis/network/windows/tcp_client.hpp>
10-
#else
11-
#include <cpp_redis/network/unix/tcp_client.hpp>
12-
#endif /* _MSC_VER */
13-
148
#include <cpp_redis/builders/reply_builder.hpp>
9+
#include <cpp_redis/network/tcp_client.hpp>
1510

1611
namespace cpp_redis {
1712

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
namespace cpp_redis {
4+
5+
namespace network {
6+
7+
#ifdef _WIN32
8+
#include <WinSock2.h>
9+
typedef SOCKET _sock_t;
10+
#else
11+
typedef int _sock_t;
12+
#endif /* _WIN32 */
13+
14+
#ifndef INVALID_SOCKET
15+
#define INVALID_SOCKET -1
16+
#endif /* INVALID_SOCKET */
17+
18+
} //! network
19+
20+
} //! cpp_redis

includes/cpp_redis/network/unix/tcp_client.hpp renamed to includes/cpp_redis/network/tcp_client.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#pragma once
22

33
#include <atomic>
4+
#include <list>
45
#include <mutex>
56
#include <stdexcept>
67
#include <string>
78
#include <thread>
89
#include <vector>
910

10-
#include <cpp_redis/network/unix/io_service.hpp>
11+
#include <cpp_redis/network/io_service.hpp>
12+
#include <cpp_redis/network/socket.hpp>
1113
#include <cpp_redis/redis_error.hpp>
1214

1315
#ifndef __CPP_REDIS_READ_SIZE
@@ -56,20 +58,21 @@ class tcp_client {
5658
void reset_state(void);
5759
void clear_buffer(void);
5860

61+
void setup_socket(void);
62+
5963
private:
6064
//! io service instance
6165
std::shared_ptr<io_service> m_io_service;
6266

6367
//! socket fd
64-
int m_fd;
68+
_sock_t m_sock;
6569

6670
//! is connected
6771
std::atomic_bool m_is_connected;
6872

6973
//! buffers
70-
static const unsigned int READ_SIZE = __CPP_REDIS_READ_SIZE;
7174
std::vector<char> m_read_buffer;
72-
std::vector<char> m_write_buffer;
75+
std::list<std::vector<char>> m_write_buffer;
7376

7477
//! handlers
7578
receive_handler_t m_receive_handler;

includes/cpp_redis/network/unix/io_service.hpp

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,34 @@
1111
#include <sys/socket.h>
1212
#include <unistd.h>
1313

14+
#include <cpp_redis/network/io_service.hpp>
15+
16+
#ifndef _CPP_REDIS_MAX_NB_FDS
17+
#define _CPP_REDIS_MAX_NB_FDS 1024
18+
#endif /* _CPP_REDIS_MAX_NB_FDS */
19+
1420
namespace cpp_redis {
1521

1622
namespace network {
1723

18-
class io_service {
19-
public:
20-
//! instance getter (singleton pattern)
21-
static const std::shared_ptr<io_service>& get_instance(void);
24+
namespace unix {
2225

23-
//! dtor
26+
class io_service : public network::io_service {
27+
public:
28+
//! ctor & dtor
29+
io_service(size_t nb_workers);
2430
~io_service(void);
2531

26-
private:
27-
//! ctor
28-
io_service(void);
29-
3032
//! copy ctor & assignment operator
3133
io_service(const io_service&) = delete;
3234
io_service& operator=(const io_service&) = delete;
3335

3436
public:
35-
//! disconnection handler declaration
36-
typedef std::function<void(io_service&)> disconnection_handler_t;
37-
38-
//! add or remove a given fd from the io service
39-
//! untrack should never be called from inside a callback
40-
void track(int fd, const disconnection_handler_t& handler);
41-
void untrack(int fd);
42-
43-
//! asynchronously read read_size bytes and append them to the given buffer
44-
//! on completion, call the read_callback to notify of the success or failure of the operation
45-
//! return false if another async_read operation is in progress or fd is not registered
46-
typedef std::function<void(std::size_t)> read_callback_t;
47-
bool async_read(int fd, std::vector<char>& buffer, std::size_t read_size, const read_callback_t& callback);
48-
49-
//! asynchronously write write_size bytes from buffer to the specified fd
50-
//!on completion, call the write_callback to notify of the success or failure of the operation
51-
//! return false if another async_write operation is in progress or fd is not registered
52-
typedef std::function<void(std::size_t)> write_callback_t;
53-
bool async_write(int fd, const std::vector<char>& buffer, std::size_t write_size, const write_callback_t& callback);
37+
void track(_sock_t fd, const disconnection_handler_t& handler) override;
38+
void untrack(_sock_t fd) override;
39+
40+
bool async_read(_sock_t fd, std::vector<char>& buffer, std::size_t read_size, const read_callback_t& callback) override;
41+
bool async_write(_sock_t fd, const std::vector<char>& buffer, std::size_t write_size, const write_callback_t& callback) override;
5442

5543
private:
5644
//! simple struct to keep track of ongoing operations on a given fd
@@ -73,7 +61,7 @@ class io_service {
7361

7462
private:
7563
//! listen for incoming events and notify
76-
void listen(void);
64+
void process_io(void) override;
7765

7866
//! notify the poll call so that it can wake up to process new events
7967
void notify_poll(void);
@@ -108,6 +96,8 @@ class io_service {
10896
std::recursive_mutex m_fds_mutex;
10997
};
11098

99+
} //! unix
100+
111101
} //! network
112102

113103
} //! cpp_redis

0 commit comments

Comments
 (0)