2
2
//
3
3
// This source file is part of the swift-nio-redis open source project
4
4
//
5
- // Copyright (c) 2018-2019 ZeeZide GmbH. and the swift-nio-redis project authors
5
+ // Copyright (c) 2018-2020 ZeeZide GmbH. and the swift-nio-redis project authors
6
6
// Licensed under Apache License v2.0
7
7
//
8
8
// See LICENSE.txt for license information
@@ -16,18 +16,18 @@ import NIO
16
16
17
17
open class RESPChannelHandler : ChannelDuplexHandler {
18
18
19
- public typealias InboundErr = RESPParserError
19
+ public typealias InboundErr = RESPParserError
20
20
21
- public typealias InboundIn = ByteBuffer
22
- public typealias InboundOut = RESPValue
21
+ public typealias InboundIn = ByteBuffer
22
+ public typealias InboundOut = RESPValue
23
23
24
- public typealias OutboundIn = RESPEncodable
25
- public typealias OutboundOut = ByteBuffer
24
+ public typealias OutboundIn = RESPEncodable
25
+ public typealias OutboundOut = ByteBuffer
26
26
27
27
private final let nilStringBuffer = ConstantBuffers . nilStringBuffer
28
28
private final let nilArrayBuffer = ConstantBuffers . nilArrayBuffer
29
29
30
- public final var parser = RESPParser ( )
30
+ public final var parser = RESPParser ( )
31
31
32
32
public init ( ) { }
33
33
@@ -133,27 +133,15 @@ open class RESPChannelHandler : ChannelDuplexHandler {
133
133
context. write ( wrapOutboundOut ( out) , promise: promise)
134
134
}
135
135
136
- #if swift(>=5)
137
- @inline ( __always)
138
- final func encode< S: Collection > ( simpleString bytes: S ,
139
- out: inout ByteBuffer )
140
- where S. Element == UInt8
141
- {
142
- out. writeInteger ( UInt8 ( 43 ) ) // +
143
- out. writeBytes ( bytes)
144
- out. writeBytes ( eol)
145
- }
146
- #else
147
- @inline ( __always)
148
- final func encode< S: ContiguousCollection > ( simpleString bytes: S ,
149
- out: inout ByteBuffer )
150
- where S. Element == UInt8
151
- {
152
- out. writeInteger ( UInt8 ( 43 ) ) // +
153
- out. writeBytes ( bytes)
154
- out. writeBytes ( eol)
155
- }
156
- #endif
136
+ @inline ( __always)
137
+ final func encode< S: Collection > ( simpleString bytes: S ,
138
+ out: inout ByteBuffer )
139
+ where S. Element == UInt8
140
+ {
141
+ out. writeInteger ( UInt8 ( 43 ) ) // +
142
+ out. writeBytes ( bytes)
143
+ out. writeBytes ( eol)
144
+ }
157
145
158
146
@inline ( __always)
159
147
final func encode( simpleString bytes: ByteBuffer , out: inout ByteBuffer ) {
@@ -177,41 +165,22 @@ open class RESPChannelHandler : ChannelDuplexHandler {
177
165
}
178
166
}
179
167
180
- #if swift(>=5)
181
- @inline ( __always)
182
- final func encode< S: Collection > ( bulkString bytes: S ? ,
183
- out: inout ByteBuffer )
184
- where S. Element == UInt8
185
- {
186
- if let s = bytes {
187
- out. writeInteger ( UInt8 ( 36 ) ) // $
188
- out. write ( integerAsString : Int ( s. count) )
189
- out. writeBytes ( eol)
190
- out. writeBytes ( s)
191
- out. writeBytes ( eol)
192
- }
193
- else {
194
- out. writeBytes ( nilString)
195
- }
168
+ @inline ( __always)
169
+ final func encode< S: Collection > ( bulkString bytes: S ? ,
170
+ out: inout ByteBuffer )
171
+ where S. Element == UInt8
172
+ {
173
+ if let s = bytes {
174
+ out. writeInteger ( UInt8 ( 36 ) ) // $
175
+ out. write ( integerAsString : Int ( s. count) )
176
+ out. writeBytes ( eol)
177
+ out. writeBytes ( s)
178
+ out. writeBytes ( eol)
196
179
}
197
- #else
198
- @inline ( __always)
199
- final func encode< S: ContiguousCollection > ( bulkString bytes: S ? ,
200
- out: inout ByteBuffer )
201
- where S. Element == UInt8
202
- {
203
- if let s = bytes {
204
- out. writeInteger ( UInt8 ( 36 ) ) // $
205
- out. write ( integerAsString : Int ( s. count) )
206
- out. writeBytes ( eol)
207
- out. writeBytes ( s)
208
- out. writeBytes ( eol)
209
- }
210
- else {
211
- out. writeBytes ( nilString)
212
- }
180
+ else {
181
+ out. writeBytes ( nilString)
213
182
}
214
- #endif
183
+ }
215
184
216
185
@inline ( __always)
217
186
final func encode( integer i: Int , out: inout ByteBuffer ) {
@@ -270,36 +239,6 @@ open class RESPChannelHandler : ChannelDuplexHandler {
270
239
}
271
240
}
272
241
}
273
-
274
-
275
- #if swift(>=5) // NIO 2 API - default
276
- #else // NIO 1 API Shims
277
- open func channelActive( ctx context: ChannelHandlerContext ) {
278
- channelActive ( context: context)
279
- }
280
- open func channelInactive( ctx context: ChannelHandlerContext ) {
281
- channelInactive ( context: context)
282
- }
283
- public func channelRead( ctx context: ChannelHandlerContext , data: NIOAny ) {
284
- channelRead ( context: context, data: data)
285
- }
286
- open func channelRead( ctx context: ChannelHandlerContext , value: RESPValue ) {
287
- channelRead ( context: context, value: value)
288
- }
289
- open func errorCaught( ctx context: ChannelHandlerContext , error: Error ) {
290
- errorCaught ( context: context, error: error)
291
- }
292
- public func write( ctx context: ChannelHandlerContext , data: NIOAny ,
293
- promise: EventLoopPromise < Void > ? )
294
- {
295
- write ( context: context, data: data, promise: promise)
296
- }
297
- public final func write( ctx context: ChannelHandlerContext , value: RESPValue ,
298
- promise: EventLoopPromise < Void > ? )
299
- {
300
- write ( context: context, value: value, promise: promise)
301
- }
302
- #endif // NIO 1 API Shims
303
242
}
304
243
305
244
private let eol : ContiguousArray < UInt8 > = [ 13 , 10 ] // \r\n
@@ -310,49 +249,16 @@ fileprivate enum ConstantBuffers {
310
249
311
250
static let nilStringBuffer : ByteBuffer = {
312
251
let alloc = ByteBufferAllocator ( )
313
- var bb = alloc. buffer ( capacity: 6 )
252
+ var bb = alloc. buffer ( capacity: 6 )
314
253
bb. writeBytes ( nilString)
315
254
return bb
316
255
} ( )
317
256
318
257
static let nilArrayBuffer : ByteBuffer = {
319
258
let alloc = ByteBufferAllocator ( )
320
- var bb = alloc. buffer ( capacity: 6 )
259
+ var bb = alloc. buffer ( capacity: 6 )
321
260
bb. writeBytes ( nilArray)
322
261
return bb
323
262
} ( )
324
263
}
325
264
326
- #if swift(>=5)
327
- // NIO 2
328
- #else
329
- fileprivate extension ByteBuffer {
330
- // NIO 2 API for NIO 1
331
-
332
- @inline ( __always) @discardableResult
333
- mutating func writeString( _ string: String ) -> Int {
334
- return self . write ( string: string) ?? - 1337 // never fails
335
- }
336
-
337
- @inline ( __always) @discardableResult
338
- mutating func writeInteger< T: FixedWidthInteger > ( _ integer: T ) -> Int {
339
- return self . write ( integer: integer)
340
- }
341
-
342
- @inline ( __always) @discardableResult
343
- mutating func writeBuffer( _ buffer: inout ByteBuffer ) -> Int {
344
- return self . write ( buffer: & buffer)
345
- }
346
-
347
- @inline ( __always) @discardableResult
348
- mutating func writeBytes( _ bytes: UnsafeRawBufferPointer ) -> Int {
349
- return self . write ( bytes: bytes)
350
- }
351
- @inline ( __always) @discardableResult
352
- mutating func writeBytes< Bytes: Sequence > ( _ bytes: Bytes ) -> Int
353
- where Bytes. Element == UInt8
354
- {
355
- return self . write ( bytes: bytes)
356
- }
357
- }
358
- #endif // swift(<5)
0 commit comments