-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TCP Transport Rewrite #946
TCP Transport Rewrite #946
Conversation
@@ -23,6 +23,7 @@ NS_ASSUME_NONNULL_BEGIN | |||
- (void)onProtocolOpened; | |||
- (void)onProtocolClosed; | |||
- (void)onError:(NSString *)info exception:(NSException *)e; | |||
- (void)onTransportError:(NSError *)error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "onError:exception:" delegate (one line above) isn't and hasn't been used anywhere in the project. We might want to remove it to avoid confusion.
A few notes:
Thanks! |
If a TCP connection is refused or timed out, onError is notified from the transport. Then an error will be delivered to the app through NSNotification.
5cf9017
to
6bf797f
Compare
I have rebased the commits onto latest |
SmartDeviceLink/SDLError.h
Outdated
@@ -65,6 +66,13 @@ extern SDLErrorDomain *const SDLErrorDomainChoiceSetManager; | |||
+ (NSError *)sdl_choiceSetManager_choiceDeletionFailed:(NSDictionary *)userInfo; | |||
+ (NSError *)sdl_choiceSetManager_choiceUploadFailed:(NSDictionary *)userInfo; | |||
|
|||
#pragma mark Transport | |||
|
|||
+ (NSError *)sdl_transport_OthersError; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename sdl_transport_unknownError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 72e4cb3.
SmartDeviceLink/SDLErrorConstants.h
Outdated
/** | ||
* Connection cannot be established due to a reason not listed here. | ||
*/ | ||
SDLTransportErrorOthers = -1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SDLTransportErrorUnknown
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 72e4cb3.
SmartDeviceLink/SDLTCPTransport.h
Outdated
@@ -27,6 +25,7 @@ NS_ASSUME_NONNULL_BEGIN | |||
* The port number of Core | |||
*/ | |||
@property (strong, nonatomic) NSString *portNumber; | |||
@property (nonatomic, assign) NSUInteger receiveBufferSize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this public facing, can it just be in the private interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it should be private, fixed with commit 4aa7820.
SmartDeviceLink/SDLTCPTransport.m
Outdated
[self disconnect]; | ||
} | ||
|
||
#pragma mark - SDLAbstractTransport methods |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no more SDLAbstractTransport
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed with commit 72e4cb3.
SmartDeviceLink/SDLTCPTransport.m
Outdated
} | ||
|
||
self.ioThread = [[NSThread alloc] initWithTarget:self selector:@selector(sdl_tcpTransportEventLoop) object:nil]; | ||
[self.ioThread setName:TCPIOThreadName]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.ioThread.name = TCPIOThreadName;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit 72e4cb3.
SmartDeviceLink/SDLTCPTransport.m
Outdated
[self.delegate onError:error]; | ||
self.transportErrorNotified = YES; | ||
} else { | ||
SDLLogW(@"Unhandled stream error! %@", stream.streamError); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an error log?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 4aa7820.
SmartDeviceLink/SDLTCPTransport.m
Outdated
// of NSPOSIXErrorDomain are actually errno values. | ||
NSError *error; | ||
switch (stream.streamError.code) { | ||
case ECONNREFUSED: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use braces as noted in the comment above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 72e4cb3.
SmartDeviceLink/SDLTCPTransport.m
Outdated
error = [NSError sdl_transport_OthersError]; | ||
break; | ||
} | ||
[self.delegate onError:error]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: All the errors here are errors causing us to not be able to connect, should we be checking anything here to make sure we're disconnected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I didn't catch your question. Are you suggesting that we should verify stream.streamStatus
property here and make sure that the stream is closed?
Well, once sdl_onStreamError:
is triggered, the ioThread
is cancelled and eventually sdl_teardownStream:
will be called for both streams. So I think there may not be much point adding extra logic inside sdl_onStreamError:
.
SmartDeviceLink/SDLTCPTransport.m
Outdated
} | ||
} | ||
|
||
- (void)sdl_doNothing { | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one line - (void)sdl_doNothing {}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 72e4cb3.
- (void)onClientError; | ||
@end | ||
|
||
@interface TestTCPServer : NSObject { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put this class in its own file(s), similar to how the TestConnectionManager
is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, done in commit 713f409.
- Rename an error enum - Update / remove comments, pragmas and logs - Resolve styling issues
- Change receiveBufferSize to private - Remove `default` case from switch statement - Simplify some if statements - Update logs
- Use NSAssert to check a flag that should be always false
Conflicts: SmartDeviceLink-iOS.xcodeproj/project.pbxproj
Conflicts: SmartDeviceLink-iOS.xcodeproj/project.pbxproj
Hi @joeljfischer , I think I resolved all of your review comments. I also merged latest |
gethostname(localhost, sizeof localhost); | ||
hostname = (const char *)&localhost; | ||
#pragma mark - Stream event handlers | ||
// these methods run only on the I/O thread (i.e. invoked from the run loop) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be an assert statement in each method making sure they're running on the correct thread?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, added in commit c1883ff.
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | ||
return (-1); | ||
NSData *data = [NSData dataWithBytesNoCopy:buffer length:(NSUInteger)readBytes freeWhenDone:YES]; | ||
[self.delegate onDataReceived:data]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This delegate will be called on the io thread. Should we dispatch to a different queue first or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed the design of iAP transport where onDataReceived
is invoked from its I/O thread. I don't think we need to dispatch to another queue.
- Add assertions in I/O callback methods
This PR is a part of implementation of #900. It also fixes issue #911.
This PR is ready for review.
Risk
This PR makes minor API changes: addition of NSNotification.
Testing Plan
Unit tests are included in the PR.
Summary
This PR rewrites TCP transport as requested by project maintainer (see smartdevicelink/sdl_evolution#405 (comment)).
The project maintainer suggested to use NSURLSessionStreamTask API and I had implemented with it. Then I found a few issues. 1) it didn't work with my iOS 9.0.2 phone, and 2) the API doesn't seem to provide "TCP connected" event. After a short offline discussion with the project maintainer, I decided to use CFNetwork API instead.
Changelog
Breaking Changes
Enhancements
Bug Fixes
CLA