Skip to content

Commit c3b1465

Browse files
Improved DataSubscriber shutdown operations when auto-reconnect timer is enabled.
1 parent 57ea4e7 commit c3b1465

File tree

5 files changed

+43
-19
lines changed

5 files changed

+43
-19
lines changed

src/lib/CommonTypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ namespace sttp
247247
typedef boost::asio::io_context IOContext;
248248
typedef boost::asio::io_context::strand Strand;
249249
#endif
250-
typedef boost::asio::deadline_timer DeadlineTimer;
250+
typedef boost::asio::steady_timer SteadyTimer;
251251
typedef boost::asio::ip::address IPAddress;
252252
typedef boost::asio::ip::tcp::socket TcpSocket;
253253
typedef boost::asio::ip::udp::socket UdpSocket;

src/lib/Convert.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -428,20 +428,17 @@ Guid sttp::ParseGuid(const uint8_t* data, bool swapEndianness)
428428
copy[i] = swappedBytes[i];
429429
}
430430

431-
if (swapEndianness)
432-
{
433-
// Convert Microsoft encoding to RFC
434-
swappedBytes[3] = copy[0];
435-
swappedBytes[2] = copy[1];
436-
swappedBytes[1] = copy[2];
437-
swappedBytes[0] = copy[3];
431+
// Convert Microsoft encoding to RFC
432+
swappedBytes[3] = copy[0];
433+
swappedBytes[2] = copy[1];
434+
swappedBytes[1] = copy[2];
435+
swappedBytes[0] = copy[3];
438436

439-
swappedBytes[4] = copy[5];
440-
swappedBytes[5] = copy[4];
437+
swappedBytes[4] = copy[5];
438+
swappedBytes[5] = copy[4];
441439

442-
swappedBytes[6] = copy[7];
443-
swappedBytes[7] = copy[6];
444-
}
440+
swappedBytes[6] = copy[7];
441+
swappedBytes[7] = copy[6];
445442

446443
encodedBytes = swappedBytes;
447444
}

src/lib/Version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define __VERSION_H
2626

2727
#define STTP_TITLE "STTP C++ Library"
28-
#define STTP_VERSION "1.0.7"
29-
#define STTP_UPDATEDON "2019-07-01"
28+
#define STTP_VERSION "1.0.8"
29+
#define STTP_UPDATEDON "2019-07-02"
3030

3131
#endif

src/lib/transport/DataSubscriber.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <boost/bind.hpp>
3434

3535
using namespace std;
36+
using namespace std::chrono;
3637
using namespace boost;
3738
using namespace boost::asio;
3839
using namespace boost::asio::ip;
@@ -60,6 +61,7 @@ SubscriptionInfo::SubscriptionInfo() :
6061

6162
SubscriberConnector::SubscriberConnector() :
6263
m_port(0),
64+
m_timer(nullptr),
6365
m_maxRetries(-1),
6466
m_retryInterval(2000),
6567
m_maxRetryInterval(120000),
@@ -113,8 +115,15 @@ void SubscriberConnector::AutoReconnect(DataSubscriber* subscriber)
113115
if (retryInterval > 0)
114116
{
115117
IOContext io;
116-
DeadlineTimer timer(io, Milliseconds(retryInterval));
117-
timer.wait();
118+
119+
connector.m_timer = new SteadyTimer(io, milliseconds(retryInterval));
120+
connector.m_timer->wait();
121+
122+
if (connector.m_cancel)
123+
return;
124+
125+
delete connector.m_timer;
126+
connector.m_timer = nullptr;
118127
}
119128

120129
connector.Connect(*subscriber, true);
@@ -208,8 +217,15 @@ bool SubscriberConnector::Connect(DataSubscriber& subscriber, bool autoReconnect
208217
if (retryInterval > 0)
209218
{
210219
IOContext io;
211-
DeadlineTimer timer(io, Milliseconds(retryInterval));
212-
timer.wait();
220+
221+
m_timer = new SteadyTimer(io, milliseconds(retryInterval));
222+
m_timer->wait();
223+
224+
if (m_cancel)
225+
return false;
226+
227+
delete m_timer;
228+
m_timer = nullptr;
213229
}
214230
}
215231
}
@@ -222,6 +238,16 @@ bool SubscriberConnector::Connect(DataSubscriber& subscriber, bool autoReconnect
222238
void SubscriberConnector::Cancel()
223239
{
224240
m_cancel = true;
241+
242+
if (m_timer != nullptr)
243+
{
244+
// Cancel any waiting timer operations by setting immediate timer expiration
245+
m_timer->expires_at(SteadyTimer::time_point::min());
246+
m_timer->wait();
247+
248+
delete m_timer;
249+
m_timer = nullptr;
250+
}
225251
}
226252

227253
// Sets the hostname of the publisher to connect to.

src/lib/transport/DataSubscriber.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ namespace transport
8080

8181
std::string m_hostname;
8282
uint16_t m_port;
83+
SteadyTimer* m_timer;
8384

8485
int32_t m_maxRetries;
8586
int32_t m_retryInterval;

0 commit comments

Comments
 (0)