Skip to content
Browse files

Merging latest changes from CocoaAsyncSocket project

  • Loading branch information...
1 parent fecc859 commit 1604d51bde238c4484abfbea72a5e55f0bc9a05a @robbiehanson committed
Showing with 75 additions and 29 deletions.
  1. +75 −29 Vendor/CocoaAsyncSocket/GCDAsyncSocket.m
View
104 Vendor/CocoaAsyncSocket/GCDAsyncSocket.m
@@ -13,6 +13,32 @@
// For more information see: https://github.com/robbiehanson/CocoaAsyncSocket/wiki/ARC
#endif
+/**
+ * Does ARC support support GCD objects?
+ * It does if the minimum deployment target is iOS 6+ or Mac OS X 8+
+**/
+#if TARGET_OS_IPHONE
+
+ // Compiling for iOS
+
+ #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000 // iOS 6.0 or later
+ #define NEEDS_DISPATCH_RETAIN_RELEASE 0
+ #else // iOS 5.X or earlier
+ #define NEEDS_DISPATCH_RETAIN_RELEASE 1
+ #endif
+
+#else
+
+ // Compiling for Mac OS X
+
+ #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080 // Mac OS X 10.8 or later
+ #define NEEDS_DISPATCH_RETAIN_RELEASE 0
+ #else
+ #define NEEDS_DISPATCH_RETAIN_RELEASE 1 // Mac OS X 10.7 or earlier
+ #endif
+
+#endif
+
#import "GCDAsyncSocket.h"
#if TARGET_OS_IPHONE
@@ -972,12 +998,11 @@ - (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq socketQu
if((self = [super init]))
{
delegate = aDelegate;
+ delegateQueue = dq;
- if (dq)
- {
- dispatch_retain(dq);
- delegateQueue = dq;
- }
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
+ if (dq) dispatch_retain(dq);
+ #endif
socket4FD = SOCKET_NULL;
socket6FD = SOCKET_NULL;
@@ -992,8 +1017,10 @@ - (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq socketQu
NSAssert(sq != dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
@"The given socketQueue parameter must not be a concurrent queue.");
- dispatch_retain(sq);
socketQueue = sq;
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
+ dispatch_retain(sq);
+ #endif
}
else
{
@@ -1027,12 +1054,15 @@ - (void)dealloc
}
delegate = nil;
- if (delegateQueue)
- dispatch_release(delegateQueue);
+
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
+ if (delegateQueue) dispatch_release(delegateQueue);
+ #endif
delegateQueue = NULL;
- if (socketQueue)
- dispatch_release(socketQueue);
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
+ if (socketQueue) dispatch_release(socketQueue);
+ #endif
socketQueue = NULL;
LogInfo(@"%@ - %@ (finish)", THIS_METHOD, self);
@@ -1109,11 +1139,10 @@ - (void)setDelegateQueue:(dispatch_queue_t)newDelegateQueue synchronously:(BOOL)
{
dispatch_block_t block = ^{
- if (delegateQueue)
- dispatch_release(delegateQueue);
-
- if (newDelegateQueue)
- dispatch_retain(newDelegateQueue);
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
+ if (delegateQueue) dispatch_release(delegateQueue);
+ if (newDelegateQueue) dispatch_retain(newDelegateQueue);
+ #endif
delegateQueue = newDelegateQueue;
};
@@ -1167,11 +1196,10 @@ - (void)setDelegate:(id)newDelegate delegateQueue:(dispatch_queue_t)newDelegateQ
delegate = newDelegate;
- if (delegateQueue)
- dispatch_release(delegateQueue);
-
- if (newDelegateQueue)
- dispatch_retain(newDelegateQueue);
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
+ if (delegateQueue) dispatch_release(delegateQueue);
+ if (newDelegateQueue) dispatch_retain(newDelegateQueue);
+ #endif
delegateQueue = newDelegateQueue;
};
@@ -1612,8 +1640,10 @@ - (BOOL)acceptOnInterface:(NSString *)inInterface port:(uint16_t)port error:(NSE
dispatch_source_set_cancel_handler(accept4Source, ^{
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
LogVerbose(@"dispatch_release(accept4Source)");
dispatch_release(acceptSource);
+ #endif
LogVerbose(@"close(socket4FD)");
close(socketFD);
@@ -1644,8 +1674,10 @@ - (BOOL)acceptOnInterface:(NSString *)inInterface port:(uint16_t)port error:(NSE
dispatch_source_set_cancel_handler(accept6Source, ^{
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
LogVerbose(@"dispatch_release(accept6Source)");
dispatch_release(acceptSource);
+ #endif
LogVerbose(@"close(socket6FD)");
close(socketFD);
@@ -1779,8 +1811,9 @@ - (BOOL)doAccept:(int)parentSocketFD
}
// Release the socket queue returned from the delegate (it was retained by acceptedSocket)
- if (childSocketQueue)
- dispatch_release(childSocketQueue);
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
+ if (childSocketQueue) dispatch_release(childSocketQueue);
+ #endif
// The accepted socket should have been retained by the delegate.
// Otherwise it gets properly released when exiting the block.
@@ -2340,6 +2373,11 @@ - (BOOL)connectWithAddress4:(NSData *)address4 address6:(NSData *)address6 error
}
}
+ // Prevent SIGPIPE signals
+
+ int nosigpipe = 1;
+ setsockopt(socketFD, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe));
+
// Start the connection process in a background queue
int aConnectIndex = connectIndex;
@@ -2488,11 +2526,6 @@ - (void)didConnect:(int)aConnectIndex
return;
}
- // Prevent SIGPIPE signals
-
- int nosigpipe = 1;
- setsockopt(socketFD, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe));
-
// Setup our read/write sources
[self setupReadAndWriteSourcesForNewlyConnectedSocket:socketFD];
@@ -2533,11 +2566,13 @@ - (void)startConnectTimeout:(NSTimeInterval)timeout
[self doConnectTimeout];
}});
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_source_t theConnectTimer = connectTimer;
dispatch_source_set_cancel_handler(connectTimer, ^{
LogVerbose(@"dispatch_release(connectTimer)");
dispatch_release(theConnectTimer);
});
+ #endif
dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (timeout * NSEC_PER_SEC));
dispatch_source_set_timer(connectTimer, tt, DISPATCH_TIME_FOREVER, 0);
@@ -2638,8 +2673,9 @@ - (void)closeWithError:(NSError *)error
SSLClose(sslContext);
- #if !TARGET_OS_IPHONE
- // SSLDisposeContext doesn't exist in iOS for some odd reason.
+ #if TARGET_OS_IPHONE
+ CFRelease(sslContext);
+ #else
SSLDisposeContext(sslContext);
#endif
@@ -3597,15 +3633,19 @@ - (void)setupReadAndWriteSourcesForNewlyConnectedSocket:(int)socketFD
__block int socketFDRefCount = 2;
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_source_t theReadSource = readSource;
dispatch_source_t theWriteSource = writeSource;
+ #endif
dispatch_source_set_cancel_handler(readSource, ^{
LogVerbose(@"readCancelBlock");
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
LogVerbose(@"dispatch_release(readSource)");
dispatch_release(theReadSource);
+ #endif
if (--socketFDRefCount == 0)
{
@@ -3618,8 +3658,10 @@ - (void)setupReadAndWriteSourcesForNewlyConnectedSocket:(int)socketFD
LogVerbose(@"writeCancelBlock");
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
LogVerbose(@"dispatch_release(writeSource)");
dispatch_release(theWriteSource);
+ #endif
if (--socketFDRefCount == 0)
{
@@ -5027,11 +5069,13 @@ - (void)setupReadTimerWithTimeout:(NSTimeInterval)timeout
[self doReadTimeout];
}});
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_source_t theReadTimer = readTimer;
dispatch_source_set_cancel_handler(readTimer, ^{
LogVerbose(@"dispatch_release(readTimer)");
dispatch_release(theReadTimer);
});
+ #endif
dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (timeout * NSEC_PER_SEC));
@@ -5655,11 +5699,13 @@ - (void)setupWriteTimerWithTimeout:(NSTimeInterval)timeout
[self doWriteTimeout];
}});
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_source_t theWriteTimer = writeTimer;
dispatch_source_set_cancel_handler(writeTimer, ^{
LogVerbose(@"dispatch_release(writeTimer)");
dispatch_release(theWriteTimer);
});
+ #endif
dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (timeout * NSEC_PER_SEC));

0 comments on commit 1604d51

Please sign in to comment.
Something went wrong with that request. Please try again.