mirror of
https://github.com/apple/swift-nio
synced 2026-11-28 05:14:23 +00:00
add PipeChannel (#1138)
Motivation: There are use-cases for SwiftNIO where there are no actual sockets but rather two pipe file descriptors - one for input, one for output. There's no real reason why SwiftNIO shouldn't work for those. Modifications: Add a PipeChannel. Result: More use-cases for SwiftNIO.
This commit is contained in:
1 parent
a796af1a96
commit
e102aa9ae9
29 files changed
+1579
-320
No files matched your search
@@ -485,6 +485,7 @@ let defaultHtdocs = "/dev/null/"
|
||||
enum BindTo {
|
||||
case ip(host: String, port: Int)
|
||||
case unixDomainSocket(path: String)
|
||||
case stdio
|
||||
}
|
||||
|
||||
let htdocs: String
|
||||
@@ -500,8 +501,12 @@ case (_, .some(let p), let maybeHtdocs, _, _):
|
||||
bindTarget = .ip(host: defaultHost, port: p)
|
||||
htdocs = maybeHtdocs ?? defaultHtdocs
|
||||
case (.some(let portString), .none, let maybeHtdocs, .none, .none):
|
||||
/* couldn't parse as number --> uds-path [htdocs] */
|
||||
bindTarget = .unixDomainSocket(path: portString)
|
||||
/* couldn't parse as number --> uds-path-or-stdio [htdocs] */
|
||||
if portString == "-" {
|
||||
bindTarget = .stdio
|
||||
} else {
|
||||
bindTarget = .unixDomainSocket(path: portString)
|
||||
}
|
||||
htdocs = maybeHtdocs ?? defaultHtdocs
|
||||
default:
|
||||
htdocs = defaultHtdocs
|
||||
@@ -512,23 +517,31 @@ let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
|
||||
let threadPool = NIOThreadPool(numberOfThreads: 6)
|
||||
threadPool.start()
|
||||
|
||||
func childChannelInitializer(channel: Channel) -> EventLoopFuture<Void> {
|
||||
return channel.pipeline.configureHTTPServerPipeline(withErrorHandling: true).flatMap {
|
||||
channel.pipeline.addHandler(HTTPHandler(fileIO: fileIO, htdocsPath: htdocs))
|
||||
}
|
||||
}
|
||||
|
||||
let fileIO = NonBlockingFileIO(threadPool: threadPool)
|
||||
let bootstrap = ServerBootstrap(group: group)
|
||||
let socketBootstrap = ServerBootstrap(group: group)
|
||||
// Specify backlog and enable SO_REUSEADDR for the server itself
|
||||
.serverChannelOption(ChannelOptions.backlog, value: 256)
|
||||
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
|
||||
|
||||
// Set the handlers that are applied to the accepted Channels
|
||||
.childChannelInitializer { channel in
|
||||
channel.pipeline.configureHTTPServerPipeline(withErrorHandling: true).flatMap {
|
||||
channel.pipeline.addHandler(HTTPHandler(fileIO: fileIO, htdocsPath: htdocs))
|
||||
}
|
||||
}
|
||||
.childChannelInitializer(childChannelInitializer(channel:))
|
||||
|
||||
// Enable SO_REUSEADDR for the accepted Channels
|
||||
.childChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
|
||||
.childChannelOption(ChannelOptions.maxMessagesPerRead, value: 1)
|
||||
.childChannelOption(ChannelOptions.allowRemoteHalfClosure, value: allowHalfClosure)
|
||||
let pipeBootstrap = NIOPipeBootstrap(group: group)
|
||||
// Set the handlers that are applied to the accepted Channels
|
||||
.channelInitializer(childChannelInitializer(channel:))
|
||||
|
||||
.channelOption(ChannelOptions.maxMessagesPerRead, value: 1)
|
||||
.channelOption(ChannelOptions.allowRemoteHalfClosure, value: allowHalfClosure)
|
||||
|
||||
defer {
|
||||
try! group.syncShutdownGracefully()
|
||||
@@ -540,14 +553,23 @@ print("htdocs = \(htdocs)")
|
||||
let channel = try { () -> Channel in
|
||||
switch bindTarget {
|
||||
case .ip(let host, let port):
|
||||
return try bootstrap.bind(host: host, port: port).wait()
|
||||
return try socketBootstrap.bind(host: host, port: port).wait()
|
||||
case .unixDomainSocket(let path):
|
||||
return try bootstrap.bind(unixDomainSocketPath: path).wait()
|
||||
print("hi \(path)")
|
||||
return try socketBootstrap.bind(unixDomainSocketPath: path).wait()
|
||||
case .stdio:
|
||||
return try pipeBootstrap.withPipes(inputDescriptor: STDIN_FILENO, outputDescriptor: STDOUT_FILENO).wait()
|
||||
}
|
||||
}()
|
||||
|
||||
guard let localAddress = channel.localAddress else {
|
||||
fatalError("Address was unable to bind. Please check that the socket was not closed or that the address family was understood.")
|
||||
let localAddress: String
|
||||
if case .stdio = bindTarget {
|
||||
localAddress = "STDIO"
|
||||
} else {
|
||||
guard let channelLocalAddress = channel.localAddress else {
|
||||
fatalError("Address was unable to bind. Please check that the socket was not closed or that the address family was understood.")
|
||||
}
|
||||
localAddress = "\(channelLocalAddress)"
|
||||
}
|
||||
print("Server started and listening on \(localAddress), htdocs path \(htdocs)")
|
||||
|
||||
|
||||
Reference in new issue
Block a user