DatagramBootstrap
public final class DatagramBootstrap
A DatagramBootstrap
is an easy way to bootstrap a DatagramChannel
when creating datagram clients
and servers.
Example:
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! group.syncShutdownGracefully()
}
let bootstrap = DatagramBootstrap(group: group)
// Enable SO_REUSEADDR.
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
.channelInitializer { channel in
channel.pipeline.addHandler(MyChannelHandler())
}
let channel = try! bootstrap.bind(host: "127.0.0.1", port: 53).wait()
/* the Channel is now ready to send/receive datagrams */
try channel.closeFuture.wait() // Wait until the channel un-binds.
The DatagramChannel
will operate on AddressedEnvelope<ByteBuffer>
as inbound and outbound messages.
-
Create a
DatagramBootstrap
on theEventLoopGroup
group
.The
EventLoopGroup
group
must be compatible, otherwise the program will crash.DatagramBootstrap
is compatible only withMultiThreadedEventLoopGroup
as well as theEventLoop
s returned byMultiThreadedEventLoopGroup.next
. Seeinit(validatingGroup:)
for a fallible initializer for situations where it’s impossible to tell ahead of time if theEventLoopGroup
is compatible or not.Declaration
Swift
public convenience init(group: EventLoopGroup)
Parameters
group
The
EventLoopGroup
to use. -
Create a
DatagramBootstrap
on theEventLoopGroup
group
, validating thatgroup
is compatible.Declaration
Swift
public init?(validatingGroup group: EventLoopGroup)
Parameters
group
The
EventLoopGroup
to use. -
Initialize the bound
DatagramChannel
withinitializer
. The most common task in initializer is to addChannelHandler
s to theChannelPipeline
.Declaration
Swift
public func channelInitializer(_ handler: @escaping (Channel) -> EventLoopFuture<Void>) -> Self
Parameters
handler
A closure that initializes the provided
Channel
. -
Specifies a
ChannelOption
to be applied to theDatagramChannel
.Declaration
Swift
@inlinable public func channelOption<Option>(_ option: Option, value: Option.Value) -> Self where Option : ChannelOption
Parameters
option
The option to be applied.
value
The value for the option.
-
Use the existing bound socket file descriptor.
Declaration
Swift
@available(*, deprecated, renamed: "withBoundSocket(_:﹚") public func withBoundSocket(descriptor: CInt) -> EventLoopFuture<Channel>
Parameters
descriptor
The Unix file descriptor representing the bound datagram socket.
-
Use the existing bound socket file descriptor.
Declaration
Swift
public func withBoundSocket(_ socket: NIOBSDSocket.Handle) -> EventLoopFuture<Channel>
Parameters
descriptor
The Unix file descriptor representing the bound datagram socket.
-
Bind the
DatagramChannel
tohost
andport
.Declaration
Swift
public func bind(host: String, port: Int) -> EventLoopFuture<Channel>
Parameters
host
The host to bind on.
port
The port to bind on.
-
Bind the
DatagramChannel
toaddress
.Declaration
Swift
public func bind(to address: SocketAddress) -> EventLoopFuture<Channel>
Parameters
address
The
SocketAddress
to bind on. -
Bind the
DatagramChannel
to a UNIX Domain Socket.Declaration
Swift
public func bind(unixDomainSocketPath: String) -> EventLoopFuture<Channel>
Parameters
unixDomainSocketPath
The path of the UNIX Domain Socket to bind on.
path
must not exist, it will be created by the system. -
Bind the
DatagramChannel
to a UNIX Domain Socket.Declaration
Swift
public func bind(unixDomainSocketPath: String, cleanupExistingSocketFile: Bool) -> EventLoopFuture<Channel>
Parameters
unixDomainSocketPath
The path of the UNIX Domain Socket to bind on.
path
must not exist, it will be created by the system.cleanupExistingSocketFile
Whether to cleanup an existing socket file at
path
.