Replace local code with updated Roxas and SideKit

Signed-off-by: Joseph Mattello <mail@joemattiello.com>
This commit is contained in:
Joseph Mattello
2023-02-25 01:37:52 -05:00
committed by Joe Mattiello
parent 4e84dc4cc8
commit f270ecc537
14 changed files with 124 additions and 1200 deletions

View File

@@ -1,23 +0,0 @@
//
// ALTConnection.h
// AltKit
//
// Created by Riley Testut on 6/1/20.
// Copyright © 2020 Riley Testut. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
NS_SWIFT_NAME(Connection)
@protocol ALTConnection <NSObject>
- (void)sendData:(NSData *)data completionHandler:(void (^)(BOOL, NSError * _Nullable))completionHandler NS_REFINED_FOR_SWIFT;
- (void)receiveDataWithExpectedSize:(NSInteger)expectedSize completionHandler:(void (^)(NSData * _Nullable, NSError * _Nullable))completionHandler NS_SWIFT_NAME(__receiveData(expectedSize:completionHandler:));
- (void)disconnect;
@end
NS_ASSUME_NONNULL_END

View File

@@ -8,17 +8,19 @@
import Foundation
import Network
import SideKit
public extension Connection
{
func send(_ data: Data, completionHandler: @escaping (Result<Void, ALTServerError>) -> Void)
{
self.__send(data) { (success, error) in
let result = Result(success, error).mapError { (error) -> ALTServerError in
guard let nwError = error as? NWError else { return ALTServerError(error) }
return ALTServerError(.lostConnection, underlyingError: nwError)
let result = Result(success, error).mapError { (failure :Error) -> ALTServerError in
guard let nwError = failure as? NWError else { return ALTServerError.init(failure) }
return ALTServerError.lostConnection(underlyingError: nwError)
}
completionHandler(result)
}
}
@@ -26,9 +28,9 @@ public extension Connection
func receiveData(expectedSize: Int, completionHandler: @escaping (Result<Data, ALTServerError>) -> Void)
{
self.__receiveData(expectedSize: expectedSize) { (data, error) in
let result = Result(data, error).mapError { (error) -> ALTServerError in
guard let nwError = error as? NWError else { return ALTServerError(error) }
return ALTServerError(.lostConnection, underlyingError: nwError)
let result = Result(data, error).mapError { (failure :Error) -> ALTServerError in
guard let nwError = failure as? NWError else { return ALTServerError.init(failure) }
return ALTServerError.lostConnection(underlyingError: nwError)
}
completionHandler(result)
@@ -72,7 +74,7 @@ public extension Connection
}
catch
{
finish(.failure(.init(.invalidResponse, underlyingError: error)))
finish(.failure(.invalidResponse(underlyingError: error)))
}
}

View File

@@ -8,6 +8,7 @@
import Foundation
import Network
import SideKit
public protocol RequestHandler
{
@@ -26,24 +27,25 @@ public protocol RequestHandler
public protocol ConnectionHandler: AnyObject
{
var connectionHandler: ((Connection) -> Void)? { get set }
var disconnectionHandler: ((Connection) -> Void)? { get set }
associatedtype ConnectionType = Connection
var connectionHandler: ((ConnectionType) -> Void)? { get set }
var disconnectionHandler: ((ConnectionType) -> Void)? { get set }
func startListening()
func stopListening()
}
public class ConnectionManager<RequestHandlerType: RequestHandler>
public class ConnectionManager<RequestHandlerType: RequestHandler, ConnectionType: NetworkConnection & AnyObject, ConnectionHandlerType: ConnectionHandler> where ConnectionHandlerType.ConnectionType == ConnectionType
{
public let requestHandler: RequestHandlerType
public let connectionHandlers: [ConnectionHandler]
public let connectionHandlers: [ConnectionHandlerType]
public var isStarted = false
private var connections = [Connection]()
private var connections = [ConnectionType]()
private let connectionsLock = NSLock()
public init(requestHandler: RequestHandlerType, connectionHandlers: [ConnectionHandler])
public init(requestHandler: RequestHandlerType, connectionHandlers: [ConnectionHandlerType])
{
self.requestHandler = requestHandler
self.connectionHandlers = connectionHandlers
@@ -87,7 +89,7 @@ public class ConnectionManager<RequestHandlerType: RequestHandler>
private extension ConnectionManager
{
func prepare(_ connection: Connection)
func prepare(_ connection: ConnectionType)
{
self.connectionsLock.lock()
defer { self.connectionsLock.unlock() }
@@ -98,7 +100,7 @@ private extension ConnectionManager
self.handleRequest(for: connection)
}
func disconnect(_ connection: Connection)
func disconnect(_ connection: ConnectionType)
{
self.connectionsLock.lock()
defer { self.connectionsLock.unlock() }
@@ -107,7 +109,7 @@ private extension ConnectionManager
self.connections.remove(at: index)
}
func handleRequest(for connection: Connection)
func handleRequest(for connection: ConnectionType)
{
func finish<T: ServerMessageProtocol>(_ result: Result<T, Error>)
{
@@ -167,7 +169,7 @@ private extension ConnectionManager
}
case .success(.unknown):
finish(Result<ErrorResponse, Error>.failure(ALTServerError(.unknownRequest)))
finish(Result<ErrorResponse, Error>.failure(ALTServerError.unknownRequest))
}
}
}

View File

@@ -8,6 +8,7 @@
import Foundation
import Network
import SideKit
public class NetworkConnection: NSObject, Connection
{
@@ -29,7 +30,7 @@ public class NetworkConnection: NSObject, Connection
{
self.nwConnection.receive(minimumIncompleteLength: expectedSize, maximumLength: expectedSize) { (data, context, isComplete, error) in
guard data != nil || error != nil else {
return completionHandler(nil, ALTServerError(.lostConnection))
return completionHandler(nil, ALTServerError.lostConnection(underlyingError: error))
}
completionHandler(data, error)

View File

@@ -7,6 +7,7 @@
//
import Foundation
import SideKit
@objc private protocol XPCConnectionProxy
{
@@ -44,7 +45,7 @@ public class XPCConnection: NSObject, Connection
super.init()
xpcConnection.interruptionHandler = {
self.error = ALTServerError(.lostConnection)
self.error = ALTServerError.lostConnection(underlyingError: nil)
}
xpcConnection.exportedObject = self