Bladeren bron

GCD Cleanup

 * Made agnostic of OS_OBJECT_USE_OBJC_RETAIN_RELEASE (FIXES #52)
 * Removed dependency on dispatch_get_current_queue (FIXES #68)
Mike Lewis 12 jaren geleden
bovenliggende
commit
9f89e684db
3 gewijzigde bestanden met toevoegingen van 53 en 23 verwijderingen
  1. 12 1
      README.rst
  2. 2 4
      SocketRocket.xcodeproj/project.pbxproj
  3. 39 18
      SocketRocket/SRWebSocket.m

+ 12 - 1
README.rst

@@ -16,13 +16,24 @@ Features/Design
 - TLS (wss) support.  It uses CFStream so we get this for *free*
 - Uses NSStream/CFNetworking.  Earlier implementations used ``dispatch_io``,
   however, this proved to be make TLS nearly impossible.  Also I wanted this to
-  work in iOS 4.x.
+  work in iOS 4.x. (SocketRocket only supports 5.0 and above now)
 - Uses ARC.  It uses the 4.0 compatible subset (no weak refs).
 - Seems to perform quite well
 - Parallel architecture. Most of the work is done in background worker queues.
 - Delegate-based. Had older versions that could use blocks too, but I felt it
   didn't blend well with retain cycles and just objective C in general.
 
+Changes
+-------
+
+v0.3.1-beta1 - 2013-01-12
+`````````````````````````
+
+- Cleaned up GCD so OS_OBJECT_USE_OBJC_RETAIN_RELEASE is optional
+- Removed deprecated ``dispatch_get_current_queue`` in favor of ``dispatch_queue_set_specific`` and ``dispatch_get_specific``
+- Dropping support for iOS 4.0 (it may still work)
+
+
 Installing (iOS)
 ----------------
 There's a few options. Choose one, or just figure it out

+ 2 - 4
SocketRocket.xcodeproj/project.pbxproj

@@ -649,7 +649,6 @@
 				GCC_OPTIMIZATION_LEVEL = 0;
 				GCC_PREPROCESSOR_DEFINITIONS = (
 					"DEBUG=1",
-					"OS_OBJECT_USE_OBJC_RETAIN_RELEASE=0",
 					"$(inherited)",
 				);
 				GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
@@ -659,7 +658,7 @@
 				GCC_WARN_SIGN_COMPARE = YES;
 				GCC_WARN_UNINITIALIZED_AUTOS = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
-				IPHONEOS_DEPLOYMENT_TARGET = 4.0;
+				IPHONEOS_DEPLOYMENT_TARGET = 5.1;
 				RUN_CLANG_STATIC_ANALYZER = YES;
 				SDKROOT = iphoneos;
 			};
@@ -674,7 +673,6 @@
 				CLANG_ENABLE_OBJC_ARC = YES;
 				COPY_PHASE_STRIP = YES;
 				GCC_PREPROCESSOR_DEFINITIONS = (
-					"OS_OBJECT_USE_OBJC_RETAIN_RELEASE=0",
 					NDEBUG,
 					"$(inherited)",
 				);
@@ -686,7 +684,7 @@
 				GCC_WARN_SIGN_COMPARE = YES;
 				GCC_WARN_UNINITIALIZED_AUTOS = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
-				IPHONEOS_DEPLOYMENT_TARGET = 4.0;
+				IPHONEOS_DEPLOYMENT_TARGET = 5.1;
 				RUN_CLANG_STATIC_ANALYZER = YES;
 				SDKROOT = iphoneos;
 				VALIDATE_PRODUCT = YES;

+ 39 - 18
SocketRocket/SRWebSocket.m

@@ -37,6 +37,21 @@
 #import "base64.h"
 #import "NSData+SRB64Additions.h"
 
+#if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
+#define sr_dispatch_retain(x)
+#define sr_dispatch_release(x)
+#define maybe_bridge(x) ((__bridge void *) x)
+#else
+#define sr_dispatch_retain(x) dispatch_retain(x)
+#define sr_dispatch_release(x) dispatch_release(x)
+#define maybe_bridge(x) (x)
+#endif
+
+#if !__has_feature(objc_arc) 
+#error SocketRocket muust be compiled with ARC enabled
+#endif
+
+
 typedef enum  {
     SROpCodeTextFrame = 0x1,
     SROpCodeBinaryFrame = 0x2,
@@ -325,17 +340,17 @@ static __strong NSData *CRLFCRLF;
         _secure = YES;
     }
     
-    
     _readyState = SR_CONNECTING;
-
     _consumerStopped = YES;
-    
     _webSocketVersion = 13;
     
     _workQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
     
+    // Going to set a specific on the queue so we can validate we're on the work queue
+    dispatch_queue_set_specific(_workQueue, (__bridge void *)self, maybe_bridge(_workQueue), NULL);
+    
     _delegateDispatchQueue = dispatch_get_main_queue();
-    dispatch_retain(_delegateDispatchQueue);
+    sr_dispatch_retain(_delegateDispatchQueue);
     
     _readBuffer = [[NSMutableData alloc] init];
     _outputBuffer = [[NSMutableData alloc] init];
@@ -350,6 +365,11 @@ static __strong NSData *CRLFCRLF;
     // default handlers
 }
 
+- (void)assertOnWorkQueue;
+{
+    assert(dispatch_get_specific((__bridge void *)self) == maybe_bridge(_workQueue));
+}
+
 - (void)dealloc
 {
     _inputStream.delegate = nil;
@@ -358,7 +378,8 @@ static __strong NSData *CRLFCRLF;
     [_inputStream close];
     [_outputStream close];
     
-    dispatch_release(_workQueue);
+    sr_dispatch_release(_workQueue);
+    _workQueue = NULL;
     
     if (_receivedHTTPHeaders) {
         CFRelease(_receivedHTTPHeaders);
@@ -366,7 +387,7 @@ static __strong NSData *CRLFCRLF;
     }
     
     if (_delegateDispatchQueue) {
-        dispatch_release(_delegateDispatchQueue);
+        sr_dispatch_release(_delegateDispatchQueue);
         _delegateDispatchQueue = NULL;
     }
 }
@@ -407,11 +428,11 @@ static __strong NSData *CRLFCRLF;
 - (void)setDelegateDispatchQueue:(dispatch_queue_t)queue;
 {
     if (queue) {
-        dispatch_retain(queue);
+        sr_dispatch_retain(queue);
     }
     
     if (_delegateDispatchQueue) {
-        dispatch_release(_delegateDispatchQueue);
+        sr_dispatch_release(_delegateDispatchQueue);
     }
     
     _delegateDispatchQueue = queue;
@@ -679,7 +700,7 @@ static __strong NSData *CRLFCRLF;
 
 - (void)_writeData:(NSData *)data;
 {    
-    assert(dispatch_get_current_queue() == _workQueue);
+    [self assertOnWorkQueue];
 
     if (_closeWhenFinishedWriting) {
             return;
@@ -792,7 +813,7 @@ static inline BOOL closeCodeIsValid(int closeCode) {
         _closeCode = SRStatusNoStatusReceived;
     }
     
-    assert(dispatch_get_current_queue() == _workQueue);
+    [self assertOnWorkQueue];
     
     if (self.readyState == SR_OPEN) {
         [self closeWithCode:1000 reason:nil];
@@ -804,7 +825,7 @@ static inline BOOL closeCodeIsValid(int closeCode) {
 
 - (void)_disconnect;
 {
-    assert(dispatch_get_current_queue() == _workQueue);
+    [self assertOnWorkQueue];
     SRFastLog(@"Trying to disconnect");
     _closeWhenFinishedWriting = YES;
     [self _pumpWriting];
@@ -1039,7 +1060,7 @@ static const uint8_t SRPayloadLenMask   = 0x7F;
 
 - (void)_pumpWriting;
 {
-    assert(dispatch_get_current_queue() == _workQueue);
+    [self assertOnWorkQueue];
     
     NSUInteger dataLength = _outputBuffer.length;
     if (dataLength - _outputBufferOffset > 0 && _outputStream.hasSpaceAvailable) {
@@ -1081,13 +1102,13 @@ static const uint8_t SRPayloadLenMask   = 0x7F;
 
 - (void)_addConsumerWithScanner:(stream_scanner)consumer callback:(data_callback)callback;
 {
-    assert(dispatch_get_current_queue() == _workQueue);
+    [self assertOnWorkQueue];
     [self _addConsumerWithScanner:consumer callback:callback dataLength:0];
 }
 
 - (void)_addConsumerWithDataLength:(size_t)dataLength callback:(data_callback)callback readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
 {   
-    assert(dispatch_get_current_queue() == _workQueue);
+    [self assertOnWorkQueue];
     assert(dataLength);
     
     [_consumers addObject:[_consumerPool consumerWithScanner:nil handler:callback bytesNeeded:dataLength readToCurrentFrame:readToCurrentFrame unmaskBytes:unmaskBytes]];
@@ -1096,7 +1117,7 @@ static const uint8_t SRPayloadLenMask   = 0x7F;
 
 - (void)_addConsumerWithScanner:(stream_scanner)consumer callback:(data_callback)callback dataLength:(size_t)dataLength;
 {    
-    assert(dispatch_get_current_queue() == _workQueue);
+    [self assertOnWorkQueue];
     [_consumers addObject:[_consumerPool consumerWithScanner:consumer handler:callback bytesNeeded:dataLength readToCurrentFrame:NO unmaskBytes:NO]];
     [self _pumpScanner];
 }
@@ -1244,7 +1265,7 @@ static const char CRLFCRLFBytes[] = {'\r', '\n', '\r', '\n'};
 
 -(void)_pumpScanner;
 {
-    assert(dispatch_get_current_queue() == _workQueue);
+    [self assertOnWorkQueue];
     
     if (!_isPumping) {
         _isPumping = YES;
@@ -1265,7 +1286,7 @@ static const size_t SRFrameHeaderOverhead = 32;
 
 - (void)_sendFrameWithOpcode:(SROpCode)opcode data:(id)data;
 {
-    assert(dispatch_get_current_queue() == _workQueue);
+    [self assertOnWorkQueue];
     
     NSAssert(data == nil || [data isKindOfClass:[NSData class]] || [data isKindOfClass:[NSString class]], @"Function expects nil, NSString or NSData");
     
@@ -1688,7 +1709,7 @@ static NSRunLoop *networkRunLoop = nil;
 
 - (void)dealloc
 {
-    dispatch_release(_waitGroup);
+    sr_dispatch_release(_waitGroup);
 }
 
 - (id)init