Skip to content
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

Minor features (NSNetService and nullability fixes) #674

Merged
merged 3 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Source/GCD/GCDAsyncSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ typedef NS_ERROR_ENUM(GCDAsyncSocketErrorDomain, GCDAsyncSocketError) {
*/
- (BOOL)connectToUrl:(NSURL *)url withTimeout:(NSTimeInterval)timeout error:(NSError **)errPtr;

/**
* Iterates over the given NetService's addresses in order, and invokes connectToAddress:error:. Stops at the
* first invocation that succeeds and returns YES; otherwise returns NO.
*/
- (BOOL)connectToNetService:(NSNetService *)netService error:(NSError **)errPtr;

#pragma mark Disconnecting

/**
Expand Down Expand Up @@ -521,7 +527,7 @@ typedef NS_ERROR_ENUM(GCDAsyncSocketErrorDomain, GCDAsyncSocketError) {
* For performance reasons, the socket will retain it, not copy it.
* So if it is immutable, don't modify it while the socket is using it.
**/
- (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
- (void)readDataToData:(nullable NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;

/**
* Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
Expand Down Expand Up @@ -656,7 +662,7 @@ typedef NS_ERROR_ENUM(GCDAsyncSocketErrorDomain, GCDAsyncSocketError) {
* completes writing the bytes (which is NOT immediately after this method returns, but rather at a later time
* when the delegate method notifies you), then you should first copy the bytes, and pass the copy to this method.
**/
- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
- (void)writeData:(nullable NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would it be valid to pass nil data here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it nullable to conform to the docs, line 651: "If you pass in nil or zero-length data, this method does nothing and the delegate will not be called." There could be code out there relying on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with reverting it to nonnull and changing the docs if you'd prefer...


/**
* Returns progress of the current write, from 0.0 to 1.0, or NaN if no current write (use isnan() to check).
Expand Down
20 changes: 18 additions & 2 deletions Source/GCD/GCDAsyncSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -1864,8 +1864,9 @@ - (BOOL)acceptOnUrl:(NSURL *)url error:(NSError **)errPtr;

NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:url.path]) {
if (![[NSFileManager defaultManager] removeItemAtURL:url error:&error]) {
NSString *urlPath = url.path;
if (urlPath && [fileManager fileExistsAtPath:urlPath]) {
if (![fileManager removeItemAtURL:url error:&error]) {
NSString *msg = @"Could not remove previous unix domain socket at given url.";
err = [self otherError:msg];

Expand Down Expand Up @@ -2542,6 +2543,21 @@ - (BOOL)connectToUrl:(NSURL *)url withTimeout:(NSTimeInterval)timeout error:(NSE
return result;
}

- (BOOL)connectToNetService:(NSNetService *)netService error:(NSError **)errPtr
{
NSArray* addresses = [netService addresses];
for (NSData* address in addresses)
{
BOOL result = [self connectToAddress:address error:errPtr];
if (result)
{
return YES;
}
}

return NO;
}

- (void)lookup:(int)aStateIndex didSucceedWithAddress4:(NSData *)address4 address6:(NSData *)address6
{
LogTrace();
Expand Down