1
1
import NIO
2
2
3
+ /// Handler encoding MQTT Messages into ByteBuffers
3
4
final class MQTTEncodeHandler : ChannelOutboundHandler {
4
5
public typealias OutboundIn = MQTTOutboundMessage
5
6
public typealias OutboundOut = ByteBuffer
@@ -19,6 +20,7 @@ final class MQTTEncodeHandler: ChannelOutboundHandler {
19
20
}
20
21
}
21
22
23
+ /// Decode ByteBuffers into MQTT Messages
22
24
struct ByteToMQTTMessageDecoder : ByteToMessageDecoder {
23
25
typealias InboundOut = MQTTInboundMessage
24
26
@@ -90,79 +92,3 @@ struct ByteToMQTTMessageDecoder: ByteToMessageDecoder {
90
92
}
91
93
}
92
94
93
- /// Channel handler for sending PINGREQ messages to keep connect alive
94
- final class PingreqHandler : ChannelDuplexHandler {
95
- typealias OutboundIn = MQTTOutboundMessage
96
- typealias OutboundOut = MQTTOutboundMessage
97
- typealias InboundIn = MQTTInboundMessage
98
- typealias InboundOut = MQTTInboundMessage
99
-
100
- let client : MQTTClient
101
- let timeout : TimeAmount
102
- var lastEventTime : NIODeadline
103
- var task : Scheduled < Void > ?
104
-
105
- init ( client: MQTTClient , timeout: TimeAmount ) {
106
- self . client = client
107
- self . timeout = timeout
108
- self . lastEventTime = . now( )
109
- self . task = nil
110
- }
111
-
112
- public func handlerAdded( context: ChannelHandlerContext ) {
113
- if context. channel. isActive {
114
- scheduleTask ( context)
115
- }
116
- }
117
-
118
- public func handlerRemoved( context: ChannelHandlerContext ) {
119
- cancelTask ( )
120
- }
121
-
122
- public func channelActive( context: ChannelHandlerContext ) {
123
- if self . task == nil {
124
- scheduleTask ( context)
125
- }
126
- context. fireChannelActive ( )
127
- }
128
-
129
- func channelRead( context: ChannelHandlerContext , data: NIOAny ) {
130
- self . lastEventTime = . now( )
131
- context. fireChannelRead ( data)
132
- }
133
-
134
- func write( context: ChannelHandlerContext , data: NIOAny , promise: EventLoopPromise < Void > ? ) {
135
- self . lastEventTime = . now( )
136
- context. write ( data, promise: promise)
137
- }
138
-
139
- func scheduleTask( _ context: ChannelHandlerContext ) {
140
- guard context. channel. isActive else { return }
141
-
142
- self . task = context. eventLoop. scheduleTask ( deadline: lastEventTime + timeout) {
143
- // if lastEventTime plus the timeout is less than now send PINGREQ
144
- // otherwise reschedule task
145
- if self . lastEventTime + self . timeout <= . now( ) {
146
- guard context. channel. isActive else { return }
147
-
148
- self . client. pingreq ( ) . whenComplete { result in
149
- switch result {
150
- case . failure( let error) :
151
- context. fireErrorCaught ( error)
152
- case . success:
153
- break
154
- }
155
- self . lastEventTime = . now( )
156
- self . scheduleTask ( context)
157
- }
158
- } else {
159
- self . scheduleTask ( context)
160
- }
161
- }
162
- }
163
-
164
- func cancelTask( ) {
165
- self . task? . cancel ( )
166
- self . task = nil
167
- }
168
- }
0 commit comments