Skip to content

Commit 5747a3a

Browse files
committed
Fixed namespace related issues
- fixed namespace pollution - rename boost::python::eventloop to boost::python::asio - remove eventloop.hpp from include list in python.hpp - rename define guards in eventloop.cpp - reorder class members in order: public, protected, private - rename class EventLoop to event_loop - remove `run()` from eventloop
1 parent 7d8fae8 commit 5747a3a

File tree

3 files changed

+43
-60
lines changed

3 files changed

+43
-60
lines changed

include/boost/python.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
# include <boost/python/docstring_options.hpp>
2626
# include <boost/python/enum.hpp>
2727
# include <boost/python/errors.hpp>
28-
# include <boost/python/eventloop.hpp>
2928
# include <boost/python/exception_translator.hpp>
3029
# include <boost/python/exec.hpp>
3130
# include <boost/python/extract.hpp>

include/boost/python/eventloop.hpp

+26-40
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,25 @@
66
// TODO:
77
// 1. posix::stream_descriptor need windows version
88
// 2. call_* need return async.Handle
9-
# ifndef EVENT_LOOP_PY2021_H_
10-
# define EVENT_LOOP_PY2021_H_
9+
# ifndef EVENT_LOOP_PY2021_HPP
10+
# define EVENT_LOOP_PY2021_HPP
1111

1212
#include <unordered_map>
1313
#include <boost/asio.hpp>
1414
#include <boost/python.hpp>
1515

