From 88cdc0b5867b4f3d0d31e61b8b5d81a46d68f13f Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 22 Aug 2016 15:23:50 -0700 Subject: [PATCH 1/2] Optimize input queue processing in SRProxyConnect. --- SocketRocket/Internal/Proxy/SRProxyConnect.m | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/SocketRocket/Internal/Proxy/SRProxyConnect.m b/SocketRocket/Internal/Proxy/SRProxyConnect.m index 528cc8b46..f30cedc1b 100644 --- a/SocketRocket/Internal/Proxy/SRProxyConnect.m +++ b/SocketRocket/Internal/Proxy/SRProxyConnect.m @@ -407,13 +407,17 @@ - (void)_processInputStream - (void)_dequeueInput { while (_inputQueue.count > 0) { - NSData *data = _inputQueue[0]; - [self _proxyProcessHTTPResponseWithData:data]; + NSData *data = _inputQueue.firstObject; [_inputQueue removeObjectAtIndex:0]; + + // No need to process any data further, we got the full header data. + if ([self _proxyProcessHTTPResponseWithData:data]) { + break; + } } } //handle checking the proxy connection status -- (void)_proxyProcessHTTPResponseWithData:(NSData *)data +- (BOOL)_proxyProcessHTTPResponseWithData:(NSData *)data { if (_receivedHTTPHeaders == NULL) { _receivedHTTPHeaders = CFHTTPMessageCreateEmpty(NULL, NO); @@ -423,7 +427,10 @@ - (void)_proxyProcessHTTPResponseWithData:(NSData *)data if (CFHTTPMessageIsHeaderComplete(_receivedHTTPHeaders)) { SRDebugLog(@"Finished reading headers %@", CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(_receivedHTTPHeaders))); [self _proxyHTTPHeadersDidFinish]; + return YES; } + + return NO; } - (void)_proxyHTTPHeadersDidFinish From 9c2201f099439cce6e31192b6954fdeb0c4c9865 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 22 Aug 2016 15:23:50 -0700 Subject: [PATCH 2/2] Cleanup proxy connect reference when socket is opened asynchronously. --- SocketRocket/SRWebSocket.m | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/SocketRocket/SRWebSocket.m b/SocketRocket/SRWebSocket.m index 86c47a1b4..30d13801a 100644 --- a/SocketRocket/SRWebSocket.m +++ b/SocketRocket/SRWebSocket.m @@ -326,8 +326,6 @@ - (void)open - (void)_connectionDoneWithError:(NSError *)error readStream:(NSInputStream *)readStream writeStream:(NSOutputStream *)writeStream { - _proxyConnect = nil; // Job's done! This is not longer required. - if (error != nil) { [self _failWithError:error]; } else { @@ -350,6 +348,11 @@ - (void)_connectionDoneWithError:(NSError *)error readStream:(NSInputStream *)re }); } } + // Schedule to run on a work queue, to make sure we don't run this inline and deallocate `self` inside `SRProxyConnect`. + // TODO: (nlutsenko) Find a better structure for this, maybe Bolts Tasks? + dispatch_async(_workQueue, ^{ + _proxyConnect = nil; + }); } - (BOOL)_checkHandshake:(CFHTTPMessageRef)httpMessage;