Skip to content

Commit 3fa1547

Browse files
author
hamidr
committed
Add asio support #19
1 parent 8dd752d commit 3fa1547

16 files changed

+159
-514
lines changed

CMakeLists.txt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,12 @@ set(PROJECT_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/includes)
2323

2424
include_directories(${PROJECT_INCLUDE_DIR})
2525
include_directories("/usr/include/")
26-
include_directories("/usr/local/include")
26+
include_directories(${CMAKE_SOURCE_DIR}/libs/asio/asio/include/)
2727

2828
link_directories("/usr/lib")
2929
link_directories("/usr/local/lib")
3030

3131

32-
add_library(event_loop
33-
${PROJECT_SOURCE_DIR}/event_loop/socket_watcher.cpp
34-
${PROJECT_SOURCE_DIR}/event_loop/event_loop_ev.cpp)
35-
36-
add_library(network
37-
${PROJECT_SOURCE_DIR}/network/async_socket.cpp
38-
${PROJECT_SOURCE_DIR}/network/unix_socket.cpp
39-
${PROJECT_SOURCE_DIR}/network/tcp_socket.cpp)
40-
4132
add_library(parser
4233
${PROJECT_SOURCE_DIR}/parser/base_resp_parser.cpp
4334
${PROJECT_SOURCE_DIR}/parser/array_parser.cpp
@@ -59,18 +50,19 @@ if(CMAKE_COMPILER_IS_GNUCXX)
5950
set(CMAKE_EXE_LINKER_FLAGS "-s") ## Strip binary
6051
endif()
6152

62-
target_link_libraries(event_loop ev)
63-
target_link_libraries(async_redis event_loop parser network)
53+
target_link_libraries(async_redis parser pthread)
6454

65-
install(TARGETS event_loop
55+
install(TARGETS async_redis
6656
LIBRARY DESTINATION /usr/local/lib/
67-
ARCHIVE DESTINATION /usr/local/lib/)
57+
ARCHIVE DESTINATION /usr/local/lib/
58+
)
6859

6960
install(TARGETS parser
7061
LIBRARY DESTINATION /usr/local/lib/
7162
ARCHIVE DESTINATION /usr/local/lib/
7263
)
7364

65+
7466
install(DIRECTORY ${PROJECT_INCLUDE_DIR}/ DESTINATION /usr/local/include)
7567

7668
add_executable (a1.out ${CMAKE_SOURCE_DIR}/test/main.cpp)

includes/connection.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@
22

33
#include <queue>
44
#include <functional>
5-
#include <memory>
65
#include <tuple>
76

7+
#include <asio/io_context.hpp>
8+
#include <asio/ip/tcp.hpp>
9+
810
#include <parser/base_resp_parser.h>
9-
#include <network/async_socket.hpp>
1011

1112
namespace async_redis
1213
{
1314
class connection
1415
{
15-
using async_socket = network::async_socket;
16+
connection(const connection&) = delete;
17+
connection& operator = (const connection&) = delete;
1618

1719
public:
20+
using connect_handler_t = std::function<void(bool)>;
1821
using parser_t = parser::base_resp_parser::parser;
1922
using reply_cb_t = std::function<void (parser_t)>;
2023

21-
connection(event_loop::event_loop_ev& event_loop);
24+
connection(asio::io_context&);
2225

23-
void connect(async_socket::connect_handler_t handler, const std::string& ip, int port);
24-
void connect(async_socket::connect_handler_t handler, const std::string& path);
26+
void connect(connect_handler_t handler, const std::string& ip, int port);
2527

2628
bool is_connected() const;
2729
void disconnect();
@@ -30,13 +32,12 @@ namespace async_redis
3032

3133
private:
3234
void do_read();
33-
void reply_received(ssize_t len);
35+
void reply_received(const asio::error_code& ec, size_t len);
3436

3537
private:
36-
std::unique_ptr<async_socket> socket_;
38+
asio::ip::tcp::socket socket_;
3739
std::queue<std::tuple<reply_cb_t, parser_t>> req_queue_;
3840

39-
event_loop::event_loop_ev& event_loop_;
4041
enum { max_data_size = 1024 };
4142
char data_[max_data_size];
4243
};

includes/monitor.hpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
#pragma once
22

3-
#include <network/async_socket.hpp>
43
#include <parser/base_resp_parser.h>
54

65
#include <unordered_map>
76
#include <list>
87
#include <string>
98

9+
#include <asio/io_context.hpp>
10+
#include <asio/ip/tcp.hpp>
11+
1012
using std::string;
1113

1214
namespace async_redis
1315
{
1416
class monitor
1517
{
16-
using async_socket = network::async_socket;
18+
monitor(const monitor&) = delete;
19+
monitor& operator = (const monitor&) = delete;
20+
21+
using connect_handler_t = std::function<void(bool)>;
1722

1823
public:
1924
enum EventState {
@@ -26,10 +31,8 @@ namespace async_redis
2631
using parser_t = parser::base_resp_parser::parser;
2732
using watcher_cb_t = std::function<void (const string&, parser_t, EventState)>;
2833

29-
monitor(event_loop::event_loop_ev &event_loop);
30-
31-
void connect(async_socket::connect_handler_t handler, const std::string& ip, int port);
32-
void connect(async_socket::connect_handler_t handler, const std::string& path);
34+
monitor(asio::io_context &event_loop);
35+
void connect(connect_handler_t handler, const std::string& ip, int port);
3336

3437
bool is_connected() const;
3538
bool is_watching() const;
@@ -41,6 +44,7 @@ namespace async_redis
4144
bool punsubscribe(const std::list<string>& channels, watcher_cb_t&& cb);
4245

4346
private:
47+
void do_read();
4448
bool send_and_receive(string&& data);
4549
void handle_message_event(parser_t& channel, parser_t& value);
4650
void handle_subscribe_event(parser_t& channel, parser_t& clients);
@@ -51,15 +55,14 @@ namespace async_redis
5155
void handle_event(parser_t&& request);
5256
void report_disconnect();
5357

54-
void stream_received(ssize_t len);
58+
void stream_received(const asio::error_code& ec, size_t len);
5559

5660
private:
5761
parser_t parser_;
5862
std::unordered_map<std::string, watcher_cb_t> watchers_;
5963
std::unordered_map<std::string, watcher_cb_t> pwatchers_;
6064

61-
std::unique_ptr<async_socket> socket_;
62-
event_loop::event_loop_ev &io_;
65+
asio::ip::tcp::socket socket_;
6366
enum { max_data_size = 1024 };
6467
char data_[max_data_size];
6568
bool is_watching_ = false;

includes/network/async_socket.hpp

Lines changed: 0 additions & 75 deletions
This file was deleted.

includes/network/tcp_socket.hpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

includes/network/unix_socket.hpp

Lines changed: 0 additions & 17 deletions
This file was deleted.

includes/redis_client.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ namespace async_redis
1212

1313
class redis_client
1414
{
15+
redis_client(const redis_client&) = delete;
16+
redis_client& operator = (const redis_client&) = delete;
17+
1518
using reply_cb_t = connection::reply_cb_t;
16-
using connect_cb_t = network::async_socket::connect_handler_t;
19+
using connect_cb_t = connection::connect_handler_t;
1720

1821
public:
1922
class connect_exception : std::exception {};
2023
using parser_t = connection::parser_t;
2124

22-
redis_client(event_loop::event_loop_ev &eventIO, int n = 1);
25+
redis_client(asio::io_context &io, uint n = 1);
2326
bool is_connected() const;
2427

2528
template <typename ...Args>

includes/sentinel.hpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
#pragma once
2-
#include <network/async_socket.hpp>
32
#include <parser/base_resp_parser.h>
43

5-
#include <monitor.hpp>
64
#include <functional>
5+
6+
#include <monitor.hpp>
77
#include <connection.hpp>
8-
// #include <memory>
98

109
namespace async_redis {
1110
class sentinel
1211
{
13-
using socket_t = ::async_redis::network::async_socket;
14-
using connect_cb_t = socket_t::connect_handler_t;
12+
using connect_cb_t = connection::connect_handler_t;
1513

1614
public:
1715
using parser_t = parser::base_resp_parser::parser;
@@ -21,7 +19,7 @@ namespace async_redis {
2119
Watching
2220
};
2321

24-
sentinel(event_loop::event_loop_ev &event_loop);
22+
sentinel(asio::io_context &io);
2523

2624
bool is_connected() const;
2725
bool connect(const string& ip, int port, connect_cb_t&& connector);
@@ -54,7 +52,7 @@ namespace async_redis {
5452

5553
private:
5654
int connected_ = 0;
57-
std::unique_ptr<monitor> stream_;
58-
std::unique_ptr<connection> conn_;
55+
monitor stream_;
56+
connection conn_;
5957
};
6058
}

0 commit comments

Comments
 (0)