Skip to content

Commit

Permalink
remove continuation from BurrowIPC
Browse files Browse the repository at this point in the history
This removes usage of continuation from BurrowIPC by
moving it to NWConnections.
  • Loading branch information
JettChenT committed Oct 23, 2023
1 parent c9f104e commit 40936e6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
51 changes: 21 additions & 30 deletions Apple/NetworkExtension/BurrowIpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class LineProtocol: NWProtocolFramerImplementation {
}
func handleInput(framer: NWProtocolFramer.Instance) -> Int {
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 {
return 0
}
Expand Down Expand Up @@ -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 {
let connection: NWConnection
private var generator = SystemRandomNumberGenerator()
private var continuations: [UInt: UnsafeContinuation<Data, Error>] = [:]
private var logger: Logger
init(logger: Logger) {
let params = NWParameters.tcp
Expand All @@ -80,36 +91,16 @@ final class BurrowIpc {
self.logger = logger
}
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)
continuations[id] = continuation
var copy = request
copy.id = id
do {
var data = try JSONEncoder().encode(request)
data.append(contentsOf: [10])
let completion: NWConnection.SendCompletion = .contentProcessed { error in
guard let error = error else { return }
continuation.resume(throwing: 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)
var data = try JSONEncoder().encode(request)
data.append(contentsOf: [10])
_ = try await self.connection.send_raw(data)
return try JSONDecoder().decode(Response<U>.self, from: data).result
} catch {
throw error
}
}

Expand All @@ -126,7 +117,7 @@ final class BurrowIpc {
do {
var data: Data = try JSONEncoder().encode(request)
data.append(contentsOf: [10])
try await send_raw(data)
_ = try await self.connection.send_raw(data)
self.logger.debug("message sent")
let receivedData = try await receive_raw()
self.logger.info("Received result: \(String(decoding: receivedData, as: UTF8.self))")
Expand Down
2 changes: 1 addition & 1 deletion Apple/NetworkExtension/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
return nil
}
// 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"])
logger.log("Initialized ipv4 settings: \(nst.ipv4Settings)")
return nst
Expand Down

0 comments on commit 40936e6

Please sign in to comment.