diff options
author | NicoleYarroch <nicole@livio.io> | 2019-05-15 13:32:06 -0400 |
---|---|---|
committer | NicoleYarroch <nicole@livio.io> | 2019-05-15 13:32:06 -0400 |
commit | e3d95d2064e8fc0f0b47381d0c228b39c525b3a2 (patch) | |
tree | 8a6b8e434817d6d4e5a71c7da152eabddf8c12f4 | |
parent | 4bb3e97366ef97e8c0e3c48b8e067f4e1396072d (diff) | |
download | sdl_ios-e3d95d2064e8fc0f0b47381d0c228b39c525b3a2.tar.gz |
Cleaned up SDLIAPSession and added documentation
* reduced IOStreamThreadWaitSecs
-rw-r--r-- | SmartDeviceLink/SDLIAPDataSession.m | 4 | ||||
-rw-r--r-- | SmartDeviceLink/SDLIAPSession.m | 20 | ||||
-rw-r--r-- | SmartDeviceLink/SDLIAPTransport.m | 2 |
3 files changed, 17 insertions, 9 deletions
diff --git a/SmartDeviceLink/SDLIAPDataSession.m b/SmartDeviceLink/SDLIAPDataSession.m index 456cf59fd..8a53a18fd 100644 --- a/SmartDeviceLink/SDLIAPDataSession.m +++ b/SmartDeviceLink/SDLIAPDataSession.m @@ -72,8 +72,8 @@ NS_ASSUME_NONNULL_BEGIN } SDLLogD(@"Stopping the data session"); - [self.session stop]; // Calling stop but easession may not yet be set to `nil` - self.session.streamDelegate = nil; // + [self.session stop]; + self.session.streamDelegate = nil; self.session = nil; } diff --git a/SmartDeviceLink/SDLIAPSession.m b/SmartDeviceLink/SDLIAPSession.m index ade553968..b397780fb 100644 --- a/SmartDeviceLink/SDLIAPSession.m +++ b/SmartDeviceLink/SDLIAPSession.m @@ -11,7 +11,7 @@ NS_ASSUME_NONNULL_BEGIN NSString *const IOStreamThreadName = @"com.smartdevicelink.iostream"; -NSTimeInterval const StreamThreadWaitSecs = 10.0; +NSTimeInterval const IOStreamThreadWaitSecs = 1.0; @interface SDLIAPSession () @@ -49,7 +49,7 @@ NSTimeInterval const StreamThreadWaitSecs = 10.0; #pragma mark - Public Stream Lifecycle - (BOOL)start { - SDLLogD(@"Opening EASession withAccessory:%@ forProtocol:%@", _accessory.name, _protocol); + SDLLogD(@"Opening EASession with accessory: %@", self.accessory.name); self.easession = [[EASession alloc] initWithAccessory:self.accessory forProtocol:self.protocol]; return [self sdl_startWithSession:self.easession]; } @@ -94,12 +94,19 @@ NSTimeInterval const StreamThreadWaitSecs = 10.0; NSAssert(NSThread.isMainThread, @"%@ must only be called on the main thread", NSStringFromSelector(_cmd)); if (self.isDataSession) { + if (self.ioStreamThread == nil) { + SDLLogV(@"Stopping data session but no thread established."); + self.isDataSession = NO; + self.easession = nil; + return; + } + [self.ioStreamThread cancel]; - // Waiting on the I/O threads of the data session to close + // Waiting for the I/O streams of the data session to close [self sdl_isIOThreadCanceled:self.canceledSemaphore completionHandler:^(BOOL success) { if (success == NO) { - SDLLogE(@"About to destroy a thread that has not yet closed."); + SDLLogE(@"Destroying thread (IOStreamThread) for data session when I/O streams have not yet closed."); } self.ioStreamThread = nil; self.isDataSession = NO; @@ -119,7 +126,7 @@ NSTimeInterval const StreamThreadWaitSecs = 10.0; * @param completionHandler Returns whether or not the data session's I/O streams were closed successfully. */ - (void)sdl_isIOThreadCanceled:(dispatch_semaphore_t)canceledSemaphore completionHandler:(void (^)(BOOL success))completionHandler { - long lWait = dispatch_semaphore_wait(canceledSemaphore, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(StreamThreadWaitSecs * NSEC_PER_SEC))); + long lWait = dispatch_semaphore_wait(canceledSemaphore, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(IOStreamThreadWaitSecs * NSEC_PER_SEC))); if (lWait == 0) { SDLLogD(@"Stream thread canceled successfully"); return completionHandler(YES); @@ -202,7 +209,7 @@ NSTimeInterval const StreamThreadWaitSecs = 10.0; SDLLogD(@"Starting the accessory event loop on thread: %@", NSThread.currentThread.name); - while (!NSThread.currentThread.cancelled) { + while (!self.ioStreamThread.cancelled) { // Enqueued data will be written to and read from the streams in the runloop [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.25f]]; } @@ -211,6 +218,7 @@ NSTimeInterval const StreamThreadWaitSecs = 10.0; // Close I/O streams of the data session. When the streams are closed. Notify the thread that it can close [self sdl_closeSession]; + dispatch_semaphore_signal(self.canceledSemaphore); } } diff --git a/SmartDeviceLink/SDLIAPTransport.m b/SmartDeviceLink/SDLIAPTransport.m index 5921adf74..243dffe0d 100644 --- a/SmartDeviceLink/SDLIAPTransport.m +++ b/SmartDeviceLink/SDLIAPTransport.m @@ -396,7 +396,7 @@ int const CreateSessionRetries = 3; [self.dataSession startSession]; } else { // No compatible accessory - SDLLogV(@"No accessory supporting SDL was found, dismissing setup"); + SDLLogV(@"No accessory supporting SDL was found, dismissing setup. Available accessories: %@", EAAccessoryManager.sharedAccessoryManager.connectedAccessories); self.sessionSetupInProgress = NO; } } else { |