Skip to content

Commit

Permalink
fix issues
Browse files Browse the repository at this point in the history
- namespace comment hint
- dup() errno check
- fix call_at and time format issue
  • Loading branch information
philoinovsky committed Jul 11, 2021
1 parent 53666c6 commit 130c40e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
6 changes: 2 additions & 4 deletions include/boost/python/eventloop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ namespace boost { namespace python { namespace asio {
class event_loop
{
public:
event_loop(boost::asio::io_context::strand& strand):
_strand{strand}, _created_time{std::chrono::steady_clock::now()}
event_loop(const boost::asio::io_context::strand& strand): _strand{strand}
{
try
{
Expand Down Expand Up @@ -51,7 +50,7 @@ class event_loop

inline double time()
{
return static_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - _created_time).count();
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() / 1e6;
}

inline void add_reader(int fd, object f)
Expand Down Expand Up @@ -125,7 +124,6 @@ class event_loop
boost::asio::io_context::strand _strand;
// 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;

inline int _read_key(int fd)
{
Expand Down
40 changes: 26 additions & 14 deletions src/eventloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// 3. _ensure_fd_no_transport
// 4. _ensure_resolve

#include <errno.h>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
Expand All @@ -27,8 +28,14 @@ bool _hasattr(object o, const char* name)
return PyObject_HasAttrString(o.ptr(), name);
}

void raise_dup_error()
{
PyErr_SetString(PyExc_OSError, std::system_category().message(errno).c_str());
throw_error_already_set();
}

} // namespace

void event_loop::_sock_connect_cb(object pymod_socket, object fut, object sock, object addr)
{
try
Expand Down Expand Up @@ -100,30 +107,28 @@ void event_loop::call_later(double delay, object f)
{
auto p_timer = std::make_shared<boost::asio::steady_timer>(
_strand.context(),
std::chrono::nanoseconds(int64_t(delay * 1e9)));
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::duration<double>(delay)));
p_timer->async_wait(boost::asio::bind_executor(_strand,
[f, p_timer, this] (const boost::system::error_code& ec) {f();}));
[f, p_timer] (const boost::system::error_code& ec) {f();}));
}

void event_loop::call_at(double when, object f)
{
double diff = when - time();
if (diff > 0)
{
auto p_timer = std::make_shared<boost::asio::steady_timer>(
_strand.context(),
std::chrono::nanoseconds(int64_t(diff * 1e9)));
p_timer->async_wait(boost::asio::bind_executor(_strand,
[f, p_timer, this] (const boost::system::error_code& ec) {f();}));
return;
}
call_soon(f);
auto p_timer = std::make_shared<boost::asio::steady_timer>(
_strand.context(),
std::chrono::steady_clock::time_point(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::duration<double>(when))));
p_timer->async_wait(boost::asio::bind_executor(_strand,
[f, p_timer] (const boost::system::error_code& ec) {f();}));
}

object event_loop::sock_recv(object sock, size_t nbytes)
{
int fd = extract<int>(sock.attr("fileno")());
int fd_dup = dup(fd);
if (fd_dup == -1)
raise_dup_error();
object py_fut = _py_wrap_future(_pymod_concurrent_future.attr("Future")());
_async_wait_fd(fd_dup,
[py_fut, nbytes, fd=fd_dup] {
Expand All @@ -139,6 +144,8 @@ object event_loop::sock_recv_into(object sock, object buffer)
{
int fd = extract<int>(sock.attr("fileno")());
int fd_dup = dup(fd);
if (fd_dup == -1)
raise_dup_error();
ssize_t nbytes = len(buffer);
object py_fut = _py_wrap_future(_pymod_concurrent_future.attr("Future")());
_async_wait_fd(fd_dup,
Expand All @@ -155,6 +162,8 @@ object event_loop::sock_sendall(object sock, object data)
{
int fd = extract<int>(sock.attr("fileno")());
int fd_dup = dup(fd);
if (fd_dup == -1)
raise_dup_error();
char const* py_str = extract<char const*>(data.attr("decode")());
ssize_t py_str_len = len(data);
object py_fut = _py_wrap_future(_pymod_concurrent_future.attr("Future")());
Expand Down Expand Up @@ -187,7 +196,10 @@ object event_loop::sock_connect(object sock, object address)
|| PyErr_ExceptionMatches(PyExc_InterruptedError))
{
PyErr_Clear();
_async_wait_fd(dup(fd), bind(_sock_connect_cb, _pymod_socket, py_fut, sock, address), _write_key(fd));
int fd_dup = dup(fd);
if (fd_dup == -1)
raise_dup_error();
_async_wait_fd(fd_dup, bind(_sock_connect_cb, _pymod_socket, py_fut, sock, address), _write_key(fd));
}
else if (PyErr_ExceptionMatches(PyExc_SystemExit)
|| PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
Expand Down

0 comments on commit 130c40e

Please sign in to comment.