mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Add BufferAllocator
This commit is contained in:
1 parent
16559385fc
commit
711cf8a2bb
5 files changed
+32
-6
No files matched your search
@@ -30,3 +30,22 @@ public class Buffer {
|
||||
self.limit = 0
|
||||
}
|
||||
}
|
||||
|
||||
public protocol BufferAllocator {
|
||||
|
||||
func buffer(capacity: Int32) -> Buffer
|
||||
|
||||
func buffer() -> Buffer
|
||||
}
|
||||
|
||||
extension BufferAllocator {
|
||||
public func buffer() -> Buffer {
|
||||
return buffer(capacity: 256)
|
||||
}
|
||||
}
|
||||
|
||||
public class DefaultBufferAllocator : BufferAllocator {
|
||||
public func buffer(capacity: Int32) -> Buffer {
|
||||
return Buffer(capacity: capacity)
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import Future
|
||||
|
||||
public class Channel : ChannelOutboundInvoker {
|
||||
public let pipeline: ChannelPipeline
|
||||
public let allocator: BufferAllocator
|
||||
private let selector: Selector
|
||||
private let socket: Socket
|
||||
private var pendingWrites: [(Buffer, Promise<Void>)]
|
||||
@@ -31,8 +32,10 @@ public class Channel : ChannelOutboundInvoker {
|
||||
pendingWrites = Array()
|
||||
outstanding = 0
|
||||
flushPending = false
|
||||
allocator = DefaultBufferAllocator()
|
||||
}
|
||||
|
||||
|
||||
public func write(data: Buffer, promise: Promise<Void>) -> Future<Void> {
|
||||
return pipeline.write(data: data, promise: promise)
|
||||
}
|
||||
@@ -131,7 +134,7 @@ public class Channel : ChannelOutboundInvoker {
|
||||
|
||||
func read0() {
|
||||
// TODO: Make this smarter
|
||||
let buffer = Buffer(capacity: 8 * 1024)
|
||||
let buffer = allocator.buffer(capacity: 8 * 1024)
|
||||
|
||||
defer {
|
||||
// Always call the method as last
|
||||
|
||||
@@ -19,13 +19,15 @@ public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvok
|
||||
|
||||
public let handler: ChannelHandler
|
||||
public let pipeline: ChannelPipeline
|
||||
public let allocator: BufferAllocator
|
||||
|
||||
var prev: ChannelHandlerContext?
|
||||
var next: ChannelHandlerContext?
|
||||
|
||||
init(handler: ChannelHandler, pipeline: ChannelPipeline) {
|
||||
init(handler: ChannelHandler, pipeline: ChannelPipeline, allocator: BufferAllocator) {
|
||||
self.handler = handler
|
||||
self.pipeline = pipeline
|
||||
self.allocator = allocator;
|
||||
}
|
||||
|
||||
public func fireChannelActive() {
|
||||
|
||||
@@ -22,14 +22,14 @@ public class ChannelPipeline : ChannelInboundInvoker, ChannelOutboundInvoker {
|
||||
var tail: ChannelHandlerContext?
|
||||
|
||||
func attach(channel: Channel) {
|
||||
head = ChannelHandlerContext(handler: HeadChannelHandler(channel: channel), pipeline: self)
|
||||
tail = ChannelHandlerContext(handler: TailChannelHandler(), pipeline: self)
|
||||
head = ChannelHandlerContext(handler: HeadChannelHandler(channel: channel), pipeline: self, allocator: channel.allocator)
|
||||
tail = ChannelHandlerContext(handler: TailChannelHandler(), pipeline: self, allocator: channel.allocator)
|
||||
head!.next = tail
|
||||
tail!.prev = head
|
||||
}
|
||||
|
||||
public func addLast(handler: ChannelHandler) {
|
||||
let ctx = ChannelHandlerContext(handler: handler, pipeline: self)
|
||||
let ctx = ChannelHandlerContext(handler: handler, pipeline: self, allocator: head!.allocator)
|
||||
let prev = tail!.prev
|
||||
ctx.prev = tail!.prev
|
||||
ctx.next = tail
|
||||
@@ -47,7 +47,7 @@ public class ChannelPipeline : ChannelInboundInvoker, ChannelOutboundInvoker {
|
||||
}
|
||||
|
||||
public func addFirst(handler: ChannelHandler) {
|
||||
let ctx = ChannelHandlerContext(handler: handler, pipeline: self)
|
||||
let ctx = ChannelHandlerContext(handler: handler, pipeline: self, allocator: head!.allocator)
|
||||
let next = head!.next
|
||||
|
||||
ctx.prev = head
|
||||
|
||||
@@ -15,6 +15,7 @@ import Foundation
|
||||
|
||||
|
||||
public class EchoHandler: ChannelHandler {
|
||||
|
||||
public func channelRead(ctx: ChannelHandlerContext, data: Buffer) {
|
||||
let f = ctx.write(data: data)
|
||||
|
||||
@@ -31,6 +32,7 @@ public class EchoHandler: ChannelHandler {
|
||||
|
||||
public func errorCaught(ctx: ChannelHandlerContext, error: Error) {
|
||||
print("error: {}", error)
|
||||
let _ = ctx.close()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user