From abe08708280fa62c263e9f24b4df381351cd4386 Mon Sep 17 00:00:00 2001 From: Sergey Abramchuk Date: Tue, 9 Jun 2020 19:29:59 +0300 Subject: [PATCH] Extract sockets invalidation to the separate method --- .../library/OpenVPNPacketFlowBridge.h | 2 ++ .../library/OpenVPNPacketFlowBridge.mm | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Sources/OpenVPNAdapter/library/OpenVPNPacketFlowBridge.h b/Sources/OpenVPNAdapter/library/OpenVPNPacketFlowBridge.h index 7f77b42..f8dc55d 100644 --- a/Sources/OpenVPNAdapter/library/OpenVPNPacketFlowBridge.h +++ b/Sources/OpenVPNAdapter/library/OpenVPNPacketFlowBridge.h @@ -21,6 +21,8 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)initWithPacketFlow:(id)packetFlow NS_DESIGNATED_INITIALIZER; - (BOOL)configureSocketsWithError:(NSError **)error; +- (void)invalidateSocketsIfNeeded; + - (void)startReading; @end diff --git a/Sources/OpenVPNAdapter/library/OpenVPNPacketFlowBridge.mm b/Sources/OpenVPNAdapter/library/OpenVPNPacketFlowBridge.mm index 5b64e26..884514c 100644 --- a/Sources/OpenVPNAdapter/library/OpenVPNPacketFlowBridge.mm +++ b/Sources/OpenVPNAdapter/library/OpenVPNPacketFlowBridge.mm @@ -131,6 +131,18 @@ static void SocketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData return YES; } +- (void)invalidateSocketsIfNeeded { + if (_openVPNSocket) { + CFSocketInvalidate(_openVPNSocket); + CFRelease(_openVPNSocket); + } + + if (_packetFlowSocket) { + CFSocketInvalidate(_packetFlowSocket); + CFRelease(_packetFlowSocket); + } +} + - (void)startReading { __weak typeof(self) weakSelf = self; @@ -170,11 +182,7 @@ static void SocketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData #pragma mark - - (void)dealloc { - CFSocketInvalidate(_openVPNSocket); - CFRelease(_openVPNSocket); - - CFSocketInvalidate(_packetFlowSocket); - CFRelease(_packetFlowSocket); + [self invalidateSocketsIfNeeded]; } @end