Skip to content

Commit ae282b9

Browse files
Added non-tls connection support.
1 parent a17932a commit ae282b9

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

include/aws/crt/mqtt/MqttClient.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ namespace Aws
157157
MqttConnection(MqttClient* client, const char* hostName, uint16_t port,
158158
const Io::SocketOptions& socketOptions,
159159
Io::TlsConnectionOptions&& tlsConnOptions) noexcept;
160+
MqttConnection(MqttClient* client, const char* hostName, uint16_t port,
161+
const Io::SocketOptions& socketOptions) noexcept;
160162

161163
MqttClient *m_owningClient;
162164
aws_mqtt_client_connection *m_underlyingConnection;
@@ -178,6 +180,8 @@ namespace Aws
178180
const aws_byte_cursor* payload,
179181
void* user_data);
180182
static void s_onOpComplete(aws_mqtt_client_connection* connection, uint16_t packetId, void* userdata);
183+
static void s_connectionInit(MqttConnection* self, const char* hostName, uint16_t port,
184+
const Io::SocketOptions& socketOptions, Io::TlsConnectionOptions* tlsConnOptions);
181185
};
182186

183187
/**
@@ -202,11 +206,17 @@ namespace Aws
202206
int LastError() const noexcept;
203207

204208
/**
205-
* Create a new connection object from the client. The client must outlive
209+
* Create a new connection object using TLS from the client. The client must outlive
206210
* all of its connection instances.
207211
*/
208212
MqttConnection NewConnection(const char* hostName, uint16_t port,
209213
const Io::SocketOptions& socketOptions, Io::TlsConnectionOptions&& tlsConnOptions) noexcept;
214+
/**
215+
* Create a new connection object over plain text from the client. The client must outlive
216+
* all of its connection instances.
217+
*/
218+
MqttConnection NewConnection(const char* hostName, uint16_t port,
219+
const Io::SocketOptions& socketOptions) noexcept;
210220

211221
private:
212222
aws_mqtt_client m_client;

source/mqtt/MqttClient.cpp

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,34 +135,50 @@ namespace Aws
135135
aws_mem_release(callbackData->allocator, reinterpret_cast<void *>(callbackData));
136136
}
137137

138-
MqttConnection::MqttConnection(MqttClient* client,
139-
const char* hostName, uint16_t port,
140-
const Io::SocketOptions& socketOptions,
141-
Io::TlsConnectionOptions&& tlsConnOptions) noexcept :
142-
m_owningClient(client),
143-
m_lastError(AWS_ERROR_SUCCESS),
144-
m_connectionState(ConnectionState::Init)
138+
void MqttConnection::s_connectionInit(MqttConnection* self,
139+
const char* hostName, uint16_t port, const Io::SocketOptions& socketOptions,
140+
Io::TlsConnectionOptions* tlsConnOptions)
145141
{
146142
aws_mqtt_client_connection_callbacks callbacks;
147143
AWS_ZERO_STRUCT(callbacks);
148-
callbacks.user_data = this;
144+
callbacks.user_data = self;
149145
callbacks.on_connack = s_onConnAck;
150146
callbacks.on_connection_failed = s_onConnectionFailed;
151147
callbacks.on_disconnect = s_onDisconnect;
152148

153149
ByteBuf hostNameBuf = aws_byte_buf_from_c_str(hostName);
154150
ByteCursor hostNameCur = aws_byte_cursor_from_buf(&hostNameBuf);
155151

156-
m_underlyingConnection =
157-
aws_mqtt_client_connection_new(&m_owningClient->m_client, callbacks,
158-
&hostNameCur, port,
159-
(Io::SocketOptions*)&socketOptions, &tlsConnOptions);
152+
self->m_underlyingConnection = aws_mqtt_client_connection_new(&self->m_owningClient->m_client,
153+
callbacks, &hostNameCur, port,
154+
const_cast<Io::SocketOptions*>(&socketOptions), tlsConnOptions);
160155

161-
if (!m_underlyingConnection)
156+
if (!self->m_underlyingConnection)
162157
{
163-
m_connectionState = ConnectionState::Error;
164-
m_lastError = aws_last_error();
165-
}
158+
self->m_connectionState = ConnectionState::Error;
159+
self->m_lastError = aws_last_error();
160+
}
161+
}
162+
163+
MqttConnection::MqttConnection(MqttClient* client,
164+
const char* hostName, uint16_t port,
165+
const Io::SocketOptions& socketOptions,
166+
Io::TlsConnectionOptions&& tlsConnOptions) noexcept :
167+
m_owningClient(client),
168+
m_lastError(AWS_ERROR_SUCCESS),
169+
m_connectionState(ConnectionState::Init)
170+
{
171+
s_connectionInit(this, hostName, port, socketOptions, &tlsConnOptions);
172+
}
173+
174+
MqttConnection::MqttConnection(MqttClient* client,
175+
const char* hostName, uint16_t port,
176+
const Io::SocketOptions& socketOptions) noexcept :
177+
m_owningClient(client),
178+
m_lastError(AWS_ERROR_SUCCESS),
179+
m_connectionState(ConnectionState::Init)
180+
{
181+
s_connectionInit(this, hostName, port, socketOptions, nullptr);
166182
}
167183

168184
MqttConnection::~MqttConnection()
@@ -495,6 +511,13 @@ namespace Aws
495511
{
496512
return MqttConnection(this, hostName, port, socketOptions, std::move(tlsConnOptions));
497513
}
514+
515+
MqttConnection MqttClient::NewConnection(const char* hostName, uint16_t port,
516+
const Io::SocketOptions& socketOptions) noexcept
517+
518+
{
519+
return MqttConnection(this, hostName, port, socketOptions);
520+
}
498521
}
499522
}
500523
}

0 commit comments

Comments
 (0)