@@ -162,90 +162,15 @@ public struct WebSocketClient: Sendable, LCLWebSocketListenable {
162
162
return self . eventloopGroup. any ( ) . makeFailedFuture ( LCLWebSocketError . invalidURL)
163
163
}
164
164
165
- @Sendable
166
- func makeChannelInitializer( _ channel: Channel ) -> EventLoopFuture < Void > {
167
- if self . eventloopGroup is MultiThreadedEventLoopGroup {
168
- if configuration. socketReuseAddress,
169
- let syncOptions = channel. syncOptions
170
- {
171
- do {
172
- try syncOptions. setOption ( . socketOption( . so_reuseaddr) , value: 1 )
173
- } catch {
174
- return channel. eventLoop. makeFailedFuture ( error)
175
- }
176
- }
177
-
178
- if configuration. socketTcpNoDelay,
179
- let syncOptions = channel. syncOptions
180
- {
181
- do {
182
- try syncOptions. setOption ( . socketOption( . tcp_nodelay) , value: 1 )
183
- } catch {
184
- return channel. eventLoop. makeFailedFuture ( error)
185
- }
186
- }
187
- }
188
-
189
- if let socketSendBufferSize = configuration. socketSendBufferSize,
190
- let syncOptions = channel. syncOptions
191
- {
192
- do {
193
- try syncOptions. setOption ( . socketOption( . so_sndbuf) , value: socketSendBufferSize)
194
- } catch {
195
- return channel. eventLoop. makeFailedFuture ( error)
196
- }
197
- }
198
-
199
- if let socketReceiveBuffer = configuration. socketReceiveBufferSize,
200
- let syncOptions = channel. syncOptions
201
- {
202
- do {
203
- try syncOptions. setOption ( . socketOption( . so_rcvbuf) , value: socketReceiveBuffer)
204
- } catch {
205
- return channel. eventLoop. makeFailedFuture ( error)
206
- }
207
- }
208
-
209
- // bind to selected device, if any
210
- if let deviceName = configuration. deviceName,
211
- let device = findDevice ( with: deviceName, protocol: resolvedAddress. protocol)
212
- {
213
- do {
214
- try bindTo ( device: device, on: channel)
215
- } catch {
216
- return channel. eventLoop. makeFailedFuture ( error)
217
- }
218
- }
219
-
220
- // enable TLS
221
- if scheme. enableTLS {
222
- let tlsConfig = configuration. tlsConfiguration ?? scheme. defaultTLSConfig!
223
- guard let sslContext = try ? NIOSSLContext ( configuration: tlsConfig) else {
224
- return channel. eventLoop. makeFailedFuture ( LCLWebSocketError . tlsInitializationFailed)
225
- }
226
-
227
- do {
228
- let sslClientHandler = try NIOSSLClientHandler ( context: sslContext, serverHostname: host)
229
- try channel. pipeline. syncOperations. addHandlers ( sslClientHandler)
230
- } catch let error as NIOSSLExtraError where error == . invalidSNIHostname {
231
- do {
232
- let sslClientHandler = try NIOSSLClientHandler ( context: sslContext, serverHostname: nil )
233
- try channel. pipeline. syncOperations. addHandlers ( sslClientHandler)
234
- } catch {
235
- return channel. eventLoop. makeFailedFuture ( error)
236
- }
237
- } catch {
238
- return channel. eventLoop. makeFailedFuture ( error)
239
- }
240
- }
241
-
242
- return channel. eventLoop. makeSucceededVoidFuture ( )
243
- }
244
-
245
165
return self . makeBootstrapAndConnect (
246
166
with: configuration,
247
167
resolvedAddress: resolvedAddress,
248
- channelInitializer: makeChannelInitializer ( _: )
168
+ channelInitializer: makeChannelInitializer (
169
+ configuration: configuration,
170
+ resolvedAddress: resolvedAddress,
171
+ scheme: scheme,
172
+ host: host
173
+ )
249
174
) . flatMap { channel in
250
175
let upgrader = NIOWebSocketClientUpgrader (
251
176
maxFrameSize: configuration. maxFrameSize,
@@ -391,6 +316,95 @@ extension WebSocketClient {
391
316
return makeClientBootstrap ( )
392
317
#endif
393
318
}
319
+
320
+ private func makeChannelInitializer(
321
+ configuration: LCLWebSocket . Configuration ,
322
+ resolvedAddress: SocketAddress ,
323
+ scheme: WebSocketScheme ,
324
+ host: String
325
+ ) -> ChannelInitializerCallback {
326
+ @Sendable
327
+ func makeChannelInitializer( _ channel: Channel ) -> EventLoopFuture < Void > {
328
+ if self . eventloopGroup is MultiThreadedEventLoopGroup {
329
+ if configuration. socketReuseAddress,
330
+ let syncOptions = channel. syncOptions
331
+ {
332
+ do {
333
+ try syncOptions. setOption ( . socketOption( . so_reuseaddr) , value: 1 )
334
+ } catch {
335
+ return channel. eventLoop. makeFailedFuture ( error)
336
+ }
337
+ }
338
+
339
+ if configuration. socketTcpNoDelay,
340
+ let syncOptions = channel. syncOptions
341
+ {
342
+ do {
343
+ try syncOptions. setOption ( . socketOption( . tcp_nodelay) , value: 1 )
344
+ } catch {
345
+ return channel. eventLoop. makeFailedFuture ( error)
346
+ }
347
+ }
348
+ }
349
+
350
+ if let socketSendBufferSize = configuration. socketSendBufferSize,
351
+ let syncOptions = channel. syncOptions
352
+ {
353
+ do {
354
+ try syncOptions. setOption ( . socketOption( . so_sndbuf) , value: socketSendBufferSize)
355
+ } catch {
356
+ return channel. eventLoop. makeFailedFuture ( error)
357
+ }
358
+ }
359
+
360
+ if let socketReceiveBuffer = configuration. socketReceiveBufferSize,
361
+ let syncOptions = channel. syncOptions
362
+ {
363
+ do {
364
+ try syncOptions. setOption ( . socketOption( . so_rcvbuf) , value: socketReceiveBuffer)
365
+ } catch {
366
+ return channel. eventLoop. makeFailedFuture ( error)
367
+ }
368
+ }
369
+
370
+ // bind to selected device, if any
371
+ if let deviceName = configuration. deviceName,
372
+ let device = findDevice ( with: deviceName, protocol: resolvedAddress. protocol)
373
+ {
374
+ do {
375
+ try bindTo ( device: device, on: channel)
376
+ } catch {
377
+ return channel. eventLoop. makeFailedFuture ( error)
378
+ }
379
+ }
380
+
381
+ // enable TLS
382
+ if scheme. enableTLS {
383
+ let tlsConfig = configuration. tlsConfiguration ?? scheme. defaultTLSConfig!
384
+ guard let sslContext = try ? NIOSSLContext ( configuration: tlsConfig) else {
385
+ return channel. eventLoop. makeFailedFuture ( LCLWebSocketError . tlsInitializationFailed)
386
+ }
387
+
388
+ do {
389
+ let sslClientHandler = try NIOSSLClientHandler ( context: sslContext, serverHostname: host)
390
+ try channel. pipeline. syncOperations. addHandlers ( sslClientHandler)
391
+ } catch let error as NIOSSLExtraError where error == . invalidSNIHostname {
392
+ do {
393
+ let sslClientHandler = try NIOSSLClientHandler ( context: sslContext, serverHostname: nil )
394
+ try channel. pipeline. syncOperations. addHandlers ( sslClientHandler)
395
+ } catch {
396
+ return channel. eventLoop. makeFailedFuture ( error)
397
+ }
398
+ } catch {
399
+ return channel. eventLoop. makeFailedFuture ( error)
400
+ }
401
+ }
402
+
403
+ return channel. eventLoop. makeSucceededVoidFuture ( )
404
+ }
405
+
406
+ return makeChannelInitializer
407
+ }
394
408
}
395
409
396
410
#if !canImport(Darwin) || swift(>=5.10)
@@ -483,90 +497,15 @@ extension WebSocketClient {
483
497
return self . eventloopGroup. any ( ) . makeFailedFuture ( LCLWebSocketError . invalidURL)
484
498
}
485
499
486
- @Sendable
487
- func makeChannelInitializer( _ channel: Channel ) -> EventLoopFuture < Void > {
488
- if self . eventloopGroup is MultiThreadedEventLoopGroup {
489
- if configuration. socketReuseAddress,
490
- let syncOptions = channel. syncOptions
491
- {
492
- do {
493
- try syncOptions. setOption ( . socketOption( . so_reuseaddr) , value: 1 )
494
- } catch {
495
- return channel. eventLoop. makeFailedFuture ( error)
496
- }
497
- }
498
-
499
- if configuration. socketTcpNoDelay,
500
- let syncOptions = channel. syncOptions
501
- {
502
- do {
503
- try syncOptions. setOption ( . socketOption( . tcp_nodelay) , value: 1 )
504
- } catch {
505
- return channel. eventLoop. makeFailedFuture ( error)
506
- }
507
- }
508
- }
509
-
510
- if let socketSendBufferSize = configuration. socketSendBufferSize,
511
- let syncOptions = channel. syncOptions
512
- {
513
- do {
514
- try syncOptions. setOption ( . socketOption( . so_sndbuf) , value: socketSendBufferSize)
515
- } catch {
516
- return channel. eventLoop. makeFailedFuture ( error)
517
- }
518
- }
519
-
520
- if let socketReceiveBuffer = configuration. socketReceiveBufferSize,
521
- let syncOptions = channel. syncOptions
522
- {
523
- do {
524
- try syncOptions. setOption ( . socketOption( . so_rcvbuf) , value: socketReceiveBuffer)
525
- } catch {
526
- return channel. eventLoop. makeFailedFuture ( error)
527
- }
528
- }
529
-
530
- // bind to selected device, if any
531
- if let deviceName = configuration. deviceName,
532
- let device = findDevice ( with: deviceName, protocol: resolvedAddress. protocol)
533
- {
534
- do {
535
- try bindTo ( device: device, on: channel)
536
- } catch {
537
- return channel. eventLoop. makeFailedFuture ( error)
538
- }
539
- }
540
-
541
- // enable TLS
542
- if scheme. enableTLS {
543
- let tlsConfig = configuration. tlsConfiguration ?? scheme. defaultTLSConfig!
544
- guard let sslContext = try ? NIOSSLContext ( configuration: tlsConfig) else {
545
- return channel. eventLoop. makeFailedFuture ( LCLWebSocketError . tlsInitializationFailed)
546
- }
547
-
548
- do {
549
- let sslClientHandler = try NIOSSLClientHandler ( context: sslContext, serverHostname: host)
550
- try channel. pipeline. syncOperations. addHandlers ( sslClientHandler)
551
- } catch let error as NIOSSLExtraError where error == . invalidSNIHostname {
552
- do {
553
- let sslClientHandler = try NIOSSLClientHandler ( context: sslContext, serverHostname: nil )
554
- try channel. pipeline. syncOperations. addHandlers ( sslClientHandler)
555
- } catch {
556
- return channel. eventLoop. makeFailedFuture ( error)
557
- }
558
- } catch {
559
- return channel. eventLoop. makeFailedFuture ( error)
560
- }
561
- }
562
-
563
- return channel. eventLoop. makeSucceededVoidFuture ( )
564
- }
565
-
566
500
let upgradeResult = makeBootstrapAndConnect (
567
501
with: configuration,
568
502
resolvedAddress: resolvedAddress,
569
- channelInitializer: makeChannelInitializer ( _: )
503
+ channelInitializer: makeChannelInitializer (
504
+ configuration: configuration,
505
+ resolvedAddress: resolvedAddress,
506
+ scheme: scheme,
507
+ host: host
508
+ )
570
509
) . flatMap { channel in
571
510
// make upgrade request
572
511
let upgrader = NIOTypedWebSocketClientUpgrader < WebSocketUpgradeResult > (
0 commit comments