Skip to content

Commit 0baa233

Browse files
committed
Simplifying ASyncTCP usage
Fixed double IsConnected call Classes don't directly use private variables from ASyncTCP Fritzbox now uses it's own buffer instead of ASyncTCP! Moved and removed correct defines between headers (BaseHardware, RXBase)
1 parent 0a04a85 commit 0baa233

38 files changed

+215
-444
lines changed

hardware/ASyncTCP.cpp

Lines changed: 42 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ASyncTCP::ASyncTCP()
2525
: mIsConnected(false), mIsClosing(false),
2626
mSocket(mIos), mReconnectTimer(mIos),
2727
mDoReconnect(true), mIsReconnecting(false),
28+
mAllowCallbacks(true),
2829
m_reconnect_delay(RECONNECT_TIME)
2930
{
3031
}
@@ -39,31 +40,6 @@ void ASyncTCP::SetReconnectDelay(int Delay)
3940
m_reconnect_delay = Delay;
4041
}
4142

42-
void ASyncTCP::setCallbacks(
43-
const boost::function<void()>&cb_connect,
44-
const boost::function<void()>&cb_disconnect,
45-
const boost::function<void(const unsigned char *pData, size_t length)>&cb_data,
46-
const boost::function<void(const std::exception e)>&cb_error_std,
47-
const boost::function<void(const boost::system::error_code& error)>&cb_error_boost
48-
)
49-
{
50-
callback_connect = cb_connect;
51-
callback_disconnect = cb_disconnect;
52-
callback_data = cb_data;
53-
callback_error_std = cb_error_std;
54-
callback_error_boost = cb_error_boost;
55-
}
56-
57-
void ASyncTCP::clearCallbacks()
58-
{
59-
callback_connect.clear();
60-
callback_disconnect.clear();
61-
callback_data.clear();
62-
callback_error_std.clear();
63-
callback_error_boost.clear();
64-
}
65-
66-
6743
void ASyncTCP::update()
6844
{
6945
if (mIsClosing)
@@ -104,9 +80,10 @@ void ASyncTCP::connect(const std::string &ip, unsigned short port)
10480
}
10581
catch(const std::exception &e)
10682
{
83+
if (!mAllowCallbacks)
84+
return;
10785
_log.Log(LOG_ERROR, "TCP: Exception: %s", e.what());
108-
if (callback_error_std)
109-
callback_error_std(e);
86+
OnError(e);
11087
}
11188
}
11289

@@ -115,6 +92,8 @@ void ASyncTCP::connect(boost::asio::ip::tcp::endpoint& endpoint)
11592
if(mIsConnected) return;
11693
if(mIsClosing) return;
11794

95+
mAllowCallbacks = true;
96+
11897
mEndPoint = endpoint;
11998

12099
// restart IO service if it has been stopped
@@ -128,33 +107,35 @@ void ASyncTCP::connect(boost::asio::ip::tcp::endpoint& endpoint)
128107
boost::bind(&ASyncTCP::handle_connect, this, boost::asio::placeholders::error));
129108
}
130109

