diff --git a/.travis.yml b/.travis.yml index e0ba9f8..2cb0052 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,15 +4,14 @@ env: matrix: include: - script: - - xcodebuild test -project $MODULE_NAME.xcodeproj -scheme $MODULE_NAME + - xcodebuild clean test -project $MODULE_NAME.xcodeproj -scheme $MODULE_NAME | xcpretty --color && exit ${PIPESTATUS[0]} os: osx - osx_image: xcode7.3 + osx_image: xcode8.1 language: objective-c before_install: - brew update - brew outdated carthage || brew upgrade carthage - brew install libuv - # - brew outdated xctool || brew upgrade xctool before_script: # bootstrap the dependencies for the project # you can remove if you don't have dependencies @@ -20,7 +19,6 @@ matrix: before_deploy: - carthage build --no-skip-current - carthage archive $MODULE_NAME - #- pod trunk push CrossroadRegex.podspec deploy: provider: releases api_key: @@ -31,7 +29,7 @@ matrix: repo: reactive-swift/UV tags: true - script: - - ./build test + - swift build && swift test sudo: required dist: trusty language: generic @@ -39,18 +37,17 @@ matrix: # install original swift distribution - wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import - - cd .. - - export SWIFT_VERSION="swift-DEVELOPMENT-SNAPSHOT-2016-03-24-a" - - wget https://swift.org/builds/development/ubuntu1404/$SWIFT_VERSION/$SWIFT_VERSION-ubuntu14.04.tar.gz - - tar xzf $SWIFT_VERSION-ubuntu14.04.tar.gz - - export PATH="${PWD}/${SWIFT_VERSION}-ubuntu14.04/usr/bin:${PATH}" + - export SWIFT_VERSION="swift-3.0.1-release" + - SWIFT_VERSION_UPPER=`echo $SWIFT_VERSION | perl -n -e'/^(.*?)-([\.\d]*?)-(.*)$/; $code = uc $3; print "$1-$2-${code}"'` + - wget https://swift.org/builds/$SWIFT_VERSION/ubuntu1404/$SWIFT_VERSION_UPPER/$SWIFT_VERSION_UPPER-ubuntu14.04.tar.gz + - tar xzf $SWIFT_VERSION_UPPER-ubuntu14.04.tar.gz + - export PATH="${PWD}/${SWIFT_VERSION_UPPER}-ubuntu14.04/usr/bin:${PATH}" + - export LD_LIBRARY_PATH="${PWD}/${SWIFT_VERSION_UPPER}-ubuntu14.04/usr/lib/swift/linux/:$LD_LIBRARY_PATH" # libuv v1 - sudo add-apt-repository ppa:swiftexpress/swiftexpress --yes - sudo apt-get update - sudo apt-get install libuv1-dev # get back home - cd $MODULE_NAME - # get crossroad build script - - wget https://raw.githubusercontent.com/crossroadlabs/utils/master/build - - chmod +x build notifications: email: false diff --git a/Cartfile b/Cartfile index d8bfdef..dd6905b 100644 --- a/Cartfile +++ b/Cartfile @@ -1,2 +1,2 @@ -github "reactive-swift/CUV" -github "crossroadlabs/Boilerplate" "master" +github "reactive-swift/CUV" ~> 1.0 +github "crossroadlabs/Boilerplate" ~> 1.0 diff --git a/Package.swift b/Package.swift index d68517d..7800e61 100644 --- a/Package.swift +++ b/Package.swift @@ -18,9 +18,8 @@ import PackageDescription let package = Package( name: "UV", - targets: [Target(name: "UV")], dependencies: [ .Package(url: "https://github.com/reactive-swift/CUV.git", majorVersion: 0, minor: 1), - .Package(url: "https://github.com/crossroadlabs/Boilerplate.git", majorVersion: 0, minor: 2) + .Package(url: "https://github.com/crossroadlabs/Boilerplate.git", Version(1, 0, 0, prereleaseIdentifiers: ["alpha", "2"])), ] ) diff --git a/UV/Async.swift b/Sources/UV/Async.swift similarity index 87% rename from UV/Async.swift rename to Sources/UV/Async.swift index 9ec4b1f..02976bf 100644 --- a/UV/Async.swift +++ b/Sources/UV/Async.swift @@ -21,7 +21,7 @@ public typealias uv_async_p = UnsafeMutablePointer public typealias AsyncCallback = (Async) -> Void -open class Async : Handle { +public class Async : Handle { fileprivate let callback:AsyncCallback public init(loop:Loop, callback:@escaping AsyncCallback) throws { @@ -33,18 +33,15 @@ open class Async : Handle { /// uv_async_send /// the only thread safe function in this lib - open func send() { + public func send() { if !handle.isNil { uv_async_send(handle.portable) } } } -private func _async_cb(_ handle:uv_async_p?) { +private func async_cb(handle:uv_async_p?) { let async:Async = Async.from(handle:handle) async.callback(async) } - private func async_cb(handle:uv_async_p?) { - return _async_cb(handle) - } diff --git a/UV/Error.swift b/Sources/UV/Error.swift similarity index 86% rename from UV/Error.swift rename to Sources/UV/Error.swift index 6ef86cf..7191569 100644 --- a/UV/Error.swift +++ b/Sources/UV/Error.swift @@ -18,12 +18,12 @@ import CUV import Boilerplate //TODO: make enum -public enum Error : Swift.Error { +public enum UVError : Error { case withCode(code:Int32) case handleClosed } -extension Error : ErrorWithCodeType { +extension UVError : ErrorWithCodeType { public init(code:Int32) { self = .withCode(code: code) } @@ -31,13 +31,13 @@ extension Error : ErrorWithCodeType { public static func isError(_ code:Int32) -> Bool { return code < 0 } - - public static func error(code:Int32) -> Error? { - return isError(code) ? Error(code: code) : nil + + public static func error(code:Int32) -> UVError? { + return isError(code) ? UVError(code: code) : nil } } -public extension Error { +public extension UVError { public var name:String { get { switch self { @@ -50,7 +50,7 @@ public extension Error { } } -extension Error : CustomStringConvertible { +extension UVError : CustomStringConvertible { public var description: String { get { switch self { diff --git a/UV/Handle.swift b/Sources/UV/Handle.swift similarity index 80% rename from UV/Handle.swift rename to Sources/UV/Handle.swift index 0f29b12..9167926 100644 --- a/UV/Handle.swift +++ b/Sources/UV/Handle.swift @@ -15,7 +15,6 @@ //===-----------------------------------------------------------------------===// import Boilerplate - import CUV public protocol uv_handle_type { @@ -25,48 +24,10 @@ public protocol uv_handle_type { var isNil:Bool {get} func testNil() throws -#if swift(>=3.0) -#else - mutating func nullify() -#endif static func alloc() -> Self mutating func dealloc() } - -//extension UnsafePointer : uv_handle_type { -// public func cast() -> UnsafeMutablePointer { -// return self.withMemoryRebound(to: T.self, capacity: 1) { pointer in -// UnsafeMutablePointer(mutating: pointer) -// } -// } -// -// public func cast() -> UnsafePointer { -// return self.withMemoryRebound(to: T.self, capacity: 1) { pointer in -// pointer -// } -// } -// -// public var isNil:Bool { -// return self == .null -// } -//// -// public func testNil() throws { -// if isNil { -// throw Error.handleClosed -// } -// } -// -// public static func alloc() -> UnsafeMutablePointer { -// return UnsafeMutablePointer.allocate(capacity: 1) -// } -//// -//// mutating public func dealloc() { -//// self.deinitialize(count: 1) -//// self.deallocate(capacity: 1) -//// } -//} - extension UnsafeMutablePointer : uv_handle_type { public func cast() -> UnsafeMutablePointer { return self.withMemoryRebound(to: T.self, capacity: 1) { pointer in @@ -81,23 +42,25 @@ extension UnsafeMutablePointer : uv_handle_type { } public var isNil:Bool { - return self == .null + return self == nil } public func testNil() throws { if isNil { - throw Error.handleClosed + throw UVError.handleClosed } } public static func alloc() -> UnsafeMutablePointer { - return UnsafeMutablePointer.allocate(capacity: 1) } + return UnsafeMutablePointer.allocate(capacity: 1) + } mutating public func dealloc() { self.deinitialize(count: 1) self.deallocate(capacity: 1) } } + extension Optional where Wrapped : uv_handle_type { public mutating func nullify() { self = nil @@ -109,7 +72,7 @@ extension Optional where Wrapped : uv_handle_type { public func testNil() throws { if isNil { - throw Error.handleClosed + throw UVError.handleClosed } } @@ -135,7 +98,7 @@ protocol PropertyType { extension PropertyType { static func read(from object:Object) throws -> Type { - return try ccall(Error.self) { code in + return try ccall(UVError.self) { code in var value:Type = getterValue code = function(object, &value) return value @@ -144,7 +107,7 @@ extension PropertyType { static func write(to object:Object, value:Type) throws { var value:Type = value - try ccall(Error.self) { + try ccall(UVError.self) { function(object, &value) } } @@ -159,7 +122,7 @@ extension PropertyType { if let value = value { do { try write(to: object, value: value) - } catch let e as Error { + } catch let e as UVError { print(e.description) } catch { print("Unknown error occured while setting ", name) @@ -233,10 +196,10 @@ public protocol HandleType : AnyObject { var fileno:uv_os_fd_t? {get} } -open class HandleBase { +public class HandleBase { fileprivate var _baseHandle:uv_handle_p? - open var baseHandle:uv_handle_p? { + public var baseHandle:uv_handle_p? { get { if _baseHandle == .null { _baseHandle = getBaseHandle() @@ -257,8 +220,8 @@ open class HandleBase { } } -open class Handle : HandleBase, HandleType { - open var handle:Type? +public class Handle : HandleBase, HandleType { + public var handle:Type? override func getBaseHandle() -> uv_handle_p? { return self.handle.map({$0.cast()}) @@ -274,10 +237,9 @@ open class Handle : HandleBase, HandleType { super.init() do { - try ccall(Error.self) { + try ccall(UVError.self) { initializer(self.handle) } - baseHandle?.pointee.data = Unmanaged.passRetained(self).toOpaque() } catch let e { //cleanum if not created @@ -286,6 +248,7 @@ open class Handle : HandleBase, HandleType { } } + fileprivate static func doWith(handle: Handle?, fun:(Handle) throws -> Ret) throws -> Ret { try handle.testNil() return try fun(handle!) @@ -299,7 +262,7 @@ open class Handle : HandleBase, HandleType { return try Handle.doWith(handle: handle, fun: fun) } - open var loop:Loop? { + public var loop:Loop? { get { return try? doWithBaseHandle { handle in Loop(loop: handle.pointee.loop) @@ -307,38 +270,38 @@ open class Handle : HandleBase, HandleType { } } - open var active:Bool { + public var active:Bool { get { return !baseHandle.isNil && uv_is_active(baseHandle!) != 0 } } - open var closing:Bool { + public var closing:Bool { get { return baseHandle.isNil || uv_is_closing(baseHandle!) != 0 } } //uv_close - open func close() { + public func close() { if !baseHandle.isNil { uv_close(baseHandle!, handle_close_cb) } } - open func ref() throws { + public func ref() throws { try doWithBaseHandle { _ in uv_ref(self.baseHandle.portable) } } - open func unref() throws { + public func unref() throws { try doWithBaseHandle { _ in uv_unref(self.baseHandle.portable) } } - open var referenced:Bool { + public var referenced:Bool { get { return !baseHandle.isNil && uv_has_ref(baseHandle.portable) != 0 } @@ -347,19 +310,19 @@ open class Handle : HandleBase, HandleType { //uv_handle_size //present, because properties can not throw. So both ways - open func getSendBufferSize() throws -> Int32 { + public func getSendBufferSize() throws -> Int32 { return try doWithBaseHandle { handle in try SendBufferSizeProperty.read(from: handle) } } - open func setSendBufferSize(_ size:Int32) throws { + public func setSendBufferSize(_ size:Int32) throws { try doWithBaseHandle { handle in try SendBufferSizeProperty.write(to: handle, value: size) } } - open var sendBufferSize:Int32? { + public var sendBufferSize:Int32? { get { return try? doWithBaseHandle { handle in return try SendBufferSizeProperty.read(from: handle) @@ -376,19 +339,19 @@ open class Handle : HandleBase, HandleType { } //present, because properties can not throw. So both ways - open func getRecvBufferSize() throws -> Int32 { + public func getRecvBufferSize() throws -> Int32 { return try doWithBaseHandle { handle in try RecvBufferSizeProperty.read(from: handle) } } - open func setRecvBufferSize(_ size:Int32) throws { + public func setRecvBufferSize(_ size:Int32) throws { try doWithBaseHandle { handle in try RecvBufferSizeProperty.write(to: handle, value: size) } } - open var recvBufferSize:Int32? { + public var recvBufferSize:Int32? { get { return try? doWithBaseHandle { handle in return try RecvBufferSizeProperty.read(from: handle) @@ -405,9 +368,9 @@ open class Handle : HandleBase, HandleType { } //present, because properties can not throw. So both ways - open func getFileno() throws -> uv_os_fd_t { + public func getFileno() throws -> uv_os_fd_t { return try doWithBaseHandle { handle in - try ccall(Error.self) { code in + try ccall(UVError.self) { code in var fileno = uv_os_fd_t() code = uv_fileno(handle, &fileno) return fileno @@ -415,7 +378,7 @@ open class Handle : HandleBase, HandleType { } } - open var fileno:uv_os_fd_t? { + public var fileno:uv_os_fd_t? { get { return try? getFileno() } @@ -429,8 +392,8 @@ extension HandleType { } } -private func _handle_close_cb(_ handle:uv_handle_p?) { - guard let handle = handle , handle != .null else { +private func handle_close_cb(_ handle:uv_handle_p?) { + guard let handle = handle else { return } @@ -443,8 +406,3 @@ private func _handle_close_cb(_ handle:uv_handle_p?) { handle.deinitialize(count: 1) handle.deallocate(capacity: 1) } - -private func handle_close_cb(handle:uv_handle_p?) { - _handle_close_cb(handle) -} - diff --git a/UV/Info.plist b/Sources/UV/Info.plist similarity index 100% rename from UV/Info.plist rename to Sources/UV/Info.plist diff --git a/UV/Loop.swift b/Sources/UV/Loop.swift similarity index 78% rename from UV/Loop.swift rename to Sources/UV/Loop.swift index 7f9b925..6be8be7 100644 --- a/UV/Loop.swift +++ b/Sources/UV/Loop.swift @@ -19,9 +19,9 @@ import Boilerplate public typealias uv_loop_p = UnsafeMutablePointer -open class Loop { +public class Loop { fileprivate let exclusive:Bool - open let loop:uv_loop_p + public let loop:uv_loop_p public init(loop:uv_loop_p) { self.loop = loop @@ -31,7 +31,7 @@ open class Loop { public init() throws { loop = uv_loop_p.allocate(capacity: 1) exclusive = true - try ccall(Error.self) { + try ccall(UVError.self) { uv_loop_init(loop) } } @@ -44,7 +44,7 @@ open class Loop { } do { try close() - } catch let e as Error { + } catch let e as UVError { print(e.description) } catch { print("Unknown error occured while destroying the loop") @@ -52,43 +52,37 @@ open class Loop { } } - open static var defaultLoop: Loop { + public static var defaultLoop: Loop { get { return Loop(loop: uv_default_loop()) } } /*public func configure(option:uv_loop_option, _ args: CVarArgType...) { - try Error.handle { + try UVError.handle { uv_loop_configure(loop, option, getVaList(args)) } }*/ fileprivate func close() throws { - try ccall(Error.self) { + try ccall(UVError.self) { uv_loop_close(loop) } } /// returns true if no more handles are there -#if swift(>=3.0) @discardableResult public func run(inMode mode:uv_run_mode = UV_RUN_DEFAULT) -> Bool { return uv_run(loop, mode) == 0 } -#else - open func run(inMode mode:uv_run_mode = UV_RUN_DEFAULT) -> Bool { - return uv_run(loop, mode) == 0 - } -#endif - open var alive:Bool { + public var alive:Bool { get { return uv_loop_alive(loop) != 0 } } - open func stop() { + public func stop() { uv_stop(loop) } @@ -96,7 +90,7 @@ open class Loop { return UInt64(uv_loop_size()) } - open var backendFd:Int32 { + public var backendFd:Int32 { get { return uv_backend_fd(loop) } @@ -104,30 +98,30 @@ open class Loop { //in millisec //wierd thing, doc says it should return -1 on no timeout, in fact - 0. Leaving as is for now. Subject to investigate - open var backendTimeout:Int32? { + public var backendTimeout:Int32? { get { let timeout = uv_backend_timeout(loop) return timeout == -1 ? nil : timeout } } - open var now:UInt64 { + public var now:UInt64 { get { return uv_now(loop) } } - open func updateTime() { + public func updateTime() { uv_update_time(loop) } - open func walk(_ f:LoopWalkCallback) { + public func walk(_ f:LoopWalkCallback) { let container = AnyContainer(f) let arg = UnsafeMutableRawPointer(Unmanaged.passUnretained(container).toOpaque()) uv_walk(loop, loop_walker, arg) } - open var handles:Array { + public var handles:Array { get { return Array(enumerator: walk) } @@ -136,24 +130,21 @@ open class Loop { public typealias LoopWalkCallback = (HandleType)->Void -private func _loop_walker(_ handle:uv_handle_p?, arg:UnsafeMutableRawPointer?) { - guard let arg = arg , arg != .null else { +private func loop_walker(_ handle:uv_handle_p?, arg:UnsafeMutableRawPointer?) { + guard let arg = arg else { return } - guard let handle = handle , handle != .null else { + guard let handle = handle else { return } + let container = Unmanaged>.fromOpaque(arg).takeUnretainedValue() let callback = container.content let handleObject:Handle = Handle.from(handle: handle) callback(handleObject) } -private func loop_walker(handle:uv_handle_p?, arg:UnsafeMutableRawPointer?) { - _loop_walker(handle, arg: arg) -} - extension Loop : Equatable { } diff --git a/UV/Prepare.swift b/Sources/UV/Prepare.swift similarity index 83% rename from UV/Prepare.swift rename to Sources/UV/Prepare.swift index 3cec721..dc6c411 100644 --- a/UV/Prepare.swift +++ b/Sources/UV/Prepare.swift @@ -22,7 +22,7 @@ public typealias uv_prepare_p = UnsafeMutablePointer public typealias PrepareCallback = (Prepare) -> Void -open class Prepare : Handle { +public class Prepare : Handle { fileprivate let callback:PrepareCallback public init(loop:Loop, callback:@escaping PrepareCallback) throws { @@ -32,33 +32,28 @@ open class Prepare : Handle { } } - open func start() throws { + public func start() throws { try doWithHandle { handle in - try ccall(Error.self) { + try ccall(UVError.self) { uv_prepare_start(handle, prepare_cb) } } } - open func stop() throws { + public func stop() throws { try doWithHandle { handle in - try ccall(Error.self) { + try ccall(UVError.self) { uv_prepare_stop(handle) } } } } -private func _prepare_cb(_ handle:uv_prepare_p?) { - guard let handle = handle , handle != .null else { +private func prepare_cb(_ handle:uv_prepare_p?) { + guard let handle = handle else { return } let prepare:Prepare = Prepare.from(handle: handle) prepare.callback(prepare) } - - -private func prepare_cb(_ handle:uv_prepare_p?) { - _prepare_cb(handle) -} diff --git a/UV/Request.swift b/Sources/UV/Request.swift similarity index 86% rename from UV/Request.swift rename to Sources/UV/Request.swift index 0d3c60b..aecc35a 100644 --- a/UV/Request.swift +++ b/Sources/UV/Request.swift @@ -52,11 +52,11 @@ extension uv_req_t : uv_request_type { } public protocol RequestCallbackCaller { - associatedtype RequestCallback = (Self, Error?)->Void + associatedtype RequestCallback = (Self, UVError?)->Void } -open class Request : RequestCallbackCaller { - public typealias RequestCallback = (Request, Error?)->Void +public class Request : RequestCallbackCaller { + public typealias RequestCallback = (Request, UVError?)->Void internal let _req:UnsafeMutablePointer fileprivate let _baseReq:UnsafeMutablePointer @@ -89,19 +89,19 @@ open class Request : RequestCallbackCaller { } fileprivate func call(result status:Int32) { - _callback(self, Error.error(code: status)) + _callback(self, UVError.error(code: status)) } - open func cancel() throws { - try ccall(Error.self) { + public func cancel() throws { + try ccall(UVError.self) { uv_cancel(_baseReq) } } - open static func perform(callback:@escaping RequestCallback, action:(UnsafeMutablePointer)->Int32) { + public static func perform(callback:@escaping RequestCallback, action:(UnsafeMutablePointer)->Int32) { let req = Request(callback) - if let error = Error.error(code: action(req.pointer)) { + if let error = UVError.error(code: action(req.pointer)) { callback(req, error) return } @@ -111,7 +111,7 @@ open class Request : RequestCallbackCaller { } internal func req_cb(_ req:UnsafeMutablePointer?, status:Int32) { - guard let req = req , req != .null else { + guard let req = req else { return } diff --git a/UV/Stream.swift b/Sources/UV/Stream.swift similarity index 73% rename from UV/Stream.swift rename to Sources/UV/Stream.swift index 18a7068..b705d07 100644 --- a/UV/Stream.swift +++ b/Sources/UV/Stream.swift @@ -107,13 +107,13 @@ private struct UVData : DataProtocol { } } -open class Data : DataProtocol { +public class Data : DataProtocol { fileprivate let buffer:uv_buf_t fileprivate let data:[UInt8] - open let buffers:UnsafePointer - open let count:UInt32 = 1 - open let len:UInt32 + public let buffers:UnsafePointer + public let count:UInt32 = 1 + public let len:UInt32 public init(data:[UInt8]) { self.data = data @@ -130,32 +130,32 @@ open class Data : DataProtocol { self.buffers = withUnsafePointer(to: &self.buffer) {p in p} } - open var array:[UInt8] { + public var array:[UInt8] { return data } - open func destroy() { + public func destroy() { } } public protocol ReadCallbackCaller { - associatedtype ReadCallback = (Self, Result)->Void + associatedtype ReadCallback = (Self, Result)->Void } -open class ShutdownRequest : Request { +public class ShutdownRequest : Request { } -open class WriteRequest : Request { +public class WriteRequest : Request { } internal protocol StreamProtocol : ReadCallbackCaller { func fresh(on loop:Loop, readCallback:Self.ReadCallback) throws -> Self } -open class Stream : Handle, SimpleCallbackCaller, ReadCallbackCaller, StreamProtocol { +public class Stream : Handle, SimpleCallbackCaller, ReadCallbackCaller, StreamProtocol { public typealias SimpleCallback = (Stream)->Void - public typealias ReadCallback = (Stream, Result)->Void + public typealias ReadCallback = (Stream, Result)->Void fileprivate lazy var streamHandle:uv_stream_p? = self.getStreamHandle() @@ -176,40 +176,40 @@ open class Stream : Handle, SimpleCallbackCaller, R try super.init(initializer) } - open func shutdown(callback fun:@escaping ShutdownRequest.RequestCallback = {_,_ in}) { + public func shutdown(callback fun:@escaping ShutdownRequest.RequestCallback = {_,_ in}) { ShutdownRequest.perform(callback: fun) { req in uv_shutdown(req, self.streamHandle.portable, shutdown_cb) } } - open func listen(backlog:Int32) throws { - try ccall(Error.self) { + public func listen(backlog:Int32) throws { + try ccall(UVError.self) { uv_listen(streamHandle.portable, backlog, connection_cb) } } - open func accept(readCallback fun:@escaping ReadCallback = {_,_ in}) throws -> Self { + public func accept(readCallback fun:@escaping ReadCallback = {_,_ in}) throws -> Self { let new = try self.fresh(on: loop!, readCallback: fun) - try ccall(Error.self) { + try ccall(UVError.self) { uv_accept(self.streamHandle.portable, new.streamHandle.portable) } return new } - - open func startReading() throws { - try ccall(Error.self) { + + public func startReading() throws { + try ccall(UVError.self) { uv_read_start(self.streamHandle.portable, alloc_cb, read_cb) } } - open func stopReading() throws { - try ccall(Error.self) { + public func stopReading() throws { + try ccall(UVError.self) { uv_read_stop(self.streamHandle.portable) } } - - open func write(data:DataProtocol, callback: @escaping WriteRequest.RequestCallback) { + + public func write(data:DataProtocol, callback: @escaping WriteRequest.RequestCallback) { WriteRequest.perform(callback: callback) { preq in let buffers = data.buffers var new:[uv_buf_t] = [] @@ -239,20 +239,20 @@ open class Stream : Handle, SimpleCallbackCaller, R } } - open func tryWrite(data:DataProtocol) -> Int? { + public func tryWrite(data:DataProtocol) -> Int? { let written = Int(uv_try_write(streamHandle.portable, data.buffers, data.count)) return written > 0 ? written : nil } } -private func _read_cb(_ stream:uv_stream_p?, nread:ssize_t, bufp:UnsafePointer?) { +private func read_cb(stream:uv_stream_p?, nread:ssize_t, bufp:UnsafePointer?) { //just skip it. No data. Optimization if nread == 0 { return } - let e = Error.error(code: Int32(nread)) - let result:Result = e.map { e in + let e = UVError.error(code: Int32(nread)) + let result:Result = e.map { e in Result(error: e) }.getOr { let data = UVData(size: nread, buffers: bufp!) @@ -268,49 +268,21 @@ private func _read_cb(_ stream:uv_stream_p?, nread:ssize_t, bufp:UnsafePointer?) { + buf?.pointee.base = UnsafeMutablePointer.allocate(capacity: suggestedSize) + buf?.pointee.len = suggestedSize +} - private func read_cb(stream:uv_stream_p?, nread:ssize_t, bufp:UnsafePointer?) { - _read_cb(stream, nread: nread, bufp: bufp) - } - -#if swift(>=3.0) - func alloc_cb(handle:uv_handle_p?, suggestedSize:size_t, buf:UnsafeMutablePointer?) { - buf?.pointee.base = UnsafeMutablePointer.allocate(capacity: suggestedSize) - buf?.pointee.len = suggestedSize - } -#else - func alloc_cb(_ handle:uv_handle_p, suggestedSize:size_t, buf:UnsafeMutablePointer) { - buf.pointee.base = UnsafeMutablePointer(allocatingCapacity: suggestedSize) - buf.pointee.len = suggestedSize - } -#endif - -#if swift(>=3.0) - func write_cb(req:UnsafeMutablePointer?, status:Int32) { - req_cb(req, status: status) - } -#else - func write_cb(_ req:UnsafeMutablePointer, status:Int32) { - req_cb(req, status: status) - } -#endif +private func write_cb(req:UnsafeMutablePointer?, status:Int32) { + req_cb(req, status: status) +} -#if swift(>=3.0) - func shutdown_cb(req:UnsafeMutablePointer?, status:Int32) { - req_cb(req, status: status) - } -#else - func shutdown_cb(_ req:UnsafeMutablePointer, status:Int32) { - req_cb(req, status: status) - } -#endif +private func shutdown_cb(req:UnsafeMutablePointer?, status:Int32) { + req_cb(req, status: status) +} -private func _connection_cb(_ server:uv_stream_p?, status:Int32) { +private func connection_cb(server:uv_stream_p?, status:Int32) { let handle:uv_handle_p? = server.map({$0.cast()}) let stream = Stream.from(handle: handle) stream.connectionCallback(stream) } - -private func connection_cb(server:uv_stream_p?, status:Int32) { - _connection_cb(server, status: status) -} diff --git a/UV/TCP.swift b/Sources/UV/TCP.swift similarity index 92% rename from UV/TCP.swift rename to Sources/UV/TCP.swift index 0e845ef..8f4cd6a 100644 --- a/UV/TCP.swift +++ b/Sources/UV/TCP.swift @@ -24,7 +24,7 @@ public typealias uv_tcp_p = UnsafeMutablePointer extension uv_connect_t : uv_request_type { } -open class ConnectRequest : Request { +public class ConnectRequest : Request { } public final class TCP : Stream { @@ -52,7 +52,7 @@ public final class TCP : Stream { public func bind(to addr:UnsafePointer, ipV6only:Bool = false) throws { let flags:UInt32 = ipV6only ? UV_TCP_IPV6ONLY.rawValue : 0 - try ccall(Error.self) { + try ccall(UVError.self) { uv_tcp_bind(handle.portable, addr, flags) } } @@ -64,6 +64,6 @@ public final class TCP : Stream { } } - func connect_cb(req:UnsafeMutablePointer?, status:Int32) { - req_cb(req, status: status) - } +private func connect_cb(req:UnsafeMutablePointer?, status:Int32) { + req_cb(req, status: status) +} diff --git a/UV/Timer.swift b/Sources/UV/Timer.swift similarity index 87% rename from UV/Timer.swift rename to Sources/UV/Timer.swift index bdc38f2..48e1f10 100644 --- a/UV/Timer.swift +++ b/Sources/UV/Timer.swift @@ -21,7 +21,7 @@ public typealias uv_timer_p = UnsafeMutablePointer public typealias TimerCallback = (Timer) -> Void -open class Timer : Handle { +public class Timer : Handle { fileprivate let callback:TimerCallback public init(loop:Loop, callback:@escaping TimerCallback) throws { @@ -32,34 +32,34 @@ open class Timer : Handle { } //uv_timer_start - open func start(timeout:Timeout, repeatTimeout:Timeout? = nil) throws { + public func start(timeout:Timeout, repeatTimeout:Timeout? = nil) throws { try doWithHandle { handle in let repeatTimeout = repeatTimeout ?? .Immediate - try ccall(Error.self) { + try ccall(UVError.self) { uv_timer_start(handle, timer_cb, timeout.uvTimeout, repeatTimeout.uvTimeout) } } } //uv_timer_stop - open func stop() throws { + public func stop() throws { try doWithHandle { handle in - try ccall(Error.self) { + try ccall(UVError.self) { uv_timer_stop(handle) } } } //uv_timer_again - open func again() throws { + public func again() throws { try doWithHandle { handle in - try ccall(Error.self) { + try ccall(UVError.self) { uv_timer_again(handle) } } } - open var repeatTimeout:Timeout { + public var repeatTimeout:Timeout { //uv_timer_get_repeat get { return handle.isNil ? .Immediate : Timeout(uvTimeout: uv_timer_get_repeat(handle.portable)) @@ -74,16 +74,11 @@ open class Timer : Handle { } -private func _timer_cb(handle:uv_timer_p?) { +private func timer_cb(handle:uv_timer_p?) { let timer:Timer = Timer.from(handle: handle) timer.callback(timer) } - -private func timer_cb(handle:uv_timer_p?) { - _timer_cb(handle: handle) -} - extension Timeout { init(uvTimeout:UInt64) { switch uvTimeout { diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 7a98212..4283c91 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -1,8 +1,9 @@ import XCTest -@testable import UVTestSuite +import UVTests -XCTMain([ - testCase(LoopTests.allTests), - testCase(UVTests.allTests), -]) \ No newline at end of file +var tests = [XCTestCaseEntry]() + +tests += UVTests.allTests() + +XCTMain(tests) diff --git a/Tests/UV/Info.plist b/Tests/UVTests/Info.plist similarity index 100% rename from Tests/UV/Info.plist rename to Tests/UVTests/Info.plist diff --git a/Tests/UV/LoopTests.swift b/Tests/UVTests/LoopTests.swift similarity index 97% rename from Tests/UV/LoopTests.swift rename to Tests/UVTests/LoopTests.swift index 19d890f..67f96f6 100644 --- a/Tests/UV/LoopTests.swift +++ b/Tests/UVTests/LoopTests.swift @@ -107,7 +107,7 @@ class LoopTests: XCTestCase { #if os(Linux) extension LoopTests { - static var allTests : [(String, LoopTests -> () throws -> Void)] { + static var allTests : [(String, (LoopTests) -> () throws -> Void)] { return [ ("testCreateDestroy", testCreateDestroy), ("testDefaultLoop", testDefaultLoop), @@ -120,4 +120,4 @@ extension LoopTests { ] } } -#endif +#endif \ No newline at end of file diff --git a/Tests/UV/StreamTests.swift b/Tests/UVTests/StreamTests.swift similarity index 93% rename from Tests/UV/StreamTests.swift rename to Tests/UVTests/StreamTests.swift index 39f835e..129c057 100644 --- a/Tests/UV/StreamTests.swift +++ b/Tests/UVTests/StreamTests.swift @@ -7,11 +7,12 @@ // import Foundation - import XCTest -@testable import UV + import CUV +@testable import UV + class StreamTests: XCTestCase { func testConnectability() { let string = "Hello TCP" @@ -92,3 +93,13 @@ class StreamTests: XCTestCase { self.waitForExpectations(timeout: 0) } } + +#if os(Linux) +extension StreamTests { + static var allTests : [(String, (StreamTests) -> () throws -> Void)] { + return [ + ("testConnectability", testConnectability), + ] + } +} +#endif \ No newline at end of file diff --git a/Tests/UV/UVTests.swift b/Tests/UVTests/UVTests.swift similarity index 95% rename from Tests/UV/UVTests.swift rename to Tests/UVTests/UVTests.swift index 8e67415..fd4fd85 100644 --- a/Tests/UV/UVTests.swift +++ b/Tests/UVTests/UVTests.swift @@ -53,11 +53,11 @@ class UVTests: XCTestCase { #if os(Linux) extension UVTests { - static var allTests : [(String, UVTests -> () throws -> Void)] { + static var allTests : [(String, (UVTests) -> () throws -> Void)] { return [ - ("testExample", testExample), ("testTimer", testTimer), + ("testExample", testExample), ] } } -#endif +#endif \ No newline at end of file diff --git a/Tests/UVTests/XCTestManifests.swift b/Tests/UVTests/XCTestManifests.swift new file mode 100644 index 0000000..d26d9c7 --- /dev/null +++ b/Tests/UVTests/XCTestManifests.swift @@ -0,0 +1,11 @@ +import XCTest + +#if os(Linux) +public func allTests() -> [XCTestCaseEntry] { + return [ + testCase(LoopTests.allTests), + testCase(StreamTests.allTests), + testCase(UVTests.allTests) + ] +} +#endif \ No newline at end of file diff --git a/UV.xcodeproj/project.pbxproj b/UV.xcodeproj/project.pbxproj index a7fd569..2aaaa1f 100644 --- a/UV.xcodeproj/project.pbxproj +++ b/UV.xcodeproj/project.pbxproj @@ -55,22 +55,22 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 65063F3B1C7CE76100E1F8FE /* Handle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Handle.swift; sourceTree = ""; }; + 65063F3B1C7CE76100E1F8FE /* Handle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Handle.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; 65063F3F1C7CECDE00E1F8FE /* LoopTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoopTests.swift; sourceTree = ""; }; - 653821811C9D7BBE00FA0F06 /* TCP.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TCP.swift; sourceTree = ""; }; - 65465E671C8B73380080BAAE /* Boilerplate.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Boilerplate.framework; path = Carthage/Build/Mac/Boilerplate.framework; sourceTree = ""; }; - 65465E681C8B73380080BAAE /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Result.framework; path = Carthage/Build/Mac/Result.framework; sourceTree = ""; }; - 654BE4441C8E106A0049ACEC /* Prepare.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Prepare.swift; sourceTree = ""; }; - 65786CC01C7DB113002692F2 /* Timer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Timer.swift; sourceTree = ""; }; - 65847EB01C99C1C800156919 /* Stream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Stream.swift; sourceTree = ""; }; - 65847EB21C99F08400156919 /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Request.swift; sourceTree = ""; }; + 653821811C9D7BBE00FA0F06 /* TCP.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = TCP.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; + 65465E671C8B73380080BAAE /* Boilerplate.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Boilerplate.framework; sourceTree = ""; }; + 65465E681C8B73380080BAAE /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Result.framework; sourceTree = ""; }; + 654BE4441C8E106A0049ACEC /* Prepare.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Prepare.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; + 65786CC01C7DB113002692F2 /* Timer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Timer.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; + 65847EB01C99C1C800156919 /* Stream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Stream.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; + 65847EB21C99F08400156919 /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Request.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; 658566821C7C7911003B0E08 /* UV.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = UV.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 658566871C7C7911003B0E08 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 6585668C1C7C7912003B0E08 /* UVTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = UVTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 658566911C7C7912003B0E08 /* UVTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UVTests.swift; sourceTree = ""; }; 658566931C7C7912003B0E08 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 6585669E1C7C92DB003B0E08 /* Loop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Loop.swift; sourceTree = ""; }; - 658566A01C7C9315003B0E08 /* Error.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Error.swift; sourceTree = ""; }; + 6585669E1C7C92DB003B0E08 /* Loop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Loop.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; + 658566A01C7C9315003B0E08 /* Error.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Error.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; 658C316F1C8DD854009CCC7A /* Async.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Async.swift; sourceTree = ""; }; 65CBF4771CC8DCA2004C27B4 /* StreamTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StreamTests.swift; sourceTree = ""; }; D0A7F3811DAD27AB005C6D51 /* Cartfile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Cartfile; sourceTree = ""; }; @@ -106,16 +106,17 @@ 65465E681C8B73380080BAAE /* Result.framework */, ); name = Frameworks; + path = Carthage/Build/Mac; sourceTree = ""; }; 658566781C7C7911003B0E08 = { isa = PBXGroup; children = ( - D0A7F3811DAD27AB005C6D51 /* Cartfile */, - 658566841C7C7911003B0E08 /* UV */, - 658566901C7C7912003B0E08 /* UVTests */, + 92F15ADB1D5C9F0B00CDF148 /* Sources */, + 92F15ADC1D5C9F2700CDF148 /* Tests */, 65465E6B1C8B733C0080BAAE /* Frameworks */, 658566831C7C7911003B0E08 /* Products */, + D0A7F3811DAD27AB005C6D51 /* Cartfile */, ); sourceTree = ""; }; @@ -153,8 +154,23 @@ 65063F3F1C7CECDE00E1F8FE /* LoopTests.swift */, 65CBF4771CC8DCA2004C27B4 /* StreamTests.swift */, ); - name = UVTests; - path = Tests/UV; + path = UVTests; + sourceTree = ""; + }; + 92F15ADB1D5C9F0B00CDF148 /* Sources */ = { + isa = PBXGroup; + children = ( + 658566841C7C7911003B0E08 /* UV */, + ); + path = Sources; + sourceTree = ""; + }; + 92F15ADC1D5C9F2700CDF148 /* Tests */ = { + isa = PBXGroup; + children = ( + 658566901C7C7912003B0E08 /* UVTests */, + ); + path = Tests; sourceTree = ""; }; /* End PBXGroup section */ @@ -215,7 +231,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0810; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Crossroad Labs, LTD."; TargetAttributes = { 658566811C7C7911003B0E08 = { @@ -364,8 +380,10 @@ MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; SWIFT_INCLUDE_PATHS = "$(PROJECT_DIR)/Carthage/Checkouts $(PROJECT_DIR)/Packages/**"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -411,6 +429,7 @@ SDKROOT = macosx; SWIFT_INCLUDE_PATHS = "$(PROJECT_DIR)/Carthage/Checkouts $(PROJECT_DIR)/Packages/**"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -431,7 +450,7 @@ "$(PROJECT_DIR)/Carthage/Build/Mac", ); FRAMEWORK_VERSION = A; - INFOPLIST_FILE = UV/Info.plist; + INFOPLIST_FILE = Sources/UV/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = xyz.crossroadlabs.UV; @@ -457,12 +476,13 @@ "$(PROJECT_DIR)/Carthage/Build/Mac", ); FRAMEWORK_VERSION = A; - INFOPLIST_FILE = UV/Info.plist; + INFOPLIST_FILE = Sources/UV/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = xyz.crossroadlabs.UV; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; SWIFT_VERSION = 3.0; }; name = Release; @@ -475,7 +495,7 @@ "$(inherited)", "$(PROJECT_DIR)/Carthage/Build/Mac", ); - INFOPLIST_FILE = Tests/UV/Info.plist; + INFOPLIST_FILE = Tests/UVTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = xyz.crossroadlabs.UVTests; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -491,10 +511,11 @@ "$(inherited)", "$(PROJECT_DIR)/Carthage/Build/Mac", ); - INFOPLIST_FILE = Tests/UV/Info.plist; + INFOPLIST_FILE = Tests/UVTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = xyz.crossroadlabs.UVTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; SWIFT_VERSION = 3.0; }; name = Release; diff --git a/UV.xcodeproj/xcshareddata/xcschemes/UV.xcscheme b/UV.xcodeproj/xcshareddata/xcschemes/UV.xcscheme index f51217a..61b579a 100644 --- a/UV.xcodeproj/xcshareddata/xcschemes/UV.xcscheme +++ b/UV.xcodeproj/xcshareddata/xcschemes/UV.xcscheme @@ -1,6 +1,6 @@