6
6
// TODO:
7
7
// 1. posix::stream_descriptor need windows version
8
8
// 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
11
11
12
12
#include < unordered_map>
13
13
#include < boost/asio.hpp>
14
14
#include < boost/python.hpp>
15
15
16
- namespace a = boost::asio;
17
- namespace c = std::chrono;
18
- namespace py = boost::python;
16
+ namespace boost { namespace python { namespace asio {
19
17
20
- namespace boost { namespace python { namespace eventloop {
21
-
22
- class EventLoop
18
+ class event_loop
23
19
{
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
-
35
20
public:
36
- EventLoop (a ::io_context& ctx):
21
+ event_loop (boost::asio ::io_context& ctx):
37
22
_strand{ctx}, _created_time{std::chrono::steady_clock::now ()}
38
23
{
39
24
}
40
25
41
26
// 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)
43
28
{
44
29
_strand.post ([f, loop=this ] {
45
30
f (boost::ref (*loop));
@@ -48,22 +33,20 @@ class EventLoop
48
33
}
49
34
50
35
// TODO: implement this
51
- inline void call_soon_thread_safe (py:: object f) {};
36
+ inline void call_soon_thread_safe (object f) {};
52
37
53
38
// Schedule callback to be called after the given delay number of seconds
54
39
// 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);
56
41
57
- void call_at (double when, py:: object f);
42
+ void call_at (double when, object f);
58
43
59
44
inline double time ()
60
45
{
61
46
return static_cast <std::chrono::duration<double >>(std::chrono::steady_clock::now () - _created_time).count ();
62
47
}
63
48
64
- // week 2 ......start......
65
-
66
- inline void add_reader (int fd, py::object f)
49
+ inline void add_reader (int fd, object f)
67
50
{
68
51
_add_reader_or_writer (fd, f, fd * 2 );
69
52
}
@@ -73,7 +56,7 @@ class EventLoop
73
56
_remove_reader_or_writer (fd * 2 );
74
57
}
75
58
76
- inline void add_writer (int fd, py:: object f)
59
+ inline void add_writer (int fd, object f)
77
60
{
78
61
_add_reader_or_writer (fd, f, fd * 2 + 1 );
79
62
}
@@ -84,27 +67,30 @@ class EventLoop
84
67
}
85
68
86
69
87
- void sock_recv (py:: object sock, int bytes);
70
+ void sock_recv (object sock, int bytes);
88
71
89
- void sock_recv_into (py:: object sock, py:: object buffer);
72
+ void sock_recv_into (object sock, object buffer);
90
73
91
- void sock_sendall (py:: object sock, py:: object data);
74
+ void sock_sendall (object sock, object data);
92
75
93
- void sock_connect (py:: object sock, py:: object address);
76
+ void sock_connect (object sock, object address);
94
77
95
- void sock_accept (py:: object sock);
78
+ void sock_accept (object sock);
96
79
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 );
98
81
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;
100
89
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);
105
92
};
106
93
107
-
108
94
}}}
109
95
110
96
0 commit comments