Skip to content

Commit 130c40e

Browse files
committed
fix issues
- namespace comment hint - dup() errno check - fix call_at and time format issue
1 parent 53666c6 commit 130c40e

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

include/boost/python/eventloop.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ namespace boost { namespace python { namespace asio {
1818
class event_loop
1919
{
2020
public:
21-
event_loop(boost::asio::io_context::strand& strand):
22-
_strand{strand}, _created_time{std::chrono::steady_clock::now()}
21+
event_loop(const boost::asio::io_context::strand& strand): _strand{strand}
2322
{
2423
try
2524
{
@@ -51,7 +50,7 @@ class event_loop
5150

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

5756
inline void add_reader(int fd, object f)
@@ -125,7 +124,6 @@ class event_loop
125124
boost::asio::io_context::strand _strand;
126125
// read: key = fd * 2 + 0, write: key = fd * 2 + 1
127126
std::unordered_map<int, std::unique_ptr<boost::asio::posix::stream_descriptor>> _descriptor_map;
128-
std::chrono::steady_clock::time_point _created_time;
129127

130128
inline int _read_key(int fd)
131129
{

src/eventloop.cpp

+26-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// 3. _ensure_fd_no_transport
1010
// 4. _ensure_resolve
1111

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

31+
void raise_dup_error()
32+
{
33+
PyErr_SetString(PyExc_OSError, std::system_category().message(errno).c_str());
34+
throw_error_already_set();
3035
}
3136

37+
} // namespace
38+
3239
void event_loop::_sock_connect_cb(object pymod_socket, object fut, object sock, object addr)
3340
{
3441
try
@@ -100,30 +107,28 @@ void event_loop::call_later(double delay, object f)
100107
{
101108
auto p_timer = std::make_shared<boost::asio::steady_timer>(
102109
_strand.context(),
103-
std::chrono::nanoseconds(int64_t(delay * 1e9)));
110+
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::duration<double>(delay)));
104111
p_timer->async_wait(boost::asio::bind_executor(_strand,
105-
[f, p_timer, this] (const boost::system::error_code& ec) {f();}));
112+
[f, p_timer] (const boost::system::error_code& ec) {f();}));
106113
}
107114

108115
void event_loop::call_at(double when, object f)
109116
{
110-
double diff = when - time();
111-
if (diff > 0)
112-
{
113-
auto p_timer = std::make_shared<boost::asio::steady_timer>(
114-
_strand.context(),
115-
std::chrono::nanoseconds(int64_t(diff * 1e9)));
116-
p_timer->async_wait(boost::asio::bind_executor(_strand,
117-
[f, p_timer, this] (const boost::system::error_code& ec) {f();}));
118-
return;
119-
}
120-
call_soon(f);
117+
auto p_timer = std::make_shared<boost::asio::steady_timer>(
118+
_strand.context(),
119+
std::chrono::steady_clock::time_point(
120+
std::chrono::duration_cast<std::chrono::nanoseconds>(
121+
std::chrono::duration<double>(when))));
122+
p_timer->async_wait(boost::asio::bind_executor(_strand,
123+
[f, p_timer] (const boost::system::error_code& ec) {f();}));
121124
}
122125

123126
object event_loop::sock_recv(object sock, size_t nbytes)
124127
{
125128
int fd = extract<int>(sock.attr("fileno")());
126129
int fd_dup = dup(fd);
130+
if (fd_dup == -1)
131+
raise_dup_error();
127132
object py_fut = _py_wrap_future(_pymod_concurrent_future.attr("Future")());
128133
_async_wait_fd(fd_dup,
129134
[py_fut, nbytes, fd=fd_dup] {
@@ -139,6 +144,8 @@ object event_loop::sock_recv_into(object sock, object buffer)
139144
{
140145
int fd = extract<int>(sock.attr("fileno")());
141146
int fd_dup = dup(fd);
147+
if (fd_dup == -1)
148+
raise_dup_error();
142149
ssize_t nbytes = len(buffer);
143150
object py_fut = _py_wrap_future(_pymod_concurrent_future.attr("Future")());
144151
_async_wait_fd(fd_dup,
@@ -155,6 +162,8 @@ object event_loop::sock_sendall(object sock, object data)
155162
{
156163
int fd = extract<int>(sock.attr("fileno")());
157164
int fd_dup = dup(fd);
165+
if (fd_dup == -1)
166+
raise_dup_error();
158167
char const* py_str = extract<char const*>(data.attr("decode")());
159168
ssize_t py_str_len = len(data);
160169
object py_fut = _py_wrap_future(_pymod_concurrent_future.attr("Future")());
@@ -187,7 +196,10 @@ object event_loop::sock_connect(object sock, object address)
187196
|| PyErr_ExceptionMatches(PyExc_InterruptedError))
188197
{
189198
PyErr_Clear();
190-
_async_wait_fd(dup(fd), bind(_sock_connect_cb, _pymod_socket, py_fut, sock, address), _write_key(fd));
199+
int fd_dup = dup(fd);
200+
if (fd_dup == -1)
201+
raise_dup_error();
202+
_async_wait_fd(fd_dup, bind(_sock_connect_cb, _pymod_socket, py_fut, sock, address), _write_key(fd));
191203
}
192204
else if (PyErr_ExceptionMatches(PyExc_SystemExit)
193205
|| PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))

0 commit comments

Comments
 (0)