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.socket(SocketOptionLevel(SOL_SOCKET), 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
.Declaration
Swift
public init(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
public func withBoundSocket(descriptor: CInt) -> 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.