131-
void ASyncTCP::disconnect()
110+
void ASyncTCP::disconnect(const bool silent)
132111
{
133-
if (isConnected())
112+
try
134113
{
135-
// tell socket to close the connection
136-
close();
137-
138-
// tell the IO service to stop
139-
mIos.stop();
140-
mIsConnected = false;
141-
mIsClosing = false;
142-
}
143-
}
114+
if (isConnected())
115+
{
116+
// tell socket to close the connection
117+
close();
144118

145-
void ASyncTCP::terminate(const bool silent)
146-
{
147-
try {
148-
clearCallbacks();
149-
disconnect();
119+
// tell the IO service to stop
120+
mIos.stop();
121+
mIsConnected = false;
122+
mIsClosing = false;
123+
}
150124
}
151-
catch (...) {
125+
catch (...)
126+
{
152127
if (silent == false) {
153128
throw;
154129
}
155130
}
156131
}
157132

133+
void ASyncTCP::terminate(const bool silent)
134+
{
135+
mAllowCallbacks = false;
136+
disconnect(silent);
137+
}
138+
158139
void ASyncTCP::StartReconnect()
159140
{
160141
if (m_reconnect_delay != 0)
@@ -256,8 +237,8 @@ void ASyncTCP::handle_connect(const boost::system::error_code& error)
256237

257238
//set_tcp_keepalive();
258239

259-
if (callback_connect)
260-
callback_connect();
240+
if (mAllowCallbacks)
241+
OnConnect();
261242

262243
// Start Reading
263244
//This gives some work to the io_service before it is started
@@ -267,14 +248,14 @@ void ASyncTCP::handle_connect(const boost::system::error_code& error)
267248
// there was an error :(
268249
mIsConnected = false;
269250

270-
if (callback_error_boost)
271-
callback_error_boost(error);
251+
if (mAllowCallbacks)
252+
OnError(error);
272253
OnErrorInt(error);
273254

274255
if (!mDoReconnect)
275256
{
276-
if (callback_disconnect)
277-
callback_disconnect();
257+
if (mAllowCallbacks)
258+
OnDisconnect();
278259
return;
279260
}
280261
if (!mIsReconnecting)
@@ -289,7 +270,7 @@ void ASyncTCP::read()
289270
if (!mIsConnected) return;
290271
if (mIsClosing) return;
291272

292-
mSocket.async_read_some(boost::asio::buffer(m_buffer, sizeof(m_buffer)),
273+
mSocket.async_read_some(boost::asio::buffer(m_rx_buffer, sizeof(m_rx_buffer)),
293274
boost::bind(&ASyncTCP::handle_read,
294275
this,
295276
boost::asio::placeholders::error,
@@ -300,8 +281,8 @@ void ASyncTCP::handle_read(const boost::system::error_code& error, size_t bytes_
300281
{
301282
if (!error)
302283
{
303-
if (callback_data)
304-
callback_data(m_buffer,bytes_transferred);
284+
if (mAllowCallbacks)
285+
OnData(m_rx_buffer,bytes_transferred);
305286
//Read next
306287
//This gives some work to the io_service before it is started
307288
mIos.post(boost::bind(&ASyncTCP::read, this));
@@ -314,12 +295,12 @@ void ASyncTCP::handle_read(const boost::system::error_code& error, size_t bytes_
314295
mIsConnected = false;
315296

316297
// let listeners know
317-
if (callback_error_boost)
318-
callback_error_boost(error);
298+
if (mAllowCallbacks)
299+
OnError(error);
319300
if (!mDoReconnect)
320301
{
321-
if (callback_disconnect)
322-
callback_disconnect();
302+
if (mAllowCallbacks)
303+
OnDisconnect();
323304
return;
324305
}
325306
if (!mIsReconnecting)
@@ -339,15 +320,15 @@ void ASyncTCP::write_end(const boost::system::error_code& error)
339320
if (error)
340321
{
341322
// let listeners know
342-
if (callback_error_boost)
343-
callback_error_boost(error);
323+
if (mAllowCallbacks)
324+
OnError(error);
344325

345326
mIsConnected = false;
346327

347328
if (!mDoReconnect)
348329
{
349-
if (callback_disconnect)
350-
callback_disconnect();
330+
if (mAllowCallbacks)
331+
OnDisconnect();
351332
return;
352333
}
353334
if (!mIsReconnecting)

hardware/ASyncTCP.h

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class ASyncTCP
2020

2121
void connect(const std::string &ip, unsigned short port);
2222
void connect(boost::asio::ip::tcp::endpoint& endpoint);
23-
void disconnect();
2423
bool isConnected() { return mIsConnected; };
24+
void disconnect(const bool silent = true);
2525
void terminate(const bool silent = true);
2626

2727
void write(const std::string &msg);
@@ -30,23 +30,13 @@ class ASyncTCP
3030
void update();
3131

3232
void SetReconnectDelay(int Delay = 30); //0 disabled retry
33-
34-
/// Read complete callback
35-
boost::function<void()> callback_connect;
36-
boost::function<void()> callback_disconnect;
37-
boost::function<void(const unsigned char *pData, size_t length)> callback_data;
38-
boost::function<void(const std::exception e)> callback_error_std;
39-
boost::function<void(const boost::system::error_code& error)> callback_error_boost;
4033
protected:
41-
void setCallbacks(
42-
const boost::function<void()>&cb_connect,
43-
const boost::function<void()>&cb_disconnect,
44-
const boost::function<void(const unsigned char *pData, size_t length)>&cb_data,
45-
const boost::function<void(const std::exception e)>&cb_error_std,
46-
const boost::function<void(const boost::system::error_code& error)>&cb_error_boost
47-
);
48-
void clearCallbacks();
49-
34+
virtual void OnConnect() = 0;
35+
virtual void OnDisconnect() = 0;
36+
virtual void OnData(const unsigned char *pData, size_t length) = 0;
37+
virtual void OnError(const std::exception e) = 0;
38+
virtual void OnError(const boost::system::error_code& error) = 0;
39+
private:
5040
void read();
5141
void close();
5242

@@ -59,22 +49,21 @@ class ASyncTCP
5949
void do_close();
6050

6151
void do_reconnect(const boost::system::error_code& error);
62-
private:
6352
void OnErrorInt(const boost::system::error_code& error);
6453
void StartReconnect();
6554
int m_reconnect_delay;
66-
protected:
6755
bool mIsConnected;
6856
bool mIsClosing;
6957
bool mDoReconnect;
7058
bool mIsReconnecting;
59+
bool mAllowCallbacks;
7160

7261
boost::asio::ip::tcp::endpoint mEndPoint;
7362

7463
boost::asio::io_service mIos;
7564
boost::asio::ip::tcp::socket mSocket;
7665

77-
unsigned char m_buffer[1024];
66+
unsigned char m_rx_buffer[1024];
7867

7968
boost::asio::deadline_timer mReconnectTimer;
8069

hardware/Comm5SMTCP.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ bool Comm5SMTCP::StartHardware()
5050
//force connect the next first time
5151
m_bIsStarted = true;
5252

53-
setCallbacks(
54-
boost::bind(&Comm5SMTCP::OnConnect, this),
55-
boost::bind(&Comm5SMTCP::OnDisconnect, this),
56-
boost::bind(&Comm5SMTCP::OnData, this, _1, _2),
57-
boost::bind(&Comm5SMTCP::OnErrorStd, this, _1),
58-
boost::bind(&Comm5SMTCP::OnErrorBoost, this, _1)
59-
);
60-
6153
//Start worker thread
6254
m_thread = std::make_shared<std::thread>(&Comm5SMTCP::Do_Work, this);
6355
SetThreadName(m_thread->native_handle(), "Comm5SMTCP");
@@ -103,7 +95,7 @@ void Comm5SMTCP::Do_Work()
10395
if (bFirstTime)
10496
{
10597
bFirstTime = false;
106-
if (!mIsConnected)
98+
if (!isConnected())
10799
{
108100
connect(m_szIPAddress, m_usIPPort);
109101
}
@@ -181,12 +173,12 @@ void Comm5SMTCP::OnData(const unsigned char *pData, size_t length)
181173
ParseData(pData, length);
182174
}
183175

184-
void Comm5SMTCP::OnErrorStd(const std::exception e)
176+
void Comm5SMTCP::OnError(const std::exception e)
185177
{
186178
_log.Log(LOG_ERROR, "Comm5 SM-XXXX: Error: %s", e.what());
187179
}
188180

189-
void Comm5SMTCP::OnErrorBoost(const boost::system::error_code& error)
181+
void Comm5SMTCP::OnError(const boost::system::error_code& error)
190182
{
191183
switch (error.value())
192184
{

hardware/Comm5SMTCP.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ class Comm5SMTCP : public CDomoticzHardwareBase, ASyncTCP
1515
bool StartHardware() override;
1616
bool StopHardware() override;
1717
protected:
18-
void OnConnect();
19-
void OnDisconnect();
20-
void OnData(const unsigned char *pData, size_t length);
21-
void OnErrorStd(const std::exception e);
22-
void OnErrorBoost(const boost::system::error_code& error);
18+
void OnConnect() override;
19+
void OnDisconnect() override;
20+
void OnData(const unsigned char *pData, size_t length) override;
21+
void OnError(const std::exception e) override;
22+
void OnError(const boost::system::error_code& error) override;
23+
2324
void Do_Work();
2425
void ParseData(const unsigned char *data, const size_t len);
2526
void querySensorState();

hardware/Comm5TCP.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ bool Comm5TCP::StartHardware()
5454
//force connect the next first time
5555
m_bIsStarted = true;
5656

57-
setCallbacks(
58-
boost::bind(&Comm5TCP::OnConnect, this),
59-
boost::bind(&Comm5TCP::OnDisconnect, this),
60-
boost::bind(&Comm5TCP::OnData, this, _1, _2),
61-
boost::bind(&Comm5TCP::OnErrorStd, this, _1),
62-
boost::bind(&Comm5TCP::OnErrorBoost, this, _1)
63-
);
64-
6557
//Start worker thread
6658
m_thread = std::make_shared<std::thread>(&Comm5TCP::Do_Work, this);
6759
SetThreadName(m_thread->native_handle(), "Comm5TCP");
@@ -110,7 +102,7 @@ void Comm5TCP::Do_Work()
110102
if (bFirstTime)
111103
{
112104
bFirstTime = false;
113-
if (!mIsConnected)
105+
if (!isConnected())
114106
{
115107
connect(m_szIPAddress, m_usIPPort);
116108
}
@@ -198,7 +190,7 @@ bool Comm5TCP::WriteToHardware(const char *pdata, const unsigned char /*length*/
198190
unsigned char packettype = pSen->ICMND.packettype;
199191
//unsigned char subtype = pSen->ICMND.subtype;
200192

201-
if (!mIsConnected)
193+
if (!isConnected())
202194
return false;
203195

204196
if (packettype == pTypeLighting2 && pSen->LIGHTING2.id3 == 0)
@@ -224,12 +216,12 @@ void Comm5TCP::OnData(const unsigned char *pData, size_t length)
224216
ParseData(pData, length);
225217
}
226218

227-
void Comm5TCP::OnErrorStd(const std::exception e)
219+
void Comm5TCP::OnError(const std::exception e)
228220
{
229221
_log.Log(LOG_ERROR, "Comm5 MA-5XXX: Error: %s", e.what());
230222
}
231223

232-
void Comm5TCP::OnErrorBoost(const boost::system::error_code& error)
224+
void Comm5TCP::OnError(const boost::system::error_code& error)
233225
{
234226
switch (error.value())
235227
{

hardware/Comm5TCP.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class Comm5TCP : public CDomoticzHardwareBase, ASyncTCP
1515
bool StartHardware() override;
1616
bool StopHardware() override;
1717
protected:
18-
void OnConnect();
19-
void OnDisconnect();
20-
void OnData(const unsigned char *pData, size_t length);
21-
void OnErrorStd(const std::exception e);
22-
void OnErrorBoost(const boost::system::error_code& error);
18+
void OnConnect() override;
19+
void OnDisconnect() override;
20+
void OnData(const unsigned char *pData, size_t length) override;
21+
void OnError(const std::exception e) override;
22+
void OnError(const boost::system::error_code& error) override;
2323

2424
void Do_Work();
2525
void ParseData(const unsigned char *data, const size_t len);

0 commit comments

Comments
 (0)