|
| 1 | +// Copyright Pan Yue 2021. |
| 2 | +// Distributed under the Boost Software License, Version 1.0. (See |
| 3 | +// accompanying file LICENSE_1_0.txt or copy at |
| 4 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +// TODO: |
| 7 | +// 1. posix::stream_descriptor need windows version |
| 8 | +// 2. call_* need return async.Handle |
| 9 | +# ifndef EVENT_LOOP_PY2021_H_ |
| 10 | +# define EVENT_LOOP_PY2021_H_ |
| 11 | + |
| 12 | +#include <unordered_map> |
| 13 | +#include <boost/asio.hpp> |
| 14 | +#include <boost/python.hpp> |
| 15 | + |
| 16 | +namespace a = boost::asio; |
| 17 | +namespace c = std::chrono; |
| 18 | +namespace py = boost::python; |
| 19 | + |
| 20 | +namespace boost { namespace python { namespace eventloop { |
| 21 | + |
| 22 | +class EventLoop |
| 23 | +{ |
| 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 | +public: |
| 36 | + EventLoop(a::io_context& ctx): |
| 37 | + _strand{ctx}, _created_time{std::chrono::steady_clock::now()} |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + // 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) |
| 43 | + { |
| 44 | + _strand.post([f, loop=this] { |
| 45 | + f(boost::ref(*loop)); |
| 46 | + }); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // TODO: implement this |
| 51 | + inline void call_soon_thread_safe(py::object f) {}; |
| 52 | + |
| 53 | + // Schedule callback to be called after the given delay number of seconds |
| 54 | + // 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); |
| 56 | + |
| 57 | + void call_at(double when, py::object f); |
| 58 | + |
| 59 | + inline double time() |
| 60 | + { |
| 61 | + return static_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - _created_time).count(); |
| 62 | + } |
| 63 | + |
| 64 | + // week 2 ......start...... |
| 65 | + |
| 66 | + inline void add_reader(int fd, py::object f) |
| 67 | + { |
| 68 | + _add_reader_or_writer(fd, f, fd * 2); |
| 69 | + } |
| 70 | + |
| 71 | + inline void remove_reader(int fd) |
| 72 | + { |
| 73 | + _remove_reader_or_writer(fd * 2); |
| 74 | + } |
| 75 | + |
| 76 | + inline void add_writer(int fd, py::object f) |
| 77 | + { |
| 78 | + _add_reader_or_writer(fd, f, fd * 2 + 1); |
| 79 | + } |
| 80 | + |
| 81 | + inline void remove_writer(int fd) |
| 82 | + { |
| 83 | + _remove_reader_or_writer(fd * 2 + 1); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + void sock_recv(py::object sock, int bytes); |
| 88 | + |
| 89 | + void sock_recv_into(py::object sock, py::object buffer); |
| 90 | + |
| 91 | + void sock_sendall(py::object sock, py::object data); |
| 92 | + |
| 93 | + void sock_connect(py::object sock, py::object address); |
| 94 | + |
| 95 | + void sock_accept(py::object sock); |
| 96 | + |
| 97 | + void sock_sendfile(py::object sock, py::object file, int offset = 0, int count = 0, bool fallback = true); |
| 98 | + |
| 99 | + // week 2 ......end...... |
| 100 | + |
| 101 | + void run() |
| 102 | + { |
| 103 | + _strand.context().run(); |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | + |
| 108 | +}}} |
| 109 | + |
| 110 | + |
| 111 | +# endif |
0 commit comments