Skip to content

Commit

Permalink
Merge pull request facebookincubator#455 from facebook/nlutsenko.prox…
Browse files Browse the repository at this point in the history
…y.crash

Optimize input queue processing in SRProxyConnect.
  • Loading branch information
nlutsenko committed Aug 23, 2016
2 parents 47971eb + 9c2201f commit 00a8a66
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 10 additions & 3 deletions SocketRocket/Internal/Proxy/SRProxyConnect.m
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,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);
Expand All @@ -422,7 +426,10 @@ - (void)_proxyProcessHTTPResponseWithData:(NSData *)data
if (CFHTTPMessageIsHeaderComplete(_receivedHTTPHeaders)) {
SRDebugLog(@"Finished reading headers %@", CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(_receivedHTTPHeaders)));
[self _proxyHTTPHeadersDidFinish];
return YES;
}

return NO;
}

- (void)_proxyHTTPHeadersDidFinish
Expand Down
7 changes: 5 additions & 2 deletions SocketRocket/SRWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 00a8a66

Please sign in to comment.