Light refactoring of the packetFlow assignment

This commit is contained in:
Sergey Abramchuk
2020-06-09 23:06:25 +03:00
parent 2ec2827e5e
commit 6de79ecaf8
5 changed files with 23 additions and 28 deletions
@@ -96,6 +96,11 @@ NS_SWIFT_NAME(openVPNAdapter(_:handleEvent:message:));
*/ */
@property (nonatomic, class, readonly) NSString *platform; @property (nonatomic, class, readonly) NSString *platform;
/**
*/
@property (nonatomic, weak) id<OpenVPNAdapterPacketFlow> packetFlow;
/** /**
The object that acts as the delegate of the adapter. The object that acts as the delegate of the adapter.
*/ */
@@ -126,11 +131,6 @@ NS_SWIFT_NAME(openVPNAdapter(_:handleEvent:message:));
*/ */
@property (nonatomic, readonly) OpenVPNTransportStats *transportStatistics; @property (nonatomic, readonly) OpenVPNTransportStats *transportStatistics;
/**
*/
- (instancetype)initWithPacketFlow:(id<OpenVPNAdapterPacketFlow>)packetFlow;
/** /**
Applies the given configuration object. Applies the given configuration object.
Call this method prior to connecting, this method has no effect after calling connect. Call this method prior to connecting, this method has no effect after calling connect.
@@ -181,8 +181,6 @@ NS_SWIFT_NAME(apply(configuration:));
*/ */
- (void)disconnect; - (void)disconnect;
- (instancetype) init NS_UNAVAILABLE;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
@@ -40,10 +40,10 @@
@implementation OpenVPNAdapter @implementation OpenVPNAdapter
- (instancetype)initWithPacketFlow:(id<OpenVPNAdapterPacketFlow>)packetFlow { - (instancetype)init {
if (self = [super init]) { if (self = [super init]) {
_vpnClient = new OpenVPNClient(self); _vpnClient = new OpenVPNClient(self);
_packetFlowBridge = [[OpenVPNPacketFlowBridge alloc] initWithPacketFlow:packetFlow]; _packetFlowBridge = [[OpenVPNPacketFlowBridge alloc] init];
} }
return self; return self;
} }
@@ -87,6 +87,8 @@
} }
- (void)connect { - (void)connect {
NSAssert(self.packetFlow != nil, @"packetFlow property shouldn't be nil, set it before trying to establish connection.");
dispatch_queue_attr_t attributes = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0); dispatch_queue_attr_t attributes = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0);
dispatch_queue_t connectQueue = dispatch_queue_create("me.ss-abramchuk.openvpn-adapter.connection.", attributes); dispatch_queue_t connectQueue = dispatch_queue_create("me.ss-abramchuk.openvpn-adapter.connection.", attributes);
dispatch_async(connectQueue, ^{ dispatch_async(connectQueue, ^{
@@ -166,6 +168,14 @@
return _networkSettingsBuilder; return _networkSettingsBuilder;
} }
- (id<OpenVPNAdapterPacketFlow>)packetFlow {
return self.packetFlowBridge.packetFlow;
}
- (void)setPacketFlow:(id<OpenVPNAdapterPacketFlow>)packetFlow {
self.packetFlowBridge.packetFlow = packetFlow;
}
#pragma mark - OpenVPNClientDelegate #pragma mark - OpenVPNClientDelegate
- (BOOL)setRemoteAddress:(NSString *)address { - (BOOL)setRemoteAddress:(NSString *)address {
@@ -14,12 +14,11 @@ NS_ASSUME_NONNULL_BEGIN
@interface OpenVPNPacketFlowBridge: NSObject @interface OpenVPNPacketFlowBridge: NSObject
@property (nonatomic, weak) id<OpenVPNAdapterPacketFlow> packetFlow;
@property (nonatomic, readonly) CFSocketRef openVPNSocket; @property (nonatomic, readonly) CFSocketRef openVPNSocket;
@property (nonatomic, readonly) CFSocketRef packetFlowSocket; @property (nonatomic, readonly) CFSocketRef packetFlowSocket;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithPacketFlow:(id<OpenVPNAdapterPacketFlow>)packetFlow NS_DESIGNATED_INITIALIZER;
- (BOOL)configureSocketsWithError:(NSError **)error; - (BOOL)configureSocketsWithError:(NSError **)error;
- (void)invalidateSocketsIfNeeded; - (void)invalidateSocketsIfNeeded;
@@ -15,21 +15,8 @@
#import "OpenVPNPacket.h" #import "OpenVPNPacket.h"
#import "OpenVPNAdapterPacketFlow.h" #import "OpenVPNAdapterPacketFlow.h"
@interface OpenVPNPacketFlowBridge ()
@property (nonatomic) id<OpenVPNAdapterPacketFlow> packetFlow;
@end
@implementation OpenVPNPacketFlowBridge @implementation OpenVPNPacketFlowBridge
- (instancetype)initWithPacketFlow:(id<OpenVPNAdapterPacketFlow>)packetFlow {
if (self = [super init]) {
_packetFlow = packetFlow;
}
return self;
}
#pragma mark - Sockets Configuration #pragma mark - Sockets Configuration
static void SocketCallback(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *obj) { static void SocketCallback(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *obj) {
@@ -32,7 +32,7 @@ class OpenVPNAdapterTests: XCTestCase {
func testApplyConfiguration() { func testApplyConfiguration() {
guard let vpnConfiguration = VPNProfile.configuration.data(using: .utf8) else { fatalError() } guard let vpnConfiguration = VPNProfile.configuration.data(using: .utf8) else { fatalError() }
let adapter = OpenVPNAdapter(packetFlow: customFlow) let adapter = OpenVPNAdapter()
let configuration = OpenVPNConfiguration() let configuration = OpenVPNConfiguration()
configuration.fileContent = vpnConfiguration configuration.fileContent = vpnConfiguration
@@ -52,7 +52,7 @@ class OpenVPNAdapterTests: XCTestCase {
} }
func testProvideCredentials() { func testProvideCredentials() {
let adapter = OpenVPNAdapter(packetFlow: customFlow) let adapter = OpenVPNAdapter()
let credentials = OpenVPNCredentials() let credentials = OpenVPNCredentials()
credentials.username = "username" credentials.username = "username"
@@ -71,7 +71,7 @@ class OpenVPNAdapterTests: XCTestCase {
func testConnection() { func testConnection() {
guard let vpnConfiguration = VPNProfile.configuration.data(using: .utf8) else { fatalError() } guard let vpnConfiguration = VPNProfile.configuration.data(using: .utf8) else { fatalError() }
let adapter = OpenVPNAdapter(packetFlow: customFlow) let adapter = OpenVPNAdapter()
let configuration = OpenVPNConfiguration() let configuration = OpenVPNConfiguration()
configuration.fileContent = vpnConfiguration configuration.fileContent = vpnConfiguration
@@ -99,6 +99,7 @@ class OpenVPNAdapterTests: XCTestCase {
expectations[.connection] = expectation(description: "me.ss-abramchuk.openvpn-adapter.connection") expectations[.connection] = expectation(description: "me.ss-abramchuk.openvpn-adapter.connection")
adapter.packetFlow = customFlow
adapter.delegate = self adapter.delegate = self
adapter.connect() adapter.connect()