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-
3520public:
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
0 commit comments