Socket connect no longer synchronous and blocking
Summary: Never really liked this code. Before this change, calls to connect were blocking. Because of this, we had to make use of promises and a bit of really not that good-looking code. So, this change makes connect non-blocking meaning that we make full use of our event handler. These changes contain: - CSR is not getting generated after each failed attempt. - Connect is no longer blocking. - Do not report events via the handler when explicitly disconnecting. Reviewed By: jknoxville Differential Revision: D46853228 fbshipit-source-id: 00e6a9c7c039a756175fe14982959e078d92bacb
This commit is contained in:
committed by
Facebook GitHub Bot
parent
65e515bdaa
commit
e42db220ee
@@ -53,9 +53,9 @@ void FlipperWebSocket::setMessageHandler(SocketMessageHandler messageHandler) {
|
||||
messageHandler_ = std::move(messageHandler);
|
||||
}
|
||||
|
||||
bool FlipperWebSocket::connect(FlipperConnectionManager* manager) {
|
||||
void FlipperWebSocket::connect(FlipperConnectionManager* manager) {
|
||||
if (socket_ != NULL) {
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
|
||||
std::string connectionURL = endpoint_.secure ? "wss://" : "ws://";
|
||||
@@ -72,43 +72,16 @@ bool FlipperWebSocket::connect(FlipperConnectionManager* manager) {
|
||||
connectionURL += payload;
|
||||
}
|
||||
|
||||
__block bool fullfilled = false;
|
||||
__block std::promise<bool> promise;
|
||||
auto connected = promise.get_future();
|
||||
|
||||
NSURL* urlObjc = [NSURL
|
||||
URLWithString:[NSString stringWithUTF8String:connectionURL.c_str()]];
|
||||
|
||||
auto eventHandler = eventHandler_;
|
||||
socket_ = [[FlipperPlatformWebSocket alloc] initWithURL:urlObjc];
|
||||
socket_.eventHandler = ^(SocketEvent event) {
|
||||
/**
|
||||
Only fulfill the promise the first time the event handler is used. If the
|
||||
open event is received, then set the promise value to true. For any other
|
||||
event, consider a failure and set to false.
|
||||
*/
|
||||
if (!fullfilled) {
|
||||
fullfilled = true;
|
||||
if (event == SocketEvent::OPEN) {
|
||||
promise.set_value(true);
|
||||
} else if (event == SocketEvent::SSL_ERROR) {
|
||||
try {
|
||||
promise.set_exception(
|
||||
std::make_exception_ptr(SSLException("SSL handshake failed")));
|
||||
} catch (...) {
|
||||
// set_exception() may throw an exception
|
||||
// In that case, just set the value to false.
|
||||
promise.set_value(false);
|
||||
}
|
||||
} else {
|
||||
promise.set_value(false);
|
||||
}
|
||||
}
|
||||
eventHandler(event);
|
||||
eventHandler_(event);
|
||||
};
|
||||
auto messageHandler = messageHandler_;
|
||||
|
||||
socket_.messageHandler = ^(const std::string& message) {
|
||||
messageHandler(message);
|
||||
messageHandler_(message);
|
||||
};
|
||||
|
||||
if (endpoint_.secure) {
|
||||
@@ -124,8 +97,6 @@ bool FlipperWebSocket::connect(FlipperConnectionManager* manager) {
|
||||
}
|
||||
|
||||
[socket_ connect];
|
||||
|
||||
return connected.get();
|
||||
}
|
||||
|
||||
void FlipperWebSocket::disconnect() {
|
||||
@@ -157,9 +128,9 @@ void FlipperWebSocket::send(
|
||||
}
|
||||
|
||||
/**
|
||||
Only ever used for insecure connections to receive the device_id from a
|
||||
signCertificate request. If the intended usage ever changes, then a better
|
||||
approach needs to be put in place.
|
||||
* Only ever used for insecure connections to receive the device_id from a
|
||||
* signCertificate request. If the intended usage ever changes, then a better
|
||||
* approach needs to be put in place.
|
||||
*/
|
||||
void FlipperWebSocket::sendExpectResponse(
|
||||
const std::string& message,
|
||||
|
||||
Reference in New Issue
Block a user