Skip to content

Commit 4fededf

Browse files
Converted reconnect operation wait timer to use sttp::Timer
1 parent c3b1465 commit 4fededf

File tree

4 files changed

+50
-34
lines changed

4 files changed

+50
-34
lines changed

src/lib/Timer.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace sttp
3232
{
3333
class Timer;
3434
typedef std::function<void(Timer* timer, void* userData)> TimerElapsedCallback;
35+
typedef sttp::SharedPtr<Timer> TimerPtr;
3536

3637
class Timer // NOLINT
3738
{
@@ -153,6 +154,26 @@ namespace sttp
153154

154155
m_timerThread.reset();
155156
}
157+
158+
void Wait() const
159+
{
160+
if (m_running && m_timerThread != nullptr)
161+
m_timerThread->join();
162+
}
163+
164+
static void EmptyCallback(Timer*, void*)
165+
{
166+
}
167+
168+
static TimerPtr WaitTimer(const int32_t interval, bool autoStart = true)
169+
{
170+
TimerPtr waitTimer = NewSharedPtr<Timer>(interval, EmptyCallback);
171+
172+
if (autoStart)
173+
waitTimer->Start();
174+
175+
return waitTimer;
176+
}
156177
};
157178

158179
typedef sttp::SharedPtr<Timer> TimerPtr;

src/lib/transport/DataSubscriber.cpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@
2424
//******************************************************************************************************
2525

2626
#include "DataSubscriber.h"
27-
#include "../Version.h"
2827
#include "Constants.h"
2928
#include "CompactMeasurement.h"
3029
#include "../Convert.h"
3130
#include "../EndianConverter.h"
31+
#include "../Version.h"
3232
#include <sstream>
3333
#include <boost/bind.hpp>
3434

3535
using namespace std;
36-
using namespace std::chrono;
3736
using namespace boost;
3837
using namespace boost::asio;
3938
using namespace boost::asio::ip;
@@ -114,19 +113,15 @@ void SubscriberConnector::AutoReconnect(DataSubscriber* subscriber)
114113

115114
if (retryInterval > 0)
116115
{
117-
IOContext io;
118-
119-
connector.m_timer = new SteadyTimer(io, milliseconds(retryInterval));
120-
connector.m_timer->wait();
116+
connector.m_timer = Timer::WaitTimer(retryInterval);
117+
connector.m_timer->Wait();
121118

122119
if (connector.m_cancel)
123120
return;
124-
125-
delete connector.m_timer;
126-
connector.m_timer = nullptr;
127121
}
128122

129-
connector.Connect(*subscriber, true);
123+
if (connector.Connect(*subscriber, true) == ConnectCanceled)
124+
return;
130125

131126
// Notify the user that reconnect attempt was completed.
132127
if (!connector.m_cancel && connector.m_reconnectCallback != nullptr)
@@ -146,14 +141,17 @@ void SubscriberConnector::RegisterReconnectCallback(const ReconnectCallback& rec
146141
m_reconnectCallback = reconnectCallback;
147142
}
148143

149-
bool SubscriberConnector::Connect(DataSubscriber& subscriber, const SubscriptionInfo& info)
144+
int SubscriberConnector::Connect(DataSubscriber& subscriber, const SubscriptionInfo& info)
150145
{
146+
if (m_cancel)
147+
return ConnectCanceled;
148+
151149
subscriber.SetSubscriptionInfo(info);
152150
return Connect(subscriber, false);
153151
}
154152

155153
// Begin connection sequence.
156-
bool SubscriberConnector::Connect(DataSubscriber& subscriber, bool autoReconnecting)
154+
int SubscriberConnector::Connect(DataSubscriber& subscriber, bool autoReconnecting)
157155
{
158156
if (m_autoReconnect)
159157
subscriber.RegisterAutoReconnectCallback(&AutoReconnect);
@@ -216,21 +214,16 @@ bool SubscriberConnector::Connect(DataSubscriber& subscriber, bool autoReconnect
216214

217215
if (retryInterval > 0)
218216
{
219-
IOContext io;
220-
221-
m_timer = new SteadyTimer(io, milliseconds(retryInterval));
222-
m_timer->wait();
217+
m_timer = Timer::WaitTimer(retryInterval);
218+
m_timer->Wait();
223219

224220
if (m_cancel)
225-
return false;
226-
227-
delete m_timer;
228-
m_timer = nullptr;
221+
return ConnectCanceled;
229222
}
230223
}
231224
}
232225

233-
return subscriber.IsConnected();
226+
return subscriber.IsConnected() ? ConnectSuccess : ConnectFailed;
234227
}
235228

236229
// Cancel all current and
@@ -239,15 +232,9 @@ void SubscriberConnector::Cancel()
239232
{
240233
m_cancel = true;
241234

235+
// Cancel any waiting timer operations by setting immediate timer expiration
242236
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-
}
237+
m_timer->Stop();
251238
}
252239

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

src/lib/transport/DataSubscriber.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "TransportTypes.h"
3030
#include "SignalIndexCache.h"
3131
#include "tssc/TSSCDecoder.h"
32+
#include "../Timer.h"
3233
#include "../ThreadSafeQueue.h"
3334

3435
namespace sttp {
@@ -80,7 +81,7 @@ namespace transport
8081

8182
std::string m_hostname;
8283
uint16_t m_port;
83-
SteadyTimer* m_timer;
84+
sttp::TimerPtr m_timer;
8485

8586
int32_t m_maxRetries;
8687
int32_t m_retryInterval;
@@ -93,9 +94,13 @@ namespace transport
9394
// Auto-reconnect handler.
9495
static void AutoReconnect(DataSubscriber* subscriber);
9596

96-
bool Connect(DataSubscriber& subscriber, bool autoReconnecting);
97+
int Connect(DataSubscriber& subscriber, bool autoReconnecting);
9798

9899
public:
100+
static constexpr int ConnectSuccess = 1;
101+
static constexpr int ConnectFailed = 0;
102+
static constexpr int ConnectCanceled = -1;
103+
99104
// Creates a new instance.
100105
SubscriberConnector();
101106

@@ -109,7 +114,7 @@ namespace transport
109114
void RegisterReconnectCallback(const ReconnectCallback& reconnectCallback);
110115

111116
// Begin connection sequence
112-
bool Connect(DataSubscriber& subscriber, const SubscriptionInfo& info);
117+
int Connect(DataSubscriber& subscriber, const SubscriptionInfo& info);
113118

114119
// Cancel all current and
115120
// future connection sequences.

src/lib/transport/SubscriberInstance.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ void SubscriberInstance::Connect()
180180
}
181181

182182
// Connect and subscribe to publisher
183-
if (connector.Connect(*m_subscriber, m_subscriptionInfo))
183+
const int result = connector.Connect(*m_subscriber, m_subscriptionInfo);
184+
185+
if (result == SubscriberConnector::ConnectSuccess)
184186
{
185187
ConnectionEstablished();
186188

@@ -194,7 +196,8 @@ void SubscriberInstance::Connect()
194196
}
195197
else
196198
{
197-
ErrorMessage("All connection attempts failed");
199+
if (result == SubscriberConnector::ConnectFailed)
200+
ErrorMessage("All connection attempts failed");
198201
}
199202
}
200203

0 commit comments

Comments
 (0)