Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit e5e2d67

Browse files
author
hamidr
committed
Cleaning sockets and testing examples
1 parent 1666e18 commit e5e2d67

File tree

7 files changed

+54
-48
lines changed

7 files changed

+54
-48
lines changed

examples/tcp_server.hpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,33 @@
1010
namespace async_redis {
1111
namespace tcp_server {
1212

13-
template<typename InputOutputHandler>
1413
class tcp_server
1514
{
1615
public:
17-
using tcp_socket = async_redis::network::tcp_socket<InputOutputHandler>;
16+
using tcp_socket = async_redis::network::tcp_socket;
17+
using async_socket = async_redis::network::async_socket;
1818

19-
tcp_server(InputOutputHandler &event_loop)
20-
: loop_(event_loop) {
21-
listener_ = std::make_shared<tcp_socket>(event_loop);
19+
tcp_server(event_loop::event_loop_ev &event_loop)
20+
: loop_(event_loop), listener_(event_loop) {
2221
}
2322

2423
void listen(int port) {
25-
if (!listener_->bind("127.0.0.1", port) || !listener_->listen())
24+
if (!listener_.bind("127.0.0.1", port) || !listener_.listen())
2625
throw;
2726

2827
auto receiver = std::bind(&tcp_server::accept, this, std::placeholders::_1);
29-
listener_->template async_accept<tcp_socket>(receiver);
28+
listener_.async_accept(receiver);
3029
}
3130

32-
void accept(std::shared_ptr<tcp_socket> socket) {
31+
void accept(std::shared_ptr<async_socket> socket) {
3332
auto receiver = std::bind(&tcp_server::chunk_received, this, std::placeholders::_1, socket);
3433
socket->async_read(buffer_, max_buffer_length, receiver);
3534

3635
conns_.emplace(socket, nullptr);
3736
}
3837

3938
private:
40-
void chunk_received(int len, std::shared_ptr<tcp_socket>& socket)
39+
void chunk_received(int len, std::shared_ptr<async_socket>& socket)
4140
{
4241
std::string command;
4342

@@ -78,10 +77,10 @@ namespace tcp_server {
7877
}
7978

8079
private:
81-
using socket_t = std::shared_ptr<tcp_socket>;
80+
using socket_t = std::shared_ptr<async_socket>;
8281

83-
socket_t listener_;
84-
InputOutputHandler& loop_;
82+
tcp_socket listener_;
83+
event_loop::event_loop_ev& loop_;
8584
std::unordered_map<socket_t, void*> conns_;
8685
enum { max_buffer_length = 1024 };
8786
char buffer_[max_buffer_length];

includes/connection.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ namespace async_redis
3030
if (!socket_ || !socket_->is_valid())
3131
socket_ = std::make_unique<tcp_socket>(event_loop_);
3232

33-
socket_->template async_connect<tcp_socket>(0, handler, ip, port);
33+
static_cast<tcp_socket*>(socket_.get())->async_connect(ip, port, handler);
3434
}
3535

3636
void connect(async_socket::connect_handler_t handler, const std::string& path)
3737
{
3838
if (!socket_ || !socket_->is_valid())
3939
socket_ = std::make_unique<unix_socket>(event_loop_);
4040

41-
socket_->template async_connect<unix_socket>(0, handler, path);
41+
static_cast<unix_socket*>(socket_.get())->async_connect(path, handler);
4242
}
4343

4444
bool is_connected() const

includes/monitor.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ namespace async_redis
3333
if (!socket_ || !socket_->is_valid())
3434
socket_ = std::make_unique<tcp_socket>(io_);
3535

36-
socket_->template async_connect<tcp_socket>(0, handler, ip, port);
36+
static_cast<tcp_socket*>(socket_.get())->async_connect(ip, port, handler);
3737
}
3838

3939
void connect(async_socket::connect_handler_t handler, const std::string& path)
4040
{
4141
if (!socket_ || !socket_->is_valid())
4242
socket_ = std::make_unique<unix_socket>(io_);
4343

44-
socket_->template async_connect<unix_socket>(0, handler, path);
44+
static_cast<unix_socket*>(socket_.get())->async_connect(path, handler);
4545
}
4646

4747
inline bool is_connected() const

includes/network/async_socket.hpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@ namespace async_redis {
3636
: io_(io)
3737
{ }
3838

39-
async_socket(event_loop::event_loop_ev &io, int fd)
40-
: io_(io)
41-
{
42-
fd_ = fd;
43-
is_connected_ = true;
44-
45-
id_ = io_.watch(fd_);
46-
}
47-
4839
~async_socket() {
4940
close();
5041
}
@@ -125,8 +116,33 @@ namespace async_redis {
125116
return true;
126117
}
127118

119+
void async_accept(const std::function<void(std::shared_ptr<async_socket>)>& cb)
120+
{
121+
return io_.async_read(id_, [this, cb]() {
122+
int fd = this->accept();
123+
auto s = std::make_shared<async_socket>(io_);
124+
s->set_fd_socket(fd);
125+
cb(s);
126+
this->async_accept(cb);
127+
});
128+
}
129+
130+
inline
131+
bool is_connected() const {
132+
return is_connected_;
133+
}
134+
135+
protected:
136+
void set_fd_socket(int fd)
137+
{
138+
fd_ = fd;
139+
is_connected_ = true;
140+
141+
id_ = io_.watch(fd_);
142+
}
143+
128144
template <typename SocketType, typename... Args>
129-
void async_connect(int timeout, async_socket::connect_handler_t handler, Args... args)
145+
void async_connect(int timeout, connect_handler_t handler, Args... args)
130146
{
131147
if (timeout == 10) // is equal to 1 second
132148
return handler(false);
@@ -140,22 +156,7 @@ namespace async_redis {
140156
});
141157
}
142158

143-
template<typename SocketType>
144-
void async_accept(const std::function<void(std::shared_ptr<SocketType>)>& cb)
145-
{
146-
return io_.async_read(id_, [&, cb]() {
147-
int fd = this->accept();
148-
cb(std::make_shared<SocketType>(io_, fd));
149-
this->async_accept(cb);
150-
});
151-
}
152159

153-
inline
154-
bool is_connected() const {
155-
return is_connected_;
156-
}
157-
158-
protected:
159160
void create_socket(int domain) {
160161
if (-1 == (fd_ = socket(domain, SOCK_STREAM, 0)))
161162
throw connect_socket_exception();

includes/network/tcp_socket.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ namespace async_redis {
1515
this->create_socket(AF_INET);
1616
}
1717

18-
tcp_socket(event_loop::event_loop_ev& io, int fd)
19-
: async_socket(io, fd)
20-
{}
18+
inline
19+
void async_connect(const string& ip, int port, connect_handler_t handler)
20+
{
21+
async_socket::template async_connect<tcp_socket>(0, handler, ip, port);
22+
}
2123

2224
bool bind(const string& host, int port)
2325
{

includes/network/unix_socket.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ namespace async_redis {
1717
}
1818

1919
inline
20-
unix_socket(event_loop::event_loop_ev &io, int fd)
21-
: async_socket(io, fd)
22-
{}
20+
void async_connect(const string& path, connect_handler_t handler)
21+
{
22+
async_socket::template async_connect<unix_socket>(0, handler, path);
23+
}
2324

2425
int connect(const string& path) {
2526
struct sockaddr_un addr = {0};

test/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <network/unix_socket.hpp>
88
#include <monitor.hpp>
99
#include <sentinel.hpp>
10+
#include "../examples/tcp_server.hpp"
1011

1112
#include <event_loop/event_loop_ev.h>
1213

@@ -169,7 +170,9 @@ int main(int argc, char** args)
169170

170171
event_loop_ev loop;
171172

172-
monitor_test monitor_mock(loop, 10000);
173+
monitor_test monitor_mock(loop, 100000);
174+
// async_redis::tcp_server::tcp_server server(loop);
175+
// server.listen(9090);
173176
// sentinel_test sentinel_mock(loop);
174177

175178
// using unix_socket_t = async_redis::network::unix_socket<event_loop_ev>;

0 commit comments

Comments
 (0)