Skip to content

Commit

Permalink
Merge pull request #95 from jyoung-man/master
Browse files Browse the repository at this point in the history
use serial queue gcd to make thread-safe method
  • Loading branch information
dung2le authored Sep 26, 2024
2 parents d0e7436 + d880980 commit 590f97a
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions Discovery/Providers/SSDPDiscoveryProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ @interface SSDPDiscoveryProvider() <NSXMLParserDelegate>
NSMutableDictionary *_helloDevices;
NSOperationQueue *_locationLoadQueue;
}
@property (strong, nonatomic) dispatch_queue_t socketQueue;

@end

Expand All @@ -75,6 +76,7 @@ - (instancetype) init
_locationLoadQueue = [[NSOperationQueue alloc] init];
_locationLoadQueue.maxConcurrentOperationCount = 10;

_socketQueue = dispatch_queue_create("ssdp.open", DISPATCH_QUEUE_SERIAL);
self.isRunning = NO;
}

Expand Down Expand Up @@ -224,19 +226,24 @@ - (void) sendRequestForFilter:(NSString *)filter userAgentToken:(NSString *)user

NSData *message = CFBridgingRelease(CFHTTPMessageCopySerializedMessage(theSearchRequest));

if (!_searchSocket)
{
_searchSocket = [[SSDPSocketListener alloc] initWithAddress:kSSDP_multicast_address andPort:0];
_searchSocket.delegate = self;
[_searchSocket open];
}

if (!_multicastSocket)
{
_multicastSocket = [[SSDPSocketListener alloc] initWithAddress:kSSDP_multicast_address andPort:kSSDP_port];
_multicastSocket.delegate = self;
[_multicastSocket open];
}
__weak typeof(self) weakSelf = self;
dispatch_async(self.socketQueue, ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!_searchSocket) {
_searchSocket = [[SSDPSocketListener alloc] initWithAddress:kSSDP_multicast_address andPort:0];
_searchSocket.delegate = strongSelf;
[_searchSocket open];
}
});

dispatch_async(self.socketQueue, ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!_multicastSocket) {
_multicastSocket = [[SSDPSocketListener alloc] initWithAddress:kSSDP_multicast_address andPort:kSSDP_port];
_multicastSocket.delegate = strongSelf;
[_multicastSocket open];
}
});

[_searchSocket sendData:message toAddress:kSSDP_multicast_address andPort:kSSDP_port];
[self performBlock:^{ [_searchSocket sendData:message toAddress:kSSDP_multicast_address andPort:kSSDP_port]; } afterDelay:1];
Expand Down

0 comments on commit 590f97a

Please sign in to comment.