|
| 1 | +#if compiler(>=5.5) |
| 2 | + |
| 3 | +import _NIOConcurrency |
| 4 | +import NIO |
| 5 | + |
| 6 | +@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) |
| 7 | +extension MQTTClient.V5 { |
| 8 | + /// Connect to MQTT server |
| 9 | + /// |
| 10 | + /// If `cleanSession` is set to false the Server MUST resume communications with the Client based on state from the current Session (as identified by the Client identifier). |
| 11 | + /// If there is no Session associated with the Client identifier the Server MUST create a new Session. The Client and Server MUST store the Session |
| 12 | + /// after the Client and Server are disconnected. If set to true then the Client and Server MUST discard any previous Session and start a new one |
| 13 | + /// |
| 14 | + /// The function returns an EventLoopFuture which will be updated with whether the server has restored a session for this client. |
| 15 | + /// |
| 16 | + /// - Parameters: |
| 17 | + /// - cleanSession: should we start with a new session |
| 18 | + /// - properties: properties to attach to connect message |
| 19 | + /// - will: Publish message to be posted as soon as connection is made |
| 20 | + /// - Returns: EventLoopFuture to be updated with connack |
| 21 | + public func connect( |
| 22 | + cleanStart: Bool = true, |
| 23 | + properties: MQTTProperties = .init(), |
| 24 | + will: (topicName: String, payload: ByteBuffer, qos: MQTTQoS, retain: Bool, properties: MQTTProperties)? = nil, |
| 25 | + authWorkflow: ((MQTTAuthV5, EventLoop) -> EventLoopFuture<MQTTAuthV5>)? = nil |
| 26 | + ) async throws -> MQTTConnackV5 { |
| 27 | + return try await connect(cleanStart: cleanStart, properties: properties, will: will, authWorkflow: authWorkflow).get() |
| 28 | + } |
| 29 | + |
| 30 | + /// Publish message to topic |
| 31 | + /// - Parameters: |
| 32 | + /// - topicName: Topic name on which the message is published |
| 33 | + /// - payload: Message payload |
| 34 | + /// - qos: Quality of Service for message. |
| 35 | + /// - retain: Whether this is a retained message. |
| 36 | + /// - properties: properties to attach to publish message |
| 37 | + /// - Returns: Future waiting for publish to complete. Depending on QoS setting the future will complete |
| 38 | + /// when message is sent, when PUBACK is received or when PUBREC and following PUBCOMP are |
| 39 | + /// received. QoS1 and above return an `MQTTAckV5` which contains a `reason` and `properties` |
| 40 | + public func publish( |
| 41 | + to topicName: String, |
| 42 | + payload: ByteBuffer, |
| 43 | + qos: MQTTQoS, |
| 44 | + retain: Bool = false, |
| 45 | + properties: MQTTProperties = .init() |
| 46 | + ) async throws -> MQTTAckV5? { |
| 47 | + return try await publish(to: topicName, payload: payload, qos: qos, retain: retain, properties: properties).get() |
| 48 | + } |
| 49 | + |
| 50 | + /// Subscribe to topic |
| 51 | + /// - Parameters: |
| 52 | + /// - subscriptions: Subscription infos |
| 53 | + /// - properties: properties to attach to subscribe message |
| 54 | + /// - Returns: Future waiting for subscribe to complete. Will wait for SUBACK message from server and |
| 55 | + /// return its contents |
| 56 | + public func subscribe( |
| 57 | + to subscriptions: [MQTTSubscribeInfoV5], |
| 58 | + properties: MQTTProperties = .init() |
| 59 | + ) async throws -> MQTTSubackV5 { |
| 60 | + return try await subscribe(to: subscriptions, properties: properties).get() |
| 61 | + } |
| 62 | + |
| 63 | + /// Unsubscribe from topic |
| 64 | + /// - Parameters: |
| 65 | + /// - subscriptions: List of subscriptions to unsubscribe from |
| 66 | + /// - properties: properties to attach to unsubscribe message |
| 67 | + /// - Returns: Future waiting for unsubscribe to complete. Will wait for UNSUBACK message from server and |
| 68 | + /// return its contents |
| 69 | + public func unsubscribe( |
| 70 | + from subscriptions: [String], |
| 71 | + properties: MQTTProperties = .init() |
| 72 | + ) async throws -> MQTTSubackV5 { |
| 73 | + return try await unsubscribe(from: subscriptions, properties: properties).get() |
| 74 | + } |
| 75 | + |
| 76 | + /// Disconnect from server |
| 77 | + /// - Parameter properties: properties to attach to disconnect packet |
| 78 | + /// - Returns: Future waiting on disconnect message to be sent |
| 79 | + public func disconnect(properties: MQTTProperties = .init()) async throws { |
| 80 | + return try await disconnect(properties: properties).get() |
| 81 | + } |
| 82 | + |
| 83 | + /// Re-authenticate with server |
| 84 | + /// |
| 85 | + /// - Parameters: |
| 86 | + /// - properties: properties to attach to auth packet. Must include `authenticationMethod` |
| 87 | + /// - authWorkflow: Respond to auth packets from server |
| 88 | + /// - Returns: final auth packet returned from server |
| 89 | + public func auth( |
| 90 | + properties: MQTTProperties, |
| 91 | + authWorkflow: ((MQTTAuthV5, EventLoop) -> EventLoopFuture<MQTTAuthV5>)? = nil |
| 92 | + ) async throws -> MQTTAuthV5 { |
| 93 | + return try await auth(properties: properties, authWorkflow: authWorkflow).get() |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#endif // compiler(>=5.5) |
0 commit comments