diff options
author | Takamitsu Yoshii <tyoshii@uievoution.com> | 2017-09-27 11:55:53 +0900 |
---|---|---|
committer | Joel Fischer <joeljfischer@gmail.com> | 2017-10-03 09:40:55 -0400 |
commit | 511a08c31faba735802cc47a26ffe38c34724da0 (patch) | |
tree | 6660e6090ec4162675c7fd517e32425ee9957f0a | |
parent | 70b92979f8677dea26071954052d78be203e019f (diff) | |
download | sdl_ios-511a08c31faba735802cc47a26ffe38c34724da0.tar.gz |
Invoke write to iAP output stream immediately after new data is queued to sendQueue
-rw-r--r-- | SmartDeviceLink/SDLIAPSession.m | 25 |
1 files changed, 9 insertions, 16 deletions
diff --git a/SmartDeviceLink/SDLIAPSession.m b/SmartDeviceLink/SDLIAPSession.m index 8e0df7f50..86132320c 100644 --- a/SmartDeviceLink/SDLIAPSession.m +++ b/SmartDeviceLink/SDLIAPSession.m @@ -111,29 +111,29 @@ NSTimeInterval const StreamThreadWaitSecs = 1.0; - (void)sendData:(NSData *)data { // Enqueue the data for transmission on the IO thread [self.sendDataQueue enqueueBuffer:data.mutableCopy]; + + [self performSelector:@selector(sdl_dequeueAndWriteToOutputStream) onThread:self.ioStreamThread withObject:nil waitUntilDone:NO]; } -- (BOOL)sdl_dequeueAndWriteToOutputStream:(NSError **)error { +- (void)sdl_dequeueAndWriteToOutputStream { NSOutputStream *ostream = self.easession.outputStream; NSMutableData *remainder = [self.sendDataQueue frontBuffer]; - BOOL allDataWritten = NO; - if (error != nil && remainder != nil && ostream.streamStatus == NSStreamStatusOpen) { + if (remainder != nil && ostream.streamStatus == NSStreamStatusOpen) { NSInteger bytesRemaining = remainder.length; NSInteger bytesWritten = [ostream write:remainder.bytes maxLength:bytesRemaining]; if (bytesWritten < 0) { - *error = ostream.streamError; + if (ostream.streamError != nil) { + [self sdl_handleOutputStreamWriteError:ostream.streamError]; + } } else if (bytesWritten == bytesRemaining) { // Remove the data from the queue [self.sendDataQueue popBuffer]; - allDataWritten = YES; } else { // Cleave the sent bytes from the data, the remainder will sit at the head of the queue [remainder replaceBytesInRange:NSMakeRange(0, bytesWritten) withBytes:NULL length:0]; } } - - return allDataWritten; } - (void)sdl_handleOutputStreamWriteError:(NSError *)error { @@ -163,10 +163,7 @@ NSTimeInterval const StreamThreadWaitSecs = 1.0; SDLLogD(@"Starting the accessory event loop"); do { if (self.sendDataQueue.count > 0 && !self.sendDataQueue.frontDequeued) { - NSError *sendErr = nil; - if (![self sdl_dequeueAndWriteToOutputStream:&sendErr] && sendErr != nil) { - [self sdl_handleOutputStreamWriteError:sendErr]; - } + [self sdl_dequeueAndWriteToOutputStream]; } // The principle here is to give the event loop enough time to process stream events while also allowing it to handle new enqueued data buffers in a timely manner. We're capping the run loop CPU time at 0.25s maximum before it will return control to the rest of the loop. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.25f]]; @@ -277,11 +274,7 @@ NSTimeInterval const StreamThreadWaitSecs = 1.0; return; } - NSError *sendErr = nil; - - if (![strongSelf sdl_dequeueAndWriteToOutputStream:&sendErr] && sendErr != nil) { - [strongSelf sdl_handleOutputStreamWriteError:sendErr]; - } + [strongSelf sdl_dequeueAndWriteToOutputStream]; }; } |