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+
1420namespace cpp_redis {
1521
1622namespace 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
3436public:
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
5543private:
5644 // ! simple struct to keep track of ongoing operations on a given fd
@@ -73,7 +61,7 @@ class io_service {
7361
7462private:
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