Skip to content

Commit eb92345

Browse files
committed
various fixes
1 parent be56a60 commit eb92345

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

Sources/LCLWebSocket/Client/WebSocketClient.swift

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -341,36 +341,40 @@ extension WebSocketClient {
341341
) -> ChannelInitializer {
342342
@Sendable
343343
func makeChannelInitializer(_ channel: Channel) -> EventLoopFuture<Void> {
344+
let promise = channel.eventLoop.makePromise(of: Void.self)
344345
if let deviceName = configuration.deviceName,
345346
let device = findDevice(with: deviceName, protocol: resolvedAddress.protocol)
346347
{
347348
// bind to selected device, if any
348-
return bindTo(device: device, on: channel).flatMap { () -> EventLoopFuture<Void> in
349-
if scheme.enableTLS {
350-
// enale TLS
351-
let tlsConfig = configuration.tlsConfiguration ?? scheme.defaultTLSConfig!
352-
guard let sslContext = try? NIOSSLContext(configuration: tlsConfig) else {
353-
return channel.eventLoop.makeFailedFuture(LCLWebSocketError.tlsInitializationFailed)
354-
}
355-
356-
do {
357-
let sslClientHandler = try NIOSSLClientHandler(context: sslContext, serverHostname: host)
358-
try channel.pipeline.syncOperations.addHandlers(sslClientHandler)
359-
} catch let error as NIOSSLExtraError where error == .invalidSNIHostname {
360-
do {
361-
let sslClientHandler = try NIOSSLClientHandler(context: sslContext, serverHostname: nil)
362-
try channel.pipeline.syncOperations.addHandlers(sslClientHandler)
363-
} catch {
364-
return channel.eventLoop.makeFailedFuture(error)
365-
}
366-
} catch {
367-
return channel.eventLoop.makeFailedFuture(error)
368-
}
349+
bindTo(device: device, on: channel).cascadeFailure(to: promise)
350+
}
351+
352+
if scheme.enableTLS {
353+
// enale TLS
354+
let tlsConfig = configuration.tlsConfiguration ?? scheme.defaultTLSConfig!
355+
guard let sslContext = try? NIOSSLContext(configuration: tlsConfig) else {
356+
promise.fail(LCLWebSocketError.tlsInitializationFailed)
357+
return promise.futureResult
358+
}
359+
360+
do {
361+
let sslClientHandler = try NIOSSLClientHandler(context: sslContext, serverHostname: host)
362+
try channel.pipeline.syncOperations.addHandlers(sslClientHandler)
363+
} catch let error as NIOSSLExtraError where error == .invalidSNIHostname {
364+
do {
365+
let sslClientHandler = try NIOSSLClientHandler(context: sslContext, serverHostname: nil)
366+
try channel.pipeline.syncOperations.addHandlers(sslClientHandler)
367+
} catch {
368+
promise.fail(error)
369+
return promise.futureResult
369370
}
370-
return channel.eventLoop.makeSucceededVoidFuture()
371+
} catch {
372+
promise.fail(error)
373+
return promise.futureResult
371374
}
372375
}
373-
return channel.eventLoop.makeSucceededVoidFuture()
376+
promise.succeed()
377+
return promise.futureResult
374378
}
375379

376380
return makeChannelInitializer

Sources/LCLWebSocket/Server/WebSocketServer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ extension WebSocketServer {
276276
if let deviceName = configuration.deviceName,
277277
let device = findDevice(with: deviceName, protocol: resolvedAddress.protocol)
278278
{
279+
logger.debug("deviceName \(deviceName), device \(device)")
279280
return bindTo(device: device, on: channel)
280281
}
281282

Sources/LCLWebSocket/WebSocketConfiguration.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ extension LCLWebSocket {
8181

8282
/// The network device name on the system to route the traffic to.
8383
///
84+
/// If the device associated with the given `deviceName` is not found, then the WebSocket will
85+
/// be bound to the default interface according to the operating system's choice.
86+
///
8487
/// - Note: You might need root privileges to use this feature.
8588
var deviceName: String?
8689

Sources/LCLWebSocket/WebSocketHandler.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ final class WebSocketHandler: ChannelInboundHandler {
2121
self.websocket = websocket
2222
}
2323

24+
#if DEBUG
2425
func channelInactive(context: ChannelHandlerContext) {
2526
logger.debug("WebSocketHandler channelInactive")
2627
}
2728

2829
func channelUnregistered(context: ChannelHandlerContext) {
2930
logger.debug("WebSocketHandler channelUnregistered")
3031
}
32+
#endif // DEBUG
3133

3234
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
3335
let frame = self.unwrapInboundIn(data)

0 commit comments

Comments
 (0)