Skip to content

Commit fc79f68

Browse files
Changed the void output functions to return bool for success or failure.
1 parent 1602886 commit fc79f68

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

include/aws/crt/mqtt/MqttClient.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,25 @@ namespace Aws
104104
/**
105105
* Sets LastWill for the connection. The memory backing payload must outlive the connection.
106106
*/
107-
void SetWill(const char* topic, QOS qos, bool retain,
107+
bool SetWill(const char* topic, QOS qos, bool retain,
108108
const ByteBuf& payload) noexcept;
109109

110110
/**
111111
* Sets login credentials for the connection. The must get set before the Connect call
112112
* if it is to be used.
113113
*/
114-
void SetLogin(const char* userName, const char* password) noexcept;
114+
bool SetLogin(const char* userName, const char* password) noexcept;
115115

116116
/**
117117
* Initiates the connection, OnConnectionFailedHandler and/or OnConnAckHandler will
118118
* be invoked in an event-loop thread.
119119
*/
120-
void Connect(const char* clientId, bool cleanSession, uint16_t keepAliveTime) noexcept;
120+
bool Connect(const char* clientId, bool cleanSession, uint16_t keepAliveTime) noexcept;
121121

122122
/**
123123
* Initiates disconnect, OnDisconnectHandler will be invoked in an event-loop thread.
124124
*/
125-
void Disconnect() noexcept;
125+
bool Disconnect() noexcept;
126126

127127
/**
128128
* Subcribes to topicFilter. OnPublishRecievedHandler will be invoked from an event-loop
@@ -185,7 +185,6 @@ namespace Aws
185185
class AWS_CRT_CPP_API MqttClient final
186186
{
187187
friend class MqttConnection;
188-
189188
public:
190189
/**
191190
* Initialize an MqttClient using bootstrap and allocator

samples/mqtt_pub_sub/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,13 @@ int main(int argc, char* argv[])
242242
/*
243243
* Actually perform the connect dance.
244244
*/
245-
connection.Connect("client_id12335456", true, 0);
245+
if (!connection.Connect("client_id12335456", true, 0))
246+
{
247+
fprintf(stderr, "MQTT Connection failed with error %s\n",
248+
ErrorDebugString(connection.LastError()));
249+
exit(-1);
250+
}
251+
246252
std::unique_lock<std::mutex> uniqueLock(mutex);
247253
conditionVariable.wait(uniqueLock, [&]() {return connectionSucceeded || connectionClosed; });
248254

source/mqtt/MqttClient.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ namespace Aws
224224
return m_lastError;
225225
}
226226

227-
void MqttConnection::SetWill(const char* topic, QOS qos, bool retain,
227+
bool MqttConnection::SetWill(const char* topic, QOS qos, bool retain,
228228
const ByteBuf& payload) noexcept
229229
{
230230
ByteBuf topicBuf = aws_byte_buf_from_c_str(topic);
@@ -234,10 +234,13 @@ namespace Aws
234234
if (aws_mqtt_client_connection_set_will(m_underlyingConnection, &topicCur, qos, retain, &payloadCur))
235235
{
236236
m_lastError = aws_last_error();
237+
return false;
237238
}
239+
240+
return true;
238241
}
239242

240-
void MqttConnection::SetLogin(const char* userName, const char* password) noexcept
243+
bool MqttConnection::SetLogin(const char* userName, const char* password) noexcept
241244
{
242245
ByteBuf userNameBuf = aws_byte_buf_from_c_str(userName);
243246
ByteCursor userNameCur = aws_byte_cursor_from_buf(&userNameBuf);
@@ -246,10 +249,13 @@ namespace Aws
246249
if (aws_mqtt_client_connection_set_login(m_underlyingConnection, &userNameCur, &pwdCur))
247250
{
248251
m_lastError = aws_last_error();
252+
return false;
249253
}
254+
255+
return true;
250256
}
251257

252-
void MqttConnection::Connect(const char* clientId, bool cleanSession,
258+
bool MqttConnection::Connect(const char* clientId, bool cleanSession,
253259
uint16_t keepAliveTime) noexcept
254260
{
255261
ByteBuf clientIdBuf = aws_byte_buf_from_c_str(clientId);
@@ -259,19 +265,21 @@ namespace Aws
259265
&clientIdCur, cleanSession, keepAliveTime))
260266
{
261267
m_lastError = aws_last_error();
268+
return false;
262269
}
263-
else
264-
{
265-
m_connectionState = ConnectionState::Connecting;
266-
}
270+
m_connectionState = ConnectionState::Connecting;
271+
return true;
267272
}
268273

269-
void MqttConnection::Disconnect() noexcept
274+
bool MqttConnection::Disconnect() noexcept
270275
{
271276
if (aws_mqtt_client_connection_disconnect(m_underlyingConnection))
272277
{
273278
m_lastError = aws_last_error();
279+
return false;
274280
}
281+
282+
return true;
275283
}
276284

277285
uint16_t MqttConnection::Subscribe(const char* topicFilter, QOS qos,

0 commit comments

Comments
 (0)