Skip to content

Commit

Permalink
Fixed namespace related issues
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
philoinovsky committed Jun 27, 2021
1 parent 7d8fae8 commit 5747a3a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 60 deletions.
1 change: 0 additions & 1 deletion include/boost/python.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
# include <boost/python/docstring_options.hpp>
# include <boost/python/enum.hpp>
# include <boost/python/errors.hpp>
# include <boost/python/eventloop.hpp>
# include <boost/python/exception_translator.hpp>
# include <boost/python/exec.hpp>
# include <boost/python/extract.hpp>
Expand Down
66 changes: 26 additions & 40 deletions include/boost/python/eventloop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,25 @@
// TODO:
// 1. posix::stream_descriptor need windows version
// 2. call_* need return async.Handle
# ifndef EVENT_LOOP_PY2021_H_
# define EVENT_LOOP_PY2021_H_
# ifndef EVENT_LOOP_PY2021_HPP
# define EVENT_LOOP_PY2021_HPP

#include <unordered_map>
#include <boost/asio.hpp>
#include <boost/python.hpp>

namespace a = boost::asio;
namespace c = std::chrono;
namespace py = boost::python;
namespace boost { namespace python { namespace asio {

namespace boost { namespace python { namespace eventloop {

class EventLoop
class event_loop
{
private:
int64_t _timer_id = 0;
a::io_context::strand _strand;
std::unordered_map<int, std::unique_ptr<a::steady_timer>> _id_to_timer_map;
// read: key = fd * 2 + 0, write: key = fd * 2 + 1
std::unordered_map<int, std::unique_ptr<a::posix::stream_descriptor>> _descriptor_map;
std::chrono::steady_clock::time_point _created_time;

void _add_reader_or_writer(int fd, py::object f, int key);
void _remove_reader_or_writer(int key);

public:
EventLoop(a::io_context& ctx):
event_loop(boost::asio::io_context& ctx):
_strand{ctx}, _created_time{std::chrono::steady_clock::now()}
{
}

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

// TODO: implement this
inline void call_soon_thread_safe(py::object f) {};
inline void call_soon_thread_safe(object f) {};

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

void call_at(double when, py::object f);
void call_at(double when, object f);

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

// week 2 ......start......

inline void add_reader(int fd, py::object f)
inline void add_reader(int fd, object f)
{
_add_reader_or_writer(fd, f, fd * 2);
}
Expand All @@ -73,7 +56,7 @@ class EventLoop
_remove_reader_or_writer(fd * 2);
}

inline void add_writer(int fd, py::object f)
inline void add_writer(int fd, object f)
{
_add_reader_or_writer(fd, f, fd * 2 + 1);
}
Expand All @@ -84,27 +67,30 @@ class EventLoop
}


void sock_recv(py::object sock, int bytes);
void sock_recv(object sock, int bytes);

void sock_recv_into(py::object sock, py::object buffer);
void sock_recv_into(object sock, object buffer);

void sock_sendall(py::object sock, py::object data);
void sock_sendall(object sock, object data);

void sock_connect(py::object sock, py::object address);
void sock_connect(object sock, object address);

void sock_accept(py::object sock);
void sock_accept(object sock);

void sock_sendfile(py::object sock, py::object file, int offset = 0, int count = 0, bool fallback = true);
void sock_sendfile(object sock, object file, int offset = 0, int count = 0, bool fallback = true);

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

void run()
{
_strand.context().run();
}
void _add_reader_or_writer(int fd, object f, int key);
void _remove_reader_or_writer(int key);
};


}}}


Expand Down
36 changes: 17 additions & 19 deletions src/eventloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,23 @@
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/python.hpp>
#include <boost/python/eventloop.hpp>

namespace a = boost::asio;
namespace c = std::chrono;
namespace py = boost::python;

namespace boost { namespace python { namespace eventloop {
namespace boost { namespace python { namespace asio {

void EventLoop::_add_reader_or_writer(int fd, py::object f, int key)
void event_loop::_add_reader_or_writer(int fd, object f, int key)
{
// add descriptor
if (_descriptor_map.find(key) == _descriptor_map.end())
{
_descriptor_map.emplace(key,
std::move(std::make_unique<a::posix::stream_descriptor>(_strand.context(), fd))
std::move(std::make_unique<boost::asio::posix::stream_descriptor>(_strand.context(), fd))
);
}

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

void EventLoop::_remove_reader_or_writer(int key)
void event_loop::_remove_reader_or_writer(int key)
{
auto iter = _descriptor_map.find(key);
if (iter != _descriptor_map.end())
Expand All @@ -52,58 +50,58 @@ void EventLoop::_remove_reader_or_writer(int key)
}
}

void EventLoop::call_later(double delay, py::object f)
void event_loop::call_later(double delay, object f)
{
// add timer
_id_to_timer_map.emplace(_timer_id,
std::move(std::make_unique<a::steady_timer>(_strand.context(),
std::move(std::make_unique<boost::asio::steady_timer>(_strand.context(),
std::chrono::steady_clock::now() + std::chrono::nanoseconds(int64_t(delay * 1e9))))
);

_id_to_timer_map.find(_timer_id)->second->async_wait(
// remove timer
a::bind_executor(_strand, [id=_timer_id, f, loop=this] (const boost::system::error_code& ec)
boost::asio::bind_executor(_strand, [id=_timer_id, f, loop=this] (const boost::system::error_code& ec)
{
loop->_id_to_timer_map.erase(id);
loop->call_soon(f);
}));
_timer_id++;
}

void EventLoop::call_at(double when, py::object f)
void event_loop::call_at(double when, object f)
{
double diff = when - time();
if (diff > 0)
return call_later(diff, f);
return call_soon(f);
}

void EventLoop::sock_recv(py::object sock, int bytes)
void event_loop::sock_recv(object sock, int bytes)
{

}

void EventLoop::sock_recv_into(py::object sock, py::object buffer)
void event_loop::sock_recv_into(object sock, object buffer)
{

}

void EventLoop::sock_sendall(py::object sock, py::object data)
void event_loop::sock_sendall(object sock, object data)
{

}

void EventLoop::sock_connect(py::object sock, py::object address)
void event_loop::sock_connect(object sock, object address)
{

}

void EventLoop::sock_accept(py::object sock)
void event_loop::sock_accept(object sock)
{

}

void EventLoop::sock_sendfile(py::object sock, py::object file, int offset, int count, bool fallback)
void event_loop::sock_sendfile(object sock, object file, int offset, int count, bool fallback)
{

}
Expand Down

0 comments on commit 5747a3a

Please sign in to comment.