Do all packet reading from dispatch queue.
This commit is contained in:
parent
ec4bc66094
commit
8e521b2b8f
|
@ -60,6 +60,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
setTunnelNetworkSettings(newSettings) { [weak self](error) in
|
setTunnelNetworkSettings(newSettings) { [weak self](error) in
|
||||||
completionHandler(error)
|
completionHandler(error)
|
||||||
self?.wireGuardWrapper.configured = true
|
self?.wireGuardWrapper.configured = true
|
||||||
|
self?.wireGuardWrapper.startReadingPackets()
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -17,6 +17,8 @@
|
||||||
- (BOOL) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString;
|
- (BOOL) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString;
|
||||||
- (void) turnOff;
|
- (void) turnOff;
|
||||||
|
|
||||||
|
- (void) startReadingPackets;
|
||||||
|
|
||||||
+ (NSString *)versionWireGuardGo;
|
+ (NSString *)versionWireGuardGo;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -26,6 +26,7 @@ static void do_log(int level, const char *tag, const char *msg);
|
||||||
@property (nonatomic, assign) BOOL isClosed;
|
@property (nonatomic, assign) BOOL isClosed;
|
||||||
@property (nonatomic, strong) NSMutableArray<NSData *> *packets;
|
@property (nonatomic, strong) NSMutableArray<NSData *> *packets;
|
||||||
@property (nonatomic, strong) NSMutableArray<NSNumber *> *protocols;
|
@property (nonatomic, strong) NSMutableArray<NSNumber *> *protocols;
|
||||||
|
@property (nonatomic, strong) dispatch_queue_t dispatchQueue;
|
||||||
|
|
||||||
@property (nonatomic, strong) NSCondition *condition;
|
@property (nonatomic, strong) NSCondition *condition;
|
||||||
|
|
||||||
|
@ -37,9 +38,11 @@ static void do_log(int level, const char *tag, const char *msg);
|
||||||
{
|
{
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if (self) {
|
if (self) {
|
||||||
|
self.packets = [[NSMutableArray alloc]initWithCapacity:100];
|
||||||
self.handle = -1;
|
self.handle = -1;
|
||||||
self.configured = false;
|
self.configured = false;
|
||||||
self.condition = [NSCondition new];
|
self.condition = [NSCondition new];
|
||||||
|
self.dispatchQueue = dispatch_queue_create("manager", NULL);
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
@ -66,6 +69,31 @@ static void do_log(int level, const char *tag, const char *msg);
|
||||||
self.handle = -1;
|
self.handle = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void) startReadingPackets {
|
||||||
|
[self readPackets];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void) readPackets {
|
||||||
|
dispatch_async(self.dispatchQueue, ^{
|
||||||
|
if (self.isClosed || self.handle < 0 || !self.configured ) {
|
||||||
|
[self readPackets];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
os_log_debug([WireGuardGoWrapper log], "readPackets - read call - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
|
||||||
|
|
||||||
|
[self.packetFlow readPacketsWithCompletionHandler:^(NSArray<NSData *> * _Nonnull packets, NSArray<NSNumber *> * _Nonnull protocols) {
|
||||||
|
@synchronized(self.packets) {
|
||||||
|
[self.packets addObjectsFromArray:packets];
|
||||||
|
[self.protocols addObjectsFromArray:protocols];
|
||||||
|
}
|
||||||
|
os_log_debug([WireGuardGoWrapper log], "readPackets - signal - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
|
||||||
|
[self.condition signal];
|
||||||
|
[self readPackets];
|
||||||
|
}];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
+ (NSString *)versionWireGuardGo {
|
+ (NSString *)versionWireGuardGo {
|
||||||
return [NSString stringWithUTF8String:wgVersion()];
|
return [NSString stringWithUTF8String:wgVersion()];
|
||||||
}
|
}
|
||||||
|
@ -94,23 +122,29 @@ static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!wrapper.packets.count) {
|
|
||||||
[wrapper.packetFlow readPacketsWithCompletionHandler:^(NSArray<NSData *> * _Nonnull packets, NSArray<NSNumber *> * _Nonnull protocols) {
|
NSData * __block packet = nil;
|
||||||
[wrapper.condition signal];
|
// NSNumber *protocol = nil;
|
||||||
[wrapper.packets addObjectsFromArray:packets];
|
dispatch_sync(wrapper.dispatchQueue, ^{
|
||||||
[wrapper.protocols addObjectsFromArray:protocols];
|
@synchronized(wrapper.packets) {
|
||||||
os_log_debug([WireGuardGoWrapper log], "do_read - signal - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
|
if (wrapper.packets.count == 0) {
|
||||||
[wrapper.condition signal];
|
os_log_debug([WireGuardGoWrapper log], "do_read - no packet - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
|
||||||
}];
|
|
||||||
os_log_debug([WireGuardGoWrapper log], "do_read - wait - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
|
return;
|
||||||
while (!wrapper.packets.count)
|
|
||||||
[wrapper.condition wait];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NSData *packet = [wrapper.packets objectAtIndex:0];
|
packet = [wrapper.packets objectAtIndex:0];
|
||||||
// NSNumber *protocol = [wrapper.protocols objectAtIndex:0];
|
// protocol = [wrapper.protocols objectAtIndex:0];
|
||||||
[wrapper.packets removeObjectAtIndex:0];
|
[wrapper.packets removeObjectAtIndex:0];
|
||||||
[wrapper.protocols removeObjectAtIndex:0];
|
[wrapper.protocols removeObjectAtIndex:0];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (packet == nil) {
|
||||||
|
os_log_debug([WireGuardGoWrapper log], "do_read - wait - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
|
||||||
|
[wrapper.condition wait];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
NSUInteger packetLength = [packet length];
|
NSUInteger packetLength = [packet length];
|
||||||
if (packetLength > len) {
|
if (packetLength > len) {
|
||||||
|
|
Loading…
Reference in New Issue