174174 // b. IPv6 addresses with ports require square brackets [fe80::1%lo0]:53
175175 YOPT_S_DNS_LIST,
176176
177-
178177 // Set ssl server cert and private key file
179178 // params:
180179 // crtfile: const char*
@@ -400,10 +399,10 @@ class YASIO_API highp_timer {
400399 void expires_from_now (const std::chrono::microseconds& duration)
401400 {
402401 this ->duration_ = duration;
403- this ->expire_time_ = yasio:: steady_clock_t::now () + this ->duration_ ;
402+ this ->expire_time_ = steady_clock_t::now () + this ->duration_ ;
404403 }
405404
406- void expires_from_now () { this ->expire_time_ = yasio:: steady_clock_t::now () + this ->duration_ ; }
405+ void expires_from_now () { this ->expire_time_ = steady_clock_t::now () + this ->duration_ ; }
407406
408407 // Wait timer timeout once.
409408 void async_wait_once (io_service& service, timerv_cb_t cb)
@@ -428,13 +427,17 @@ class YASIO_API highp_timer {
428427 YASIO__DECL void cancel (io_service& service);
429428
430429 // Check if timer is expired?
431- bool expired (io_service& service ) const { return this -> wait_duration (service ).count () <= 0 ; }
430+ bool expired () const { return wait_duration ().count () <= 0 ; }
432431
433432 // Gets wait duration of timer.
434- YASIO__DECL std::chrono::microseconds wait_duration (io_service& service) const ;
433+ std::chrono::microseconds wait_duration () const { return this ->wait_duration (steady_clock_t::now ()); }
434+ std::chrono::microseconds wait_duration (const std::chrono::time_point<steady_clock_t >& now_time) const
435+ {
436+ return std::chrono::duration_cast<std::chrono::microseconds>(this ->expire_time_ - now_time);
437+ }
435438
436- std::chrono::microseconds duration_ = {};
437- std::chrono::time_point<yasio:: steady_clock_t > expire_time_ = {};
439+ std::chrono::microseconds duration_ = {};
440+ std::chrono::time_point<steady_clock_t > expire_time_ = {};
438441};
439442
440443struct YASIO_API io_base {
@@ -1058,8 +1061,11 @@ class YASIO_API io_service // lgtm [cpp/class-many-fields]
10581061 }
10591062 void sort_timers ()
10601063 {
1061- std::sort (this ->timer_queue_ .begin (), this ->timer_queue_ .end (),
1062- [](const timer_impl_t & lhs, const timer_impl_t & rhs) { return lhs.first ->expire_time_ > rhs.first ->expire_time_ ; });
1064+ // Must ensure time now stable, otherwise will cause std::sort pass invalid iterator to sort callback
1065+ const auto now_time = steady_clock_t::now ();
1066+ std::sort (this ->timer_queue_ .begin (), this ->timer_queue_ .end (), [&now_time](const timer_impl_t & lhs, const timer_impl_t & rhs) {
1067+ return lhs.first ->wait_duration (now_time) > rhs.first ->wait_duration (now_time);
1068+ });
10631069 }
10641070
10651071 // Start a async domain name query
@@ -1128,7 +1134,7 @@ class YASIO_API io_service // lgtm [cpp/class-many-fields]
11281134 inline void fire_event (_Types&&... args)
11291135 {
11301136 auto event = cxx14::make_unique<io_event>(std::forward<_Types>(args)...);
1131- if (options_.deferred_event_ && !options_. forward_event_ )
1137+ if (options_.deferred_event_ )
11321138 {
11331139 if (options_.on_defer_event_ && !options_.on_defer_event_ (event))
11341140 return ;
@@ -1169,16 +1175,11 @@ class YASIO_API io_service // lgtm [cpp/class-many-fields]
11691175 /* For log macro only */
11701176 inline const print_fn2_t & __get_cprint () const { return options_.print_ ; }
11711177
1172- void update_time () { this ->time_ = yasio::steady_clock_t::now (); }
1173-
11741178private:
11751179 state state_ = state::UNINITIALIZED; // The service state
11761180 std::thread worker_;
11771181 std::thread::id worker_id_;
11781182
1179- /* The current time according to the event loop. in msecs. */
1180- std::chrono::time_point<yasio::steady_clock_t > time_;
1181-
11821183 privacy::concurrent_queue<event_ptr, true > events_;
11831184
11841185 std::vector<io_channel*> channels_;
@@ -1213,7 +1214,6 @@ class YASIO_API io_service // lgtm [cpp/class-many-fields]
12131214 bool deferred_event_ = true ;
12141215 defer_event_cb_t on_defer_event_;
12151216
1216- bool forward_event_ = false ; // since v3.39.7
12171217 bool forward_packet_ = false ; // since v3.39.8
12181218
12191219 // tcp keepalive settings
0 commit comments