in source/iot/MqttClient.cpp [300:381]
MqttClientConnectionConfig MqttClientConnectionConfigBuilder::Build() noexcept
{
if (m_lastError != 0)
{
return MqttClientConnectionConfig::CreateInvalid(m_lastError);
}
uint16_t port = m_portOverride;
if (!m_portOverride)
{
if (m_websocketConfig || Crt::Io::TlsContextOptions::IsAlpnSupported())
{
port = 443;
}
else
{
port = 8883;
}
}
if (port == 443 && !m_websocketConfig && Crt::Io::TlsContextOptions::IsAlpnSupported())
{
if (!m_contextOptions.SetAlpnList("x-amzn-mqtt-ca"))
{
return MqttClientConnectionConfig::CreateInvalid(m_contextOptions.LastError());
}
}
// add metrics string to username (if metrics enabled)
// note: username isn't currently exposed to users via builder, so it's always empty
Crt::String username;
if (m_enableMetricsCollection)
{
username += "?SDK=";
username += m_sdkName;
username += "&Version=";
username += m_sdkVersion;
}
auto tlsContext = Crt::Io::TlsContext(m_contextOptions, Crt::Io::TlsMode::CLIENT, m_allocator);
if (!tlsContext)
{
return MqttClientConnectionConfig::CreateInvalid(tlsContext.GetInitializationError());
}
if (!m_websocketConfig)
{
auto config = MqttClientConnectionConfig(
m_endpoint, port, m_socketOptions, std::move(tlsContext), m_proxyOptions);
config.m_username = username;
return config;
}
auto websocketConfig = m_websocketConfig.value();
auto signerTransform = [websocketConfig](
std::shared_ptr<Crt::Http::HttpRequest> req,
const Crt::Mqtt::OnWebSocketHandshakeInterceptComplete &onComplete) {
// it is only a very happy coincidence that these function signatures match. This is the callback
// for signing to be complete. It invokes the callback for websocket handshake to be complete.
auto signingComplete =
[onComplete](const std::shared_ptr<Aws::Crt::Http::HttpRequest> &req1, int errorCode) {
onComplete(req1, errorCode);
};
auto signerConfig = websocketConfig.CreateSigningConfigCb();
websocketConfig.Signer->SignRequest(req, *signerConfig, signingComplete);
};
bool useWebsocketProxyOptions = m_websocketConfig->ProxyOptions.has_value() && !m_proxyOptions.has_value();
auto config = MqttClientConnectionConfig(
m_endpoint,
port,
m_socketOptions,
std::move(tlsContext),
signerTransform,
useWebsocketProxyOptions ? m_websocketConfig->ProxyOptions : m_proxyOptions);
config.m_username = username;
return config;
}