Skip to content

Commit

Permalink
Rename DeferredCancellable to CancellableChain
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrej Mihajlov committed Nov 7, 2023
1 parent 7b590c2 commit 7adc6da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// DeferredCancellable.swift
// CancellableChain.swift
// MullvadTransport
//
// Created by pronebird on 23/10/2023.
Expand All @@ -9,23 +9,23 @@
import Foundation
import MullvadTypes

/// Cancellable object that defers cancellation until the other token is connected to it.
final class DeferredCancellable: Cancellable {
/// Cancellable object that cancels all cancellable objects linked to it.
final class CancellableChain: Cancellable {
private let stateLock = NSLock()
private var isCancelled = false
private var connectedTokens: [Cancellable] = []
private var linkedTokens: [Cancellable] = []

init() {}

/// Connect deferred cancellation token with some other.
/// Link cancellation token with some other.
///
/// The token is cancelled immediately, if the deferred object is already cancelled.
func connect(_ token: Cancellable) {
/// The token is cancelled immediately, if the chain is already cancelled.
func link(_ token: Cancellable) {
stateLock.withLock {
if isCancelled {
token.cancel()
} else {
connectedTokens.append(token)
linkedTokens.append(token)
}
}
}
Expand All @@ -36,8 +36,8 @@ final class DeferredCancellable: Cancellable {
func cancel() {
stateLock.withLock {
isCancelled = true
connectedTokens.forEach { $0.cancel() }
connectedTokens.removeAll()
linkedTokens.forEach { $0.cancel() }
linkedTokens.removeAll()
}
}
}
8 changes: 4 additions & 4 deletions ios/MullvadTransport/URLSessionSocks5Transport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ public class URLSessionSocks5Transport: RESTTransport {
request: URLRequest,
completion: @escaping (Data?, URLResponse?, Error?) -> Void
) -> Cancellable {
let deferred = DeferredCancellable()
let chain = CancellableChain()

socksProxy.start { [weak self, weak socksProxy] error in
if let error {
completion(nil, nil, error)
} else if let self, let localPort = socksProxy?.listenPort {
let token = self.startDataTask(request: request, localPort: localPort, completion: completion)

// Propagate cancellation from deferred to the data task cancellation token.
deferred.connect(token)
// Propagate cancellation from the chain to the data task cancellation token.
chain.link(token)
} else {
completion(nil, nil, URLError(.cancelled))
}
}

return deferred
return chain
}

/// Execute data task, rewriting the original URLRequest to communicate over the socks proxy listening on the local TCP port.
Expand Down
8 changes: 4 additions & 4 deletions ios/MullvadVPN.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
585F5A752AE68688001D9DF7 /* Socks5ConnectNegotiation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A652AE68687001D9DF7 /* Socks5ConnectNegotiation.swift */; };
585F5A762AE68688001D9DF7 /* NWConnection+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A662AE68688001D9DF7 /* NWConnection+Extensions.swift */; };
585F5A782AE6879A001D9DF7 /* URLSessionSocks5Transport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A772AE6879A001D9DF7 /* URLSessionSocks5Transport.swift */; };
585F5A7A2AE6A523001D9DF7 /* DeferredCancellable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A792AE6A523001D9DF7 /* DeferredCancellable.swift */; };
585F5A7A2AE6A523001D9DF7 /* CancellableChain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A792AE6A523001D9DF7 /* CancellableChain.swift */; };
585F5A7C2AE6A537001D9DF7 /* AnyIPEndpoint+Socks5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A7B2AE6A537001D9DF7 /* AnyIPEndpoint+Socks5.swift */; };
585F5A7E2AE6B62D001D9DF7 /* Socks5Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A7D2AE6B62D001D9DF7 /* Socks5Configuration.swift */; };
58607A4D2947287800BC467D /* AccountExpiryInAppNotificationProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58607A4C2947287800BC467D /* AccountExpiryInAppNotificationProvider.swift */; };
Expand Down Expand Up @@ -1313,7 +1313,7 @@
585F5A652AE68687001D9DF7 /* Socks5ConnectNegotiation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Socks5ConnectNegotiation.swift; sourceTree = "<group>"; };
585F5A662AE68688001D9DF7 /* NWConnection+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NWConnection+Extensions.swift"; sourceTree = "<group>"; };
585F5A772AE6879A001D9DF7 /* URLSessionSocks5Transport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionSocks5Transport.swift; sourceTree = "<group>"; };
585F5A792AE6A523001D9DF7 /* DeferredCancellable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeferredCancellable.swift; sourceTree = "<group>"; };
585F5A792AE6A523001D9DF7 /* CancellableChain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CancellableChain.swift; sourceTree = "<group>"; };
585F5A7B2AE6A537001D9DF7 /* AnyIPEndpoint+Socks5.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AnyIPEndpoint+Socks5.swift"; sourceTree = "<group>"; };
585F5A7D2AE6B62D001D9DF7 /* Socks5Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Socks5Configuration.swift; sourceTree = "<group>"; };
58607A4C2947287800BC467D /* AccountExpiryInAppNotificationProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountExpiryInAppNotificationProvider.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3050,7 +3050,7 @@
isa = PBXGroup;
children = (
585F5A7B2AE6A537001D9DF7 /* AnyIPEndpoint+Socks5.swift */,
585F5A792AE6A523001D9DF7 /* DeferredCancellable.swift */,
585F5A792AE6A523001D9DF7 /* CancellableChain.swift */,
A97F1F432A1F4E1A00ECEFDE /* MullvadTransport.h */,
586F2BE129F6916F009E6924 /* shadowsocks.h */,
A9467E872A2DCD57000DC21F /* ShadowsocksConfiguration.swift */,
Expand Down Expand Up @@ -4752,7 +4752,7 @@
585F5A782AE6879A001D9DF7 /* URLSessionSocks5Transport.swift in Sources */,
585F5A6B2AE68688001D9DF7 /* Socks5Handshake.swift in Sources */,
585F5A682AE68688001D9DF7 /* Socks5ForwardingProxy.swift in Sources */,
585F5A7A2AE6A523001D9DF7 /* DeferredCancellable.swift in Sources */,
585F5A7A2AE6A523001D9DF7 /* CancellableChain.swift in Sources */,
585F5A692AE68688001D9DF7 /* Socks5ConnectCommand.swift in Sources */,
A95F86B72A1F53BA00245DAC /* URLSessionTransport.swift in Sources */,
5822C0042A3724A800A3A5FB /* ShadowsocksConfigurationCache.swift in Sources */,
Expand Down

0 comments on commit 7adc6da

Please sign in to comment.