最近苹果开源了Swift版的Nettygit
SwiftNIO是一个跨平台异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端。简单来讲就是能够用来实现各类高性能服务端和客户端,如http、tcp。github
由于最近项目恰好要用到udp,因此趁机把日常用Netty实现的该用Swift。bootstrap
首先简单看一下几个用到的类swift
顾名思义,这东西是一个线程池。每一个EventLoopGroup
里面有多个EventLoop
,而每一个EventLoop
都与一个线程绑定。promise
Bootstrap
是开发Netty程序的基础,SwiftNIO也是同样。经过Bootstrap
的bind方法来建立链接,咱们也能够经过该方法返回的Channel
来判断是否建立成功。服务器
ChannelHandler
是用来处理数据,如客户端向服务端发送数据,服务端的数据处理就是在ChannelHandler
中完成。ChannelHandler
自己是一个protocol,咱们用到的有ChannelInboundHandler
和ChannelOutboundHandler
这两个,ChannlPipeline
会从头至尾顺序调用ChannelInboundHandler
处理数据,从尾到头调用ChannelOutboundHandler
数据。网络
接下来看服务端代码:app
class UDPServer {
private let loopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
func listen(on port: Int) {
let bootstrap = DatagramBootstrap(group: loopGroup)
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.channelInitializer { (channel) -> EventLoopFuture<Void> in
channel.pipeline.add(handler: UDPServerHandler())
}
do {
let channel = try bootstrap.bind(host: "127.0.0.1", port: port).wait()
print("listen on \(channel.localAddress!)")
try channel.closeFuture.wait()
} catch {
print(error)
}
}
final class UDPServerHandler: ChannelInboundHandler {
typealias InboundIn = AddressedEnvelope<ByteBuffer>
typealias OutboundOut = AddressedEnvelope<ByteBuffer>
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
let getData = unwrapInboundIn(data)
print(getData.data)
}
}
}
let server = UDPServer()
server.listen(on: 5656)
复制代码
运行代码就会能够看到控制台输出: listen on [IPv4]127.0.0.1:5656
框架
咱们经过内部类的形式实现了一个 ChannelInboundHandler
,并把它添加到 ChannlPipeline
。Handler
须要设置两个东西 InboundIn
和 OutboundOut
。异步
InboundIn
: 是入站数据的类型,就是接收客户端发过来的数据类型。 OutboundOut
: 是出站数据的类型,就是返回给客户端的数据类型,同时也是传递给下一个ChannelOutboundHandler
的类型。
咱们这里用的是 AddressedEnvelope<ByteBuffer>
它里面是一个 SocketAddress
加上 ByteBuffer
。
当客户端发来数据的时候会调用Handler
的channelRead(ctx: , data: )
这里进来的是NIOAny
类型,须要调用 Handler
的 unwrapInboundIn()
方法把 NIOAny
转成 InboundIn
类型。
就这样一个简单的UDP服务端就完成了,能够经过各类UDP工具进行测试,或者用SwiftNIO再写一个客户端:
class UDPClient {
private let loopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
private var channel: Channel!
init(port: Int) {
let bootstrap = DatagramBootstrap(group: loopGroup)
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.channelInitializer { (channel) -> EventLoopFuture<Void> in
channel.pipeline.add(handler: UDPClientHandler())
}
do {
channel = try bootstrap.bind(host: "0.0.0.0", port: port).wait()
sent(with: "test".data(using: .utf8)!)
} catch {
print(error)
}
}
func sent(with data: Data) {
var byteBuffer = ByteBufferAllocator().buffer(capacity: data.count)
byteBuffer.write(bytes: data)
let address = try! SocketAddress(ipAddress: "127.0.0.1", port: 5656)
channel.writeAndFlush(NIOAny(AddressedEnvelope<ByteBuffer>(remoteAddress: address, data: byteBuffer)), promise: nil)
}
final class UDPClientHandler: ChannelInboundHandler {
typealias InboundIn = AddressedEnvelope<ByteBuffer>
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
print(data)
}
}
}
let client = UDPClient(port: 22222)
复制代码
到这里,用SwiftNIO构建简单的UDP通信已经OK了。