Skip to content

Commit

Permalink
Merge branch 'main' into cb-strict-concurrency-nio-filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasa authored Feb 3, 2025
2 parents defff11 + 2f1c780 commit 46cb123
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
39 changes: 26 additions & 13 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ let package = Package(
"NIOCore",
"NIOEmbedded",
"NIOPosix",
]
],
swiftSettings: strictConcurrencySettings
),
.target(
name: "_NIOConcurrency",
Expand Down Expand Up @@ -274,15 +275,17 @@ let package = Package(
"NIOPosix",
"NIOCore",
],
exclude: ["README.md"]
exclude: ["README.md"],
swiftSettings: strictConcurrencySettings
),
.executableTarget(
name: "NIOTCPEchoClient",
dependencies: [
"NIOPosix",
"NIOCore",
],
exclude: ["README.md"]
exclude: ["README.md"],
swiftSettings: strictConcurrencySettings
),
.executableTarget(
name: "NIOEchoServer",
Expand All @@ -291,7 +294,8 @@ let package = Package(
"NIOCore",
"NIOConcurrencyHelpers",
],
exclude: ["README.md"]
exclude: ["README.md"],
swiftSettings: strictConcurrencySettings
),
.executableTarget(
name: "NIOEchoClient",
Expand All @@ -300,7 +304,8 @@ let package = Package(
"NIOCore",
"NIOConcurrencyHelpers",
],
exclude: ["README.md"]
exclude: ["README.md"],
swiftSettings: strictConcurrencySettings
),
.executableTarget(
name: "NIOHTTP1Server",
Expand Down Expand Up @@ -329,7 +334,8 @@ let package = Package(
"NIOCore",
"NIOConcurrencyHelpers",
],
exclude: ["README.md"]
exclude: ["README.md"],
swiftSettings: strictConcurrencySettings
),
.executableTarget(
name: "NIOChatClient",
Expand All @@ -338,7 +344,8 @@ let package = Package(
"NIOCore",
"NIOConcurrencyHelpers",
],
exclude: ["README.md"]
exclude: ["README.md"],
swiftSettings: strictConcurrencySettings
),
.executableTarget(
name: "NIOWebSocketServer",
Expand All @@ -365,23 +372,26 @@ let package = Package(
dependencies: [
"NIOPosix",
"NIOCore",
]
],
swiftSettings: strictConcurrencySettings
),
.executableTarget(
name: "NIOUDPEchoServer",
dependencies: [
"NIOPosix",
"NIOCore",
],
exclude: ["README.md"]
exclude: ["README.md"],
swiftSettings: strictConcurrencySettings
),
.executableTarget(
name: "NIOUDPEchoClient",
dependencies: [
"NIOPosix",
"NIOCore",
],
exclude: ["README.md"]
exclude: ["README.md"],
swiftSettings: strictConcurrencySettings
),
.executableTarget(
name: "NIOAsyncAwaitDemo",
Expand Down Expand Up @@ -511,15 +521,18 @@ let package = Package(
dependencies: [
"NIOCore",
"NIOFoundationCompat",
]
],
swiftSettings: strictConcurrencySettings
),
.testTarget(
name: "NIOTests",
dependencies: ["NIO"]
dependencies: ["NIO"],
swiftSettings: strictConcurrencySettings
),
.testTarget(
name: "NIOSingletonsTests",
dependencies: ["NIOCore", "NIOPosix"]
dependencies: ["NIOCore", "NIOPosix"],
swiftSettings: strictConcurrencySettings
),
.testTarget(
name: "NIOFileSystemTests",
Expand Down
18 changes: 5 additions & 13 deletions Sources/NIOChatClient/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,9 @@ private final class ChatHandler: ChannelInboundHandler {
public typealias InboundIn = ByteBuffer
public typealias OutboundOut = ByteBuffer

private func printByte(_ byte: UInt8) {
#if os(Android)
print(Character(UnicodeScalar(byte)), terminator: "")
#else
fputc(Int32(byte), stdout)
#endif
}

public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
var buffer = Self.unwrapInboundIn(data)
while let byte: UInt8 = buffer.readInteger() {
printByte(byte)
}
let buffer = Self.unwrapInboundIn(data)
print(String(buffer: buffer))
}

public func errorCaught(context: ChannelHandlerContext, error: Error) {
Expand All @@ -47,7 +37,9 @@ let bootstrap = ClientBootstrap(group: group)
// Enable SO_REUSEADDR.
.channelOption(.socketOption(.so_reuseaddr), value: 1)
.channelInitializer { channel in
channel.pipeline.addHandler(ChatHandler())
channel.eventLoop.makeCompletedFuture {
try channel.pipeline.syncOperations.addHandler(ChatHandler())
}
}
defer {
try! group.syncShutdownGracefully()
Expand Down
4 changes: 3 additions & 1 deletion Sources/NIOEchoClient/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ let bootstrap = ClientBootstrap(group: group)
// Enable SO_REUSEADDR.
.channelOption(.socketOption(.so_reuseaddr), value: 1)
.channelInitializer { channel in
channel.pipeline.addHandler(EchoHandler())
channel.eventLoop.makeCompletedFuture {
try channel.pipeline.syncOperations.addHandler(EchoHandler())
}
}
defer {
try! group.syncShutdownGracefully()
Expand Down
4 changes: 2 additions & 2 deletions Sources/NIOMulticastChat/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
var datagramBootstrap = DatagramBootstrap(group: group)
.channelOption(.socketOption(.so_reuseaddr), value: 1)
.channelInitializer { channel in
channel.pipeline.addHandler(ChatMessageEncoder()).flatMap {
channel.pipeline.addHandler(ChatMessageDecoder())
channel.eventLoop.makeCompletedFuture {
try channel.pipeline.syncOperations.addHandlers(ChatMessageEncoder(), ChatMessageDecoder())
}
}

Expand Down
6 changes: 4 additions & 2 deletions Sources/NIOUDPEchoClient/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ default:
connectTarget = .ip(host: defaultHost, sendPort: defaultServerPort, listeningPort: defaultListeningPort)
}

let remoteAddress = { () -> SocketAddress in
let remoteAddress = { @Sendable () -> SocketAddress in
switch connectTarget {
case .ip(let host, let sendPort, _):
return try SocketAddress.makeAddressResolvingHost(host, port: sendPort)
Expand All @@ -129,7 +129,9 @@ let bootstrap = DatagramBootstrap(group: group)
// Enable SO_REUSEADDR.
.channelOption(.socketOption(.so_reuseaddr), value: 1)
.channelInitializer { channel in
channel.pipeline.addHandler(EchoHandler(remoteAddressInitializer: remoteAddress))
channel.eventLoop.makeCompletedFuture {
try channel.pipeline.syncOperations.addHandler(EchoHandler(remoteAddressInitializer: remoteAddress))
}
}
defer {
try! group.syncShutdownGracefully()
Expand Down
5 changes: 3 additions & 2 deletions Sources/NIOUDPEchoServer/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ var bootstrap = DatagramBootstrap(group: group)

// Set the handlers that are applied to the bound channel
.channelInitializer { channel in
// Ensure we don't read faster than we can write by adding the BackPressureHandler into the pipeline.
channel.pipeline.addHandler(EchoHandler())
channel.eventLoop.makeCompletedFuture {
try channel.pipeline.syncOperations.addHandler(EchoHandler())
}
}

defer {
Expand Down

0 comments on commit 46cb123

Please sign in to comment.