Move socket clean inside operation queue
Summary: Set delegate and close inside the operation's queue as to make it safer i.e. all socket related operations are done inside the queue. Reviewed By: ivanmisuno Differential Revision: D47124235 fbshipit-source-id: 48b53db1cd47d017a26186a156046ba68fe358b7
This commit is contained in:
committed by
Facebook GitHub Bot
parent
14068f1ea8
commit
4ac755370d
@@ -171,16 +171,21 @@ static constexpr int connectionKeepaliveSeconds = 10;
|
|||||||
}
|
}
|
||||||
_keepAlive = nil;
|
_keepAlive = nil;
|
||||||
|
|
||||||
if (_socket) {
|
__weak auto weakSelf = self;
|
||||||
|
NSBlockOperation* disconnectOperation =
|
||||||
|
[NSBlockOperation blockOperationWithBlock:^{
|
||||||
|
__strong auto strongSelf = weakSelf;
|
||||||
// Clear the socket delegate before close. Ensures that we won't get
|
// Clear the socket delegate before close. Ensures that we won't get
|
||||||
// any messages after the disconnect takes place.
|
// any messages after the disconnect takes place.
|
||||||
[_socket setDelegate:nil];
|
if (strongSelf->_socket) {
|
||||||
[_socket close];
|
[strongSelf->_socket setDelegate:nil];
|
||||||
_socket = nil;
|
[strongSelf->_socket close];
|
||||||
};
|
|
||||||
|
|
||||||
[_dispatchQueue cancelAllOperations];
|
strongSelf->_socket = nil;
|
||||||
[_dispatchQueue waitUntilAllOperationsAreFinished];
|
}
|
||||||
|
}];
|
||||||
|
|
||||||
|
[_dispatchQueue addOperations:@[ disconnectOperation ] waitUntilFinished:YES];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)send:(NSString*)message
|
- (void)send:(NSString*)message
|
||||||
|
|||||||
@@ -107,13 +107,11 @@ void FlipperConnectionManagerImpl::handleSocketEvent(SocketEvent event) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SocketEvent::SSL_ERROR:
|
case SocketEvent::SSL_ERROR:
|
||||||
log("[conn] handleSocketEvent(SSL_ERROR)");
|
|
||||||
failedConnectionAttempts_++;
|
failedConnectionAttempts_++;
|
||||||
reconnect();
|
reconnect();
|
||||||
break;
|
break;
|
||||||
case SocketEvent::CLOSE:
|
case SocketEvent::CLOSE:
|
||||||
case SocketEvent::ERROR:
|
case SocketEvent::ERROR:
|
||||||
log("[conn] handleSocketEvent(CLOSE_ERROR)");
|
|
||||||
if (!isConnected_) {
|
if (!isConnected_) {
|
||||||
reconnect();
|
reconnect();
|
||||||
return;
|
return;
|
||||||
@@ -182,7 +180,6 @@ void FlipperConnectionManagerImpl::startSync() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FlipperConnectionManagerImpl::connectAndExchangeCertificate() {
|
void FlipperConnectionManagerImpl::connectAndExchangeCertificate() {
|
||||||
log("[conn] connectAndExchangeCertificate()");
|
|
||||||
auto port = insecurePort;
|
auto port = insecurePort;
|
||||||
auto endpoint = FlipperConnectionEndpoint(deviceData_.host, port, false);
|
auto endpoint = FlipperConnectionEndpoint(deviceData_.host, port, false);
|
||||||
|
|
||||||
@@ -265,7 +262,6 @@ void FlipperConnectionManagerImpl::reconnect() {
|
|||||||
log("Not started");
|
log("Not started");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
log("[conn] reconnect()");
|
|
||||||
scheduler_->scheduleAfter(
|
scheduler_->scheduleAfter(
|
||||||
[this]() { startSync(); }, RECONNECT_INTERVAL_SECONDS * 1000.0f);
|
[this]() { startSync(); }, RECONNECT_INTERVAL_SECONDS * 1000.0f);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user