Skip to content

Commit 8886459

Browse files
authored
Swift 6 fixes with latest NIO (#164)
* Revert some swift-format changes * Use correct EventLoopGroup in tests * fix p12 test * swift-format
1 parent 2901abd commit 8886459

15 files changed

+124
-82
lines changed

Package.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ let package = Package(
1111
dependencies: [
1212
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.0"),
1313
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
14-
.package(url: "https://github.com/apple/swift-nio.git", from: "2.42.0"),
14+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.80.0"),
1515
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.14.0"),
16-
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.6.0"),
16+
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.20.0"),
1717
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
1818
],
1919
targets: [
2020
.target(
2121
name: "MQTTNIO",
22-
dependencies:
23-
[
22+
dependencies: [
2423
.product(name: "Atomics", package: "swift-atomics"),
2524
.product(name: "Logging", package: "swift-log"),
2625
.product(name: "NIO", package: "swift-nio"),

Sources/MQTTNIO/ChannelHandlers/MQTTTaskHandler.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,21 @@ final class MQTTTaskHandler: ChannelInboundHandler, RemovableChannelHandler {
8787

8888
switch packet.type {
8989
case .PUBREC:
90-
_ = connection.sendMessageNoWait(MQTTPubAckPacket(type: .PUBREL, packetId: packet.packetId, reason: .packetIdentifierNotFound))
90+
_ = connection.sendMessageNoWait(
91+
MQTTPubAckPacket(
92+
type: .PUBREL,
93+
packetId: packet.packetId,
94+
reason: .packetIdentifierNotFound
95+
)
96+
)
9197
case .PUBREL:
92-
_ = connection.sendMessageNoWait(MQTTPubAckPacket(type: .PUBCOMP, packetId: packet.packetId, reason: .packetIdentifierNotFound))
98+
_ = connection.sendMessageNoWait(
99+
MQTTPubAckPacket(
100+
type: .PUBCOMP,
101+
packetId: packet.packetId,
102+
reason: .packetIdentifierNotFound
103+
)
104+
)
93105
default:
94106
break
95107
}

Sources/MQTTNIO/ChannelHandlers/WebSocketHandler.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ final class WebSocketHandler: ChannelDuplexHandler {
7777
}
7878
case .connectionClose:
7979
self.receivedClose(context: context, frame: frame)
80-
8180
default:
8281
break
8382
}

Sources/MQTTNIO/ChannelHandlers/WebSocketInitialRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import NIOHTTP1
1717
// The HTTP handler to be used to initiate the request.
1818
// This initial request will be adapted by the WebSocket upgrader to contain the upgrade header parameters.
1919
// Channel read will only be called if the upgrade fails.
20-
final class WebSocketInitialRequestHandler: ChannelInboundHandler, RemovableChannelHandler {
20+
final class WebSocketInitialRequestHandler: ChannelInboundHandler, RemovableChannelHandler, Sendable {
2121
public typealias InboundIn = HTTPClientResponsePart
2222
public typealias OutboundOut = HTTPClientRequestPart
2323

Sources/MQTTNIO/MQTTClient.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
import Atomics
1515
import Dispatch
1616
import Logging
17+
import NIO
18+
import NIOConcurrencyHelpers
19+
1720
#if canImport(Network)
1821
import Network
22+
import NIOTransportServices
1923
#endif
20-
import NIO
21-
import NIOConcurrencyHelpers
22-
#if canImport(NIOSSL)
24+
#if os(macOS) || os(Linux)
2325
import NIOSSL
2426
#endif
25-
import NIOTransportServices
2627

2728
/// Swift NIO MQTT Client
2829
///
@@ -124,7 +125,6 @@ public final class MQTTClient {
124125
case .createNew:
125126
#if canImport(Network)
126127
switch configuration.tlsConfiguration {
127-
// This should use canImport(NIOSSL), will change when it works with SwiftUI previews.
128128
#if os(macOS) || os(Linux)
129129
case .niossl:
130130
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
@@ -180,11 +180,13 @@ public final class MQTTClient {
180180
/// - Throws: MQTTError.alreadyShutdown: You have already shutdown the client
181181
public func syncShutdownGracefully() throws {
182182
if let eventLoop = MultiThreadedEventLoopGroup.currentEventLoop {
183-
preconditionFailure("""
184-
BUG DETECTED: syncShutdown() must not be called when on an EventLoop.
185-
Calling syncShutdown() on any EventLoop can lead to deadlocks.
186-
Current eventLoop: \(eventLoop)
187-
""")
183+
preconditionFailure(
184+
"""
185+
BUG DETECTED: syncShutdown() must not be called when on an EventLoop.
186+
Calling syncShutdown() on any EventLoop can lead to deadlocks.
187+
Current eventLoop: \(eventLoop)
188+
"""
189+
)
188190
}
189191
let errorStorageLock = NIOLock()
190192
var errorStorage: Error?
@@ -403,12 +405,12 @@ public final class MQTTClient {
403405
/// Disconnect from server
404406
/// - Returns: Future waiting on disconnect message to be sent
405407
public func disconnect() -> EventLoopFuture<Void> {
406-
return self.disconnect(packet: MQTTDisconnectPacket())
408+
self.disconnect(packet: MQTTDisconnectPacket())
407409
}
408410

409411
/// Return if client has an active connection to broker
410412
public func isActive() -> Bool {
411-
return self.connection?.channel.isActive ?? false
413+
self.connection?.channel.isActive ?? false
412414
}
413415

414416
/// Add named publish listener. Called whenever a PUBLISH message is received from the server
@@ -530,7 +532,7 @@ extension MQTTClient {
530532
func resendOnRestart() {
531533
let inflight = self.inflight.packets
532534
self.inflight.clear()
533-
inflight.forEach { packet in
535+
for packet in inflight {
534536
switch packet {
535537
case let publish as MQTTPublishPacket:
536538
let newPacket = MQTTPublishPacket(

Sources/MQTTNIO/MQTTConfiguration.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import NIO
1515
import NIOHTTP1
16-
#if canImport(NIOSSL)
16+
#if os(macOS) || os(Linux)
1717
import NIOSSL
1818
#endif
1919

@@ -31,7 +31,6 @@ extension MQTTClient {
3131
/// by this variable. It is recommended on iOS you use NIO Transport Services.
3232
public enum TLSConfigurationType {
3333
/// NIOSSL TLS configuration
34-
// This should use canImport(NIOSSL), will change when it works with SwiftUI previews.
3534
#if os(macOS) || os(Linux)
3635
case niossl(TLSConfiguration)
3736
#endif

Sources/MQTTNIO/MQTTConnection.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
import NIO
15+
import NIOHTTP1
16+
import NIOWebSocket
17+
1418
#if canImport(FoundationEssentials)
1519
import FoundationEssentials
1620
#else
1721
import Foundation
1822
#endif
1923
#if canImport(Network)
2024
import Network
25+
import NIOTransportServices
2126
#endif
22-
import NIO
23-
import NIOHTTP1
24-
#if canImport(NIOSSL)
27+
#if os(macOS) || os(Linux)
2528
import NIOSSL
2629
#endif
27-
import NIOTransportServices
28-
import NIOWebSocket
2930

3031
final class MQTTConnection {
3132
let channel: Channel
@@ -72,7 +73,7 @@ final class MQTTConnection {
7273
webSocketConfiguration: webSocketConfiguration,
7374
upgradePromise: promise
7475
) {
75-
return channel.pipeline.addHandlers(handlers)
76+
try channel.pipeline.syncOperations.addHandlers(handlers)
7677
}
7778
} else {
7879
return channel.pipeline.addHandlers(handlers)
@@ -113,7 +114,6 @@ final class MQTTConnection {
113114
switch client.configuration.tlsConfiguration {
114115
case .ts(let config):
115116
options = try config.getNWProtocolTLSOptions()
116-
// This should use canImport(NIOSSL), will change when it works with SwiftUI previews.
117117
#if os(macOS) || os(Linux)
118118
case .niossl:
119119
throw MQTTError.wrongTLSConfig
@@ -130,7 +130,7 @@ final class MQTTConnection {
130130
return bootstrap
131131
}
132132
#endif
133-
// This should use canImport(NIOSSL), will change when it works with SwiftUI previews.
133+
134134
#if os(macOS) || os(Linux) // canImport(Network)
135135
if let clientBootstrap = ClientBootstrap(validatingGroup: client.eventLoopGroup) {
136136
let tlsConfiguration: TLSConfiguration
@@ -159,7 +159,7 @@ final class MQTTConnection {
159159
channel: Channel,
160160
webSocketConfiguration: MQTTClient.WebSocketConfiguration,
161161
upgradePromise promise: EventLoopPromise<Void>,
162-
afterHandlerAdded: @escaping () -> EventLoopFuture<Void>
162+
afterHandlerAdded: @escaping () throws -> Void
163163
) -> EventLoopFuture<Void> {
164164
// initial HTTP request handler, before upgrade
165165
let httpHandler = WebSocketInitialRequestHandler(
@@ -174,10 +174,10 @@ final class MQTTConnection {
174174
requestKey: Data(requestKey).base64EncodedString(),
175175
maxFrameSize: client.configuration.webSocketMaxFrameSize
176176
) { channel, _ in
177-
let future = channel.pipeline.addHandler(WebSocketHandler())
178-
.flatMap { _ in
179-
afterHandlerAdded()
180-
}
177+
let future = channel.eventLoop.makeCompletedFuture {
178+
try channel.pipeline.syncOperations.addHandler(WebSocketHandler())
179+
try afterHandlerAdded()
180+
}
181181
future.cascade(to: promise)
182182
return future
183183
}

Sources/MQTTNIO/MQTTCoreTypes.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ public struct MQTTPublishInfo: Sendable {
6262
/// Message payload.
6363
public let payload: ByteBuffer
6464

65-
public init(qos: MQTTQoS, retain: Bool, dup: Bool = false, topicName: String, payload: ByteBuffer, properties: MQTTProperties) {
65+
public init(
66+
qos: MQTTQoS,
67+
retain: Bool,
68+
dup: Bool = false,
69+
topicName: String,
70+
payload: ByteBuffer,
71+
properties: MQTTProperties
72+
) {
6673
self.qos = qos
6774
self.retain = retain
6875
self.dup = dup

Sources/MQTTNIO/MQTTListeners.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class MQTTListeners<ReturnType> {
2121
let listeners = self.lock.withLock {
2222
return self.listeners
2323
}
24-
listeners.values.forEach { listener in
24+
for listener in listeners.values {
2525
listener(result)
2626
}
2727
}

Sources/MQTTNIO/TSTLSConfiguration.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import Foundation
1717
import Network
18-
#if canImport(NIOSSL)
18+
#if os(macOS) || os(Linux)
1919
import NIOSSL
2020
#endif
2121

@@ -98,7 +98,6 @@ public struct TSTLSConfiguration {
9898
/// Create certificate array from already loaded SecCertificate array
9999
public static func certificates(_ secCertificates: [SecCertificate]) -> Self { .init(certificates: secCertificates) }
100100

101-
// This should use canImport(NIOSSL), will change when it works with SwiftUI previews.
102101
#if os(macOS) || os(Linux)
103102
/// Create certificate array from PEM file
104103
public static func pem(_ filename: String) throws -> Self {
@@ -234,8 +233,8 @@ extension TSTLSConfiguration {
234233
sec_protocol_options_set_local_identity(options.securityProtocolOptions, secClientIdentity)
235234
}
236235

237-
self.applicationProtocols.forEach {
238-
sec_protocol_options_add_tls_application_protocol(options.securityProtocolOptions, $0)
236+
for applicationProtocol in self.applicationProtocols {
237+
sec_protocol_options_add_tls_application_protocol(options.securityProtocolOptions, applicationProtocol)
239238
}
240239

241240
if self.certificateVerification != .fullVerification || self.trustRoots != nil {

0 commit comments

Comments
 (0)