Skip to content

Commit a9efac6

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

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

include/boost/python/eventloop.hpp

+3-5
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::steady_clock::now().time_since_epoch().count();
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
{
@@ -161,7 +159,7 @@ class event_loop
161159
static void _sock_accept(event_loop& loop, object fut, object sock);
162160
};
163161

164-
}}}
162+
}}} // namespace boost::python
165163

166164

167165
# endif

src/eventloop.cpp

+26-15
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,27 @@ 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+
using sc = std::chrono::steady_clock;
118+
auto p_timer = std::make_shared<boost::asio::steady_timer>(
119+
_strand.context(),
120+
sc::duration(static_cast<sc::time_point::rep>(when)));
121+
p_timer->async_wait(boost::asio::bind_executor(_strand,
122+
[f, p_timer] (const boost::system::error_code& ec) {f();}));
121123
}
122124

123125
object event_loop::sock_recv(object sock, size_t nbytes)
124126
{
125127
int fd = extract<int>(sock.attr("fileno")());
126128
int fd_dup = dup(fd);
129+
if (fd_dup == -1)
130+
raise_dup_error();
127131
object py_fut = _py_wrap_future(_pymod_concurrent_future.attr("Future")());
128132
_async_wait_fd(fd_dup,
129133
[py_fut, nbytes, fd=fd_dup] {
@@ -139,6 +143,8 @@ object event_loop::sock_recv_into(object sock, object buffer)
139143
{
140144
int fd = extract<int>(sock.attr("fileno")());
141145
int fd_dup = dup(fd);
146+
if (fd_dup == -1)
147+
raise_dup_error();
142148
ssize_t nbytes = len(buffer);
143149
object py_fut = _py_wrap_future(_pymod_concurrent_future.attr("Future")());
144150
_async_wait_fd(fd_dup,
@@ -155,6 +161,8 @@ object event_loop::sock_sendall(object sock, object data)
155161
{
156162
int fd = extract<int>(sock.attr("fileno")());
157163
int fd_dup = dup(fd);
164+
if (fd_dup == -1)
165+
raise_dup_error();
158166
char const* py_str = extract<char const*>(data.attr("decode")());
159167
ssize_t py_str_len = len(data);
160168
object py_fut = _py_wrap_future(_pymod_concurrent_future.attr("Future")());
@@ -187,7 +195,10 @@ object event_loop::sock_connect(object sock, object address)
187195
|| PyErr_ExceptionMatches(PyExc_InterruptedError))
188196
{
189197
PyErr_Clear();
190-
_async_wait_fd(dup(fd), bind(_sock_connect_cb, _pymod_socket, py_fut, sock, address), _write_key(fd));
198+
int fd_dup = dup(fd);
199+
if (fd_dup == -1)
200+
raise_dup_error();
201+
_async_wait_fd(fd_dup, bind(_sock_connect_cb, _pymod_socket, py_fut, sock, address), _write_key(fd));
191202
}
192203
else if (PyErr_ExceptionMatches(PyExc_SystemExit)
193204
|| PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
@@ -388,4 +399,4 @@ void event_loop::call_exception_handler(object context)
388399
}
389400

390401

391-
}}}
402+
}}} // namespace boost::python

0 commit comments

Comments
 (0)