Skip to content

Commit dae7d95

Browse files
committed
fix build on linux
1 parent 76b2a6b commit dae7d95

File tree

6 files changed

+55
-33
lines changed

6 files changed

+55
-33
lines changed

Sources/LCLWebSocket/Client/WebSocketClient.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,18 @@ extension WebSocketClient {
338338
resolvedAddress: SocketAddress,
339339
channelInitializer: @escaping (Channel) -> EventLoopFuture<Void>
340340
) -> EventLoopFuture<Channel> {
341-
if self.eventloopGroup is MultiThreadedEventLoopGroup {
341+
342+
func makeClientBootstrap() -> EventLoopFuture<Channel> {
342343
return ClientBootstrap(group: self.eventloopGroup)
343344
.channelOption(.socketOption(.tcp_nodelay), value: 1)
344345
.channelOption(.socketOption(.so_reuseaddr), value: 1)
345346
.connectTimeout(configuration.connectionTimeout)
346347
.channelInitializer(channelInitializer)
347348
.connect(to: resolvedAddress)
348-
} else {
349+
}
350+
351+
#if canImport(Network)
352+
func makeNIOTSConnectionBootstrap() -> EventLoopFuture<Channel> {
349353
let tcpOptions = NWProtocolTCP.Options()
350354
tcpOptions.connectionTimeout = Int(configuration.connectionTimeout.seconds)
351355
tcpOptions.noDelay = true
@@ -355,6 +359,17 @@ extension WebSocketClient {
355359
.channelInitializer(channelInitializer)
356360
.connect(to: resolvedAddress)
357361
}
362+
#endif
363+
364+
#if canImport(Network)
365+
if self.eventloopGroup is MultiThreadedEventLoopGroup {
366+
return makeClientBootstrap()
367+
} else {
368+
return makeNIOTSConnectionBootstrap()
369+
}
370+
#else
371+
return makeClientBootstrap()
372+
#endif
358373
}
359374
}
360375

@@ -372,7 +387,7 @@ extension WebSocketClient {
372387
///
373388
/// - Note: this method is functionally the same as `connect(to:headers:configuration:)`. But this method relies on
374389
/// infrastructures that are available on Swift >= 5.10.
375-
@available(macOS 13, iOS 16, watchOS 9, tvOS 16, visionOS 1.0, *)
390+
@available(macOS 13, iOS 16, watchOS 9, tvOS 16, *)
376391
public func typedConnect(
377392
to endpoint: String,
378393
headers: [String: String] = [:],
@@ -396,7 +411,7 @@ extension WebSocketClient {
396411
///
397412
/// - Note: this method is functionally the same as `connect(to:headers:configuration:)`. But this method relies on
398413
/// infrastructures that are available on Swift >= 5.10.
399-
@available(macOS 13, iOS 16, watchOS 9, tvOS 16, visionOS 1.0, *)
414+
@available(macOS 13, iOS 16, watchOS 9, tvOS 16, *)
400415
public func typedConnect(
401416
to url: URL,
402417
headers: [String: String] = [:],
@@ -420,7 +435,7 @@ extension WebSocketClient {
420435
///
421436
/// - Note: this method is functionally the same as `connect(to:headers:configuration:)`. But this method relies on
422437
/// infrastructures that are available on Swift >= 5.10.
423-
@available(macOS 13, iOS 16, watchOS 9, tvOS 16, visionOS 1.0, *)
438+
@available(macOS 13, iOS 16, watchOS 9, tvOS 16, *)
424439
public func typedConnect(
425440
to endpoint: URLComponents,
426441
headers: [String: String],

Sources/LCLWebSocket/LCLWebSocket+ChannelInitializer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal func bindTo(device: NIONetworkDevice, on channel: Channel) throws {
2929
throw LCLWebSocketError.invalidDevice
3030
}
3131
#elseif canImport(Glibc) || canImport(Musl)
32-
return (channel as! SocketOptionProvider).setBindToDevice(device.name)
32+
try (channel as! SocketOptionProvider).setBindToDevice(device.name).wait()
3333
#endif
3434
}
3535

Sources/LCLWebSocket/LCLWebSocket+EventloopGroup.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import NIOWebSocket
1919
import Network
2020
#endif
2121

22-
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
22+
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
2323
import NIOTransportServices
2424
#endif
2525

@@ -28,7 +28,7 @@ extension LCLWebSocket {
2828
/// The default `EventLoopGroup` for the `LCLWebSocket`.
2929
public static var defaultEventloopGroup: EventLoopGroup {
3030
#if canImport(Network)
31-
if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, visionOS 1.0, *) {
31+
if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
3232
NIOTSEventLoopGroup.singleton
3333
} else {
3434
MultiThreadedEventLoopGroup.singleton
@@ -44,7 +44,7 @@ extension LCLWebSocket {
4444
/// - size: the number of event loop in this `EventLoopGroup`
4545
public static func makeEventLoopGroup(size: Int) -> EventLoopGroup {
4646
#if canImport(Network)
47-
if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, visionOS 1.0, *) {
47+
if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
4848
NIOTSEventLoopGroup(loopCount: size)
4949
} else {
5050
MultiThreadedEventLoopGroup(numberOfThreads: size)

Sources/LCLWebSocket/Server/WebSocketServer.swift

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ extension WebSocketServer {
241241
resolvedAddress: SocketAddress,
242242
childChannelInitializer: @escaping (Channel) -> EventLoopFuture<Void>
243243
) -> EventLoopFuture<Channel> {
244-
if self.eventloopGroup is MultiThreadedEventLoopGroup {
244+
245+
func makeServerBootstrap() -> EventLoopFuture<Channel> {
245246
return ServerBootstrap(group: self.eventloopGroup)
246247
.serverChannelOption(.socketOption(.so_reuseaddr), value: 1)
247248
.serverChannelInitializer { channel in
@@ -282,7 +283,10 @@ extension WebSocketServer {
282283
.childChannelOption(.socketOption(.so_reuseaddr), value: 1)
283284
.childChannelInitializer(childChannelInitializer)
284285
.bind(to: resolvedAddress)
285-
} else {
286+
}
287+
288+
#if canImport(Network)
289+
func makeNIOTSListenerBootstrap() -> EventLoopFuture<Channel> {
286290
return NIOTSListenerBootstrap(group: self.eventloopGroup)
287291
.serverChannelOption(.socketOption(.so_reuseaddr), value: 1)
288292
.serverChannelInitializer { channel in
@@ -324,20 +328,23 @@ extension WebSocketServer {
324328
.childChannelInitializer(childChannelInitializer)
325329
.bind(to: resolvedAddress)
326330
}
331+
#endif
332+
333+
#if canImport(Network)
334+
if self.eventloopGroup is MultiThreadedEventLoopGroup {
335+
return makeServerBootstrap()
336+
} else {
337+
return makeNIOTSListenerBootstrap()
338+
}
339+
#else
340+
return makeServerBootstrap()
341+
#endif
342+
327343
}
328344
}
329345

330346
#if !canImport(Darwin) || swift(>=5.10)
331347
extension WebSocketServer {
332-
// @available(macOS 13, iOS 16, watchOS 9, tvOS 16, visionOS 1.0, *)
333-
// public func listen(
334-
// to host: String,
335-
// port: Int,
336-
// configuration: LCLWebSocket.Configuration
337-
// ) throws -> EventLoopFuture<Void> {
338-
// let addr = try SocketAddress(ipAddress: host, port: port)
339-
// return self.listen(to: addr, configuration: configuration)
340-
// }
341348

342349
/// Let the WebSocket server bind and listen to the given address, using the provided configuration.
343350
///
@@ -348,7 +355,7 @@ extension WebSocketServer {
348355
///
349356
/// - Note: this is functionally the same as `listen(to:configuration:)`. But this function relies on infrastructures that
350357
/// is available only on Swift >= 5.10
351-
@available(macOS 13, iOS 16, watchOS 9, tvOS 16, visionOS 1.0, *)
358+
@available(macOS 13, iOS 16, watchOS 9, tvOS 16, *)
352359
public func typedListen(
353360
to address: SocketAddress,
354361
configuration: LCLWebSocket.Configuration
@@ -375,7 +382,7 @@ extension WebSocketServer {
375382
}
376383
}
377384

378-
@available(macOS 13, iOS 16, watchOS 9, tvOS 16, visionOS 1.0, *)
385+
@available(macOS 13, iOS 16, watchOS 9, tvOS 16, *)
379386
private func configureTypedWebSocketServerUpgrade(
380387
on channel: Channel,
381388
configuration: LCLWebSocket.Configuration
@@ -520,9 +527,9 @@ extension WebSocketServer {
520527
case (.end, .ok):
521528
context.fireChannelRead(data)
522529
case (.end, .invalidMethod):
523-
let resposneHead = makeResponse(with: .methodNotAllowed)
524-
context.channel.write(self.wrapOutboundOut(.head(resposneHead)), promise: nil)
525-
context.channel.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
530+
let responseHead = makeResponse(with: .methodNotAllowed)
531+
context.channel.write(HTTPServerResponsePart.head(responseHead), promise: nil)
532+
context.channel.writeAndFlush(HTTPServerResponsePart.end(nil), promise: nil)
526533
context.fireErrorCaught(LCLWebSocketError.methodNotAllowed)
527534
context.close(mode: .all, promise: nil)
528535
}
@@ -576,8 +583,8 @@ extension WebSocketServer {
576583
responseHead = makeResponse(with: .internalServerError)
577584
}
578585
logger.debug("closing channel due to error \(error). response head \(responseHead)")
579-
context.channel.write(self.wrapOutboundOut(.head(responseHead)), promise: nil)
580-
context.channel.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
586+
context.channel.write(HTTPServerResponsePart.head(responseHead), promise: nil)
587+
context.channel.writeAndFlush(HTTPServerResponsePart.end(nil), promise: nil)
581588
context.close(mode: .all, promise: nil)
582589
}
583590
}

Sources/LCLWebSocket/WebSocket.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public final class WebSocket: Sendable {
276276
self._onBinary.value?(self, data)
277277
case .text:
278278
if data.readableBytes > 0 {
279-
guard let text = data.readString(length: data.readableBytes, encoding: .utf8) else {
279+
guard let text = data.readString(length: data.readableBytes) else {
280280
self.close(code: .dataInconsistentWithMessage, promise: nil)
281281
return
282282
}
@@ -326,7 +326,7 @@ public final class WebSocket: Sendable {
326326
}
327327

328328
let bytesLeftForReason = data.readableBytes
329-
let reason = data.readString(length: data.readableBytes, encoding: .utf8)
329+
let reason = data.readString(length: data.readableBytes)
330330

331331
if bytesLeftForReason > 0 && reason == nil {
332332
self.close(code: .dataInconsistentWithMessage, promise: nil)

Sources/LCLWebSocket/WebSocketConfiguration.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ extension LCLWebSocket {
8383
var autoPingConfiguration: AutoPingConfiguration
8484

8585
/// Socket send buffer size in bytes.
86-
var socketSendBufferSize: Int32?
86+
var socketSendBufferSize: Int?
8787

8888
/// Socket receive buffer size in bytes.
89-
var socketReceiveBufferSize: Int32?
89+
var socketReceiveBufferSize: Int?
9090

9191
/// Strategy for handling leftover bytes after upgrade.
9292
///
@@ -109,8 +109,8 @@ extension LCLWebSocket {
109109
),
110110
leftoverBytesStrategy: RemoveAfterUpgradeStrategy = .dropBytes,
111111
deviceName: String? = nil,
112-
socketSendBufferSize: Int32? = nil,
113-
socketReceiveBufferSize: Int32? = nil
112+
socketSendBufferSize: Int? = nil,
113+
socketReceiveBufferSize: Int? = nil
114114
) {
115115
self.tlsConfiguration = tlsConfiguration
116116
self.maxFrameSize = maxFrameSize

0 commit comments

Comments
 (0)