Tighten up API and access rights

This commit is contained in:
Norman Maurer committed 2017-06-07 11:15:06 +02:00
1 parent 329b474f45
commit aa1f7d04ed
6 files changed
+81 -88

No files matched your search

+2 -2
View File
@@ -78,7 +78,7 @@ public class ServerBootstrap {
let promise = eventLoop.newPromise(type: Channel.self)
do {
let serverChannel = try ServerSocketChannel(eventLoop: eventLoop, group: chEvGroup)
let serverChannel = try ServerSocketChannel(eventLoop: eventLoop as! SelectableEventLoop, group: chEvGroup)
func finishServerSetup() {
do {
@@ -228,7 +228,7 @@ public class ClientBootstrap {
let promise = eventLoop.newPromise(type: Channel.self)
do {
let channel = try SocketChannel(eventLoop: eventLoop)
let channel = try SocketChannel(eventLoop: eventLoop as! SelectableEventLoop)
func finishClientSetup() {
do {
+1 -2
View File
@@ -88,8 +88,7 @@ public struct ByteBuffer : Equatable { // TODO: Comparable
return false
}
return lhs.readableBytes == rhs.readableBytes &&
lhs.withReadPointer { lhsPtr, lhsSize in
return lhs.withReadPointer { lhsPtr, lhsSize in
rhs.withReadPointer { rhsPtr, rhsSize in
// Shouldn't get here otherwise because of readableBytes check
assert(rhsSize == lhsSize)
+48 -35
View File
@@ -338,9 +338,9 @@ fileprivate class PendingWrites {
/*
All operations on SocketChannel are thread-safe
*/
final public class SocketChannel : BaseSocketChannel {
final class SocketChannel : BaseSocketChannel {
public init(eventLoop: EventLoop) throws {
init(eventLoop: SelectableEventLoop) throws {
let socket = try Socket()
do {
try socket.setNonBlocking()
@@ -352,7 +352,7 @@ final public class SocketChannel : BaseSocketChannel {
super.init(socket: socket, eventLoop: eventLoop)
}
fileprivate init(socket: Socket, eventLoop: EventLoop) throws {
fileprivate init(socket: Socket, eventLoop: SelectableEventLoop) throws {
try socket.setNonBlocking()
super.init(socket: socket, eventLoop: eventLoop)
}
@@ -405,12 +405,12 @@ final public class SocketChannel : BaseSocketChannel {
/*
All operations on ServerSocketChannel are thread-safe
*/
final public class ServerSocketChannel : BaseSocketChannel {
final class ServerSocketChannel : BaseSocketChannel {
private var backlog: Int32 = 128
private let group: EventLoopGroup
public init(eventLoop: EventLoop, group: EventLoopGroup) throws {
init(eventLoop: SelectableEventLoop, group: EventLoopGroup) throws {
let serverSocket = try ServerSocket()
do {
try serverSocket.setNonBlocking()
@@ -462,7 +462,7 @@ final public class ServerSocketChannel : BaseSocketChannel {
override fileprivate func readFromSocket() throws -> Any? {
if let accepted = try (self.socket as! ServerSocket).accept() {
do {
return try SocketChannel(socket: accepted, eventLoop: group.next())
return try SocketChannel(socket: accepted, eventLoop: group.next() as! SelectableEventLoop)
} catch let err {
let _ = try? accepted.close()
throw err
@@ -476,7 +476,7 @@ final public class ServerSocketChannel : BaseSocketChannel {
return true
}
override public func channelRead(data: Any) {
override public func channelRead0(data: Any) {
assert(eventLoop.inEventLoop)
if let ch = data as? Channel {
@@ -492,7 +492,10 @@ final public class ServerSocketChannel : BaseSocketChannel {
}
public protocol ChannelCore {
/*
All methods must be called from the EventLoop thread
*/
public protocol ChannelCore : class{
func register0(promise: Promise<Void>)
func bind0(local: SocketAddress, promise: Promise<Void>)
func connect0(remote: SocketAddress, promise: Promise<Void>)
@@ -502,8 +505,9 @@ public protocol ChannelCore {
func startReading0()
func stopReading0()
func close0(promise: Promise<Void>, error: Error)
var closePromise: Promise<Void> { get }
func channelRead0(data: Any)
var closed: Bool { get }
var eventLoop: EventLoop { get }
}
extension ChannelCore {
@@ -512,6 +516,9 @@ extension ChannelCore {
}
}
/*
All methods exposed by Channel are thread-safe
*/
public protocol Channel : class, ChannelOutboundInvoker {
var allocator: ByteBufferAllocator { get }
@@ -521,14 +528,12 @@ public protocol Channel : class, ChannelOutboundInvoker {
var localAddress: SocketAddress? { get }
var remoteAddress: SocketAddress? { get }
func setOption<T: ChannelOption>(option: T, value: T.OptionType) throws
func getOption<T: ChannelOption>(option: T) throws -> T.OptionType
@discardableResult func bind(local: SocketAddress, promise: Promise<Void>) -> Future<Void>
@discardableResult func connect(remote: SocketAddress, promise: Promise<Void>) -> Future<Void>
func read()
func channelRead(data: Any)
@discardableResult func write(data: Any, promise: Promise<Void>) -> Future<Void>
func flush()
@discardableResult func writeAndFlush(data: Any, promise: Promise<Void>) -> Future<Void>
@@ -546,7 +551,13 @@ protocol SelectableChannel : Channel {
}
extension Channel {
var closed: Bool { return _unsafe.closed }
public var open: Bool {
return !closeFuture.fulfilled
}
var eventLoop: EventLoop {
return _unsafe.eventLoop
}
@discardableResult public func bind(local: SocketAddress, promise: Promise<Void>) -> Future<Void> {
return pipeline.bind(local: local, promise: promise)
@@ -585,17 +596,12 @@ extension Channel {
}
}
/*
All operations on Channel are thread-safe
*/
public class BaseSocketChannel : SelectableChannel, ChannelCore {
class BaseSocketChannel : SelectableChannel, ChannelCore {
public var selectable: Selectable { return socket }
public var _unsafe: ChannelCore { return self }
public let eventLoop: EventLoop
// Visible to access from EventLoop directly
let socket: BaseSocket
public var interestedEvent: InterestedEvent = .none
@@ -605,16 +611,18 @@ public class BaseSocketChannel : SelectableChannel, ChannelCore {
private var readPending = false
private var neverRegistered = true
private var pendingConnect: Promise<Void>?
public let closePromise: Promise<Void>
private let closePromise: Promise<Void>
public var closeFuture: Future<Void> {
return closePromise.futureResult
}
public var open: Bool {
return !closeFuture.fulfilled
private let selectableEventLoop: SelectableEventLoop
public var eventLoop: EventLoop {
return selectableEventLoop
}
public var allocator: ByteBufferAllocator {
if eventLoop.inEventLoop {
return bufferAllocator
@@ -628,8 +636,13 @@ public class BaseSocketChannel : SelectableChannel, ChannelCore {
fileprivate var autoRead: Bool = true
fileprivate var maxMessagesPerRead: UInt = 1
public lazy var pipeline: ChannelPipeline! = ChannelPipeline(channel: self)
// We don't use lazy var here as this is more expensive then doing this :/
public var pipeline: ChannelPipeline {
return _pipeline
}
private var _pipeline: ChannelPipeline!
public func setOption<T: ChannelOption>(option: T, value: T.OptionType) throws {
if eventLoop.inEventLoop {
try setOption0(option: option, value: value)
@@ -718,6 +731,10 @@ public class BaseSocketChannel : SelectableChannel, ChannelCore {
}
}
@discardableResult public func register(promise: Promise<Void>) -> Future<Void> {
return pipeline.register(promise: promise)
}
@discardableResult public func bind(local: SocketAddress, promise: Promise<Void>) -> Future<Void> {
return pipeline.bind(local: local, promise: promise)
}
@@ -893,10 +910,6 @@ public class BaseSocketChannel : SelectableChannel, ChannelCore {
}
}
public func register(promise: Promise<Void>) -> Future<Void> {
return pipeline.register(promise: promise)
}
// Methods invoked from the EventLoop itself
public func writable() {
@@ -1020,7 +1033,7 @@ public class BaseSocketChannel : SelectableChannel, ChannelCore {
fatalError("this must be overridden by sub class")
}
public func channelRead(data: Any) {
public func channelRead0(data: Any) {
// Do nothing by default
}
@@ -1032,7 +1045,7 @@ public class BaseSocketChannel : SelectableChannel, ChannelCore {
private func safeDeregister() {
interestedEvent = .none
do {
try eventLoop.deregister(channel: self)
try selectableEventLoop.deregister(channel: self)
} catch let err {
pipeline.fireErrorCaught0(error: err)
close0(error: err)
@@ -1046,7 +1059,7 @@ public class BaseSocketChannel : SelectableChannel, ChannelCore {
}
interestedEvent = interested
do {
try eventLoop.reregister(channel: self)
try selectableEventLoop.reregister(channel: self)
} catch let err {
pipeline.fireErrorCaught0(error: err)
close0(error: err)
@@ -1061,7 +1074,7 @@ public class BaseSocketChannel : SelectableChannel, ChannelCore {
}
interestedEvent = interested
do {
try eventLoop.register(channel: self)
try selectableEventLoop.register(channel: self)
return true
} catch let err {
pipeline.fireErrorCaught0(error: err)
@@ -1098,9 +1111,9 @@ public class BaseSocketChannel : SelectableChannel, ChannelCore {
return true
}
fileprivate init(socket: BaseSocket, eventLoop: EventLoop) {
fileprivate init(socket: BaseSocket, eventLoop: SelectableEventLoop) {
self.socket = socket
self.eventLoop = eventLoop
self.selectableEventLoop = eventLoop
self.closePromise = eventLoop.newPromise(type: Void.self)
self._pipeline = ChannelPipeline(channel: self)
}
+1 -1
View File
@@ -523,7 +523,7 @@ private class TailChannelHandler : ChannelHandler {
}
func channelRead(ctx: ChannelHandlerContext, data: Any) {
ctx.channel!.channelRead(data: data)
ctx.channel!._unsafe.channelRead0(data: data)
}
}
+12 -16
View File
@@ -26,18 +26,6 @@ class EmbeddedEventLoop : EventLoop {
// Would be better to have this as a Queue
var tasks: [() -> ()] = Array()
func register(channel: Channel) throws {
let _ = channel.register()
}
func deregister(channel: Channel) throws {
// Doesn't apply
}
func reregister(channel: Channel) throws {
// Doesn't apply
}
// We're not really running a loop here. Tasks aren't run until run() is called,
// at which point we run everything that's been submitted. Anything newly submitted
// either gets on that train if it's still moving or
@@ -72,6 +60,8 @@ class EmbeddedChannelCore : ChannelCore {
var closed: Bool { return closePromise.futureResult.fulfilled }
var closePromise: Promise<Void> = Promise<Void>()
var eventLoop: EventLoop = EmbeddedEventLoop()
deinit { closePromise.succeed(result: ()) }
@@ -111,12 +101,20 @@ class EmbeddedChannelCore : ChannelCore {
func readIfNeeded0() {
}
func channelRead0(data: Any) {
}
}
public class EmbeddedChannel : Channel {
public var closeFuture: Future<Void> { return _unsafe.closePromise.futureResult }
public var closeFuture: Future<Void> { return channelcore.closePromise.futureResult }
public var _unsafe: ChannelCore = EmbeddedChannelCore()
private let channelcore: EmbeddedChannelCore = EmbeddedChannelCore()
public var _unsafe: ChannelCore {
return channelcore
}
public var pipeline: ChannelPipeline {
return _pipeline
}
@@ -144,6 +142,4 @@ public class EmbeddedChannel : Channel {
public func read() {
pipeline.read()
}
public func channelRead(data: Any) { }
}
+17 -32
View File
@@ -17,17 +17,10 @@ import Future
import Sockets
import ConcurrencyHelpers
public protocol EventLoop {
public protocol EventLoop: EventLoopGroup {
var inEventLoop: Bool { get }
func register(channel: Channel) throws
func deregister(channel: Channel) throws
func reregister(channel: Channel) throws
func execute(task: @escaping () -> ())
func submit<T>(task: @escaping () throws-> (T)) -> Future<T>
func run() throws
func close() throws
}
extension EventLoop {
@@ -60,11 +53,19 @@ extension EventLoop {
promise.succeed(result: result)
return promise.futureResult
}
public func next() -> EventLoop {
return self
}
public func close() throws {
// Do nothing
}
}
// TODO: Implement scheduling tasks in the future (a.k.a ScheduledExecutoreService
final class SelectableEventLoop : EventLoop, EventLoopGroup {
final class SelectableEventLoop : EventLoop {
private let selector: Sockets.Selector
private var thread: Thread?
private var tasks: [() -> ()]
@@ -81,27 +82,15 @@ final class SelectableEventLoop : EventLoop, EventLoopGroup {
try selector.register(selectable: channel.selectable, interested: channel.interestedEvent, attachment: channel)
}
func register(channel: Channel) throws {
try register(channel: channel as! SelectableChannel)
}
func deregister(channel: SelectableChannel) throws {
assert(inEventLoop)
try selector.deregister(selectable: channel.selectable)
}
func deregister(channel: Channel) throws {
try deregister(channel: channel as! SelectableChannel)
}
func reregister(channel: SelectableChannel) throws {
assert(inEventLoop)
try selector.reregister(selectable: channel.selectable, interested: channel.interestedEvent)
}
func reregister(channel: Channel) throws {
try reregister(channel: channel as! SelectableChannel)
}
var inEventLoop: Bool {
return Thread.current.isEqual(thread)
@@ -151,7 +140,7 @@ final class SelectableEventLoop : EventLoop, EventLoopGroup {
}
// Ensure we never reach here if the channel is not open anymore.
assert(!channel.closed)
assert(!channel._unsafe.closed)
}
// TODO: Better locking
@@ -166,8 +155,8 @@ final class SelectableEventLoop : EventLoop, EventLoopGroup {
}
}
private func handleEvents(_ channel: Channel) -> Bool {
if !channel.closed {
private func handleEvents(_ channel: SelectableChannel) -> Bool {
if !channel._unsafe.closed {
return true
}
do {
@@ -179,7 +168,7 @@ final class SelectableEventLoop : EventLoop, EventLoopGroup {
return false
}
func close() throws {
func close0() throws {
if inEventLoop {
try self.selector.close()
} else {
@@ -189,10 +178,6 @@ final class SelectableEventLoop : EventLoop, EventLoopGroup {
}).wait()
}
}
func next() -> EventLoop {
return self
}
}
/**
@@ -212,10 +197,10 @@ final public class MultiThreadedEventLoopGroup : EventLoopGroup {
private let index = Atomic<Int>(value: 0)
private let eventLoops: [EventLoop]
private let eventLoops: [SelectableEventLoop]
public init(numThreads: Int) throws {
var loops: [EventLoop] = []
var loops: [SelectableEventLoop] = []
for _ in 0..<numThreads {
let loop = try SelectableEventLoop()
loops.append(loop)
@@ -246,7 +231,7 @@ final public class MultiThreadedEventLoopGroup : EventLoopGroup {
public func close() throws {
for loop in eventLoops {
// TODO: Should we log this somehow or just rethrow the first error ?
_ = try loop.close()
_ = try loop.close0()
}
}
}