88#include < logging.h>
99#include < netaddress.h>
1010#include < netbase.h>
11+ #include < scheduler.h>
1112#include < util/sock.h>
1213#include < util/thread.h>
1314
@@ -38,26 +39,19 @@ RawSender::RawSender(const std::string& host, uint16_t port, bool use_tcp, std::
3839
3940 if (m_interval_ms == 0 ) {
4041 LogPrint (BCLog::NET, " %s: Send interval is zero, not starting RawSender queueing thread.\n " , __func__);
41- } else {
42- m_interrupt.reset ();
43- m_thread = std::thread (&util::TraceThread, " rawsender" , [this ] { QueueThreadMain (); });
4442 }
4543
4644 if (m_use_tcp) {
4745 m_reconn = std::thread (&util::TraceThread, " rawreconnect" , [this ] { ReconnectThread (); });
4846 }
4947
50- LogPrint (BCLog::NET, " %s: Started %sinstance sending messages to %s over %s\n " , __func__,
51- m_thread. joinable () ? " threaded " : " " , this -> ToStringHostPort (), m_use_tcp ? " TCP" : " UDP" );
48+ LogPrint (BCLog::NET, " %s: Started instance sending messages to %s over %s\n " , __func__, this -> ToStringHostPort () ,
49+ m_use_tcp ? " TCP" : " UDP" );
5250}
5351
5452RawSender::~RawSender ()
5553{
5654 // If there are threads, interrupt and stop it
57- if (m_thread.joinable ()) {
58- m_interrupt ();
59- m_thread.join ();
60- }
6155 if (m_reconn.joinable ()) {
6256 m_reconn_interrupt ();
6357 m_reconn.join ();
@@ -67,6 +61,12 @@ RawSender::~RawSender()
6761 QueueFlush (m_queue);
6862}
6963
64+ void RawSender::Schedule (CScheduler& scheduler)
65+ {
66+ if (m_interval_ms == 0 ) return ;
67+ scheduler.scheduleEvery ([this ] { this ->QueueThreadMain (); }, std::chrono::milliseconds{m_interval_ms});
68+ }
69+
7070std::optional<bilingual_str> RawSender::Connect ()
7171{
7272 AssertLockHeld (cs_net);
@@ -167,8 +167,8 @@ std::optional<bilingual_str> RawSender::Send(const RawMessage& msg)
167167 AssertLockNotHeld (cs);
168168 AssertLockNotHeld (cs_net);
169169
170- // If there is a thread , append to queue
171- if (m_thread. joinable () ) {
170+ // If queueing is enabled , append
171+ if (m_interval_ms > 0 ) {
172172 WITH_LOCK (cs, QueueAdd (m_queue, msg));
173173 return std::nullopt ;
174174 }
@@ -283,14 +283,8 @@ void RawSender::QueueThreadMain()
283283 AssertLockNotHeld (cs);
284284 AssertLockNotHeld (cs_net);
285285
286- while (!m_interrupt) {
287- // Swap the queues to commit the existing queue of messages
288- std::vector<RawMessage> queue;
289- WITH_LOCK (cs, m_queue.swap (queue));
290- QueueFlush (queue);
291-
292- if (!m_interrupt.sleep_for (std::chrono::milliseconds (m_interval_ms))) {
293- return ;
294- }
295- }
286+ // Swap the queues to commit the existing queue of messages
287+ std::vector<RawMessage> queue;
288+ WITH_LOCK (cs, m_queue.swap (queue));
289+ QueueFlush (queue);
296290}
0 commit comments