16-
namespace a = boost::asio;
17-
namespace c = std::chrono;
18-
namespace py = boost::python;
16+
namespace boost { namespace python { namespace asio {
1917

20-
namespace boost { namespace python { namespace eventloop {
21-
22-
class EventLoop
18+
class event_loop
2319
{
24-
private:
25-
int64_t _timer_id = 0;
26-
a::io_context::strand _strand;
27-
std::unordered_map<int, std::unique_ptr<a::steady_timer>> _id_to_timer_map;
28-
// read: key = fd * 2 + 0, write: key = fd * 2 + 1
29-
std::unordered_map<int, std::unique_ptr<a::posix::stream_descriptor>> _descriptor_map;
30-
std::chrono::steady_clock::time_point _created_time;
31-
32-
void _add_reader_or_writer(int fd, py::object f, int key);
33-
void _remove_reader_or_writer(int key);
34-
3520
public:
36-
EventLoop(a::io_context& ctx):
21+
event_loop(boost::asio::io_context& ctx):
3722
_strand{ctx}, _created_time{std::chrono::steady_clock::now()}
3823
{
3924
}
4025

4126
// TODO: An instance of asyncio.Handle is returned, which can be used later to cancel the callback.
42-
inline void call_soon(py::object f)
27+
inline void call_soon(object f)
4328
{
4429
_strand.post([f, loop=this] {
4530
f(boost::ref(*loop));
@@ -48,22 +33,20 @@ class EventLoop
4833
}
4934

5035
// TODO: implement this
51-
inline void call_soon_thread_safe(py::object f) {};
36+
inline void call_soon_thread_safe(object f) {};
5237

5338
// Schedule callback to be called after the given delay number of seconds
5439
// TODO: An instance of asyncio.Handle is returned, which can be used later to cancel the callback.
55-
void call_later(double delay, py::object f);
40+
void call_later(double delay, object f);
5641

57-
void call_at(double when, py::object f);
42+
void call_at(double when, object f);
5843

5944
inline double time()
6045
{
6146
return static_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - _created_time).count();
6247
}
6348

64-
// week 2 ......start......
65-
66-
inline void add_reader(int fd, py::object f)
49+
inline void add_reader(int fd, object f)
6750
{
6851
_add_reader_or_writer(fd, f, fd * 2);
6952
}
@@ -73,7 +56,7 @@ class EventLoop
7356
_remove_reader_or_writer(fd * 2);
7457
}
7558

76-
inline void add_writer(int fd, py::object f)
59+
inline void add_writer(int fd, object f)
7760
{
7861
_add_reader_or_writer(fd, f, fd * 2 + 1);
7962
}
@@ -84,27 +67,30 @@ class EventLoop
8467
}
8568

8669

87-
void sock_recv(py::object sock, int bytes);
70+
void sock_recv(object sock, int bytes);
8871

89-
void sock_recv_into(py::object sock, py::object buffer);
72+
void sock_recv_into(object sock, object buffer);
9073

91-
void sock_sendall(py::object sock, py::object data);
74+
void sock_sendall(object sock, object data);
9275

93-
void sock_connect(py::object sock, py::object address);
76+
void sock_connect(object sock, object address);
9477

95-
void sock_accept(py::object sock);
78+
void sock_accept(object sock);
9679

97-
void sock_sendfile(py::object sock, py::object file, int offset = 0, int count = 0, bool fallback = true);
80+
void sock_sendfile(object sock, object file, int offset = 0, int count = 0, bool fallback = true);
9881

99-
// week 2 ......end......
82+
private:
83+
int64_t _timer_id = 0;
84+
boost::asio::io_context::strand _strand;
85+
std::unordered_map<int, std::unique_ptr<boost::asio::steady_timer>> _id_to_timer_map;
86+
// read: key = fd * 2 + 0, write: key = fd * 2 + 1
87+
std::unordered_map<int, std::unique_ptr<boost::asio::posix::stream_descriptor>> _descriptor_map;
88+
std::chrono::steady_clock::time_point _created_time;
10089

101-
void run()
102-
{
103-
_strand.context().run();
104-
}
90+
void _add_reader_or_writer(int fd, object f, int key);
91+
void _remove_reader_or_writer(int key);
10592
};
10693

107-
10894
}}}
10995

11096

src/eventloop.cpp

+17-19
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,23 @@
1010
#include <boost/asio.hpp>
1111
#include <boost/bind.hpp>
1212
#include <boost/python.hpp>
13+
#include <boost/python/eventloop.hpp>
1314

14-
namespace a = boost::asio;
15-
namespace c = std::chrono;
16-
namespace py = boost::python;
1715

18-
namespace boost { namespace python { namespace eventloop {
16+
namespace boost { namespace python { namespace asio {
1917

20-
void EventLoop::_add_reader_or_writer(int fd, py::object f, int key)
18+
void event_loop::_add_reader_or_writer(int fd, object f, int key)
2119
{
2220
// add descriptor
2321
if (_descriptor_map.find(key) == _descriptor_map.end())
2422
{
2523
_descriptor_map.emplace(key,
26-
std::move(std::make_unique<a::posix::stream_descriptor>(_strand.context(), fd))
24+
std::move(std::make_unique<boost::asio::posix::stream_descriptor>(_strand.context(), fd))
2725
);
2826
}
2927

30-
_descriptor_map.find(key)->second->async_wait(a::posix::descriptor::wait_type::wait_read,
31-
a::bind_executor(_strand, [key, f, loop=this] (const boost::system::error_code& ec)
28+
_descriptor_map.find(key)->second->async_wait(boost::asio::posix::descriptor::wait_type::wait_read,
29+
boost::asio::bind_executor(_strand, [key, f, loop=this] (const boost::system::error_code& ec)
3230
{
3331
// move descriptor
3432
auto iter = loop->_descriptor_map.find(key);
@@ -42,7 +40,7 @@ void EventLoop::_add_reader_or_writer(int fd, py::object f, int key)
4240
return;
4341
}
4442

45-
void EventLoop::_remove_reader_or_writer(int key)
43+
void event_loop::_remove_reader_or_writer(int key)
4644
{
4745
auto iter = _descriptor_map.find(key);
4846
if (iter != _descriptor_map.end())
@@ -52,58 +50,58 @@ void EventLoop::_remove_reader_or_writer(int key)
5250
}
5351
}
5452

55-
void EventLoop::call_later(double delay, py::object f)
53+
void event_loop::call_later(double delay, object f)
5654
{
5755
// add timer
5856
_id_to_timer_map.emplace(_timer_id,
59-
std::move(std::make_unique<a::steady_timer>(_strand.context(),
57+
std::move(std::make_unique<boost::asio::steady_timer>(_strand.context(),
6058
std::chrono::steady_clock::now() + std::chrono::nanoseconds(int64_t(delay * 1e9))))
6159
);
6260

6361
_id_to_timer_map.find(_timer_id)->second->async_wait(
6462
// remove timer
65-
a::bind_executor(_strand, [id=_timer_id, f, loop=this] (const boost::system::error_code& ec)
63+
boost::asio::bind_executor(_strand, [id=_timer_id, f, loop=this] (const boost::system::error_code& ec)
6664
{
6765
loop->_id_to_timer_map.erase(id);
6866
loop->call_soon(f);
6967
}));
7068
_timer_id++;
7169
}
7270

73-
void EventLoop::call_at(double when, py::object f)
71+
void event_loop::call_at(double when, object f)
7472
{
7573
double diff = when - time();
7674
if (diff > 0)
7775
return call_later(diff, f);
7876
return call_soon(f);
7977
}
8078

81-
void EventLoop::sock_recv(py::object sock, int bytes)
79+
void event_loop::sock_recv(object sock, int bytes)
8280
{
8381

8482
}
8583

86-
void EventLoop::sock_recv_into(py::object sock, py::object buffer)
84+
void event_loop::sock_recv_into(object sock, object buffer)
8785
{
8886

8987
}
9088

91-
void EventLoop::sock_sendall(py::object sock, py::object data)
89+
void event_loop::sock_sendall(object sock, object data)
9290
{
9391

9492
}
9593

96-
void EventLoop::sock_connect(py::object sock, py::object address)
94+
void event_loop::sock_connect(object sock, object address)
9795
{
9896

9997
}
10098

101-
void EventLoop::sock_accept(py::object sock)
99+
void event_loop::sock_accept(object sock)
102100
{
103101

104102
}
105103

106-
void EventLoop::sock_sendfile(py::object sock, py::object file, int offset, int count, bool fallback)
104+
void event_loop::sock_sendfile(object sock, object file, int offset, int count, bool fallback)
107105
{
108106

109107
}

0 commit comments

Comments
 (0)