mirror of
https://github.com/qvacua/vimr.git
synced 2024-11-23 19:21:53 +03:00
Refactor slightly
and format with swiftformat
This commit is contained in:
parent
881a3768af
commit
c3be5b6441
9
.swiftformat
Normal file
9
.swiftformat
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
--swiftversion 5.2
|
||||||
|
|
||||||
|
--exclude Carthage,third-party,**/*.generated.swift
|
||||||
|
|
||||||
|
--indent 2
|
||||||
|
--maxwidth 100
|
||||||
|
|
||||||
|
--self insert
|
||||||
|
--wraparguments before-first
|
@ -7,30 +7,48 @@ import Foundation
|
|||||||
import RxSwift
|
import RxSwift
|
||||||
|
|
||||||
public final class RxMessagePortClient {
|
public final class RxMessagePortClient {
|
||||||
|
public enum ResponseCode {
|
||||||
|
// Unfortunately, case success = kCFMessagePortSuccess is not possible.
|
||||||
|
case success
|
||||||
|
case sendTimeout
|
||||||
|
case receiveTimeout
|
||||||
|
case isInvalid
|
||||||
|
case transportError
|
||||||
|
case becameInvalidError
|
||||||
|
case unknown
|
||||||
|
|
||||||
public enum Error: Swift.Error {
|
fileprivate init(rawResponseCode code: Int32) {
|
||||||
|
|
||||||
case serverInit
|
|
||||||
case clientInit
|
|
||||||
case portInvalid
|
|
||||||
case send(msgid: Int32, response: Int32)
|
|
||||||
|
|
||||||
public static func responseCodeToString(code: Int32) -> String {
|
|
||||||
switch code {
|
switch code {
|
||||||
case kCFMessagePortSendTimeout:return "kCFMessagePortSendTimeout"
|
case kCFMessagePortSuccess: self = .success
|
||||||
case kCFMessagePortReceiveTimeout:return "kCFMessagePortReceiveTimeout"
|
case kCFMessagePortSendTimeout: self = .sendTimeout
|
||||||
case kCFMessagePortIsInvalid:return "kCFMessagePortIsInvalid"
|
case kCFMessagePortReceiveTimeout: self = .receiveTimeout
|
||||||
case kCFMessagePortTransportError:return "kCFMessagePortTransportError"
|
case kCFMessagePortIsInvalid: self = .isInvalid
|
||||||
case kCFMessagePortBecameInvalidError:return "kCFMessagePortBecameInvalidError"
|
case kCFMessagePortTransportError: self = .transportError
|
||||||
default:return "unknown"
|
case kCFMessagePortBecameInvalidError: self = .becameInvalidError
|
||||||
|
default: self = .unknown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum Error: Swift.Error {
|
||||||
|
case serverInit
|
||||||
|
case clientInit
|
||||||
|
case portInvalid
|
||||||
|
case send(msgid: Int32, response: ResponseCode)
|
||||||
|
}
|
||||||
|
|
||||||
public static let defaultTimeout = CFTimeInterval(5)
|
public static let defaultTimeout = CFTimeInterval(5)
|
||||||
|
|
||||||
|
public let uuid = UUID()
|
||||||
public var timeout = RxMessagePortClient.defaultTimeout
|
public var timeout = RxMessagePortClient.defaultTimeout
|
||||||
|
|
||||||
|
public init() {
|
||||||
|
self.queue = DispatchQueue(
|
||||||
|
label: "\(String(reflecting: RxMessagePortClient.self))-\(self.uuid.uuidString)",
|
||||||
|
qos: .userInitiated
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
public func send(
|
public func send(
|
||||||
msgid: Int32,
|
msgid: Int32,
|
||||||
data: Data?,
|
data: Data?,
|
||||||
@ -43,8 +61,7 @@ public final class RxMessagePortClient {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let returnDataPtr: UnsafeMutablePointer<Unmanaged<CFData>?>
|
let returnDataPtr = UnsafeMutablePointer<Unmanaged<CFData>?>.allocate(capacity: 1)
|
||||||
= UnsafeMutablePointer.allocate(capacity: 1)
|
|
||||||
defer { returnDataPtr.deallocate() }
|
defer { returnDataPtr.deallocate() }
|
||||||
|
|
||||||
let responseCode = CFMessagePortSendRequest(
|
let responseCode = CFMessagePortSendRequest(
|
||||||
@ -58,7 +75,9 @@ public final class RxMessagePortClient {
|
|||||||
)
|
)
|
||||||
|
|
||||||
guard responseCode == kCFMessagePortSuccess else {
|
guard responseCode == kCFMessagePortSuccess else {
|
||||||
single(.error(Error.send(msgid: msgid, response: responseCode)))
|
single(.error(
|
||||||
|
Error.send(msgid: msgid, response: ResponseCode(rawResponseCode: responseCode))
|
||||||
|
))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,6 +113,7 @@ public final class RxMessagePortClient {
|
|||||||
self.portIsValid = true
|
self.portIsValid = true
|
||||||
completable(.completed)
|
completable(.completed)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Disposables.create()
|
return Disposables.create()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -115,22 +135,19 @@ public final class RxMessagePortClient {
|
|||||||
private var portIsValid = false
|
private var portIsValid = false
|
||||||
private var port: CFMessagePort?
|
private var port: CFMessagePort?
|
||||||
|
|
||||||
private let queue = DispatchQueue(
|
private let queue: DispatchQueue
|
||||||
label: String(reflecting: RxMessagePortClient.self),
|
|
||||||
qos: .userInitiated
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class RxMessagePortServer {
|
public final class RxMessagePortServer {
|
||||||
|
|
||||||
public typealias SyncReplyBody = (Int32, Data?) -> Data?
|
public typealias SyncReplyBody = (Int32, Data?) -> Data?
|
||||||
|
|
||||||
public struct Message {
|
public struct Message {
|
||||||
|
public var msgid: Int32
|
||||||
public let msgid: Int32
|
public var data: Data?
|
||||||
public let data: Data?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public let uuid = UUID()
|
||||||
|
|
||||||
public var syncReplyBody: SyncReplyBody? {
|
public var syncReplyBody: SyncReplyBody? {
|
||||||
get { self.messageHandler.syncReplyBody }
|
get { self.messageHandler.syncReplyBody }
|
||||||
set { self.messageHandler.syncReplyBody = newValue }
|
set { self.messageHandler.syncReplyBody = newValue }
|
||||||
@ -138,7 +155,13 @@ public final class RxMessagePortServer {
|
|||||||
|
|
||||||
public var stream: Observable<Message> { self.streamSubject.asObservable() }
|
public var stream: Observable<Message> { self.streamSubject.asObservable() }
|
||||||
|
|
||||||
public init() { self.messageHandler = MessageHandler(subject: self.streamSubject) }
|
public init() {
|
||||||
|
self.queue = DispatchQueue(
|
||||||
|
label: "\(String(reflecting: RxMessagePortClient.self))-\(self.uuid.uuidString)",
|
||||||
|
qos: .userInitiated
|
||||||
|
)
|
||||||
|
self.messageHandler = MessageHandler(subject: self.streamSubject)
|
||||||
|
}
|
||||||
|
|
||||||
public func run(as name: String) -> Completable {
|
public func run(as name: String) -> Completable {
|
||||||
Completable.create { completable in
|
Completable.create { completable in
|
||||||
@ -170,7 +193,8 @@ public final class RxMessagePortServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.portThread = Thread { self.runServer() }
|
self.portThread = Thread { self.runServer() }
|
||||||
self.portThread?.name = String(reflecting: RxMessagePortServer.self)
|
self.portThread?.name
|
||||||
|
= "\(String(reflecting: RxMessagePortServer.self))-\(self.uuid.uuidString)"
|
||||||
self.portThread?.start()
|
self.portThread?.start()
|
||||||
|
|
||||||
completable(.completed)
|
completable(.completed)
|
||||||
@ -203,10 +227,7 @@ public final class RxMessagePortServer {
|
|||||||
private var portThread: Thread?
|
private var portThread: Thread?
|
||||||
private var portRunLoop: CFRunLoop?
|
private var portRunLoop: CFRunLoop?
|
||||||
|
|
||||||
private let queue = DispatchQueue(
|
private let queue: DispatchQueue
|
||||||
label: String(reflecting: RxMessagePortClient.self),
|
|
||||||
qos: .userInitiated
|
|
||||||
)
|
|
||||||
|
|
||||||
private var messageHandler: MessageHandler
|
private var messageHandler: MessageHandler
|
||||||
private let streamSubject = PublishSubject<Message>()
|
private let streamSubject = PublishSubject<Message>()
|
||||||
@ -220,7 +241,6 @@ public final class RxMessagePortServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class MessageHandler {
|
private class MessageHandler {
|
||||||
|
|
||||||
fileprivate var syncReplyBody: RxMessagePortServer.SyncReplyBody?
|
fileprivate var syncReplyBody: RxMessagePortServer.SyncReplyBody?
|
||||||
|
|
||||||
fileprivate init(subject: PublishSubject<RxMessagePortServer.Message>) { self.subject = subject }
|
fileprivate init(subject: PublishSubject<RxMessagePortServer.Message>) { self.subject = subject }
|
||||||
@ -245,16 +265,13 @@ private class MessageHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private extension Data {
|
private extension Data {
|
||||||
|
|
||||||
var cfdata: CFData { self as NSData }
|
var cfdata: CFData { self as NSData }
|
||||||
}
|
}
|
||||||
|
|
||||||
private extension CFData {
|
private extension CFData {
|
||||||
|
|
||||||
var data: Data { self as NSData as Data }
|
var data: Data { self as NSData as Data }
|
||||||
}
|
}
|
||||||
|
|
||||||
private extension String {
|
private extension String {
|
||||||
|
|
||||||
var cfstr: CFString { self as NSString }
|
var cfstr: CFString { self as NSString }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user