Skip to content

Commit c30203d

Browse files
committed
formatting
1 parent dde09d8 commit c30203d

File tree

9 files changed

+91
-75
lines changed

9 files changed

+91
-75
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let package = Package(
66
name: "mqtt-nio",
77
platforms: [.macOS(.v10_14), .iOS(.v12), .tvOS(.v12), .watchOS(.v6)],
88
products: [
9-
.library(name: "MQTTNIO", targets: ["MQTTNIO"])
9+
.library(name: "MQTTNIO", targets: ["MQTTNIO"]),
1010
],
1111
dependencies: [
1212
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.0"),

Sources/MQTTNIO/ChannelHandlers/MQTTTaskHandler.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,16 @@ final class MQTTTaskHandler: ChannelInboundHandler, RemovableChannelHandler {
8989
case .PUBREC:
9090
_ = connection.sendMessageNoWait(
9191
MQTTPubAckPacket(
92-
type: .PUBREL,
93-
packetId: packet.packetId,
92+
type: .PUBREL,
93+
packetId: packet.packetId,
9494
reason: .packetIdentifierNotFound
9595
)
9696
)
9797
case .PUBREL:
9898
_ = connection.sendMessageNoWait(
9999
MQTTPubAckPacket(
100-
type: .PUBCOMP,
101-
packetId: packet.packetId,
100+
type: .PUBCOMP,
101+
packetId: packet.packetId,
102102
reason: .packetIdentifierNotFound
103103
)
104104
)

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/MQTTClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ extension MQTTClient {
530530
func resendOnRestart() {
531531
let inflight = self.inflight.packets
532532
self.inflight.clear()
533-
inflight.forEach { packet in
533+
for packet in inflight {
534534
switch packet {
535535
case let publish as MQTTPublishPacket:
536536
let newPacket = MQTTPublishPacket(

Sources/MQTTNIO/MQTTConnection.swift

Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import NIOTransportServices
1717
import 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

3131
final 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 {

Sources/MQTTNIO/MQTTCoreTypes.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public struct MQTTPublishInfo: Sendable {
6363
public let payload: ByteBuffer
6464

6565
public init(
66-
qos: MQTTQoS,
67-
retain: Bool,
68-
dup: Bool = false,
69-
topicName: String,
66+
qos: MQTTQoS,
67+
retain: Bool,
68+
dup: Bool = false,
69+
topicName: String,
7070
payload: ByteBuffer,
7171
properties: MQTTProperties
7272
) {

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ extension TSTLSConfiguration {
234234
sec_protocol_options_set_local_identity(options.securityProtocolOptions, secClientIdentity)
235235
}
236236

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

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

Tests/MQTTNIOTests/MQTTNIOTests+async.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import NIO
1717
import NIOFoundationCompat
1818
import NIOHTTP1
1919
import XCTest
20+
21+
@testable import MQTTNIO
22+
2023
#if canImport(NIOSSL)
2124
import NIOSSL
2225
#endif
23-
@testable import MQTTNIO
2426

2527
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
2628
final class AsyncMQTTNIOTests: XCTestCase {
@@ -36,7 +38,7 @@ final class AsyncMQTTNIOTests: XCTestCase {
3638
host: Self.hostname,
3739
port: 1883,
3840
identifier: identifier,
39-
eventLoopGroupProvider: .createNew,
41+
eventLoopGroupProvider: .shared(MultiThreadedEventLoopGroup.singleton),
4042
logger: Self.logger,
4143
configuration: .init(version: version, timeout: timeout)
4244
)
@@ -91,7 +93,7 @@ final class AsyncMQTTNIOTests: XCTestCase {
9193
host: Self.hostname,
9294
port: 1883,
9395
identifier: "TestPing",
94-
eventLoopGroupProvider: .createNew,
96+
eventLoopGroupProvider: .shared(MultiThreadedEventLoopGroup.singleton),
9597
logger: Self.logger,
9698
configuration: .init(disablePing: true)
9799
)
@@ -184,7 +186,8 @@ final class AsyncMQTTNIOTests: XCTestCase {
184186
}
185187

186188
func testMQTTPublishRetain() async throws {
187-
let payloadString = #"{"from":1000000,"to":1234567,"type":1,"content":"I am a beginner in swift and I am studying hard!!测试\n\n test, message","timestamp":1607243024,"nonce":"pAx2EsUuXrVuiIU3GGOGHNbUjzRRdT5b","sign":"ff902e31a6a5f5343d70a3a93ac9f946adf1caccab539c6f3a6"}"#
189+
let payloadString =
190+
#"{"from":1000000,"to":1234567,"type":1,"content":"I am a beginner in swift and I am studying hard!!测试\n\n test, message","timestamp":1607243024,"nonce":"pAx2EsUuXrVuiIU3GGOGHNbUjzRRdT5b","sign":"ff902e31a6a5f5343d70a3a93ac9f946adf1caccab539c6f3a6"}"#
188191
let payload = ByteBufferAllocator().buffer(string: payloadString)
189192

190193
let client = self.createClient(identifier: "testMQTTPublishRetain_publisher")

0 commit comments

Comments
 (0)