@@ -17,15 +17,15 @@ import NIOTransportServices
1717import NIOWebSocket
1818
1919#if canImport(FoundationEssentials)
20- import FoundationEssentials
20+ import FoundationEssentials
2121#else
22- import Foundation
22+ import Foundation
2323#endif
2424#if canImport(Network)
25- import Network
25+ import Network
2626#endif
2727#if canImport(NIOSSL)
28- import NIOSSL
28+ import NIOSSL
2929#endif
3030
3131final class MQTTConnection {
@@ -39,21 +39,25 @@ final class MQTTConnection {
3939 self . taskHandler = taskHandler
4040 }
4141
42- static func create( client: MQTTClient , pingInterval: TimeAmount ) -> EventLoopFuture <
43- MQTTConnection
44- > {
42+ static func create(
43+ client: MQTTClient ,
44+ pingInterval: TimeAmount
45+ ) -> EventLoopFuture < MQTTConnection > {
4546 let taskHandler = MQTTTaskHandler ( client: client)
4647 return self . createBootstrap (
47- client: client, pingInterval: pingInterval, taskHandler: taskHandler
48+ client: client,
49+ pingInterval: pingInterval,
50+ taskHandler: taskHandler
4851 )
4952 . map {
50- MQTTConnection (
51- channel: $0, timeout: client. configuration. timeout, taskHandler: taskHandler)
53+ MQTTConnection ( channel: $0, timeout: client. configuration. timeout, taskHandler: taskHandler)
5254 }
5355 }
5456
5557 static func createBootstrap(
56- client: MQTTClient , pingInterval: TimeAmount , taskHandler: MQTTTaskHandler
58+ client: MQTTClient ,
59+ pingInterval: TimeAmount ,
60+ taskHandler: MQTTTaskHandler
5761 ) -> EventLoopFuture < Channel > {
5862 let eventLoop = client. eventLoopGroup. next ( )
5963 let channelPromise = eventLoop. makePromise ( of: Channel . self)
@@ -114,54 +118,58 @@ final class MQTTConnection {
114118 var bootstrap : NIOClientTCPBootstrap
115119 let serverName = client. configuration. sniServerName ?? client. host
116120 #if canImport(Network)
117- // if eventLoop is compatible with NIOTransportServices create a NIOTSConnectionBootstrap
118- if #available( OSX 10 . 14 , iOS 12 . 0 , tvOS 12 . 0 , watchOS 6 . 0 , * ) ,
119- let tsBootstrap = NIOTSConnectionBootstrap ( validatingGroup: client. eventLoopGroup)
120- {
121- // create NIOClientTCPBootstrap with NIOTS TLS provider
122- let options : NWProtocolTLS . Options
123- switch client. configuration. tlsConfiguration {
124- case . ts( let config) :
125- options = try config. getNWProtocolTLSOptions ( )
126- // This should use canImport(NIOSSL), will change when it works with SwiftUI previews.
127- #if os(macOS) || os(Linux)
128- case . niossl:
129- throw MQTTError . wrongTLSConfig
130- #endif
131- default :
132- options = NWProtocolTLS . Options ( )
133- }
134- sec_protocol_options_set_tls_server_name (
135- options. securityProtocolOptions, serverName )
136- let tlsProvider = NIOTSClientTLSProvider ( tlsOptions : options )
137- bootstrap = NIOClientTCPBootstrap ( tsBootstrap , tls : tlsProvider )
138- if client . configuration . useSSL {
139- return bootstrap . enableTLS ( )
140- }
141- return bootstrap
121+ // if eventLoop is compatible with NIOTransportServices create a NIOTSConnectionBootstrap
122+ if #available( OSX 10 . 14 , iOS 12 . 0 , tvOS 12 . 0 , watchOS 6 . 0 , * ) ,
123+ let tsBootstrap = NIOTSConnectionBootstrap ( validatingGroup: client. eventLoopGroup)
124+ {
125+ // create NIOClientTCPBootstrap with NIOTS TLS provider
126+ let options : NWProtocolTLS . Options
127+ switch client. configuration. tlsConfiguration {
128+ case . ts( let config) :
129+ options = try config. getNWProtocolTLSOptions ( )
130+ // This should use canImport(NIOSSL), will change when it works with SwiftUI previews.
131+ #if os(macOS) || os(Linux)
132+ case . niossl:
133+ throw MQTTError . wrongTLSConfig
134+ #endif
135+ default :
136+ options = NWProtocolTLS . Options ( )
137+ }
138+ sec_protocol_options_set_tls_server_name (
139+ options. securityProtocolOptions,
140+ serverName
141+ )
142+ let tlsProvider = NIOTSClientTLSProvider ( tlsOptions : options )
143+ bootstrap = NIOClientTCPBootstrap ( tsBootstrap , tls : tlsProvider )
144+ if client . configuration . useSSL {
145+ return bootstrap. enableTLS ( )
142146 }
147+ return bootstrap
148+ }
143149 #endif
144150 // This should use canImport(NIOSSL), will change when it works with SwiftUI previews.
145151 #if os(macOS) || os(Linux) // canImport(Network)
146- if let clientBootstrap = ClientBootstrap ( validatingGroup: client. eventLoopGroup) {
147- let tlsConfiguration : TLSConfiguration
148- switch client. configuration. tlsConfiguration {
149- case . niossl( let config) :
150- tlsConfiguration = config
151- default :
152- tlsConfiguration = TLSConfiguration . makeClientConfiguration ( )
153- }
154- if client. configuration. useSSL {
155- let sslContext = try NIOSSLContext ( configuration: tlsConfiguration)
156- let tlsProvider = try NIOSSLClientTLSProvider < ClientBootstrap > (
157- context: sslContext, serverHostname : serverName )
158- bootstrap = NIOClientTCPBootstrap ( clientBootstrap , tls : tlsProvider )
159- return bootstrap . enableTLS ( )
160- } else {
161- bootstrap = NIOClientTCPBootstrap ( clientBootstrap , tls : NIOInsecureNoTLS ( ) )
162- }
163- return bootstrap
152+ if let clientBootstrap = ClientBootstrap ( validatingGroup: client. eventLoopGroup) {
153+ let tlsConfiguration : TLSConfiguration
154+ switch client. configuration. tlsConfiguration {
155+ case . niossl( let config) :
156+ tlsConfiguration = config
157+ default :
158+ tlsConfiguration = TLSConfiguration . makeClientConfiguration ( )
159+ }
160+ if client. configuration. useSSL {
161+ let sslContext = try NIOSSLContext ( configuration: tlsConfiguration)
162+ let tlsProvider = try NIOSSLClientTLSProvider < ClientBootstrap > (
163+ context: sslContext,
164+ serverHostname : serverName
165+ )
166+ bootstrap = NIOClientTCPBootstrap ( clientBootstrap , tls : tlsProvider )
167+ return bootstrap. enableTLS ( )
168+ } else {
169+ bootstrap = NIOClientTCPBootstrap ( clientBootstrap , tls : NIOInsecureNoTLS ( ) )
164170 }
171+ return bootstrap
172+ }
165173 #endif
166174 preconditionFailure ( " Cannot create bootstrap for the supplied EventLoop " )
167175 }
@@ -208,7 +216,7 @@ final class MQTTConnection {
208216 }
209217
210218 func sendMessageNoWait( _ message: MQTTPacket ) -> EventLoopFuture < Void > {
211- return self . channel. writeAndFlush ( message)
219+ self . channel. writeAndFlush ( message)
212220 }
213221
214222 func close( ) -> EventLoopFuture < Void > {
@@ -219,11 +227,17 @@ final class MQTTConnection {
219227 }
220228 }
221229
222- func sendMessage( _ message: MQTTPacket , checkInbound: @escaping ( MQTTPacket ) throws -> Bool )
230+ func sendMessage(
231+ _ message: MQTTPacket ,
232+ checkInbound: @escaping ( MQTTPacket ) throws -> Bool
233+ )
223234 -> EventLoopFuture < MQTTPacket >
224235 {
225236 let task = MQTTTask (
226- on: channel. eventLoop, timeout: self . timeout, checkInbound: checkInbound)
237+ on: channel. eventLoop,
238+ timeout: self . timeout,
239+ checkInbound: checkInbound
240+ )
227241
228242 self . taskHandler. addTask ( task)
229243 . flatMap {
0 commit comments