remove continuation from BurrowIPC

This removes usage of continuation from BurrowIPC by
moving it to NWConnections.
This commit is contained in:
Jett Chen 2023-10-16 11:44:38 +08:00
parent c9f104e523
commit 759311e4f4
2 changed files with 22 additions and 31 deletions

View file

@ -26,7 +26,7 @@ final class LineProtocol: NWProtocolFramerImplementation {
} }
func handleInput(framer: NWProtocolFramer.Instance) -> Int { func handleInput(framer: NWProtocolFramer.Instance) -> Int {
var result: [Data] = [] var result: [Data] = []
framer.parseInput(minimumIncompleteLength: 1, maximumLength: 16_000) { buffer, _ in _ = framer.parseInput(minimumIncompleteLength: 1, maximumLength: 16_000) { buffer, _ in
guard let (lines, size) = lines(from: buffer) else { guard let (lines, size) = lines(from: buffer) else {
return 0 return 0
} }
@ -62,12 +62,23 @@ extension NWConnection {
} }
} }
} }
func send_raw(_ request: Data) async throws -> Data {
try await withCheckedThrowingContinuation { continuation in
let comp: NWConnection.SendCompletion = .contentProcessed {error in
if let error = error {
continuation.resume(with: .failure(error))
} else {
continuation.resume(with: .success(request))
}
}
self.send(content: request, completion: comp)
}
}
} }
final class BurrowIpc { final class BurrowIpc {
let connection: NWConnection let connection: NWConnection
private var generator = SystemRandomNumberGenerator() private var generator = SystemRandomNumberGenerator()
private var continuations: [UInt: UnsafeContinuation<Data, Error>] = [:]
private var logger: Logger private var logger: Logger
init(logger: Logger) { init(logger: Logger) {
let params = NWParameters.tcp let params = NWParameters.tcp
@ -80,36 +91,16 @@ final class BurrowIpc {
self.logger = logger self.logger = logger
} }
func send<T: Request, U: Decodable>(_ request: T) async throws -> U { func send<T: Request, U: Decodable>(_ request: T) async throws -> U {
let data: Data = try await withUnsafeThrowingContinuation { continuation in do {
let id: UInt = generator.next(upperBound: UInt.max) let id: UInt = generator.next(upperBound: UInt.max)
continuations[id] = continuation
var copy = request var copy = request
copy.id = id copy.id = id
do { var data = try JSONEncoder().encode(request)
var data = try JSONEncoder().encode(request) data.append(contentsOf: [10])
data.append(contentsOf: [10]) _ = try await self.connection.send_raw(data)
let completion: NWConnection.SendCompletion = .contentProcessed { error in return try JSONDecoder().decode(Response<U>.self, from: data).result
guard let error = error else { return } } catch {
continuation.resume(throwing: error) throw error
}
connection.send(content: data, completion: completion)
} catch {
continuation.resume(throwing: error)
return
}
}
return try JSONDecoder().decode(Response<U>.self, from: data).result
}
func send_raw(_ request: Data) async throws -> Data {
try await withCheckedThrowingContinuation { continuation in
let comp: NWConnection.SendCompletion = .contentProcessed {error in
if let error = error {
continuation.resume(with: .failure(error))
} else {
continuation.resume(with: .success(request))
}
}
self.connection.send(content: request, completion: comp)
} }
} }
@ -126,7 +117,7 @@ final class BurrowIpc {
do { do {
var data: Data = try JSONEncoder().encode(request) var data: Data = try JSONEncoder().encode(request)
data.append(contentsOf: [10]) data.append(contentsOf: [10])
try await send_raw(data) _ = try await self.connection.send_raw(data)
self.logger.debug("message sent") self.logger.debug("message sent")
let receivedData = try await receive_raw() let receivedData = try await receive_raw()
self.logger.info("Received result: \(String(decoding: receivedData, as: UTF8.self))") self.logger.info("Received result: \(String(decoding: receivedData, as: UTF8.self))")

View file

@ -45,7 +45,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
return nil return nil
} }
// Using a makeshift remote tunnel address // Using a makeshift remote tunnel address
var nst = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "1.1.1.1") let nst = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "1.1.1.1")
nst.ipv4Settings = NEIPv4Settings(addresses: [addr], subnetMasks: ["255.255.255.0"]) nst.ipv4Settings = NEIPv4Settings(addresses: [addr], subnetMasks: ["255.255.255.0"])
logger.log("Initialized ipv4 settings: \(nst.ipv4Settings)") logger.log("Initialized ipv4 settings: \(nst.ipv4Settings)")
return nst return nst